-
Notifications
You must be signed in to change notification settings - Fork 0
/
ilsvrc.py
160 lines (147 loc) · 6.16 KB
/
ilsvrc.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
# python3 -m venv venv
# venv/bin/pip3 install torch
# venv/bin/pip3 install torchvision
import torch
import os
import torchvision
import random
class alexnet(torch.nn.Module):
def __init__(self, outputs):
super(alexnet, self).__init__()
self.features = torch.nn.Sequential(
torch.nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
torch.nn.ReLU(True),
torch.nn.MaxPool2d(kernel_size=3, stride=2),
torch.nn.Conv2d(64, 192, kernel_size=5, padding=2),
torch.nn.ReLU(True),
torch.nn.MaxPool2d(kernel_size=3, stride=2),
torch.nn.Conv2d(192, 384, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(384, 256, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(256, 256, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.MaxPool2d(kernel_size=3, stride=2),
torch.nn.AdaptiveAvgPool2d((6, 6)))
self.classifier = torch.nn.Sequential(
torch.nn.Dropout(0.5),
torch.nn.Linear(256*6*6, 4096),
torch.nn.ReLU(True),
torch.nn.Dropout(0.5),
torch.nn.Linear(4096, 4096),
torch.nn.ReLU(True),
torch.nn.Linear(4096, outputs))
def forward(self, x):
return self.classifier(torch.flatten(self.features(x), 0))
class vgg19(torch.nn.Module):
def __init__(self, outputs):
super(vgg19, self).__init__()
self.features = torch.nn.Sequential(
# first block
torch.nn.Conv2d(3, 64, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(64, 64, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
# second block
torch.nn.Conv2d(64, 128, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(128, 128, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
# third block
torch.nn.Conv2d(128, 256, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(256, 256, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(256, 256, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(256, 256, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
# fourth block
torch.nn.Conv2d(256, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(512, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(512, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(512, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
# fifth block
torch.nn.Conv2d(512, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(512, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(512, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.Conv2d(512, 512, kernel_size=3, padding=1),
torch.nn.ReLU(True),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
# tail
torch.nn.AdaptiveAvgPool2d((7, 7)))
self.classifier = torch.nn.Sequential(
torch.nn.Linear(512*7*7, 4096),
torch.nn.ReLU(True),
torch.nn.Dropout(0.5),
torch.nn.Linear(4096, 4096),
torch.nn.ReLU(True),
torch.nn.Dropout(0.5),
torch.nn.Linear(4096, outputs))
def forward(self, x):
return self.classifier(torch.flatten(self.features(x), 0))
classes = [("dog", "n02106662"),
("cat", "n02124075"),
("elephant", "n02504458"),
("panda", "n02510455")]
directory = "/aux/qobi/Imagenet/ILSVRC2012_img_train"
training_set = [((torchvision.io.read_image(directory+"/"+
class_id+"/"+
filename)/255.0).cuda(),
torch.tensor(list(map(lambda c: c[0],
classes)).index(class_name)).cuda())
for class_name, class_id in classes
for filename in os.listdir(directory+"/"+class_id)]
net = alexnet(4).cuda()
training_transforms = torchvision.transforms.Compose([
torchvision.transforms.RandomResizedCrop(224),
torchvision.transforms.RandomHorizontalFlip(),
torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
test_transforms = torchvision.transforms.Compose([
torchvision.transforms.Resize(256),
torchvision.transforms.CenterCrop(224),
torchvision.transforms.RandomHorizontalFlip(),
torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
optimizer = getattr(torch.optim, "SGD")(net.parameters(), lr = 1e-5)
criterion = torch.nn.CrossEntropyLoss().cuda()
net.train()
for epoch in range(1):
random.shuffle(training_set)
total_loss = 0
for input, label in training_set:
if input.size(0)==3:
output = net(training_transforms(input))
loss = criterion(output, label)
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.tolist()
print("epoch %d, loss %g"%(epoch, total_loss))
net.eval()
total_loss = 0
for input, label in training_set:
if input.size(0)==3:
output = net(test_transforms(input))
if output.tolist().index(max(output.tolist()))==label.tolist():
print("correct")
else:
print("incorrect")
loss = criterion(output, label)
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.tolist()
print("test, loss %g"%total_loss)