-
Notifications
You must be signed in to change notification settings - Fork 0
/
INR_training_pipeline_v1.py
342 lines (299 loc) · 13 KB
/
INR_training_pipeline_v1.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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms
import os
import argparse
from models import *
import numpy as np
import time
import cv2
from PIL import Image
import torch.utils.data as Data
import re
from torch.utils.data import Dataset
num_images = 50000
hidden_dimension = 15
weights = []
batch_size = 128
start_epoch = 0
lr = 0.01
test_nSamples = 10000
init_width = 32
init_height = 32
batch_size_test = 100
for i in range (num_images):
INR_weights = torch.load("./weights_1/%d.pt"%(i), map_location = torch.device('cpu'))
weights.append(INR_weights)
test_imgDir = './cifar_10_images/test_cifar10'
def is_image_file(filename):
return any(filename.endswith(extension) for extension in ['.png', '.jpg', '.jpeg', '.PNG', '.JPG', '.JPEG'])
linear_0_weight = torch.zeros(num_images, 2, hidden_dimension )
linear_0_bias = torch.zeros(num_images, hidden_dimension )
linear_1_weight = torch.zeros(num_images, hidden_dimension, hidden_dimension )
linear_1_bias = torch.zeros(num_images, hidden_dimension )
linear_2_weight = torch.zeros(num_images, hidden_dimension, hidden_dimension )
linear_2_bias = torch.zeros(num_images, hidden_dimension )
linear_3_weight = torch.zeros(num_images, hidden_dimension, 3 )
linear_3_bias = torch.zeros(num_images, 3 )
for i in range (num_images):
linear_0_weight[i] = weights[i]['net.0.linear.weight'].t()
linear_0_bias[i] = weights[i]['net.0.linear.bias']
linear_1_weight[i] = weights[i]['net.1.linear.weight'].t()
linear_1_bias[i] = weights[i]['net.1.linear.bias']
linear_2_weight[i] = weights[i]['net.2.linear.weight'].t()
linear_2_bias[i] = weights[i]['net.2.linear.bias']
linear_3_weight[i] = weights[i]['last_layer.linear.weight'].t()
linear_3_bias[i] = weights[i]['last_layer.linear.bias']
def load_image_path_test(imgDir):
all_training_files=os.walk(imgDir)
train_files=[]
train_imageNames=[]
train_nSamples=0
for path,direction,filelist in all_training_files:
files = [file for file in filelist if os.path.isfile(os.path.join(path, file))]
imageNames = [file.split('.')[0] for file in files if is_image_file(file)]
files = [os.path.join(path, file) for file in files if is_image_file(file)]
train_files.append(files)
train_imageNames.append(imageNames)
train_nSamples=train_nSamples+len(files)
train_files=sum(train_files,[])
train_imageNames=sum(train_imageNames,[])
#print(train_imageNames)
train_imageNames.sort(key = lambda i:int(re.match(r'(\d+)',i).group()))
#train_imageNames.sort(key = lambda x: int(x[:-4]))
train_image_path = []
for i in range (len(train_imageNames)):
string = imgDir + '/' + train_imageNames[i] + '.jpg'
train_image_path.append(string)
return train_image_path
test_image_path = load_image_path_test(test_imgDir)
class listDataset_RAM(Dataset):
def __init__(self,data, target, nsamples,shape=None, shuffle=True, transform=None, target_transform=None, train=False, seen=0, batch_size=32, num_workers=0):
self.data=data
self.target=target
self.nSamples=nsamples
self.transform = transform
self.target_transform = target_transform
self.train = train
self.shape = shape
self.seen = seen
self.batch_size = batch_size
self.num_workers = num_workers
def __len__(self):
return self.nSamples
def __getitem__(self, index):
#imgpath = self.image_root[index]
#img = Image.open(imgpath).convert('RGB')
#print(img)
img = self.data[index]
if self.shape is not None:
img = img.resize(self.shape)
if self.transform is not None:
img = self.transform(img)
label=self.target[index]
#print(label.type)
label = torch.from_numpy(np.array(label, dtype = np.int64))
return (img, label)
def psnr(img1, img2):
"""Calculates PSNR between two images.
Args:
img1 (torch.Tensor):
img2 (torch.Tensor):
"""
return 20. * np.log10(1.) - 10. * (img1 - img2).pow(2).mean().log10().to('cpu').item()
def to_coordinates_and_features(img):
"""Converts an image to a set of coordinates and features.
Args:
img (torch.Tensor): Shape (channels, height, width).
"""
# Coordinates are indices of all non zero locations of a tensor of ones of
# same shape as spatial dimensions of image
coordinates = torch.ones(img.shape[1:]).nonzero(as_tuple=False).float()
#coordinates = torch.ones(img.shape[1:]).float()
# Normalize coordinates to lie in [-.5, .5]
coordinates = coordinates / (img.shape[1] - 1) - 0.5
# Convert to range [-1, 1]
coordinates *= 2
# Convert image to a tensor of features of shape (num_points, channels)
features = img.reshape(img.shape[0], -1).T
return coordinates, features
img = torch.zeros([batch_size, 3,32,32])
coordinates = torch.zeros([batch_size, img.shape[2] * img.shape[3], 2])
for i in range (batch_size):
coordinates[i], _= to_coordinates_and_features(img[i])
device = 'cuda:0'
coordinates = coordinates.to(device)
linear_0_bias = linear_0_bias.view(-1,1,linear_0_bias.shape[1])
linear_1_bias = linear_1_bias.view(-1,1,linear_1_bias.shape[1])
linear_2_bias = linear_2_bias.view(-1,1,linear_2_bias.shape[1])
linear_3_bias = linear_3_bias.view(-1,1,linear_3_bias.shape[1])
# linear_0_weight = linear_0_weight.half()
# linear_0_bias = linear_0_bias.half()
# linear_1_weight = linear_1_weight.half()
# linear_1_bias = linear_1_bias.half()
# linear_2_weight = linear_2_weight.half()
# linear_2_bias = linear_2_bias.half()
# linear_3_weight = linear_3_weight.half()
# linear_3_bias = linear_3_bias.half()
# coordinates = coordinates.half()
linear_0_weight = linear_0_weight.to(device)
linear_0_bias = linear_0_bias.to(device)
linear_1_weight = linear_1_weight.to(device)
linear_1_bias = linear_1_bias.to(device)
linear_2_weight = linear_2_weight.to(device)
linear_2_bias = linear_2_bias.to(device)
linear_3_weight = linear_3_weight.to(device)
linear_3_bias = linear_3_bias.to(device)
def clamp_image(img):
"""Clamp image values to like in [0, 1] and convert to unsigned int.
Args:
img (torch.Tensor):
"""
# Values may lie outside [0, 1], so clamp input
img_ = torch.clamp(img, 0., 1.)
# Pixel values lie in {0, ..., 255}, so round float tensor
return torch.round(img_ * 255) / 255.
transform_train = transforms.Compose([
#transforms.ToPILImage(),
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
#transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
train_label = np.load("cifar_10_labels.npy")
test_label = np.load("cifar_10_labels_test.npy")
train_label_tensor=torch.from_numpy(train_label.astype(np.int64))
test_label_tensor=torch.from_numpy(test_label.astype(np.int64))
train_label_tensor = train_label_tensor.to(device)
test_label_tensor=test_label_tensor.to(device)
test_data = []
for i in range (len(test_image_path)):
img = Image.open(test_image_path[i]).convert('RGB')
test_data.append(img)
test_loader = torch.utils.data.DataLoader(
listDataset_RAM(test_data, test_label, test_nSamples, shape=(init_width, init_height),
shuffle=False,
transform=transform_test,
train=False,
seen=0,
batch_size=batch_size,
num_workers=0),
batch_size=batch_size_test, shuffle=False, num_workers=8)
iteration = int(50000/128)
# net = VGG('VGG19')
net = ResNet18()
# net = PreActResNet18()
# net = GoogLeNet()
# net = DenseNet121()
# net = ResNeXt29_2x64d()
# net = MobileNet()
# net = MobileNetV2()
# net = DPN92()
# net = ShuffleNetG2()
# net = SENet18()
# net = ShuffleNetV2(1)
# net = EfficientNetB0()
# net = RegNetX_200MF()
# net = SimpleDLA()
net = net.to(device)
if device == 'cuda':
net = torch.nn.DataParallel(net)
cudnn.benchmark = True
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=lr,
momentum=0.9, weight_decay=5e-4)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=200)
def train(epoch, coordinates, linear_0_weight, linear_0_bias,linear_1_weight, linear_1_bias,linear_2_weight, linear_2_bias, linear_3_weight, linear_3_bias, train_label_tensor):
print('\nEpoch: %d' % epoch)
net.train()
train_loss = 0
correct = 0
total = 0
iter = 0
index_list = torch.randperm(50000)
linear_0_weight = linear_0_weight[index_list]
#print(linear_0_bias.shape)
linear_0_bias = linear_0_bias[index_list]
linear_1_weight = linear_1_weight[index_list]
linear_1_bias = linear_1_bias[index_list]
linear_2_weight = linear_2_weight[index_list]
linear_2_bias = linear_2_bias[index_list]
linear_3_weight = linear_3_weight[index_list]
linear_3_bias = linear_3_bias[index_list]
train_label_tensor = train_label_tensor[index_list]
for batch_idx in range (iteration):
#inputs, targets = inputs.to(device), targets.to(device)
output1 = coordinates.matmul(linear_0_weight[batch_size * batch_idx : batch_size * (batch_idx + 1)]) + linear_0_bias[batch_size * batch_idx : batch_size * (batch_idx + 1)]
output1 = torch.sin(30 * output1)
output2 = output1.matmul(linear_1_weight[batch_size * batch_idx : batch_size * (batch_idx + 1)]) + linear_1_bias[batch_size * batch_idx : batch_size * (batch_idx + 1)]
output2 = torch.sin(30 * output2)
output3 = output2.matmul(linear_2_weight[batch_size * batch_idx : batch_size * (batch_idx + 1)]) + linear_2_bias[batch_size * batch_idx : batch_size * (batch_idx + 1)]
output3 = torch.sin(30 * output3)
output4 = output3.matmul(linear_3_weight[batch_size * batch_idx : batch_size * (batch_idx + 1)]) + linear_3_bias[batch_size * batch_idx : batch_size * (batch_idx + 1)]
#print(output4.shape)
output4 = output4[:,:,[2,1,0]]
output4 = output4.reshape(batch_size, 32,32,3)
output4 = clamp_image(output4)
output4 = output4.permute(0,3,1,2)
#print(linear_0_weight.requires_grad)
reconstructed_augment = transform_train(output4)
targets = train_label_tensor[ batch_size * batch_idx : batch_size * (batch_idx + 1)]
optimizer.zero_grad()
outputs = net(reconstructed_augment)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
train_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
iter = iter + 1
#progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
#% (train_loss/(batch_idx+1), 100.*correct/total, correct, total))
print('Loss: %.8f | Acc: %.8f%% (%d/%d)'% (train_loss/(iter * 128), 100.*correct/total, correct, total))
def test(epoch):
best_acc = 0
net.eval()
test_loss = 0
correct = 0
total = 0
iter = 0
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(test_loader):
inputs, targets = inputs.to(device), targets.to(device)
outputs = net(inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
iter = iter + 1
#progress_bar(batch_idx, len(testloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
#% (test_loss/(batch_idx+1), 100.*correct/total, correct, total))
print('Loss: %.8f | Acc: %.8f%% (%d/%d)'% (test_loss/(iter * 100), 100.*correct/total, correct, total))
# Save checkpoint.
acc = 100.*correct/total
if acc > best_acc:
print('Saving..')
state = {
'net': net.state_dict(),
'acc': acc,
'epoch': epoch,
}
if not os.path.isdir('checkpoint'):
os.mkdir('checkpoint')
torch.save(state, './checkpoint/ckpt_whole_pipeline.pth')
best_acc = acc
for epoch in range(start_epoch, start_epoch+250):
#lr = adjust_learning_rate(epoch)
train(epoch, coordinates, linear_0_weight, linear_0_bias,linear_1_weight, linear_1_bias,linear_2_weight, linear_2_bias, linear_3_weight, linear_3_bias, train_label_tensor)
test(epoch)
scheduler.step()