-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
150 lines (127 loc) · 4.62 KB
/
train.py
File metadata and controls
150 lines (127 loc) · 4.62 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
#resnet34
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import datasets, transforms, models
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report, confusion_matrix
import numpy as np
import seaborn as sns
# Set basic parameters
img_size = 48
epochs = 15
batch_size = 64
learning_rate = 0.0001
# Data augmentation and loading
train_transform = transforms.Compose([
transforms.Resize((img_size, img_size)),
transforms.RandomHorizontalFlip(),
transforms.RandomAffine(degrees=0, translate=(0.1, 0.1)),
#transforms.Grayscale(num_output_channels=1),
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])
test_transform = transforms.Compose([
transforms.Resize((img_size, img_size)),
#transforms.Grayscale(num_output_channels=1),
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])
train_dataset = datasets.ImageFolder(root='/content/data/train', transform=train_transform)
test_dataset = datasets.ImageFolder(root='/content/data/test', transform=test_transform)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
# Load pre-trained ResNet model and modify
model = models.resnet34(pretrained=True)
# Replace the last fully connected layer
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 7)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = model.to(device)
# Loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# Training model code and visualization part remain unchanged, can directly use the code above
# Train the model
train_acc = []
train_loss = []
test_acc = []
test_loss = []
y_true = []
y_pred = []
for epoch in range(epochs):
model.train()
running_loss = 0.0
correct = 0
total = 0
# Training
for i, (images, labels) in enumerate(train_loader):
images, labels = images.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
train_epoch_loss = running_loss / len(train_loader)
train_epoch_acc = 100 * correct / total
train_loss.append(train_epoch_loss)
train_acc.append(train_epoch_acc)
# Test
model.eval()
test_running_loss = 0.0
test_correct = 0
test_total = 0
with torch.no_grad():
for images, labels in test_loader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
loss = criterion(outputs, labels)
test_running_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
test_total += labels.size(0)
test_correct += (predicted == labels).sum().item()
y_true.extend(labels.cpu().numpy())
y_pred.extend(predicted.cpu().numpy())
test_epoch_loss = test_running_loss / len(test_loader)
test_epoch_acc = 100 * test_correct / test_total
test_loss.append(test_epoch_loss)
test_acc.append(test_epoch_acc)
print(f'Epoch [{epoch+1}/{epochs}], '
f'Train Loss: {train_epoch_loss:.4f}, Train Accuracy: {train_epoch_acc:.2f}%, '
f'Test Loss: {test_epoch_loss:.4f}, Test Accuracy: {test_epoch_acc:.2f}%')
# Visualize results
fig, axs = plt.subplots(1, 2, figsize=(12, 4))
# Training and test accuracy
axs[0].plot(train_acc, label='Train Accuracy')
axs[0].plot(test_acc, label='Test Accuracy')
axs[0].set_title('Training and Test Accuracy')
axs[0].set_ylabel('Accuracy')
axs[0].set_xlabel('Epoch')
axs[0].legend()
# Training and test loss
axs[1].plot(train_loss, label='Train Loss')
axs[1].plot(test_loss, label='Test Loss')
axs[1].set_title('Training and Test Loss')
axs[1].set_ylabel('Loss')
axs[1].set_xlabel('Epoch')
axs[1].legend()
plt.show()
class_labels = test_dataset.classes
# Generate confusion matrix and classification report
cm = confusion_matrix(y_true, y_pred)
print('Confusion Matrix')
print(cm)
print('\nClassification Report')
print(classification_report(y_true, y_pred, target_names=class_labels))
# Visualize confusion matrix
plt.figure(figsize=(8,8))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=class_labels, yticklabels=class_labels)
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.title('Confusion Matrix')
plt.show()