-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpurePytorch.py
More file actions
298 lines (238 loc) · 9.55 KB
/
purePytorch.py
File metadata and controls
298 lines (238 loc) · 9.55 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
# %%
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from PIL import Image, ImageDraw, ImageFilter, ImageFont
import os
from pathlib import Path
import mimetypes
from glob import glob
import matplotlib.pyplot as plt
import numpy as np
import itertools
import logging
from os.path import splitext
from os import listdir
from torch.utils.data import Dataset
from torchvision.models import resnet34
from torchvision.transforms import Compose
os.environ["TORCH_HOME"] = "/media/subhaditya/DATA/COSMO/Datasets-Useful"
# %%
path = "/media/subhaditya/DATA/COSMO/Datasets/deHazer"
path_hr = path + "/normal"
path_lr = path + "/hazy"
# %%
def plot_img_and_mask(img, mask):
classes = mask.shape[2] if len(mask.shape) > 2 else 1
fig, ax = plt.subplots(1, classes + 1)
ax[0].set_title('Input image')
ax[0].imshow(img)
if classes > 1:
for i in range(classes):
ax[i+1].set_title(f'Output mask (class {i+1})')
ax[i+1].imshow(mask[:, :, i])
else:
ax[1].set_title(f'Output mask')
ax[1].imshow(mask)
plt.xticks([]), plt.yticks([])
plt.show()
# %%
class BasicDataset(Dataset):
def __init__(self, imgs_dir, masks_dir, scale=1):
self.imgs_dir = imgs_dir
self.masks_dir = masks_dir
self.scale = scale
assert 0 < scale <= 1, 'Scale must be between 0 and 1'
self.ids = [splitext(file)[0] for file in listdir(imgs_dir)
if not file.startswith('.')]
# self.c = len(self.ids)
# logging.info(f'Creating dataset with {len(self.ids)} examples')
def __len__(self):
return len(self.ids)
@classmethod
def preprocess(cls, pil_img, scale):
w, h = pil_img.size
newW, newH = int(scale * w), int(scale * h)
assert newW > 0 and newH > 0, 'Scale is too small'
pil_img = pil_img.resize((newW, newH))
img_nd = np.array(pil_img)
if len(img_nd.shape) == 2:
img_nd = np.expand_dims(img_nd, axis=2)
# HWC to CHW
img_trans = img_nd.transpose((2, 0, 1))
if img_trans.max() > 1:
img_trans = img_trans / 255
return img_trans
def __getitem__(self, i):
idx = self.ids[i]
mask_file = self.masks_dir + "/" + idx + '.png'
img_file = self.imgs_dir + "/"+idx + '.png'
mask = Image.open(mask_file)
img = Image.open(img_file)
assert img.size == mask.size, \
f'Image and mask {idx} should be the same size, but are {img.size} and {mask.size}'
img = self.preprocess(img, self.scale)
mask = self.preprocess(mask, self.scale)
return {'image': torch.from_numpy(img), 'mask': torch.from_numpy(mask)}
# %%
dataset = BasicDataset(path_hr, path_lr, scale=.5)
# %%
plt.imshow(dataset.__getitem__(1)['image'].permute(1, 2, 0))
#%%
plt.imshow(dataset.__getitem__(1)['mask'].permute(1, 2, 0))
#%% [markdown]
# inits
# %%
def truncated_normal_(tensor, mean=0, std=1):
size = tensor.shape
tmp = tensor.new_empty(size + (4,)).normal_()
valid = (tmp < 2) & (tmp > -2)
ind = valid.max(-1, keepdim=True)[1]
tensor.data.copy_(tmp.gather(-1, ind).squeeze(-1))
tensor.data.mul_(std).add_(mean)
def init_weights(m):
if type(m) == nn.Conv2d or type(m) == nn.ConvTranspose2d:
nn.init.kaiming_normal_(m.weight, mode='fan_in', nonlinearity='relu')
#nn.init.normal_(m.weight, std=0.001)
#nn.init.normal_(m.bias, std=0.001)
truncated_normal_(m.bias, mean=0, std=0.001)
#%% [markdown]
# Blocks for Unet
class DownConvBlock(nn.Module):
"""
A block of three convolutional layers where each layer is followed by a non-linear activation function
Between each block we add a pooling operation.
"""
def __init__(self, input_dim, output_dim, initializers, padding, pool=True):
super(DownConvBlock, self).__init__()
layers = []
if pool:
layers.append(nn.AvgPool2d(kernel_size=2, stride=2, padding=0, ceil_mode=True))
layers.append(nn.Conv2d(input_dim, output_dim, kernel_size=3, stride=1, padding=int(padding)))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.Conv2d(output_dim, output_dim, kernel_size=3, stride=1, padding=int(padding)))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.Conv2d(output_dim, output_dim, kernel_size=3, stride=1, padding=int(padding)))
layers.append(nn.ReLU(inplace=True))
self.layers = nn.Sequential(*layers)
self.layers.apply(init_weights)
def forward(self, patch):
return self.layers(patch)
class UpConvBlock(nn.Module):
"""
A block consists of an upsampling layer followed by a convolutional layer to reduce the amount of channels and then a DownConvBlock
If bilinear is set to false, we do a transposed convolution instead of upsampling
"""
def __init__(self, input_dim, output_dim, initializers, padding, bilinear=True):
super(UpConvBlock, self).__init__()
self.bilinear = bilinear
if not self.bilinear:
self.upconv_layer = nn.ConvTranspose2d(input_dim, output_dim, kernel_size=2, stride=2)
self.upconv_layer.apply(init_weights)
self.conv_block = DownConvBlock(input_dim, output_dim, initializers, padding, pool=False)
def forward(self, x, bridge):
if self.bilinear:
up = nn.functional.interpolate(x, mode='bilinear', scale_factor=2, align_corners=True)
else:
up = self.upconv_layer(x)
assert up.shape[3] == bridge.shape[3]
out = torch.cat([up, bridge], 1)
out = self.conv_block(out)
return out
# %% [markdown]
# Unet
# %%
class Unet(nn.Module):
"""
A UNet (https://arxiv.org/abs/1505.04597) implementation.
input_channels: the number of channels in the image (1 for greyscale and 3 for RGB)
num_classes: the number of classes to predict
num_filters: list with the amount of filters per layer
apply_last_layer: boolean to apply last layer or not (not used in Probabilistic UNet)
padidng: Boolean, if true we pad the images with 1 so that we keep the same dimensions
"""
def __init__(self, input_channels, num_classes, num_filters, initializers, apply_last_layer=True, padding=True):
super(Unet, self).__init__()
self.input_channels = input_channels
self.num_classes = num_classes
self.num_filters = num_filters
self.padding = padding
self.activation_maps = []
self.apply_last_layer = apply_last_layer
self.contracting_path = nn.ModuleList()
for i in range(len(self.num_filters)):
input = self.input_channels if i == 0 else output
output = self.num_filters[i]
if i == 0:
pool = False
else:
pool = True
self.contracting_path.append(DownConvBlock(input, output, initializers, padding, pool=pool))
self.upsampling_path = nn.ModuleList()
n = len(self.num_filters) - 2
for i in range(n, -1, -1):
input = output + self.num_filters[i]
output = self.num_filters[i]
self.upsampling_path.append(UpConvBlock(input, output, initializers, padding))
if self.apply_last_layer:
self.last_layer = nn.Conv2d(output, num_classes, kernel_size=1)
#nn.init.kaiming_normal_(self.last_layer.weight, mode='fan_in',nonlinearity='relu')
#nn.init.normal_(self.last_layer.bias)
def forward(self, x, val):
blocks = []
for i, down in enumerate(self.contracting_path):
x = down(x)
if i != len(self.contracting_path)-1:
blocks.append(x)
for i, up in enumerate(self.upsampling_path):
x = up(x, blocks[-i-1])
del blocks
#Used for saving the activations and plotting
if val:
self.activation_maps.append(x)
if self.apply_last_layer:
x = self.last_layer(x)
return x
# %%
from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.data import DataLoader
from tqdm import tqdm_notebook,tqdm
def l2_regularisation(m):
l2_reg = None
for W in m.parameters():
if l2_reg is None:
l2_reg = W.norm(2)
else:
l2_reg = l2_reg + W.norm(2)
return l2_reg
# %%
dataset_size = len(dataset)
indices = list(range(dataset_size))
split = int(np.floor(0.1 * dataset_size))
np.random.shuffle(indices)
train_indices, test_indices = indices[split:], indices[:split]
#%%
train_sampler = SubsetRandomSampler(train_indices)
test_sampler = SubsetRandomSampler(test_indices)
train_loader = DataLoader(dataset, batch_size=5, sampler=train_sampler)
test_loader = DataLoader(dataset, batch_size=1, sampler=test_sampler)
print("Number of training/test patches:", (len(train_indices),len(test_indices)))
# %%
net = Unet(input_channels=3, num_classes=2, num_filters=[32,64,128,192],initializers = {'w':'he_normal', 'b':'normal'}).to('cuda')
# %%
optimizer = torch.optim.AdamW(net.parameters(), lr=1e-4, weight_decay=0)
epochs = 10
#%%
for epoch in tqdm(range(epochs)):
for step, (patch, mask, _) in enumerate(train_loader):
patch = patch.to(device)
mask = mask.to(device)
mask = torch.unsqueeze(mask,1)
net.forward(patch, mask, training=True)
elbo = net.elbo(mask)
reg_loss = l2_regularisation(net.posterior) + l2_regularisation(net.prior) + l2_regularisation(net.fcomb.layers)
loss = -elbo + 1e-5 * reg_loss
optimizer.zero_grad()
loss.backward()
optimizer.step()