-
Notifications
You must be signed in to change notification settings - Fork 1
/
NN_model_pytorch.py
371 lines (317 loc) · 16.6 KB
/
NN_model_pytorch.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from torchvision.models.resnet import resnet50
from torchvision.datasets import CIFAR10, MNIST
from tqdm import tqdm
import numpy as np
from PIL import Image
import itertools
import time
train_transform = transforms.Compose([
transforms.RandomResizedCrop(32),
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomApply([transforms.ColorJitter(0.4, 0.4, 0.4, 0.1)], p=0.8),
transforms.RandomGrayscale(p=0.2),
transforms.ToTensor(),
transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])])
test_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])])
Lenet_transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5, ))])
SymbolNet_transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (1,))])
class ResNet50(nn.Module):
def __init__(self, num_class, pretrained_path, loss_criterion, batch_size=512, freeze_feature=True, num_workers=16):
super(ResNet50, self).__init__()
# encoder
self.f = []
for name, module in resnet50().named_children():
if name == 'conv1':
module = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
if not isinstance(module, nn.Linear) and not isinstance(module, nn.MaxPool2d):
self.f.append(module)
self.f = nn.Sequential(*self.f)
# classifier
self.fc = nn.Linear(2048, 10, bias=True)
if pretrained_path is not None:
self.load_state_dict(torch.load(pretrained_path, map_location='cpu'), strict=False)
self.fc = nn.Linear(2048, num_class, bias=True) # Initialize new weight
self.loss_criterion = loss_criterion
self.batch_size = batch_size
self.optimizer = None
self.set_freeze_feature(freeze_feature)
self.num_workers = num_workers
def forward(self, x):
x = self.f(x)
feature = torch.flatten(x, start_dim=1)
out = self.fc(feature)
return out, F.normalize(feature, dim=-1)
def set_freeze_feature(self, freeze):
for param in self.f.parameters():
param.requires_grad = not freeze
parameters = self.fc.parameters() if freeze else self.parameters()
self.optimizer = optim.Adam(parameters, lr=1e-3, weight_decay=1e-6)
# train or test for several epochs
def train_val(self, epochs, is_train, data_loader=None, X=None, y=None):
if data_loader is None:
dataset = TorchDataset(X, y, train_transform if is_train else test_transform)
data_loader = DataLoader(dataset, batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers, pin_memory=True)
if is_train:
self.train() # for BN
else:
self.eval()
epochs = 1
#for name, parameters in self.named_parameters():
# if name == "f.1.weight" or name == "f.6.2.bn3.bias" or name == "f.6.0.bn1.weight":
# print(name, ':', parameters.size(), parameters)
for epoch in range(1, epochs+1):
total_loss, total_correct_1, total_num, data_bar = 0.0, 0.0, 0, tqdm(data_loader)
with (torch.enable_grad() if is_train else torch.no_grad()):
for data, target in data_bar:
data, target = data.cuda(non_blocking=True), target.cuda(non_blocking=True)
out, feature = self(data)
loss = self.loss_criterion(out, target)
if is_train:
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
total_num += data.size(0)
total_loss += loss.item() * data.size(0)
prediction = torch.argsort(out, dim=-1, descending=True)
total_correct_1 += torch.sum((prediction[:, 0:1] == target.unsqueeze(dim=-1)).any(dim=-1).float()).item()
data_bar.set_description('{} Epoch: [{}/{}] Loss: {:.4f} ACC@1: {:.2f}%'
.format('Train' if is_train else 'Test', epoch, epochs, total_loss / total_num, total_correct_1 / total_num * 100))
return total_loss / total_num, total_correct_1 / total_num * 100
def predict(self, data_loader=None, X=None, is_train=False):
if data_loader is None:
y = np.zeros(X.shape[0], dtype='long')
dataset = TorchDataset(X, y, train_transform if is_train else test_transform)
data_loader = DataLoader(dataset, batch_size=self.batch_size, shuffle=False, num_workers=self.num_workers, pin_memory=True)
self.eval()
results_out, results_feature = [], []
with torch.no_grad():
for data, _ in data_loader:
data = data.cuda(non_blocking=True)
out, feature = self(data)
results_out.append(out), results_feature.append(feature)
return torch.softmax(torch.cat(results_out, axis=0), axis=1), torch.cat(results_feature, axis=0)
def predict_classes(self, data_loader=None, X=None, is_train=False):
return self.predict(data_loader, X, is_train)[0].argmax(axis=1)
class LeNet(nn.Module):
def __init__(self, num_class, loss_criterion, pretrained_path=None, batch_size=512, num_workers=16):
super(LeNet, self).__init__()
# encoder
self.f1 = [
nn.Conv2d(1, 6, 5),
nn.MaxPool2d(2, 2), # 6 24 24 -> 6 12 12
nn.ReLU(True),
nn.Conv2d(6, 16, 5), # 6 12 12 -> 16 8 8
nn.MaxPool2d(2, 2), # 16 8 8 -> 16 4 4
nn.ReLU(True)
]
self.f2 = [
nn.Linear(16 * 4 * 4, 120),
nn.ReLU(),
nn.Linear(120, 84)
]
self.f3 = [
nn.ReLU(),
nn.Linear(84, num_class)
]
self.f1 = nn.Sequential(*self.f1)
self.f2 = nn.Sequential(*self.f2)
self.f3 = nn.Sequential(*self.f3)
if pretrained_path is not None:
self.load_state_dict(torch.load(pretrained_path, map_location='cpu'), strict=False)
self.loss_criterion = loss_criterion
self.batch_size = batch_size
self.optimizer = optim.Adam(self.parameters(), lr=1e-3)
self.num_workers = num_workers
def forward(self, x):
x = self.f1(x)
x = x.view(-1, 16 * 4 * 4)
feature = self.f2(x)
out = self.f3(feature)
return out, F.normalize(feature, dim=-1)
# train or test for several epochs
def train_val(self, epochs, is_train, data_loader=None, X=None, y=None):
if data_loader is None:
dataset = TorchDataset(X, y, Lenet_transform)
data_loader = DataLoader(dataset, batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers, pin_memory=True)
if is_train:
self.train() # for BN
else:
self.eval()
epochs = 1
for epoch in range(1, epochs+1):
total_loss, total_correct_1, total_num, data_bar = 0.0, 0.0, 0, tqdm(data_loader)
with (torch.enable_grad() if is_train else torch.no_grad()):
for data, target in data_bar:
data, target = data.cuda(non_blocking=True), target.cuda(non_blocking=True)
out, feature = self(data)
loss = self.loss_criterion(out, target)
if is_train:
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
total_num += data.size(0)
total_loss += loss.item() * data.size(0)
prediction = torch.argsort(out, dim=-1, descending=True)
total_correct_1 += torch.sum((prediction[:, 0:1] == target.unsqueeze(dim=-1)).any(dim=-1).float()).item()
data_bar.set_description('{} Epoch: [{}/{}] Loss: {:.4f} ACC@1: {:.2f}%'
.format('Train' if is_train else 'Test', epoch, epochs, total_loss / total_num, total_correct_1 / total_num * 100))
return total_loss / total_num, total_correct_1 / total_num * 100
def predict(self, data_loader=None, X=None, is_train=False):
if data_loader is None:
y = np.zeros(X.shape[0], dtype='long')
dataset = TorchDataset(X, y, Lenet_transform)
data_loader = DataLoader(dataset, batch_size=self.batch_size, shuffle=False, num_workers=self.num_workers, pin_memory=True)
self.eval()
results_out, results_feature = [], []
with torch.no_grad():
for data, _ in data_loader:
data = data.cuda(non_blocking=True)
out, feature = self(data)
results_out.append(out), results_feature.append(feature)
return torch.softmax(torch.cat(results_out, axis=0), axis=1), torch.cat(results_feature, axis=0)
def predict_classes(self, data_loader=None, X=None, is_train=False):
return self.predict(data_loader, X, is_train)[0].argmax(axis=1)
class SymbolNet(nn.Module):
def __init__(self, num_class, loss_criterion, pretrained_path=None, batch_size=512, num_workers=16):
super(SymbolNet, self).__init__()
# encoder
self.conv1 = nn.Conv2d(1, 32, 3, stride = 1, padding = 1)
self.conv2 = nn.Conv2d(32, 64, 3, stride = 1, padding = 1)
self.dropout1 = nn.Dropout2d(0.25)
self.dropout2 = nn.Dropout2d(0.5)
self.fc1 = nn.Linear(30976, 128)
self.fc2 = nn.Linear(128, num_class)
if pretrained_path is not None:
self.load_state_dict(torch.load(pretrained_path, map_location='cpu'), strict=False)
self.loss_criterion = loss_criterion
self.batch_size = batch_size
self.optimizer = optim.Adam(self.parameters(), lr=1e-3)
self.num_workers = num_workers
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.max_pool2d(x, 2)
x = self.dropout1(x)
x = torch.flatten(x, 1)
feature = self.fc1(x)
x = F.relu(feature)
x = self.dropout2(x)
out = self.fc2(x)
return out, F.normalize(feature, dim=-1)
# train or test for several epochs
def train_val(self, epochs, is_train, data_loader=None, X=None, y=None):
if data_loader is None:
dataset = TorchDataset(X, y, SymbolNet_transform)
data_loader = DataLoader(dataset, batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers, pin_memory=True)
if is_train:
self.train() # for BN
else:
self.eval()
epochs = 1
for epoch in range(1, epochs+1):
total_loss, total_correct_1, total_num, data_bar = 0.0, 0.0, 0, tqdm(data_loader)
with (torch.enable_grad() if is_train else torch.no_grad()):
for data, target in data_bar:
data, target = data.cuda(non_blocking=True), target.cuda(non_blocking=True)
out, feature = self(data)
loss = self.loss_criterion(out, target)
if is_train:
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
total_num += data.size(0)
total_loss += loss.item() * data.size(0)
prediction = torch.argsort(out, dim=-1, descending=True)
total_correct_1 += torch.sum((prediction[:, 0:1] == target.unsqueeze(dim=-1)).any(dim=-1).float()).item()
data_bar.set_description('{} Epoch: [{}/{}] Loss: {:.4f} ACC@1: {:.2f}%'
.format('Train' if is_train else 'Test', epoch, epochs, total_loss / total_num, total_correct_1 / total_num * 100))
return total_loss / total_num, total_correct_1 / total_num * 100
def predict(self, data_loader=None, X=None, is_train=False):
if data_loader is None:
y = np.zeros(X.shape[0], dtype='long')
dataset = TorchDataset(X, y, SymbolNet_transform)
data_loader = DataLoader(dataset, batch_size=self.batch_size, shuffle=False, num_workers=self.num_workers, pin_memory=True)
self.eval()
results_out, results_feature = [], []
with torch.no_grad():
for data, _ in data_loader:
data = data.cuda(non_blocking=True)
out, feature = self(data)
results_out.append(out), results_feature.append(feature)
return torch.softmax(torch.cat(results_out, axis=0), axis=1), torch.cat(results_feature, axis=0)
def predict_classes(self, data_loader=None, X=None, is_train=False):
return self.predict(data_loader, X, is_train)[0].argmax(axis=1)
def get_eqs_predict(model, X, num2sign):
images_np = np.array(list(itertools.chain.from_iterable(X)))
predict_labels_list = model.predict_classes(X=images_np, is_train=False).cpu().numpy()
predict_labels_str_list, cur_idx = [], 0
for eq in X:
predict_eq_labels_list = predict_labels_list[cur_idx : cur_idx + len(eq)]
predict_eq_labels_list = [num2sign[str(label)] for label in predict_eq_labels_list]
predict_labels_str = "".join(predict_eq_labels_list)
predict_labels_str_list.append(predict_labels_str)
cur_idx += len(eq)
assert (cur_idx == len(predict_labels_list))
return predict_labels_str_list
class TorchDataset(Dataset):
def __init__(self, images_np, label, transform=None):
self.data = images_np
self.label = torch.LongTensor(label.flatten())
self.transform = transform
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
img, label = self.data[idx], self.label[idx]
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
return img, label
if __name__ == '__main__':
model_path = 'results/128_0.5_200_512_500_model.pth'
batch_size, epochs = 128, 50
'''
import pickle
fr = open("cifar10.pk", "rb")
cifar10 = pickle.load(fr)
(X_train, y_train), (X_test, y_test) = cifar10
train_data = TorchDataset(X_train, y_train, train_transform)
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=16, pin_memory=True)
test_data = TorchDataset(X_test, y_test, test_transform)
test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=False, num_workers=16, pin_memory=True)
train_data = CIFAR10(root='data', train=True, transform=train_transform, download=True)
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=16, pin_memory=True)
test_data = CIFAR10(root='data', train=False, transform=test_transform, download=True)
test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=False, num_workers=16, pin_memory=True)
model = ResNet50(num_class=13, pretrained_path=model_path, loss_criterion=nn.CrossEntropyLoss(), batch_size=batch_size, freeze_feature=False).cuda()
'''
train_data = MNIST(root='data', train=True, download=True,transform=Lenet_transform)
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=8, pin_memory=True)
test_data = MNIST(root='data', train=False, download=True,transform=Lenet_transform)
test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=False, num_workers=8, pin_memory=True)
model = LeNet(num_class=10, loss_criterion=nn.CrossEntropyLoss(), batch_size=batch_size).cuda()
#model = SymbolNet(num_class=10, loss_criterion=nn.CrossEntropyLoss(), batch_size=batch_size).cuda()
'''
train_data = TorchDataset(X_train, np.zeros(X_train.shape[0]), train_transform)
print(train_data[0])
y = model(train_data)
print(y)
'''
best_acc = 0.0
for epoch in range(1, epochs + 1):
train_loss, train_acc_1 = model.train_val(4, True, data_loader=train_loader)
test_loss, test_acc_1 = model.train_val(1, False, data_loader=test_loader)
# train_loss, train_acc_1 = model.train_val(3, True, X=X_train, y=y_train)
# test_loss, test_acc_1 = model.train_val(1, False, X=X_test, y=y_test)
# if test_acc_1 > best_acc:
# best_acc = test_acc_1
# torch.save(model.state_dict(), 'results/linear_model.pth')
#y_pred, feature = model.predict(X=X_test)