-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathutils.py
297 lines (245 loc) · 10.8 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
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
import numpy as np
import os
import errno
import parser
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets,transforms
BATCH_SIZE = 1
class DCGAN_XRAY(nn.Module):
def __init__(self, nz, ngf=64, output_size=256, nc=3, num_measurements=1000):
super(DCGAN_XRAY, self).__init__()
self.nc = nc
self.output_size = output_size
self.conv1 = nn.ConvTranspose2d(nz, ngf, 4, 1, 0, bias=False)
self.bn1 = nn.BatchNorm2d(ngf)
self.conv2 = nn.ConvTranspose2d(ngf, ngf, 6, 2, 2, bias=False)
self.bn2 = nn.BatchNorm2d(ngf)
self.conv3 = nn.ConvTranspose2d(ngf, ngf, 6, 2, 2, bias=False)
self.bn3 = nn.BatchNorm2d(ngf)
self.conv4 = nn.ConvTranspose2d(ngf, ngf, 6, 2, 2, bias=False)
self.bn4 = nn.BatchNorm2d(ngf)
self.conv5 = nn.ConvTranspose2d(ngf, ngf, 6, 2, 2, bias=False)
self.bn5 = nn.BatchNorm2d(ngf)
self.conv6 = nn.ConvTranspose2d(ngf, ngf, 6, 2, 2, bias=False)
self.bn6 = nn.BatchNorm2d(ngf)
self.conv7 = nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False) #output is image
def forward(self, z):
input_size = z.size()
x = F.relu(self.bn1(self.conv1(z)))
x = F.relu(self.bn2(self.conv2(x)))
x = F.relu(self.bn3(self.conv3(x)))
x = F.relu(self.bn4(self.conv4(x)))
x = F.relu(self.bn5(self.conv5(x)))
x = F.relu(self.bn6(self.conv6(x)))
x = torch.tanh(self.conv7(x,output_size=(-1,self.nc,self.output_size,self.output_size)))
return x
class DCGAN_MNIST(nn.Module):
def __init__(self, nz, ngf=64, output_size=28, nc=1, num_measurements=10):
super(DCGAN_MNIST, self).__init__()
self.nc = nc
self.output_size = output_size
self.conv1 = nn.ConvTranspose2d(nz, ngf*8, 2, 1, 0, bias=False)
self.bn1 = nn.BatchNorm2d(ngf*8)
self.conv2 = nn.ConvTranspose2d(ngf*8, ngf*4, 4, 1, 0, bias=False)
self.bn2 = nn.BatchNorm2d(ngf*4)
self.conv3 = nn.ConvTranspose2d(ngf*4, ngf*2, 3, 1, 1, bias=False)
self.bn3 = nn.BatchNorm2d(ngf*2)
self.conv4 = nn.ConvTranspose2d(ngf*2, ngf, 3, 1, 1, bias=False)
self.bn4 = nn.BatchNorm2d(ngf)
self.conv5 = nn.ConvTranspose2d(ngf, nc, 3, 1, 1, bias=False)
def forward(self, x):
input_size = x.size()
# DCGAN_MNIST with old PyTorch version
# x = F.upsample(F.relu(self.bn1(self.conv1(x))),scale_factor=2)
# x = F.relu(self.bn2(self.conv2(x)))
# x = F.upsample(F.relu(self.bn3(self.conv3(x))),scale_factor=2)
# x = F.upsample(F.relu(self.bn4(self.conv4(x))),scale_factor=2)
# x = torch.tanh(self.conv5(x,output_size=(-1,self.nc,self.output_size,self.output_size)))
x = F.interpolate(F.relu(self.bn1(self.conv1(x))),scale_factor=2)
x = F.relu(self.bn2(self.conv2(x)))
x = F.interpolate(F.relu(self.bn3(self.conv3(x))),scale_factor=2)
x = F.interpolate(F.relu(self.bn4(self.conv4(x))),scale_factor=2)
x = torch.tanh(self.conv5(x,output_size=(-1,self.nc,self.output_size,self.output_size)))
return x
class DCGAN_RETINO(nn.Module):
def __init__(self, nz, ngf=64, output_size=256, nc=3, num_measurements=1000):
super(DCGAN_RETINO, self).__init__()
self.nc = nc
self.output_size = output_size
self.conv1 = nn.ConvTranspose2d(nz, ngf, 4, 1, 0, bias=False)
self.bn1 = nn.BatchNorm2d(ngf)
self.conv2 = nn.ConvTranspose2d(ngf, ngf, 6, 2, 2, bias=False)
self.bn2 = nn.BatchNorm2d(ngf)
self.conv3 = nn.ConvTranspose2d(ngf, ngf, 6, 2, 2, bias=False)
self.bn3 = nn.BatchNorm2d(ngf)
self.conv4 = nn.ConvTranspose2d(ngf, ngf, 6, 2, 2, bias=False)
self.bn4 = nn.BatchNorm2d(ngf)
self.conv5 = nn.ConvTranspose2d(ngf, ngf, 6, 2, 2, bias=False)
self.bn5 = nn.BatchNorm2d(ngf)
self.conv6 = nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False)
#self.fc = nn.Linear((output_size)*(output_size)*nc,num_measurements, bias=False) #fc layer - old version
def forward(self, x):
input_size = x.size()
x = F.relu(self.bn1(self.conv1(x)))
x = F.relu(self.bn2(self.conv2(x)))
x = F.relu(self.bn3(self.conv3(x)))
x = F.relu(self.bn4(self.conv4(x)))
x = F.relu(self.bn5(self.conv5(x)))
x = torch.tanh(self.conv6(x,output_size=(-1,self.nc,self.output_size,self.output_size)))
return x
NGF = 64
def init_dcgan(args):
if args.DATASET == 'xray':
net = DCGAN_XRAY(args.Z_DIM, NGF, args.IMG_SIZE,\
args.NUM_CHANNELS, args.NUM_MEASUREMENTS)
elif args.DATASET == 'mnist':
net = DCGAN_MNIST(args.Z_DIM, NGF, args.IMG_SIZE,\
args.NUM_CHANNELS, args.NUM_MEASUREMENTS)
elif args.DATASET == 'retino':
net = DCGAN_RETINO(args.Z_DIM, NGF, args.IMG_SIZE,\
args.NUM_CHANNELS, args.NUM_MEASUREMENTS)
return net
def init_output_arrays(args):
loss_re = np.zeros((args.NUM_RESTARTS, BATCH_SIZE))
recons_re = np.zeros((args.NUM_RESTARTS, BATCH_SIZE, args.NUM_CHANNELS, \
args.IMG_SIZE, args.IMG_SIZE))
return loss_re, recons_re
lambdas_tv = {'mnist': 1e-2, 'xray': 5e-2, 'retino': 2e-2}
lambdas_lr = {'mnist': 0, 'xray': 100, 'retino': 1000}
def get_constants(args, dtype):
MU_FN = 'mu_{0}.npy'.format(args.NUM_MEASUREMENTS)
MU_PATH = os.path.join(args.LR_FOLDER,MU_FN)
SIG_FN = "sig_{0}.npy".format(args.NUM_MEASUREMENTS)
SIG_PATH = os.path.join(args.LR_FOLDER,SIG_FN)
mu_ = np.load(MU_PATH)
sig_ = np.load(SIG_PATH)
mu = torch.FloatTensor(mu_).type(dtype)
sig_inv = torch.FloatTensor(np.linalg.inv(sig_)).type(dtype)
try:
tvc = lambdas_tv[args.DATASET]
except AttributeError:
tvc = 1e-2
try:
lrc = lambdas_lr[args.DATASET]
except AttributeError:
lrc = 0
return mu, sig_inv, tvc, lrc
def renorm(x):
return 0.5*x + 0.5
def plot(x,renormalize=True):
if renormalize:
plt.imshow(renorm(x).data[0].cpu().numpy(), cmap='gray')
else:
plt.imshow(x.data[0].cpu().numpy(), cmap='gray')
exit_window = 50 # number of consecutive MSE values upon which we compare
thresh_ratio = 45 # number of MSE values that must be larger for us to exit
def exit_check(window, i): # if converged, then exit current experiment
mse_base = window[0] # get first mse value in window
if len(np.where(window > mse_base)[0]) >= thresh_ratio: # if 20/25 values in window are higher than mse_base
return True, mse_base
else:
mse_last = window[exit_window-1] #get the last value of MSE in window
return False, mse_last
def define_compose(NC, IMG_SIZE): # define compose based on NUM_CHANNELS, IMG_SIZE
if NC == 1: #grayscale
compose = transforms.Compose([
transforms.Resize((IMG_SIZE,IMG_SIZE)),
transforms.Grayscale(),
transforms.ToTensor(),
transforms.Normalize((.5,.5,.5),(.5,.5,.5))
])
elif NC == 3: #rgb
compose = transforms.Compose([
transforms.Resize((IMG_SIZE,IMG_SIZE)),
transforms.ToTensor(),
transforms.Normalize((.5,.5,.5),(.5,.5,.5))
])
return compose
def set_dtype(CUDA):
if CUDA: # if cuda is available
return torch.cuda.FloatTensor
else:
return torch.FloatTensor
def get_path_out(args, path_in):
fn = path_leaf(path_in[0]) # format filename from path
if args.ALG == 'bm3d' or args.ALG == 'tval3':
file_ext = 'mat' # if algorithm is implemented in matlab
else:
file_ext = 'npy' # if algorithm is implemented in python
path_out = 'reconstructions/{0}/{1}/meas{2}/im{3}.{4}'.format( \
args.DATASET, args.ALG, args.NUM_MEASUREMENTS, fn, file_ext)
full_path = os.getcwd() + '/' + path_out
return full_path
def recons_exists(args, path_in):
path_out = get_path_out(args, path_in)
print(path_out)
if os.path.isfile(path_out):
return True
else:
return False
def save_reconstruction(x_hat, args, path_in):
path_out = get_path_out(args, path_in)
if not os.path.exists(os.path.dirname(path_out)):
try:
os.makedirs(os.path.dirname(path_out))
except OSError as exc: # guard against race condition
if exc.errno != errno.EEXIST:
raise
np.save(path_out, x_hat)
def check_args(args): # check args for correctness
IM_DIMN = args.IMG_SIZE * args.IMG_SIZE * args.NUM_CHANNELS
if isinstance(args.NUM_MEASUREMENTS, int):
if args.NUM_MEASUREMENTS > IM_DIMN:
raise ValueError('NUM_MEASUREMENTS must be less than image dimension ' \
+ str(IM_DIMN))
else:
for num_measurements in args.NUM_MEASUREMENTS:
if num_measurements > IM_DIMN:
raise ValueError('NUM_MEASUREMENTS must be less than image dimension ' \
+ str(IM_DIMN))
if not args.DEMO == 'False':
if not args.DEMO == 'True':
raise ValueError('DEMO must be either True or False.')
def convert_to_list(args): # returns list for NUM_MEAS, BATCH
if not isinstance(args.NUM_MEASUREMENTS, list):
NUM_MEASUREMENTS_LIST = [args.NUM_MEASUREMENTS]
else:
NUM_MEASUREMENTS_LIST = args.NUM_MEASUREMENTS
if not isinstance(args.ALG, list):
ALG_LIST = [args.ALG]
else:
ALG_LIST = args.ALG
return NUM_MEASUREMENTS_LIST, ALG_LIST
def path_leaf(path):
# if '/' in path and if '\\' in path:
# raise ValueError('Path to image cannot contain both forward and backward slashes')
if '.' in path: # remove file extension
path_no_extn = os.path.splitext(path)[0]
else:
raise ValueError('Filename does not contain extension')
head, tail = os.path.split(path_no_extn)
return tail or os.path.basename(head)
def get_data(args):
compose = define_compose(args.NUM_CHANNELS, args.IMG_SIZE)
if args.DEMO == 'True':
image_direc = 'data/{0}_demo/'.format(args.DATASET)
else:
image_direc = 'data/{0}/'.format(args.DATASET)
dataset = ImageFolderWithPaths(image_direc, transform = compose)
dataloader = torch.utils.data.DataLoader(dataset, shuffle=False, batch_size=BATCH_SIZE)
return dataloader
class ImageFolderWithPaths(datasets.ImageFolder):
"""Custom dataset that includes image file paths. Extends
torchvision.datasets.ImageFolder
"""
# override the __getitem__ method. this is the method dataloader calls
def __getitem__(self, index):
# this is what ImageFolder normally returns
original_tuple = super(ImageFolderWithPaths, self).__getitem__(index)
# the image file path
path = self.imgs[index][0]
# make a new tuple that includes original and the path
tuple_with_path = (original_tuple + (path,))
return tuple_with_path