Commit
·
3352589
1
Parent(s):
15b2f03
modularised resnet_execute into individual scripts
Browse files- data_utils.py +31 -0
- main.py +62 -0
- train_test.py +68 -0
- utils.py +65 -0
data_utils.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from torchvision import datasets
|
2 |
+
from torch.utils.data import DataLoader
|
3 |
+
import numpy as np
|
4 |
+
import albumentations as A
|
5 |
+
from albumentations.pytorch import ToTensorV2
|
6 |
+
|
7 |
+
def get_train_transform():
|
8 |
+
return A.Compose([
|
9 |
+
A.RandomResizedCrop(height=224, width=224, scale=(0.08, 1.0), ratio=(3/4, 4/3), p=1.0),
|
10 |
+
A.HorizontalFlip(p=0.5),
|
11 |
+
A.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4, hue=0.1, p=0.8),
|
12 |
+
A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
|
13 |
+
ToTensorV2()
|
14 |
+
])
|
15 |
+
|
16 |
+
def get_test_transform():
|
17 |
+
return A.Compose([
|
18 |
+
A.Resize(height=256, width=256),
|
19 |
+
A.CenterCrop(height=224, width=224),
|
20 |
+
A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
|
21 |
+
ToTensorV2()
|
22 |
+
])
|
23 |
+
|
24 |
+
def get_data_loaders(train_transform, test_transform, batch_size_train=128, batch_size_test=500):
|
25 |
+
trainset = datasets.ImageFolder(root='/mnt/imagenet/ILSVRC/Data/CLS-LOC/train', transform=lambda img: train_transform(image=np.array(img))['image'])
|
26 |
+
trainloader = DataLoader(trainset, batch_size=batch_size_train, shuffle=True, num_workers=8, pin_memory=True)
|
27 |
+
|
28 |
+
testset = datasets.ImageFolder(root='/mnt/imagenet/ILSVRC/Data/CLS-LOC/val', transform=lambda img: test_transform(image=np.array(img))['image'])
|
29 |
+
testloader = DataLoader(testset, batch_size=batch_size_test, shuffle=False, num_workers=8, pin_memory=True)
|
30 |
+
|
31 |
+
return trainloader, testloader
|
main.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.optim as optim
|
4 |
+
from resnet_model import ResNet50
|
5 |
+
from data_utils import get_train_transform, get_test_transform, get_data_loaders
|
6 |
+
from train_test import train, test
|
7 |
+
from utils import save_checkpoint, load_checkpoint, plot_training_curves, plot_misclassified_samples
|
8 |
+
|
9 |
+
def main():
|
10 |
+
# Initialize model, loss function, and optimizer
|
11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
+
model = ResNet50().to(device)
|
13 |
+
criterion = nn.CrossEntropyLoss()
|
14 |
+
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9, weight_decay=5e-4)
|
15 |
+
|
16 |
+
# Load data
|
17 |
+
train_transform = get_train_transform()
|
18 |
+
test_transform = get_test_transform()
|
19 |
+
trainloader, testloader = get_data_loaders(train_transform, test_transform)
|
20 |
+
|
21 |
+
# Load checkpoint if it exists
|
22 |
+
checkpoint_path = "checkpoint.pth"
|
23 |
+
try:
|
24 |
+
model, optimizer, start_epoch, _ = load_checkpoint(model, optimizer, checkpoint_path)
|
25 |
+
except FileNotFoundError:
|
26 |
+
print("No checkpoint found, starting from scratch.")
|
27 |
+
start_epoch = 1
|
28 |
+
|
29 |
+
# Store results for plotting
|
30 |
+
results = []
|
31 |
+
learning_rates = []
|
32 |
+
|
33 |
+
# Training loop
|
34 |
+
for epoch in range(start_epoch, 26):
|
35 |
+
train_accuracy1, train_accuracy5, train_loss = train(model, device, trainloader, optimizer, criterion, epoch)
|
36 |
+
test_accuracy1, test_accuracy5, test_loss, misclassified_images, misclassified_labels, misclassified_preds = test(model, device, testloader, criterion)
|
37 |
+
print(f'Epoch {epoch} | Train Top-1 Acc: {train_accuracy1:.2f} | Test Top-1 Acc: {test_accuracy1:.2f}')
|
38 |
+
|
39 |
+
# Append results for this epoch
|
40 |
+
results.append((epoch, train_accuracy1, train_accuracy5, test_accuracy1, test_accuracy5, train_loss, test_loss))
|
41 |
+
learning_rates.append(optimizer.param_groups[0]['lr'])
|
42 |
+
|
43 |
+
# Save checkpoint
|
44 |
+
save_checkpoint(model, optimizer, epoch, test_loss, checkpoint_path)
|
45 |
+
|
46 |
+
# Extract results for plotting
|
47 |
+
epochs = [r[0] for r in results]
|
48 |
+
train_acc1 = [r[1] for r in results]
|
49 |
+
train_acc5 = [r[2] for r in results]
|
50 |
+
test_acc1 = [r[3] for r in results]
|
51 |
+
test_acc5 = [r[4] for r in results]
|
52 |
+
train_losses = [r[5] for r in results]
|
53 |
+
test_losses = [r[6] for r in results]
|
54 |
+
|
55 |
+
# Plot training curves
|
56 |
+
plot_training_curves(epochs, train_acc1, test_acc1, train_acc5, test_acc5, train_losses, test_losses, learning_rates)
|
57 |
+
|
58 |
+
# Plot misclassified samples
|
59 |
+
plot_misclassified_samples(misclassified_images, misclassified_labels, misclassified_preds, classes=['class1', 'class2', ...]) # Replace with actual class names
|
60 |
+
|
61 |
+
if __name__ == '__main__':
|
62 |
+
main()
|
train_test.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from tqdm import tqdm
|
3 |
+
from torch.amp import autocast
|
4 |
+
|
5 |
+
def train(model, device, train_loader, optimizer, criterion, epoch, accumulation_steps=4):
|
6 |
+
model.train()
|
7 |
+
running_loss = 0.0
|
8 |
+
correct1 = 0
|
9 |
+
correct5 = 0
|
10 |
+
total = 0
|
11 |
+
pbar = tqdm(train_loader)
|
12 |
+
|
13 |
+
for batch_idx, (inputs, targets) in enumerate(pbar):
|
14 |
+
inputs, targets = inputs.to(device), targets.to(device)
|
15 |
+
|
16 |
+
with autocast(device_type='cuda'):
|
17 |
+
outputs = model(inputs)
|
18 |
+
loss = criterion(outputs, targets) / accumulation_steps
|
19 |
+
|
20 |
+
loss.backward()
|
21 |
+
|
22 |
+
if (batch_idx + 1) % accumulation_steps == 0 or (batch_idx + 1) == len(train_loader):
|
23 |
+
optimizer.step()
|
24 |
+
optimizer.zero_grad()
|
25 |
+
|
26 |
+
running_loss += loss.item() * accumulation_steps
|
27 |
+
_, predicted = outputs.topk(5, 1, True, True)
|
28 |
+
total += targets.size(0)
|
29 |
+
correct1 += predicted[:, :1].eq(targets.view(-1, 1).expand_as(predicted[:, :1])).sum().item()
|
30 |
+
correct5 += predicted.eq(targets.view(-1, 1).expand_as(predicted)).sum().item()
|
31 |
+
|
32 |
+
pbar.set_description(desc=f'Epoch {epoch} | Loss: {running_loss / (batch_idx + 1):.4f} | Top-1 Acc: {100. * correct1 / total:.2f} | Top-5 Acc: {100. * correct5 / total:.2f}')
|
33 |
+
|
34 |
+
return 100. * correct1 / total, 100. * correct5 / total, running_loss / len(train_loader)
|
35 |
+
|
36 |
+
def test(model, device, test_loader, criterion):
|
37 |
+
model.eval()
|
38 |
+
test_loss = 0
|
39 |
+
correct1 = 0
|
40 |
+
correct5 = 0
|
41 |
+
total = 0
|
42 |
+
misclassified_images = []
|
43 |
+
misclassified_labels = []
|
44 |
+
misclassified_preds = []
|
45 |
+
|
46 |
+
with torch.no_grad():
|
47 |
+
for inputs, targets in test_loader:
|
48 |
+
inputs, targets = inputs.to(device), targets.to(device)
|
49 |
+
outputs = model(inputs)
|
50 |
+
loss = criterion(outputs, targets)
|
51 |
+
|
52 |
+
test_loss += loss.item()
|
53 |
+
_, predicted = outputs.topk(5, 1, True, True)
|
54 |
+
total += targets.size(0)
|
55 |
+
correct1 += predicted[:, :1].eq(targets.view(-1, 1).expand_as(predicted[:, :1])).sum().item()
|
56 |
+
correct5 += predicted.eq(targets.view(-1, 1).expand_as(predicted)).sum().item()
|
57 |
+
|
58 |
+
# Collect misclassified samples
|
59 |
+
for i in range(inputs.size(0)):
|
60 |
+
if targets[i] not in predicted[i, :1]:
|
61 |
+
misclassified_images.append(inputs[i].cpu())
|
62 |
+
misclassified_labels.append(targets[i].cpu())
|
63 |
+
misclassified_preds.append(predicted[i, :1].cpu())
|
64 |
+
|
65 |
+
test_accuracy1 = 100. * correct1 / total
|
66 |
+
test_accuracy5 = 100. * correct5 / total
|
67 |
+
print(f'Test Loss: {test_loss/len(test_loader):.4f}, Top-1 Accuracy: {test_accuracy1:.2f}, Top-5 Accuracy: {test_accuracy5:.2f}')
|
68 |
+
return test_accuracy1, test_accuracy5, test_loss / len(test_loader), misclassified_images, misclassified_labels, misclassified_preds
|
utils.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
from torchvision.utils import make_grid
|
4 |
+
|
5 |
+
def save_checkpoint(model, optimizer, epoch, loss, path):
|
6 |
+
torch.save({
|
7 |
+
'epoch': epoch,
|
8 |
+
'model_state_dict': model.state_dict(),
|
9 |
+
'optimizer_state_dict': optimizer.state_dict(),
|
10 |
+
'loss': loss,
|
11 |
+
}, path)
|
12 |
+
|
13 |
+
def load_checkpoint(model, optimizer, path):
|
14 |
+
checkpoint = torch.load(path)
|
15 |
+
model.load_state_dict(checkpoint['model_state_dict'])
|
16 |
+
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
|
17 |
+
epoch = checkpoint['epoch']
|
18 |
+
loss = checkpoint['loss']
|
19 |
+
return model, optimizer, epoch, loss
|
20 |
+
|
21 |
+
def plot_training_curves(epochs, train_acc1, test_acc1, train_acc5, test_acc5, train_losses, test_losses, learning_rates):
|
22 |
+
plt.figure(figsize=(12, 8))
|
23 |
+
plt.subplot(2, 2, 1)
|
24 |
+
plt.plot(epochs, train_acc1, label='Train Top-1 Acc')
|
25 |
+
plt.plot(epochs, test_acc1, label='Test Top-1 Acc')
|
26 |
+
plt.xlabel('Epoch')
|
27 |
+
plt.ylabel('Accuracy')
|
28 |
+
plt.legend()
|
29 |
+
plt.title('Top-1 Accuracy')
|
30 |
+
|
31 |
+
plt.subplot(2, 2, 2)
|
32 |
+
plt.plot(epochs, train_acc5, label='Train Top-5 Acc')
|
33 |
+
plt.plot(epochs, test_acc5, label='Test Top-5 Acc')
|
34 |
+
plt.xlabel('Epoch')
|
35 |
+
plt.ylabel('Accuracy')
|
36 |
+
plt.legend()
|
37 |
+
plt.title('Top-5 Accuracy')
|
38 |
+
|
39 |
+
plt.subplot(2, 2, 3)
|
40 |
+
plt.plot(epochs, train_losses, label='Train Loss')
|
41 |
+
plt.plot(epochs, test_losses, label='Test Loss')
|
42 |
+
plt.xlabel('Epoch')
|
43 |
+
plt.ylabel('Loss')
|
44 |
+
plt.legend()
|
45 |
+
plt.title('Loss')
|
46 |
+
|
47 |
+
plt.subplot(2, 2, 4)
|
48 |
+
plt.plot(epochs, learning_rates, label='Learning Rate')
|
49 |
+
plt.xlabel('Epoch')
|
50 |
+
plt.ylabel('Learning Rate')
|
51 |
+
plt.legend()
|
52 |
+
plt.title('Learning Rate')
|
53 |
+
|
54 |
+
plt.tight_layout()
|
55 |
+
plt.show()
|
56 |
+
|
57 |
+
def plot_misclassified_samples(misclassified_images, misclassified_labels, misclassified_preds, classes):
|
58 |
+
if misclassified_images:
|
59 |
+
print("\nDisplaying some misclassified samples:")
|
60 |
+
misclassified_grid = make_grid(misclassified_images[:16], nrow=4, normalize=True, scale_each=True)
|
61 |
+
plt.figure(figsize=(8, 8))
|
62 |
+
plt.imshow(misclassified_grid.permute(1, 2, 0))
|
63 |
+
plt.title("Misclassified Samples")
|
64 |
+
plt.axis('off')
|
65 |
+
plt.show()
|