-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.py
More file actions
327 lines (265 loc) · 12.7 KB
/
Copy pathclassifier.py
File metadata and controls
327 lines (265 loc) · 12.7 KB
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
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, Dataset
from torchvision import transforms, models
from PIL import Image
import numpy as np
from pathlib import Path
from sklearn.metrics import classification_report, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
from tqdm import tqdm
import json
class ImageClassifierDataset(Dataset):
"""Custom Dataset for loading images from folder structure"""
def __init__(self, root_dir, transform=None):
self.root_dir = Path(root_dir)
self.transform = transform
self.classes = sorted([d.name for d in self.root_dir.iterdir() if d.is_dir()])
self.class_to_idx = {cls_name: idx for idx, cls_name in enumerate(self.classes)}
self.images = []
self.labels = []
for class_name in self.classes:
class_dir = self.root_dir / class_name
for img_path in class_dir.glob('*'):
if img_path.suffix.lower() in ['.jpg', '.jpeg', '.png', '.bmp']:
self.images.append(img_path)
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.transform:
image = self.transform(image)
return image, label
class AdvancedImageClassifier:
"""Advanced Image Classifier with Transfer Learning"""
def __init__(self, num_classes, model_name='resnet50', pretrained=True):
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"Using device: {self.device}")
self.num_classes = num_classes
self.model_name = model_name
# Load pre-trained model
if model_name == 'resnet50':
self.model = models.resnet50(pretrained=pretrained)
num_features = self.model.fc.in_features
self.model.fc = nn.Sequential(
nn.Dropout(0.5),
nn.Linear(num_features, 512),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, num_classes)
)
elif model_name == 'efficientnet_b0':
self.model = models.efficientnet_b0(pretrained=pretrained)
num_features = self.model.classifier[1].in_features
self.model.classifier = nn.Sequential(
nn.Dropout(0.5),
nn.Linear(num_features, 512),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, num_classes)
)
self.model = self.model.to(self.device)
# Training data transforms with augmentation
self.train_transform = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(15),
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
# Validation/Test transforms
self.val_transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
self.criterion = nn.CrossEntropyLoss()
self.optimizer = None
self.scheduler = None
self.history = {'train_loss': [], 'train_acc': [], 'val_loss': [], 'val_acc': []}
def prepare_data(self, train_dir, val_dir, batch_size=32, num_workers=4):
"""Prepare training and validation data loaders"""
train_dataset = ImageClassifierDataset(train_dir, transform=self.train_transform)
val_dataset = ImageClassifierDataset(val_dir, transform=self.val_transform)
self.train_loader = DataLoader(train_dataset, batch_size=batch_size,
shuffle=True, num_workers=num_workers)
self.val_loader = DataLoader(val_dataset, batch_size=batch_size,
shuffle=False, num_workers=num_workers)
self.class_names = train_dataset.classes
print(f"Classes: {self.class_names}")
print(f"Training samples: {len(train_dataset)}")
print(f"Validation samples: {len(val_dataset)}")
return train_dataset, val_dataset
def train_epoch(self):
"""Train for one epoch"""
self.model.train()
running_loss = 0.0
correct = 0
total = 0
pbar = tqdm(self.train_loader, desc='Training')
for inputs, labels in pbar:
inputs, labels = inputs.to(self.device), labels.to(self.device)
self.optimizer.zero_grad()
outputs = self.model(inputs)
loss = self.criterion(outputs, labels)
loss.backward()
self.optimizer.step()
running_loss += loss.item()
_, predicted = outputs.max(1)
total += labels.size(0)
correct += predicted.eq(labels).sum().item()
pbar.set_postfix({'loss': running_loss/len(pbar), 'acc': 100.*correct/total})
epoch_loss = running_loss / len(self.train_loader)
epoch_acc = 100. * correct / total
return epoch_loss, epoch_acc
def validate(self):
"""Validate the model"""
self.model.eval()
running_loss = 0.0
correct = 0
total = 0
with torch.no_grad():
for inputs, labels in tqdm(self.val_loader, desc='Validation'):
inputs, labels = inputs.to(self.device), labels.to(self.device)
outputs = self.model(inputs)
loss = self.criterion(outputs, labels)
running_loss += loss.item()
_, predicted = outputs.max(1)
total += labels.size(0)
correct += predicted.eq(labels).sum().item()
epoch_loss = running_loss / len(self.val_loader)
epoch_acc = 100. * correct / total
return epoch_loss, epoch_acc
def train(self, epochs=10, lr=0.001, weight_decay=1e-4):
"""Train the model"""
self.optimizer = optim.Adam(self.model.parameters(), lr=lr, weight_decay=weight_decay)
self.scheduler = optim.lr_scheduler.ReduceLROnPlateau(self.optimizer, mode='min',
factor=0.5, patience=3)
best_val_acc = 0.0
for epoch in range(epochs):
print(f"\nEpoch {epoch+1}/{epochs}")
print("-" * 50)
train_loss, train_acc = self.train_epoch()
val_loss, val_acc = self.validate()
self.history['train_loss'].append(train_loss)
self.history['train_acc'].append(train_acc)
self.history['val_loss'].append(val_loss)
self.history['val_acc'].append(val_acc)
print(f"Train Loss: {train_loss:.4f} | Train Acc: {train_acc:.2f}%")
print(f"Val Loss: {val_loss:.4f} | Val Acc: {val_acc:.2f}%")
self.scheduler.step(val_loss)
# Save best model
if val_acc > best_val_acc:
best_val_acc = val_acc
self.save_model('best_model.pth')
print(f"✓ Saved best model with validation accuracy: {val_acc:.2f}%")
print(f"\nTraining completed! Best validation accuracy: {best_val_acc:.2f}%")
def evaluate(self, test_loader=None):
"""Evaluate model and generate detailed metrics"""
if test_loader is None:
test_loader = self.val_loader
self.model.eval()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in tqdm(test_loader, desc='Evaluating'):
inputs = inputs.to(self.device)
outputs = self.model(inputs)
_, predicted = outputs.max(1)
all_preds.extend(predicted.cpu().numpy())
all_labels.extend(labels.numpy())
# Classification report
print("\nClassification Report:")
print(classification_report(all_labels, all_preds, target_names=self.class_names))
# Confusion matrix
cm = confusion_matrix(all_labels, all_preds)
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=self.class_names, yticklabels=self.class_names)
plt.title('Confusion Matrix')
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
plt.tight_layout()
plt.savefig('confusion_matrix.png', dpi=300, bbox_inches='tight')
print("Confusion matrix saved as 'confusion_matrix.png'")
return all_preds, all_labels
def plot_history(self):
"""Plot training history"""
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 5))
# Loss plot
ax1.plot(self.history['train_loss'], label='Train Loss')
ax1.plot(self.history['val_loss'], label='Validation Loss')
ax1.set_title('Model Loss')
ax1.set_xlabel('Epoch')
ax1.set_ylabel('Loss')
ax1.legend()
ax1.grid(True)
# Accuracy plot
ax2.plot(self.history['train_acc'], label='Train Accuracy')
ax2.plot(self.history['val_acc'], label='Validation Accuracy')
ax2.set_title('Model Accuracy')
ax2.set_xlabel('Epoch')
ax2.set_ylabel('Accuracy (%)')
ax2.legend()
ax2.grid(True)
plt.tight_layout()
plt.savefig('training_history.png', dpi=300, bbox_inches='tight')
print("Training history saved as 'training_history.png'")
def predict(self, image_path):
"""Predict class for a single image"""
self.model.eval()
image = Image.open(image_path).convert('RGB')
image_tensor = self.val_transform(image).unsqueeze(0).to(self.device)
with torch.no_grad():
outputs = self.model(image_tensor)
probabilities = torch.softmax(outputs, dim=1)
confidence, predicted = probabilities.max(1)
predicted_class = self.class_names[predicted.item()]
confidence_score = confidence.item() * 100
return predicted_class, confidence_score, probabilities[0].cpu().numpy()
def save_model(self, filepath):
"""Save model checkpoint"""
torch.save({
'model_state_dict': self.model.state_dict(),
'optimizer_state_dict': self.optimizer.state_dict() if self.optimizer else None,
'class_names': self.class_names,
'num_classes': self.num_classes,
'model_name': self.model_name,
'history': self.history
}, filepath)
def load_model(self, filepath):
"""Load model checkpoint"""
checkpoint = torch.load(filepath, map_location=self.device)
self.model.load_state_dict(checkpoint['model_state_dict'])
self.class_names = checkpoint['class_names']
self.history = checkpoint.get('history', self.history)
print(f"Model loaded from {filepath}")
# Example usage
if __name__ == "__main__":
# Initialize classifier
# Replace with your actual number of classes
classifier = AdvancedImageClassifier(num_classes=10, model_name='resnet50')
# Prepare data (organize images in folders: train/class1/, train/class2/, etc.)
# classifier.prepare_data(
# train_dir='data/train',
# val_dir='data/val',
# batch_size=32,
# num_workers=4
# )
# Train the model
# classifier.train(epochs=20, lr=0.001)
# Plot training history
# classifier.plot_history()
# Evaluate on test set
# classifier.evaluate()
# Predict single image
# predicted_class, confidence, probs = classifier.predict('path/to/image.jpg')
# print(f"Predicted: {predicted_class} (Confidence: {confidence:.2f}%)")
print("\nClassifier initialized. Uncomment the code above to use it with your data.")