forked from jatentaki/sharpened_cosine_similarity_torch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_cifar10_18_4.py
185 lines (155 loc) · 5.97 KB
/
model_cifar10_18_4.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
# -*- coding: utf-8 -*-
"""
Compare with the CIFAR-10 Papers With Code pareto frontier here
https://paperswithcode.com/sota/image-classification-on-cifar-10?dimension=PARAMS
"""
import os
import sys
import time
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim.lr_scheduler import OneCycleLR
from torch.utils.data import DataLoader
import torchvision
from torchvision.datasets import CIFAR10
import torchvision.transforms as transforms
from absolute_pooling import MaxAbsPool2d
from sharpened_cosine_similarity import SharpenedCosineSimilarity
batch_size = 128
n_epochs = 100
max_lr = .01
n_runs = 1000
# Allow for a version to be provided at the command line, as in
if len(sys.argv) > 1:
version = sys.argv[1]
else:
version = "test"
# Lay out the desitinations for all the results.
accuracy_results_path = os.path.join(f"results", f"accuracy_{version}.npy")
accuracy_history_path = os.path.join(
"results", f"accuracy_history_{version}.npy")
loss_results_path = os.path.join("results", f"loss_{version}.npy")
os.makedirs("results", exist_ok=True)
training_set = CIFAR10(
root=os.path.join('.', 'data', 'CIFAR10'),
train=True,
download=True,
transform=transforms.Compose([
transforms.RandomCrop(32, padding=4),
# transforms.ColorJitter(
# brightness=0.2, contrast=0.2, saturation=0.2, hue=0.2),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()
]))
testing_set = CIFAR10(
root=os.path.join('.', 'data', 'CIFAR10'),
train=False,
download=True,
transform=transforms.Compose([transforms.ToTensor()]))
training_loader = DataLoader(
training_set,
batch_size=batch_size,
shuffle=True)
testing_loader = DataLoader(
testing_set,
batch_size=batch_size,
shuffle=False)
class Network(nn.Module):
def __init__(self):
super().__init__()
self.scs1 = SharpenedCosineSimilarity(
in_channels=3, out_channels=24, kernel_size=3, padding=0)
self.pool1 = MaxAbsPool2d(kernel_size=2, stride=2, ceil_mode=True)
self.scs2 = SharpenedCosineSimilarity(
in_channels=24, out_channels=48, kernel_size=3, padding=1)
self.pool2 = MaxAbsPool2d(kernel_size=2, stride=2, ceil_mode=True)
self.scs3 = SharpenedCosineSimilarity(
in_channels=48, out_channels=96, kernel_size=3, padding=1)
self.pool3 = MaxAbsPool2d(kernel_size=2, stride=2, ceil_mode=True)
self.out = nn.Linear(in_features=96*4*4, out_features=10)
def forward(self, t):
t = self.scs1(t)
t = self.pool1(t)
t = self.scs2(t)
t = self.pool2(t)
t = self.scs3(t)
t = self.pool3(t)
t = t.reshape(-1, 96*4*4)
t = self.out(t)
return t
# Restore any previously generated results.
try:
accuracy_results = np.load(accuracy_results_path).tolist()
accuracy_histories = np.load(accuracy_history_path).tolist()
loss_results = np.load(loss_results_path).tolist()
except Exception:
loss_results = []
accuracy_results = []
accuracy_histories = []
for i_run in range(n_runs):
network = Network()
optimizer = optim.Adam(network.parameters(), lr=max_lr)
scheduler = OneCycleLR(
optimizer,
max_lr=max_lr,
steps_per_epoch=len(training_loader),
epochs=n_epochs)
epoch_accuracy_history = []
for i_epoch in range(n_epochs):
epoch_start_time = time.time()
epoch_training_loss = 0
epoch_testing_loss = 0
epoch_training_num_correct = 0
epoch_testing_num_correct = 0
for batch in training_loader:
images = batch[0]
labels = batch[1]
preds = network(images)
loss = F.cross_entropy(preds, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step()
epoch_training_loss += loss.item() * training_loader.batch_size
epoch_training_num_correct += (
preds.argmax(dim=1).eq(labels).sum().item())
epoch_duration = time.time() - epoch_start_time
epoch_duration = time.time() - epoch_start_time
training_loss = epoch_training_loss / len(training_loader.dataset)
training_accuracy = (
epoch_training_num_correct / len(training_loader.dataset))
# At the end of each epoch run the testing data through an
# evaluation pass to see how the model is doing.
# Specify no_grad() to prevent a nasty out-of-memory condition.
with torch.no_grad():
test_preds = torch.tensor([])
for batch in testing_loader:
images, labels = batch
preds = network(images)
loss = F.cross_entropy(preds, labels)
test_preds = torch.cat((test_preds, preds), dim = 0)
epoch_testing_loss += loss.item() * testing_loader.batch_size
epoch_testing_num_correct += (
preds.argmax(dim=1).eq(labels).sum().item())
testing_loss = epoch_testing_loss / len(testing_loader.dataset)
testing_accuracy = (
epoch_testing_num_correct / len(testing_loader.dataset))
epoch_accuracy_history.append(testing_accuracy)
print(
f"run: {i_run} "
f"epoch: {i_epoch} "
f"duration: {epoch_duration:.04} "
f"training loss: {training_loss:.04} "
f"testing loss: {testing_loss:.04} "
f"training accuracy: {100 * training_accuracy:.04}% "
f"testing accuracy: {100 * testing_accuracy:.04}%"
)
accuracy_histories.append(epoch_accuracy_history)
accuracy_results.append(testing_accuracy)
loss_results.append(testing_loss)
np.save(accuracy_history_path, np.array(accuracy_histories))
np.save(accuracy_results_path, np.array(accuracy_results))
np.save(loss_results_path, np.array(loss_results))