-
Notifications
You must be signed in to change notification settings - Fork 34
/
cifar10.py
216 lines (175 loc) · 6.61 KB
/
cifar10.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
from argparse import ArgumentParser
from functools import partial
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn.functional as F
import torchvision
import torchvision.transforms as T
from torch import nn
from torch.utils.data import DataLoader
from torchvision.datasets import CIFAR10
import torchsort
NUM_CLASSES = 10
def topk_loss(input, target, regularization="kl", regularization_strength=1.0):
# TODO: not sure if this is what they mean by logistic map
# "On the other hand, for top-k classification, we find that applying a
# logistic map to squash \theta to [0, 1] and tuning \epsilon is important "
input = F.softmax(input, dim=-1)
# computes ranks of logits
ranks = torchsort.soft_rank(input, regularization, regularization_strength)
# gather ranks at label
ranks_label = ranks.gather(-1, target.view(-1, 1))
# See https://github.com/teddykoker/torchsort/issues/19#issuecomment-831525303
return F.relu(NUM_CLASSES - ranks_label).mean()
class AverageMeter:
def __init__(self, name):
self.name = name
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __str__(self):
return f"{self.name} {self.avg:.4f}"
def main(args):
torch.manual_seed(0)
train_transform = T.Compose(
[
T.RandomCrop(32, padding=4, padding_mode="reflect"),
T.RandomHorizontalFlip(),
T.ToTensor(),
T.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261)),
]
)
test_transform = T.Compose(
[T.ToTensor(), T.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))]
)
train_ds = CIFAR10("./data", train=True, transform=train_transform, download=True)
train_dl = DataLoader(
train_ds, batch_size=args.batch_size, shuffle=True, num_workers=4
)
test_ds = CIFAR10("./data", train=False, transform=test_transform, download=True)
test_dl = DataLoader(
test_ds, batch_size=args.batch_size, shuffle=False, num_workers=4
)
# from the paper https://arxiv.org/abs/2002.08871:
#
# > Following Cuturi et al. (2019), we use a vanilla CNN (4 Conv2D with 2 maxpooling
# > layers, ReLU activation, 2 fully connected layers with batch norm on each) ), the
# > ADAM optimizer (Kingma & Ba, 2014) with a constant step size of 10−4, and set k = 1.
#
# there are no other details about the architecture in the paper. It reads they are
# applying batch norm after the fully connected layers, but I think they meant on the
# Conv2D.
hidden = args.hidden_size
model = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=hidden, kernel_size=3, padding=1),
nn.BatchNorm2d(hidden),
nn.ReLU(),
nn.Conv2d(in_channels=hidden, out_channels=hidden, kernel_size=3, padding=1),
nn.BatchNorm2d(hidden),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.Conv2d(in_channels=hidden, out_channels=hidden, kernel_size=3, padding=1),
nn.BatchNorm2d(hidden),
nn.ReLU(),
nn.Conv2d(in_channels=hidden, out_channels=hidden, kernel_size=3, padding=1),
nn.BatchNorm2d(hidden),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.Flatten(),
nn.Linear(8 * 8 * hidden, 512),
nn.ReLU(),
nn.Linear(512, NUM_CLASSES),
).to(args.device)
loss_fn = (
F.cross_entropy
if args.loss_fn == "cross_entropy"
else partial(
topk_loss,
regularization=args.regularization,
regularization_strength=args.regularization_strength,
)
)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
test_accs = []
for epoch in range(args.epochs):
train_loss = AverageMeter("train_loss")
test_acc = AverageMeter("test_acc")
# train step
model.train()
for (img, label) in train_dl:
img, label = img.to(args.device), label.to(args.device)
optimizer.zero_grad()
pred = model(img)
loss = loss_fn(pred, label)
loss.backward()
optimizer.step()
train_loss.update(loss.item(), n=img.shape[0])
# test step
model.eval()
with torch.no_grad():
for (img, label) in test_dl:
img, label = img.to(args.device), label.to(args.device)
logit = model(img)
test_acc.update(
(logit.argmax(-1) == label).float().mean(), img.shape[0]
)
print(epoch, test_acc, train_loss)
test_accs.append(test_acc.avg)
def smooth(xs, factor=0.9):
out = [xs[0]]
for x in xs[1:]:
out.append(out[-1] * factor + x * (1 - factor))
return out
test_accs = torch.stack(test_accs).cpu().numpy()
regularization = (
f"_{args.regularization}_{args.regularization_strength}"
if args.loss_fn == "topk"
else ""
)
np.save(f"{args.loss_fn}{regularization}_acc.npy", test_accs)
def plot():
def smooth(xs, factor=0.9):
out = [xs[0]]
for x in xs[1:]:
out.append(out[-1] * factor + x * (1 - factor))
return out
colors = ["tab:blue", "tab:orange"]
plt.figure(figsize=(5, 3))
for i, file in enumerate(Path("./").glob("*.npy")):
print(file)
test_accs = np.load(file)
plt.plot(test_accs, alpha=0.1, color=colors[i])
plt.plot(smooth(test_accs), color=colors[i], label=file.stem)
plt.ylim(0.78, 0.88)
plt.xlabel("Epochs")
plt.ylabel("Test accuracy")
plt.title("CIFAR-10")
plt.legend()
plt.savefig("extra/cifar10_test_accuracy.png", dpi=150, bbox_inches="tight")
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--batch_size", type=int, default=1024)
parser.add_argument(
"--loss_fn", choices=["cross_entropy", "topk"], default="cross_entropy"
)
parser.add_argument("--regularization", default="kl")
parser.add_argument("--regularization_strength", type=float, default=1.0)
parser.add_argument("--hidden_size", type=int, default=64)
parser.add_argument("--epochs", type=int, default=600)
parser.add_argument("--plot", action="store_true")
args = parser.parse_args()
args.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if args.plot:
plot()
else:
main(args)