-
Notifications
You must be signed in to change notification settings - Fork 0
/
convolve.py
345 lines (281 loc) · 12.3 KB
/
convolve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, models
import os
from PIL import Image
import numpy as np
from sklearn.model_selection import train_test_split
from tqdm import tqdm
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix, classification_report
import cv2
import torch.backends.cudnn as cudnn
# Enable CUDA optimizations
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.enabled = True
# Rest of your ASLDataset class remains the same
class ASLDataset(Dataset):
"""
Custom Dataset for loading ASL images.
Expected directory structure:
root_dir/
class_1/
img1.jpg
img2.jpg
...
class_2/
img1.jpg
img2.jpg
...
...
Note: You can combine datasets by adding photos from different datasets into the respective class folders.
The file names can be anything as long as they are in the correct subfolder.
"""
def __init__(self, root_dir, transform=None, preprocessing=None):
self.root_dir = root_dir
self.transform = transform
self.preprocessing = preprocessing
self.classes = sorted([d for d in os.listdir(root_dir)
if os.path.isdir(os.path.join(root_dir, d))])
self.class_to_idx = {cls_name: i for i, cls_name in enumerate(self.classes)}
self.images = []
self.labels = []
for class_name in self.classes:
class_dir = os.path.join(root_dir, class_name)
if os.path.isdir(class_dir):
for img_name in os.listdir(class_dir):
if img_name.endswith(('.jpg', '.jpeg', '.png')):
self.images.append(os.path.join(class_dir, img_name))
self.labels.append(self.class_to_idx[class_name])
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
img_path = self.images[idx]
image = Image.open(img_path).convert('RGB')
label = self.labels[idx]
if self.preprocessing:
image = self.preprocessing(image)
if self.transform:
image = self.transform(image)
return image, label
class TrainingProgress:
def __init__(self):
self.train_losses = []
self.train_accuracies = []
self.val_losses = []
self.val_accuracies = []
def update(self, train_loss, train_acc, val_loss, val_acc):
self.train_losses.append(train_loss)
self.train_accuracies.append(train_acc)
self.val_losses.append(val_loss)
self.val_accuracies.append(val_acc)
def plot_progress(self):
epochs = range(1, len(self.train_losses) + 1)
# Create a figure with two subplots
plt.figure(figsize=(15, 5))
# Plot losses
plt.subplot(1, 2, 1)
plt.plot(epochs, self.train_losses, 'b-', label='Training Loss')
plt.plot(epochs, self.val_losses, 'r-', label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
# Plot accuracies
plt.subplot(1, 2, 2)
plt.plot(epochs, self.train_accuracies, 'b-', label='Training Accuracy')
plt.plot(epochs, self.val_accuracies, 'r-', label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.tight_layout()
plt.savefig('training_progress.png')
plt.close()
pass
def evaluate_model(model, test_loader, criterion, device, classes):
if len(test_loader.dataset) == 0:
print("Test dataset is empty. Please check the test dataset and DataLoader.")
return 0.0, 0.0
model.eval()
test_loss = 0
correct = 0
total = 0
all_preds = []
all_labels = []
print(f"Number of test samples: {len(test_loader.dataset)}") # Check the size of the test dataset
with torch.no_grad():
for images, labels in tqdm(test_loader, desc='Testing'):
images, labels = images.to(device), labels.to(device)
outputs = model(images)
loss = criterion(outputs, labels)
test_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
all_preds.extend(predicted.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
if len(all_preds) == 0 or len(all_labels) == 0:
print("No predictions were made. Please check the test dataset and DataLoader.")
return test_loss / len(test_loader), 0.0
# Calculate confusion matrix
cm = confusion_matrix(all_labels, all_preds)
# Plot confusion matrix
plt.figure(figsize=(15, 15))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=classes, yticklabels=classes)
plt.title('Confusion Matrix')
plt.xlabel('Predicted')
plt.ylabel('True')
plt.tight_layout()
plt.savefig('confusion_matrix.png')
plt.close()
# Generate classification report
report = classification_report(all_labels, all_preds,
target_names=classes, digits=3)
# Save report to file
with open('classification_report.txt', 'w') as f:
f.write(report)
return test_loss / len(test_loader), 100 * correct / total
def train_model(model, train_loader, val_loader, criterion, optimizer,
scheduler, num_epochs, device):
# Add GPU memory optimization
torch.cuda.empty_cache()
progress = TrainingProgress()
best_val_acc = 0.0
scaler = torch.amp.GradScaler('cuda') # Mixed precision scaler
for epoch in range(num_epochs):
# Training phase
model.train()
running_loss = 0.0
train_correct = 0
train_total = 0
for images, labels in tqdm(train_loader, desc=f'Epoch {epoch+1}/{num_epochs}'):
images, labels = images.to(device, non_blocking=True), labels.to(device, non_blocking=True)
optimizer.zero_grad(set_to_none=True) # More efficient than zero_grad()
with torch.amp.autocast('cuda'): # Mixed precision training
outputs = model(images)
loss = criterion(outputs, labels)
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
running_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
train_total += labels.size(0)
train_correct += (predicted == labels).sum().item()
train_loss = running_loss / len(train_loader)
train_acc = 100 * train_correct / train_total
# Validation phase
model.eval()
val_loss = 0.0
val_correct = 0
val_total = 0
with torch.no_grad():
for images, labels in val_loader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
loss = criterion(outputs, labels)
val_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
val_total += labels.size(0)
val_correct += (predicted == labels).sum().item()
val_loss = val_loss / len(val_loader)
val_acc = 100 * val_correct / val_total
# Update learning rate
scheduler.step(val_loss)
# Update progress tracker
progress.update(train_loss, train_acc, val_loss, val_acc)
print(f'Epoch [{epoch+1}/{num_epochs}]')
print(f'Training Loss: {train_loss:.4f}, Accuracy: {train_acc:.2f}%')
print(f'Validation Loss: {val_loss:.4f}, Accuracy: {val_acc:.2f}%')
# Save best model
if val_acc > best_val_acc:
best_val_acc = val_acc
torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'val_acc': val_acc,
}, 'best_model.pth')
print('Model saved!')
# Plot progress after each epoch
progress.plot_progress()
print('-' * 60)
pass
def main():
# Check GPU availability and set device
if not torch.cuda.is_available():
print("CUDA is not available. Please check your PyTorch installation and GPU setup.")
return
device = torch.device("cuda:0")
print(f"Using device: {device}")
print(f"GPU Name: {torch.cuda.get_device_name(0)}")
print(f"Available GPU Memory: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.2f} GB")
# Create mixed precision scaler
scaler = torch.amp.GradScaler('cuda')
# Hyperparameters optimized for GPU
batch_size = 64 # Increased batch size for GPU
num_epochs = 4
learning_rate = 0.001
# Data augmentation and normalization for training
train_transform = transforms.Compose([
transforms.Resize((224, 224)), # Standard DenseNet input size
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ColorJitter(brightness=0.2, contrast=0.2),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
# Validation transform
val_transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
# Define any additional preprocessing steps
preprocessing = transforms.Compose([
transforms.Resize((256, 256)),
transforms.CenterCrop(224)
])
# Update these paths to match your actual dataset location
train_data_dir = "C:/Users/Prakarsh/Desktop/asl_dataset/train"
test_data_dir = "C:/Users/Prakarsh/Desktop/asl_dataset/test"
# Create dataset and dataloaders with GPU optimizations
full_dataset = ASLDataset(train_data_dir, transform=train_transform, preprocessing=False)
test_dataset = ASLDataset(test_data_dir, transform=val_transform, preprocessing=False)
train_size = int(0.8 * len(full_dataset))
val_size = len(full_dataset) - train_size
train_dataset, val_dataset = torch.utils.data.random_split(
full_dataset, [train_size, val_size])
# Use pin_memory=True for faster data transfer to GPU
train_loader = DataLoader(train_dataset, batch_size=batch_size,
shuffle=True, num_workers=4,
pin_memory=True, persistent_workers=True)
val_loader = DataLoader(val_dataset, batch_size=batch_size,
shuffle=False, num_workers=4,
pin_memory=True, persistent_workers=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size,
shuffle=False, num_workers=4,
pin_memory=True, persistent_workers=True)
# Load model and move to GPU
model = models.densenet121(weights='IMAGENET1K_V1') # Use pretrained weights
num_ftrs = model.classifier.in_features
model.classifier = nn.Linear(num_ftrs, len(full_dataset.class_to_idx))
model = model.to(device)
# Loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=2)
# Train the model
train_model(model, train_loader, val_loader, criterion, optimizer,
scheduler, num_epochs, device)
# Evaluate the model on the test dataset
test_loss, test_acc = evaluate_model(model, test_loader, criterion, device, full_dataset.classes)
print(f'Test Loss: {test_loss:.4f}, Test Accuracy: {test_acc:.2f}%')
if __name__ == "__main__":
main()