-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
288 lines (220 loc) · 8.76 KB
/
train.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import torch
from torch import nn
import torchvision as tv
import time
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import torchvision.transforms as transforms
import os
from os import listdir
from sys import argv
#получили параметры файла
SCRIPT_NAME, DATA_DIR, SAVE_PATH, MODEL_NAME, PRETRAINED = argv
print(SCRIPT_NAME)
print(DATA_DIR)
# Define a transform to convert PIL image to a Torch tensor
#Data augmentation added
transform = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
num_classes=2
# Collecting data from the DATA_DIR folder (train)
def get_data(DATA_DIR):
train_data=[]
for (root,dirs,files) in os.walk(DATA_DIR, topdown=True):
class_val=-1
for name_dir in dirs:
class_val+=1 #для муравьев -0, для пчел - 1
for file_name in listdir(DATA_DIR+name_dir+"/"):
file_name=DATA_DIR+name_dir+"/"+file_name
image = Image.open(file_name)
img_tensor = transform(image)
train_data.append((img_tensor, class_val))
return train_data
#DATA_DIR="hymenoptera_data/train/"
train_data=get_data(DATA_DIR)
if len(train_data)==0:
print("There is no such dirrectory")
exit()
BATCH_SIZE=32
train_iter = torch.utils.data.DataLoader(train_data, batch_size=BATCH_SIZE, shuffle=True)
def train(net, train_iter, trainer, num_epochs):
loss = nn.CrossEntropyLoss(reduction="mean")
net.train()
for epoch in range(num_epochs):
train_l_sum, train_acc_sum, n, start = 0.0, 0.0, 0, time.time()
for X, y in train_iter:
trainer.zero_grad()
y_hat = net(X)
l = loss(y_hat, y)
l.backward()
trainer.step()
train_l_sum += l.item()
train_acc_sum += (y_hat.argmax(axis=1) == y).sum().item()
n += y.shape[0]
print("Step. time since epoch: {:.3f}. Train acc: {:.3f}. Train Loss: {:.3f}".format(time.time() - start,
(y_hat.argmax(axis=1) == y).sum().item() / y.shape[0], l.item()))
print('epoch %d, loss %.4f, train acc %.3f, time %.1f sec'
% (epoch + 1, train_l_sum / n, train_acc_sum / n, time.time() - start))
#VGG16 model
class VGG16(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.layer2 = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.layer3=nn.Sequential(
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.layer4 = nn.Sequential(
nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.layer5 = nn.Sequential(
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.classifier=nn.Sequential(
nn.Flatten(),
nn.Linear(25088, 4096), nn.ReLU(), nn.Dropout(0.5),
nn.Linear(4096, 4096), nn.ReLU(), nn.Dropout(0.5),
nn.Linear(4096,num_classes))
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.layer5(x)
x = self.classifier(x)
return x
# Define ResNet block
class ResBlock(nn.Module):
def __init__(self, in_channels, out_channels, downsample):
super().__init__()
if downsample:
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=2, padding=1)
self.shortcut = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=2),
nn.BatchNorm2d(out_channels)
)
else:
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1)
self.shortcut = nn.Sequential()
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(out_channels)
self.bn2 = nn.BatchNorm2d(out_channels)
def forward(self, input):
shortcut = self.shortcut(input)
input = nn.ReLU()(self.bn1(self.conv1(input)))
input = nn.ReLU()(self.bn2(self.conv2(input)))
input = input + shortcut
return nn.ReLU()(input)
#ResNet18 model
class ResNet18(nn.Module):
def __init__(self, in_channels, resblock, outputs=2):
super().__init__()
self.layer0 = nn.Sequential(
nn.Conv2d(in_channels, 64, kernel_size=7, stride=2, padding=3),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.ReLU()
)
self.layer1 = nn.Sequential(
resblock(64, 64, downsample=False),
resblock(64, 64, downsample=False)
)
self.layer2 = nn.Sequential(
resblock(64, 128, downsample=True),
resblock(128, 128, downsample=False)
)
self.layer3 = nn.Sequential(
resblock(128, 256, downsample=True),
resblock(256, 256, downsample=False)
)
self.layer4 = nn.Sequential(
resblock(256, 512, downsample=True),
resblock(512, 512, downsample=False)
)
self.gap = torch.nn.AdaptiveAvgPool2d(1)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(512, num_classes))
def forward(self, x):
x=self.layer0(x)
x=self.layer1(x)
x=self.layer2(x)
x=self.layer3(x)
x=self.layer4(x)
x=self.gap(x)
x=self.classifier(x)
return x
#choosing model
if MODEL_NAME=='VGG16':
if PRETRAINED=="False":
#model=VGG16(conv_arch, vgg_block, num_classes)
model=VGG16(num_classes)
trainer = torch.optim.SGD(model.parameters(), lr=0.0001, momentum=0.9)
else:
model = tv.models.vgg16(pretrained=True)
for param in model.parameters():
param.requires_grad = False
#дообучиваем classifier
model.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(25088, 4096), nn.ReLU(), nn.Dropout(0.5),
nn.Linear(4096, 4096), nn.ReLU(), nn.Dropout(0.5),
nn.Linear(4096,num_classes))
params_to_update = []
for name, param in model.named_parameters():
if param.requires_grad == True:
params_to_update.append(param)
trainer = torch.optim.SGD(params_to_update, lr=0.0001, momentum=0.9)
elif MODEL_NAME=='ResNet18':
if PRETRAINED =="False":
model = ResNet18(3, ResBlock, outputs=2)
trainer = torch.optim.SGD(model.parameters(), lr=0.0001, momentum=0.9)
else:
model = tv.models.resnet18(pretrained=True)
for param in model.parameters():
param.requires_grad = False
# дообучиваем classifier
model.fc = nn.Linear(512, num_classes)
params_to_update = []
for name, param in model.named_parameters():
if param.requires_grad == True:
params_to_update.append(param)
trainer = torch.optim.SGD(params_to_update, lr=0.0001, momentum=0.9)
#тренируем модель
num_epochs=20
train(model, train_iter, trainer, 20)
#сохраняем модель
torch.save(model.state_dict(), SAVE_PATH)