-
Notifications
You must be signed in to change notification settings - Fork 0
/
convmiver.py
264 lines (212 loc) · 7.63 KB
/
convmiver.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
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data import DataLoader
batch_size = 32
img_height = 128
img_width = 128
learning_rate = 0.001
weight_decay = 0.0001
num_epochs = 25
train_path = "train/"
test_path = "test/"
train_transform = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.Resize((img_height, img_width)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
test_transform = transforms.Compose([
transforms.Resize((img_height, img_width)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
train_dataset = datasets.ImageFolder(
train_path,
transform=train_transform
)
test_dataset = datasets.ImageFolder(
test_path,
transform=test_transform
)
train_size = int(0.85 * len(train_dataset))
val_size = len(train_dataset) - train_size
train_dataset, val_dataset = torch.utils.data.random_split(train_dataset, [train_size, val_size])
train_loader = DataLoader(
train_dataset,
batch_size=batch_size,
shuffle=True,
num_workers=4,
pin_memory=True
)
val_loader = DataLoader(
val_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=4,
pin_memory=True
)
test_loader = DataLoader(
test_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=4,
pin_memory=True
)
class ActivationBlock(nn.Module):
def __init__(self, num_features):
super(ActivationBlock, self).__init__()
self.activation = nn.GELU()
self.batch_norm = nn.BatchNorm2d(num_features)
def forward(self, x):
x = self.activation(x)
x = self.batch_norm(x)
return x
class ConvStem(nn.Module):
def __init__(self, filters, patch_size):
super(ConvStem, self).__init__()
self.conv = nn.Conv2d(3, filters, kernel_size=patch_size, stride=patch_size)
def forward(self, x):
x = self.conv(x)
return x
class ConvMixerBlock(nn.Module):
def __init__(self, filters, kernel_size):
super(ConvMixerBlock, self).__init__()
self.depthwise_conv = nn.Conv2d(filters, filters, kernel_size=kernel_size, padding=kernel_size//2, groups=filters)
self.pointwise_conv = nn.Conv2d(filters, filters, kernel_size=1)
self.activation_block = ActivationBlock(filters)
def forward(self, x):
x0 = x
x = self.depthwise_conv(x)
x = self.activation_block(x + x0)
x = self.pointwise_conv(x)
x = self.activation_block(x)
return x
class ConvMixer(nn.Module):
def __init__(self, image_size, filters, depth, kernel_size, patch_size, num_classes):
super(ConvMixer, self).__init__()
self.image_size = image_size
self.filters = filters
self.depth = depth
self.kernel_size = kernel_size
self.patch_size = patch_size
self.data_augmentation = nn.Sequential(
transforms.ColorJitter(brightness=0.2),
)
self.stem = ConvStem(filters, patch_size)
self.mixer_blocks = nn.Sequential()
for i in range(depth):
self.mixer_blocks.add_module(f"block_{i}", ConvMixerBlock(filters, kernel_size))
self.classification_block = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Flatten(),
nn.Linear(filters, num_classes),
nn.Softmax(dim=1)
)
def forward(self, x):
x = self.data_augmentation(x)
x = x / 255.0
x = self.stem(x)
x = self.mixer_blocks(x)
x = self.classification_block(x)
return x
model = ConvMixer(image_size=img_height, filters=256, depth=8, kernel_size=5, patch_size=2, num_classes=6)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.AdamW(model.parameters(), lr=learning_rate, weight_decay=weight_decay)
def train(model, train_loader, val_loader, criterion, optimizer, num_epochs):
best_accuracy = 0.0
for epoch in range(num_epochs):
model.train()
train_loss = 0.0
train_correct = 0
train_total = 0
for images, labels in train_loader:
images = images.to(device)
labels = labels.to(device)
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
train_loss += loss.item()
_, predicted = outputs.max(1)
train_total += labels.size(0)
train_correct += predicted.eq(labels).sum().item()
train_loss /= len(train_loader)
train_accuracy = 100.0 * train_correct / train_total
model.eval()
val_loss = 0.0
val_correct = 0
val_total = 0
with torch.no_grad():
for images, labels in val_loader:
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
loss = criterion(outputs, labels)
val_loss += loss.item()
_, predicted = outputs.max(1)
val_total += labels.size(0)
val_correct += predicted.eq(labels).sum().item()
val_loss /= len(val_loader)
val_accuracy = 100.0 * val_correct / val_total
if val_accuracy > best_accuracy:
best_accuracy = val_accuracy
torch.save(model.state_dict(), "./checkpoint.pt")
print(f"Epoch [{epoch+1}/{num_epochs}] - Train Loss: {train_loss:.4f} - Train Accuracy: {train_accuracy:.2f}% - Val Loss: {val_loss:.4f} - Val Accuracy: {val_accuracy:.2f}%")
model.load_state_dict(torch.load("./checkpoint.pt"))
model.eval()
test_loss = 0.0
test_correct = 0
test_total = 0
predictions = []
labels = []
with torch.no_grad():
for images, labels in test_loader:
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
loss = criterion(outputs, labels)
test_loss += loss.item()
_, predicted = outputs.max(1)
test_total += labels.size(0)
test_correct += predicted.eq(labels).sum().item()
predictions += predicted.tolist()
labels += labels.tolist()
test_loss /= len(test_loader)
test_accuracy = 100.0 * test_correct / test_total
print(f"Test Loss: {test_loss:.4f} - Test Accuracy: {test_accuracy:.2f}%")
train(model, train_loader, val_loader, criterion, optimizer, num_epochs)
model.load_state_dict(torch.load("./checkpoint.pt"))
model.eval()
test_loss = 0.0
test_correct = 0
test_total = 0
predictions = []
labels = []
with torch.no_grad():
for images, targets in test_loader:
images = images.to(device)
targets = targets.to(device)
outputs = model(images)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = outputs.max(1)
test_total += targets.size(0)
test_correct += predicted.eq(targets).sum().item()
predictions += predicted.tolist()
labels += targets.tolist()
test_loss /= len(test_loader)
test_accuracy = 100.0 * test_correct / test_total
print(f"Test Loss: {test_loss:.4f} - Test Accuracy: {test_accuracy:.2f}%")
from sklearn.metrics import classification_report, confusion_matrix
class_names = []
print("Confusion Matrix")
print(confusion_matrix(labels, predictions))
print("Classification Report")
print(classification_report(labels, predictions, target_names=class_names))