-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcycada_utils.py
More file actions
558 lines (454 loc) · 20 KB
/
Copy pathcycada_utils.py
File metadata and controls
558 lines (454 loc) · 20 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import torch
from sklearn.model_selection import train_test_split
from PIL import Image
import matplotlib.pyplot as plt
from torch.utils.data import Dataset
import numpy as np
import random
import torch
import torch.nn as nn
import config
import copy
from torchvision.utils import make_grid
from torch.utils.data import DataLoader
from torchvision import datasets
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch
import rasterio as rio
from rasterio.windows import Window
import numpy as np
from os import listdir
from os.path import isfile, join
np.random.seed(42)
import copy
import matplotlib
import matplotlib.pyplot as plt
import pickle as pkl
import cv2
from PIL import Image, ImageEnhance
import itertools
import torchvision.transforms as transforms
import glob
from tqdm.notebook import tqdm
import warnings
from torchvision.utils import make_grid
import functools
import os
import torchvision.models as models
import torch.nn.functional as F
from torchvision import datasets, models, transforms
import pandas as pd
import statistics
from scipy import stats
from sklearn import metrics
from sklearn.metrics import mean_squared_error
import datetime
Tensor = torch.cuda.FloatTensor
PROJECT_ROOT_DIR = "."
PROJECT_SAVE_DIR = "Figure_PDFs"
if not (os.path.isdir(PROJECT_ROOT_DIR+'/'+PROJECT_SAVE_DIR)):
print('Figure directory didn''t exist, creating now.')
os.mkdir(PROJECT_ROOT_DIR+'/'+PROJECT_SAVE_DIR)
else:
print('Figure directory exists.')
img_height = 256
img_width = 256
channels = 3
transforms_ = [
transforms.Resize(int(img_height*1.12), Image.BICUBIC),
transforms.RandomCrop((img_height, img_width)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
]
def to_rgb(image):
rgb_image = Image.new("RGB", image.size)
rgb_image.paste(image)
return rgb_image
def weights_init_normal(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
torch.nn.init.normal_(m.weight.data, 0.0, 0.02) # reset Conv2d's weight(tensor) with Gaussian Distribution
if hasattr(m, 'bias') and m.bias is not None:
torch.nn.init.constant_(m.bias.data, 0.0) # reset Conv2d's bias(tensor) with Constant(0)
elif classname.find('BatchNorm2d') != -1:
torch.nn.init.normal_(m.weight.data, 1.0, 0.02) # reset BatchNorm2d's weight(tensor) with Gaussian Distribution
torch.nn.init.constant_(m.bias.data, 0.0) # reset BatchNorm2d's bias(tensor) with Constant(0)
class ResnetGenerator(nn.Module):
def __init__(self, input_nc, output_nc, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False, n_blocks=6, padding_type='reflect'):
"""Construct a Resnet-based generator
Parameters:
input_nc (int) -- the number of channels in input images
output_nc (int) -- the number of channels in output images
ngf (int) -- the number of filters in the last conv layer
norm_layer -- normalization layer
use_dropout (bool) -- if use dropout layers
n_blocks (int) -- the number of ResNet blocks
padding_type (str) -- the name of padding layer in conv layers: reflect | replicate | zero
"""
assert(n_blocks >= 0)
super(ResnetGenerator, self).__init__()
if type(norm_layer) == functools.partial:
use_bias = norm_layer.func == nn.InstanceNorm2d
else:
use_bias = norm_layer == nn.InstanceNorm2d
model = [nn.ReflectionPad2d(3),
nn.Conv2d(input_nc, ngf, kernel_size=7, padding=0, bias=use_bias),
norm_layer(ngf),
nn.ReLU(True)]
n_downsampling = 2
for i in range(n_downsampling): # add downsampling layers
mult = 2 ** i
model += [nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3, stride=2, padding=1, bias=use_bias),
norm_layer(ngf * mult * 2),
nn.ReLU(True)]
mult = 2 ** n_downsampling
for i in range(n_blocks): # add ResNet blocks
model += [ResnetBlock(ngf * mult, padding_type=padding_type, norm_layer=norm_layer, use_dropout=use_dropout, use_bias=use_bias)]
for i in range(n_downsampling): # add upsampling layers
mult = 2 ** (n_downsampling - i)
model += [nn.ConvTranspose2d(ngf * mult, int(ngf * mult / 2),
kernel_size=3, stride=2,
padding=1, output_padding=1,
bias=use_bias),
norm_layer(int(ngf * mult / 2)),
nn.ReLU(True)]
model += [nn.ReflectionPad2d(3)]
model += [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0)]
model += [nn.Tanh()]
self.model = nn.Sequential(*model)
def forward(self, input):
"""Standard forward"""
return self.model(input)
class ResnetBlock(nn.Module):
"""Define a Resnet block"""
def __init__(self, dim, padding_type, norm_layer, use_dropout, use_bias):
"""Initialize the Resnet block
A resnet block is a conv block with skip connections
We construct a conv block with build_conv_block function,
and implement skip connections in <forward> function.
Original Resnet paper: https://arxiv.org/pdf/1512.03385.pdf
"""
super(ResnetBlock, self).__init__()
self.conv_block = self.build_conv_block(dim, padding_type, norm_layer, use_dropout, use_bias)
def build_conv_block(self, dim, padding_type, norm_layer, use_dropout, use_bias):
"""Construct a convolutional block.
Parameters:
dim (int) -- the number of channels in the conv layer.
padding_type (str) -- the name of padding layer: reflect | replicate | zero
norm_layer -- normalization layer
use_dropout (bool) -- if use dropout layers.
use_bias (bool) -- if the conv layer uses bias or not
Returns a conv block (with a conv layer, a normalization layer, and a non-linearity layer (ReLU))
"""
conv_block = []
p = 0
if padding_type == 'reflect':
conv_block += [nn.ReflectionPad2d(1)]
elif padding_type == 'replicate':
conv_block += [nn.ReplicationPad2d(1)]
elif padding_type == 'zero':
p = 1
else:
raise NotImplementedError('padding [%s] is not implemented' % padding_type)
conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias=use_bias), norm_layer(dim), nn.ReLU(True)]
if use_dropout:
conv_block += [nn.Dropout(0.5)]
p = 0
if padding_type == 'reflect':
conv_block += [nn.ReflectionPad2d(1)]
elif padding_type == 'replicate':
conv_block += [nn.ReplicationPad2d(1)]
elif padding_type == 'zero':
p = 1
else:
raise NotImplementedError('padding [%s] is not implemented' % padding_type)
conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias=use_bias), norm_layer(dim)]
return nn.Sequential(*conv_block)
def forward(self, x):
"""Forward function (with skip connections)"""
out = x + self.conv_block(x) # add skip connections
return out
class NLayerDiscriminator(nn.Module):
def __init__(self, input_nc, ndf=64, n_layers=3, norm_layer=nn.InstanceNorm2d):
super(NLayerDiscriminator, self).__init__()
if type(norm_layer) == functools.partial:
use_bias = norm_layer.func == nn.InstanceNorm2d
else:
use_bias = norm_layer == nn.InstanceNorm2d
kw = 4
padw = 1
sequence = [
nn.Conv2d(input_nc, ndf, kernel_size=kw, stride=2, padding=padw),
nn.LeakyReLU(0.2, True)
]
nf_mult = 1
nf_mult_prev = 1
for n in range(1, n_layers):
nf_mult_prev = nf_mult
nf_mult = min(2**n, 8)
sequence += [
nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult,
kernel_size=kw, stride=2, padding=padw, bias=use_bias),
norm_layer(ndf * nf_mult),
nn.LeakyReLU(0.2, True)
]
nf_mult_prev = nf_mult
nf_mult = min(2**n_layers, 8)
sequence += [
nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult,
kernel_size=kw, stride=1, padding=padw, bias=use_bias),
norm_layer(ndf * nf_mult),
nn.LeakyReLU(0.2, True)
]
sequence += [nn.Conv2d(ndf * nf_mult, 1, kernel_size=kw, stride=1, padding=padw)]
# Add global average pooling
# sequence += [nn.AdaptiveAvgPool2d(1)]
# Add final convolution layer to reduce spatial dimensions to 1x1
# sequence += [nn.Conv2d(1, 1, kernel_size=1, stride=1, padding=0)]
self.model = nn.Sequential(*sequence)
def forward(self, input):
return self.model(input)
class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)
class LeNet(nn.Module): # df_t
def __init__(self, input_nc):
super(LeNet, self).__init__()
sequence1 = [
nn.Conv2d(input_nc, 20, kernel_size=5, stride=1, padding=0),
nn.MaxPool2d(2, 2),
nn.ReLU(True),
nn.Conv2d(20, 50, kernel_size=5, stride=1, padding=0),
nn.Dropout2d(0.5),
nn.MaxPool2d(2, 2),
nn.ReLU(True),
Flatten(),
nn.Linear(140450, 500),
nn.ReLU(True),
nn.Dropout(0.5),
nn.Linear(500, 1)
]
self.netdft = nn.Sequential(*sequence1)
def forward(self, input):
self.out = self.netdft(input)
return self.out
class Feature_Extractor(nn.Module):
def __init__(self):
super(Feature_Extractor, self).__init__()
resnet50 = models.resnet50(pretrained=True)
for param in resnet50.parameters():
param.requires_grad = False
# Model initialization
self.resnet_pretrained = resnet50 #Running images through CNN
self.fc1 = nn.Linear(self.resnet_pretrained.fc.out_features, 50)
self.dropout = nn.Dropout(p=0.2)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(50, 2)
def forward(self, image):
img_features = self.resnet_pretrained(image)
img_features = torch.flatten(img_features, 1)
img_features = self.fc1(img_features)
x = self.relu(img_features)
x = self.dropout(x)
x = self.fc2(x.float())
return x
#input is 2 beacuse we are feeding in binary task scores (D_Feat)
class FeatureDiscriminator(nn.Module):
def __init__(self):
super(FeatureDiscriminator, self).__init__()
self.discriminator = nn.Sequential(
nn.Linear(1, 500),
nn.ReLU(),
nn.Linear(500, 500),
nn.ReLU(),
nn.Linear(500, 2),
)
def forward(self, score):
out = self.discriminator(score)
return out
class GANLoss(nn.Module):
def __init__(self, gan_mode, target_real_label=1.0, target_fake_label=0.0):
super(GANLoss, self).__init__()
self.register_buffer('real_label', torch.tensor(target_real_label))
self.register_buffer('fake_label', torch.tensor(target_fake_label))
self.gan_mode = gan_mode
if self.gan_mode == 'vanilla':
self.loss = nn.BCEWithLogitsLoss()
elif self.gan_mode == 'wgangp':
self.loss = None
def get_target_tensor(self, prediction, target_is_real):
if target_is_real:
target_tensor = self.real_label
else:
target_tensor = self.fake_label
return target_tensor.expand_as(prediction)
def __call__(self, prediction, target_is_real):
if self.gan_mode == 'vanilla':
target_tensor = self.get_target_tensor(prediction, target_is_real)
loss = self.loss(prediction, target_tensor)
elif self.gan_mode == 'wgangp':
if target_is_real:
loss = -prediction.mean()
else:
loss = prediction.mean()
return loss
def discriminator_acc(prediction, target_is_real=True):
pred_labels = torch.round(torch.sigmoid(prediction)).squeeze()
true_labels = torch.ones_like(pred_labels) if target_is_real else torch.zeros_like(pred_labels)
correct_predictions = torch.sum(pred_labels == true_labels).item()
total_predictions = true_labels.size(0)
accuracy = correct_predictions / total_predictions
return accuracy
def prediction(out, target):
label = torch.where(out.data>torch.FloatTensor([0.5]),torch.ones(out.size()[0]).long(), torch.zeros(out.size()[0]).long())
acc = (label == target).sum().item() / target.size()[0]
return label, acc
#Pretrained model for f_s (C_A)
class Multi_City_CNN(nn.Module):
def __init__(self):
super(Multi_City_CNN, self).__init__()
net = models.resnet18(pretrained=True)
for param in net.parameters():
param.requires_grad = True #True=finetune
self.resnet_pretrained = net
self.fc1 = nn.Linear(self.resnet_pretrained.fc.out_features, 512)
self.dropout = nn.Dropout(p=0.2)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(512, 512)
self.fc3 = nn.Linear(512, 1)
def forward(self, image):
img_features = self.resnet_pretrained(image)
img_features = torch.flatten(img_features, 1)
img_features = self.fc1(img_features.float())
x = self.relu(img_features)
x = self.dropout(x)
x = self.fc2(x.float())
x = self.relu(x)
x = self.dropout(x)
x = self.fc3(x)
return x
def set_requires_grad(nets, requires_grad=False):
"""Set requies_grad=Fasle for all the networks to avoid unnecessary computations
Parameters:
nets (network list) -- a list of networks
requires_grad (bool) -- whether the networks require gradients or not
"""
if not isinstance(nets, list):
nets = [nets]
for net in nets:
if net is not None:
for param in net.parameters():
param.requires_grad = requires_grad
def gen_dis_loss(genloss, disloss, iters, save = True, fig_name=''):
# epoch = range(epochs)
fig, ax = plt.subplots(1,1, figsize = (6,6))
ax.plot(iters, genloss, color='b', linewidth=0.5, label='Generator')
ax.plot(iters, disloss, color='r', linewidth=0.5, label='Discriminator')
ax.set_xlabel('Iters')
ax.set_ylabel('Loss')
ax.set_title('Generator and Discriminator Loss')
ax.legend()
plt.show()
if save==True:
fig.savefig(PROJECT_ROOT_DIR+'/'+PROJECT_SAVE_DIR+'/'+fig_name+'.png', transparent=False, facecolor='white', bbox_inches='tight')
def trainloss(mainloss, iters, save = True, fig_name=''):
fig, ax = plt.subplots(1,1, figsize = (6,6))
ax.plot(iters, mainloss, color='b', linewidth=0.5)
ax.set_xlabel('Iters')
ax.set_ylabel('Loss')
plt.show()
if save==True:
fig.savefig(PROJECT_ROOT_DIR+'/'+PROJECT_SAVE_DIR+'/'+fig_name+'.png', transparent=False, facecolor='white', bbox_inches='tight')
def cal_gradient_penalty(netD, real_data, fake_data, device, type='mixed', constant=1.0, lambda_gp=10.0):
"""Calculate the gradient penalty loss, used in WGAN-GP paper https://arxiv.org/abs/1704.00028
Arguments:
netD (network) -- discriminator network
real_data (tensor array) -- real images
fake_data (tensor array) -- generated images from the generator
device (str) -- GPU / CPU: from torch.device('cuda:{}'.format(self.gpu_ids[0])) if self.gpu_ids else torch.device('cpu')
type (str) -- if we mix real and fake data or not [real | fake | mixed].
constant (float) -- the constant used in formula ( | |gradient||_2 - constant)^2
lambda_gp (float) -- weight for this loss
Returns the gradient penalty loss
"""
if lambda_gp > 0.0:
if type == 'real': # either use real images, fake images, or a linear interpolation of two.
interpolatesv = real_data
elif type == 'fake':
interpolatesv = fake_data
elif type == 'mixed':
alpha = torch.rand(real_data.shape[0], 1)
alpha = alpha.expand(real_data.shape[0], real_data.nelement() // real_data.shape[0]).contiguous().view(*real_data.shape)
alpha = alpha.to(device)
interpolatesv = alpha * real_data + ((1 - alpha) * fake_data)
else:
raise NotImplementedError('{} not implemented'.format(type))
interpolatesv.requires_grad_(True)
disc_interpolates = netD(interpolatesv)
gradients = torch.autograd.grad(outputs=disc_interpolates, inputs=interpolatesv,
grad_outputs=torch.ones(disc_interpolates.size()).to(device),
create_graph=True, retain_graph=True, only_inputs=True)
gradients = gradients[0].view(real_data.size(0), -1) # flat the data
gradient_penalty = (((gradients + 1e-16).norm(2, dim=1) - constant) ** 2).mean() * lambda_gp # added eps
return gradient_penalty, gradients
else:
return 0.0, None
class Discriminator_Critic(nn.Module):
def __init__(self):
super(Discriminator_Critic, self).__init__()
main = nn.Sequential(
nn.Conv2d(3, 64, 5, 5), # Adjusted kernel size and stride
nn.LeakyReLU(),
nn.Conv2d(64, 64, 5, 5), # Adjusted kernel size and stride
nn.LeakyReLU(),
nn.Conv2d(64, 1, 5, 5), # Adjusted kernel size and stride
nn.LeakyReLU(),
)
self.main = main
# Adjusted the input size for the linear layer to match the downsampled feature map size
self.linear = nn.Linear(1, 1)
def forward(self, input):
output = self.main(input)
output = output.view(-1, 1)
output = self.linear(output)
return output
class CyDataset(Dataset):
def __init__(self):
self.D = []
self.L = []
with open('/datacommons/carlsonlab/srs108/old/ol/Delhi_labeled.pkl', "rb") as fp:
for station in tqdm(pkl.load(fp)):
self.D.append(tuple((station['Image'][:,:,:3], station['PM25'])))
with open('/datacommons/carlsonlab/srs108/old/ol/Lucknow.pkl', "rb") as fp:
for station in tqdm(pkl.load(fp)):
for datapoint in station:
luck_img = datapoint['Image'][:,:,:3]
if luck_img.shape == (224, 224,3):
self.L.append(tuple((luck_img, datapoint['PM'])))
self.L = random.choices(self.L, k= len(self.D))
def __len__(self): return (len(self.D))
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.ToTensor()])
d_img = self.D[idx][0]
d_img = transform(d_img)
l_img = self.L[idx][0]
l_img = transform(l_img)
sample = {
'D img': d_img,
'D pm' : torch.tensor(self.D[idx][1]),
'L img': l_img,
'L pm' : torch.tensor(self.L[idx][1])
}
return sample