-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
214 lines (167 loc) · 6.59 KB
/
utils.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
import os
import torch
from torch.autograd import Variable
def make_folder(path, version):
if not os.path.exists(os.path.join(path, version)):
os.makedirs(os.path.join(path, version))
def tensor2var(x, grad=False):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x, requires_grad=grad)
def var2tensor(x):
return x.data.cpu()
def var2numpy(x):
return x.data.cpu().numpy()
def denorm(x):
out = (x + 1) / 2
return out.clamp_(0, 1)
def encode(label):
final_res=[]
labels={'airplane':0,
'automobile':1,
'bird':2,
'cat':3,
'deer':4,
'dog':5,
'frog':6,
'horse':7,
'ship':8,
'truck':9}
for l in label:
res=[0]*10
res[l]=1
final_res.append(res)
return torch.Tensor(final_res)
#logger=Logger(log_path)
def log_scalar(numerical_info,step):
for tag, value in numerical_info.items():
logger.scalar_summary(tag, value, step+1)
#for tag, images in image_info.items():
# logger.image_summary(tag, images, step+1)
import itertools, imageio, torch
import matplotlib.pyplot as plt
import numpy as np
from torchvision import datasets
#from scipy.misc import imresize
#import PIL
#import pilutil
def ones_target(size):
#returning tensor variable containing ones, with shape =size
data=Variable(torch.ones(size,1))
return data
def zeros_target(size):
data=Variable(torch.zeros(size,1))
return data
def images_to_vectors(images):
return images.view(images.size(0),3072) #parameters: (size to flatten(28 here, row size), no. of images to be flattened)
def vectors_to_images(vectors):
return vectors.view(vectors.size(0),3,32,32) # to convert each row of vectors to 28x28 pixels images
# parameteres: (initial flattened dim, no. of sub vectors to be converted to 2d, 2d dimensions of output )
def show_result(G, x_, num_epoch, show = False, save = False, path = 'result.png'):
# G.eval()
y_ = G(x_)
y_=y_[:4]
y_ = y_.cpu()
size_figure_grid = 3
fig, ax = plt.subplots(x_.size()[0], size_figure_grid, figsize=(5, 5))
for i, j in itertools.product(range(x_.size()[0]), range(size_figure_grid)):
ax[i, j].get_xaxis().set_visible(False)
ax[i, j].get_yaxis().set_visible(False)
for i in range(y_.size()[0],2):
ax[i, 0].cla()
ax[i, 0].imshow((y_[i].numpy().transpose(1, 2, 0) + 1) / 2)
ax[i, 1].cla()
ax[i, 1].imshow((y_[i+1].numpy().transpose(1, 2, 0) + 1) / 2)
label = 'Epoch {0}'.format(num_epoch)
fig.text(0.5, 0.04, label, ha='center')
if save:
plt.savefig(path)
if show:
plt.show()
else:
plt.close()
def show_train_hist(hist, show = False, save = False, path = 'Train_hist.png'):
x = range(len(hist['D_losses']))
y1 = hist['D_losses']
y2 = hist['G_losses']
plt.plot(x, y1, label='D_loss')
plt.plot(x, y2, label='G_loss')
plt.xlabel('Iter')
plt.ylabel('Loss')
plt.legend(loc=4)
plt.grid(True)
plt.tight_layout()
if save:
plt.savefig(path)
if show:
plt.show()
else:
plt.close()
def generate_animation(root, model, opt):
images = []
for e in range(train_epoch):
img_name = root + 'Fixed_results/' + model + str(e + 1) + '.png'
images.append(imageio.imread(img_name))
imageio.mimsave(root + model + 'generate_animation.gif', images, fps=5)
def data_load(path, subfolder, transform, batch_size, shuffle=True):
dset = datasets.ImageFolder(path, transform)
ind = dset.class_to_idx[subfolder]
n = 0
for i in range(dset.__len__()):
if ind != dset.imgs[n][1]:
del dset.imgs[n]
n -= 1
n += 1
return torch.utils.data.DataLoader(dset, batch_size=batch_size, shuffle=shuffle)
def imgs_resize(imgs, resize_scale = 286):
outputs = torch.FloatTensor(imgs.size()[0], imgs.size()[1], resize_scale, resize_scale)
for i in range(imgs.size()[0]):
img = imresize(imgs[i].numpy(), [resize_scale, resize_scale])
outputs[i] = torch.FloatTensor((img.transpose(2, 0, 1).astype(np.float32).reshape(-1, imgs.size()[1], resize_scale, resize_scale) - 127.5) / 127.5)
return outputs
def random_crop(imgs1, imgs2, crop_size = 256):
outputs1 = torch.FloatTensor(imgs1.size()[0], imgs1.size()[1], crop_size, crop_size)
outputs2 = torch.FloatTensor(imgs2.size()[0], imgs2.size()[1], crop_size, crop_size)
for i in range(imgs1.size()[0]):
img1 = imgs1[i]
img2 = imgs2[i]
rand1 = np.random.randint(0, imgs1.size()[2] - crop_size)
rand2 = np.random.randint(0, imgs2.size()[2] - crop_size)
outputs1[i] = img1[:, rand1: crop_size + rand1, rand2: crop_size + rand2]
outputs2[i] = img2[:, rand1: crop_size + rand1, rand2: crop_size + rand2]
return outputs1, outputs2
def random_fliplr(imgs1, imgs2):
outputs1 = torch.FloatTensor(imgs1.size())
outputs2 = torch.FloatTensor(imgs2.size())
for i in range(imgs1.size()[0]):
if torch.rand(1)[0] < 0.5:
img1 = torch.FloatTensor(
(np.fliplr(imgs1[i].numpy().transpose(1, 2, 0)).transpose(2, 0, 1).reshape(-1, imgs1.size()[1], imgs1.size()[2], imgs1.size()[3]) + 1) / 2)
outputs1[i] = (img1 - 0.5) / 0.5
img2 = torch.FloatTensor(
(np.fliplr(imgs2[i].numpy().transpose(1, 2, 0)).transpose(2, 0, 1).reshape(-1, imgs2.size()[1], imgs2.size()[2], imgs2.size()[3]) + 1) / 2)
outputs2[i] = (img2 - 0.5) / 0.5
else:
outputs1[i] = imgs1[i]
outputs2[i] = imgs2[i]
return outputs1, outputs2
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
def noise(size):
n=Variable(torch.randn(size,200))
return n
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
def log_tboard(numerical_info, image_info,step):
for tag, value in numerical_info.items():
logger.scalar_summary(tag, value, step+1)
#for tag, images in image_info.items():
# logger.image_summary(tag, images, step+1)