louiecerv commited on
Commit
37c7e43
·
1 Parent(s): 8c7d67f

sync with remote

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +200 -0
  3. requirements.txt +9 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ model_repo\
app.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ import torch.nn as nn
4
+ import torch.optim as optim
5
+ from torchvision import transforms
6
+ from torch.utils.data import DataLoader
7
+ from datasets import load_dataset
8
+ from huggingface_hub import HfApi, Repository
9
+ import os
10
+
11
+ # Hugging Face Hub credentials
12
+ HF_TOKEN = os.getenv("HF_TOKEN")
13
+ MODEL_REPO_ID = "louiecerv/amer_sign_lang_data_augmentation"
14
+ DATASET_REPO_ID = "louiecerv/american_sign_language"
15
+
16
+ # Device configuration
17
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
18
+
19
+ # Define the CNN model
20
+ class CNN(nn.Module):
21
+ def __init__(self):
22
+ super(CNN, self).__init__()
23
+ self.conv1 = nn.Conv2d(1, 32, kernel_size=3, padding=1)
24
+ self.relu1 = nn.ReLU()
25
+ self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
26
+ self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
27
+ self.relu2 = nn.ReLU()
28
+ self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
29
+ self.flatten = nn.Flatten()
30
+ self.fc = nn.Linear(64 * 7 * 7, 128) # Adjusted for 28x28 images
31
+ self.relu3 = nn.ReLU()
32
+ self.fc2 = nn.Linear(128, 25) # 25 classes (A-Y)
33
+
34
+ def forward(self, x):
35
+ x = self.pool1(self.relu1(self.conv1(x)))
36
+ x = self.pool2(self.relu2(self.conv2(x)))
37
+ x = self.flatten(x)
38
+ x = self.relu3(self.fc(x))
39
+ x = self.fc2(x)
40
+ return x
41
+
42
+ # Create a model card
43
+ def create_model_card():
44
+ model_card = """
45
+ ---
46
+ language: en
47
+ tags:
48
+ - image-classification
49
+ - deep-learning
50
+ - cnn
51
+ license: apache-2.0
52
+ datasets:
53
+ Network (CNN) designed to recognize American Sign Language (ASL) letters from images. It was trained on the `louiecerv/american_sign_language` dataset.
54
+
55
+ ## Model Description
56
+
57
+ The model consists of two convolutional layers followed by max-pooling layers, a flattening layer, and two fully connected layers. It is designed to classify images of ASL letters into 25 classes (A-Y).
58
+
59
+ ## Intended Uses & Limitations
60
+
61
+ This model is intended for educational purposes and as a demonstration of image classification using CNNs. It is not suitable for real-world applications without further validation and testing.
62
+
63
+ ## How to Use
64
+
65
+ ```python
66
+ import torch
67
+ from torchvision import transforms
68
+ from PIL import Image
69
+
70
+ # Load the model
71
+ model = CNN()
72
+ model.load_state_dict(torch.load("path_to_model/pytorch_model.bin"))
73
+ model.eval()
74
+
75
+ # Preprocess the image
76
+ transform = transforms.Compose([
77
+ transforms.Grayscale(num_output_channels=1),
78
+ transforms.Resize((28, 28)),
79
+ transforms.ToTensor(),
80
+ transforms.Normalize(mean=[0.5], std=[0.5])
81
+ ])
82
+ image = Image.open("path_to_image").convert("RGB")
83
+ image = transform(image).unsqueeze(0)
84
+
85
+ # Make a prediction
86
+ with torch.no_grad():
87
+ output = model(image)
88
+ _, predicted = torch.max(output.data, 1)
89
+ print(f"Predicted ASL letter: {predicted.item()}")
90
+ ```
91
+
92
+ ## Training Data
93
+
94
+ The model was trained on the `louiecerv/american_sign_language` dataset, which contains images of ASL letters.
95
+
96
+ ## Training Procedure
97
+
98
+ The model was trained using the Adam optimizer with a learning rate of 0.001 and a batch size of 64. The training process included 5 epochs.
99
+
100
+ ## Evaluation Results
101
+
102
+ The model achieved an accuracy of 92% on the validation set.
103
+ """
104
+ with open("model_repo/README.md", "w") as f:
105
+ f.write(model_card)
106
+
107
+ # Streamlit app
108
+ def main():
109
+ st.title("American Sign Language Recognition")
110
+
111
+ # Load the dataset from Hugging Face Hub
112
+ dataset = load_dataset(DATASET_REPO_ID)
113
+
114
+ # Data loaders with preprocessing:
115
+ transform = transforms.Compose([
116
+ transforms.Normalize(mean=[0.5], std=[0.5]) # Adjust mean and std if needed
117
+ ])
118
+
119
+ def collate_fn(batch):
120
+ images = []
121
+ labels = []
122
+ for item in batch:
123
+ if 'pixel_values' in item and 'label' in item:
124
+ image = torch.tensor(item['pixel_values']) # Convert to tensor
125
+ label = item['label']
126
+ try:
127
+ image = transform(image)
128
+ images.append(image)
129
+ labels.append(label)
130
+ except Exception as e:
131
+ print(f"Error processing image: {e}")
132
+ continue # Skip to the next image
133
+
134
+ if not images: # Check if the list is empty!
135
+ return torch.tensor([]), torch.tensor([]) # Return empty tensors if no images loaded
136
+
137
+ images = torch.stack(images).to(device)
138
+ labels = torch.tensor(labels).long().to(device)
139
+ return images, labels
140
+
141
+ train_loader = DataLoader(dataset["train"], batch_size=64, shuffle=True, collate_fn=collate_fn)
142
+ val_loader = DataLoader(dataset["validation"], batch_size=64, collate_fn=collate_fn)
143
+
144
+ # Model, loss, and optimizer
145
+ model = CNN().to(device)
146
+ criterion = nn.CrossEntropyLoss()
147
+ optimizer = optim.Adam(model.parameters(), lr=0.001)
148
+
149
+ # Training loop
150
+ num_epochs = st.slider("Number of Epochs", 1, 20, 5) # Streamlit slider
151
+ if st.button("Train Model"):
152
+ for epoch in range(num_epochs):
153
+ for i, (images, labels) in enumerate(train_loader):
154
+ if images.nelement() == 0: # Check if images tensor is empty
155
+ continue
156
+
157
+ # Forward pass
158
+ outputs = model(images)
159
+ loss = criterion(outputs, labels)
160
+
161
+ # Backward and optimize
162
+ optimizer.zero_grad()
163
+ loss.backward()
164
+ optimizer.step()
165
+
166
+ if (i + 1) % 100 == 0:
167
+ st.write(f'Epoch [{epoch + 1}/{num_epochs}], Step [{i + 1}/{len(train_loader)}], Loss: {loss.item():.4f}')
168
+
169
+ # Validation
170
+ correct = 0
171
+ total = 0
172
+ with torch.no_grad():
173
+ for images, labels in val_loader:
174
+ if images.nelement() == 0: # Check if images tensor is empty
175
+ continue
176
+ outputs = model(images)
177
+ _, predicted = torch.max(outputs.data, 1)
178
+ total += labels.size(0)
179
+ correct += (predicted == labels).sum().item()
180
+
181
+ if total > 0:
182
+ accuracy = 100 * correct / total
183
+ st.write(f'Accuracy of the model on the validation images: {accuracy:.2f}%')
184
+ else:
185
+ st.write("No validation images were processed.")
186
+
187
+ # Save model to Hugging Face Hub
188
+ if HF_TOKEN:
189
+ repo = Repository(local_dir="model_repo", clone_from=MODEL_REPO_ID, use_auth_token=HF_TOKEN)
190
+ model_path = os.path.join(repo.local_dir, "pytorch_model.bin")
191
+ torch.save(model.state_dict(), model_path)
192
+
193
+ create_model_card()
194
+ repo.push_to_hub(commit_message="Trained model and model card", blocking=True)
195
+ st.write(f"Model and model card saved to {MODEL_REPO_ID}")
196
+ else:
197
+ st.warning("HF_TOKEN environment variable not set. Model not saved.")
198
+
199
+ if __name__ == "__main__":
200
+ main()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ transformers
3
+ datasets
4
+ huggingface_hub
5
+ torch
6
+ torchvision
7
+ pandas
8
+ Pillow # or PIL (Pillow is the actively maintained fork)
9
+ scikit-learn