-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.py
132 lines (112 loc) · 4.26 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
import numpy as np
import torch
import os
from os import listdir
from os.path import join
from PIL import Image
import torch.utils.data
from torch.utils.data import DataLoader
from torch.utils.data.dataset import Dataset
import torchvision.utils as utils
from torchvision.transforms import Compose, RandomCrop, ToTensor, ToPILImage, CenterCrop, Resize, Normalize
def is_image_file(filename):
return any(filename.endswith(extension) for extension in ['.png', '.jpg', '.jpeg', '.PNG', '.JPG', '.JPEG'])
def calculate_valid_crop_size(crop_size, upscale_factor):
return crop_size - (crop_size % upscale_factor)
def to_image():
return Compose([
ToPILImage(),
ToTensor()
])
class TrainDataset(Dataset):
def __init__(self, dataset_dir, crop_size, upscale_factor):
super(TrainDataset, self).__init__()
self.image_filenames = [join(dataset_dir, x) for x in listdir(dataset_dir) if is_image_file(x)]
crop_size = calculate_valid_crop_size(crop_size, upscale_factor)
self.hr_preprocess = Compose([CenterCrop(384), RandomCrop(crop_size), ToTensor()])
self.lr_preprocess = Compose([ToPILImage(), Resize(crop_size // upscale_factor, interpolation=Image.BICUBIC), ToTensor()])
def __getitem__(self, index):
hr_image = self.hr_preprocess(Image.open(self.image_filenames[index]))
lr_image = self.lr_preprocess(hr_image)
return lr_image, hr_image
def __len__(self):
return len(self.image_filenames)
class DevDataset(Dataset):
def __init__(self, dataset_dir, upscale_factor):
super(DevDataset, self).__init__()
self.upscale_factor = upscale_factor
self.image_filenames = [join(dataset_dir, x) for x in listdir(dataset_dir) if is_image_file(x)]
def __getitem__(self, index):
hr_image = Image.open(self.image_filenames[index])
crop_size = calculate_valid_crop_size(128, self.upscale_factor)
lr_scale = Resize(crop_size // self.upscale_factor, interpolation=Image.BICUBIC)
hr_scale = Resize(crop_size, interpolation=Image.BICUBIC)
hr_image = CenterCrop(crop_size)(hr_image)
lr_image = lr_scale(hr_image)
hr_restore_img = hr_scale(lr_image)
norm = ToTensor()
return norm(lr_image), norm(hr_restore_img), norm(hr_image)
def __len__(self):
return len(self.image_filenames)
def print_first_parameter(net):
for name, param in net.named_parameters():
if param.requires_grad:
print (str(name) + ':' + str(param.data[0]))
return
def check_grads(model, model_name):
grads = []
for p in model.parameters():
if not p.grad is None:
grads.append(float(p.grad.mean()))
grads = np.array(grads)
if grads.any() and grads.mean() > 100:
print('WARNING!' + model_name + ' gradients mean is over 100.')
return False
if grads.any() and grads.max() > 100:
print('WARNING!' + model_name + ' gradients max is over 100.')
return False
return True
def get_grads_D(net):
top = 0
bottom = 0
for name, param in net.named_parameters():
if param.requires_grad:
# Hardcoded param name, subject to change of the network
if name == 'net.0.weight':
top = param.grad.abs().mean()
#print (name + str(param.grad))
# Hardcoded param name, subject to change of the network
if name == 'net.26.weight':
bottom = param.grad.abs().mean()
#print (name + str(param.grad))
return top, bottom
def get_grads_D_WAN(net):
top = 0
bottom = 0
for name, param in net.named_parameters():
if param.requires_grad:
# Hardcoded param name, subject to change of the network
if name == 'net.0.weight':
top = param.grad.abs().mean()
#print (name + str(param.grad))
# Hardcoded param name, subject to change of the network
if name == 'net.19.weight':
bottom = param.grad.abs().mean()
#print (name + str(param.grad))
return top, bottom
def get_grads_G(net):
top = 0
bottom = 0
#torch.set_printoptions(precision=10)
#torch.set_printoptions(threshold=50000)
for name, param in net.named_parameters():
if param.requires_grad:
# Hardcoded param name, subject to change of the network
if name == 'conv1.0.weight':
top = param.grad.abs().mean()
#print (name + str(param.grad))
# Hardcoded param name, subject to change of the network
if name == 'upsample.2.weight':
bottom = param.grad.abs().mean()
#print (name + str(param.grad))
return top, bottom