-
Notifications
You must be signed in to change notification settings - Fork 4
/
im2height.py
170 lines (117 loc) · 4.46 KB
/
im2height.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from pytorch_lightning.core.lightning import LightningModule
from nadam import Nadam
from ssim import ssim
class Pool(LightningModule):
def __init__(self, kernel_size=2, stride=2, **kwargs):
super(Pool, self).__init__()
self.pool_fn = nn.MaxPool2d(kernel_size, stride, **kwargs)
def forward(self, x, *args, **kwargs):
size = x.size()
x, indices = self.pool_fn(x, **kwargs)
return x, indices, size
class Unpool(LightningModule):
def __init__(self, fn, kernel_size=2, stride=2, **kwargs):
super(Unpool, self).__init__()
self.pool_fn = nn.MaxUnpool2d(kernel_size, stride, **kwargs)
def forward(self, x, indices, output_size, *args, **kwargs):
return self.pool_fn(x, indices=indices, output_size=output_size, *args, **kwargs)
class Block(LightningModule):
""" A Block performs three rounds of conv, batchnorm, relu
"""
def __init__(self, fn, in_channels, out_channels, kernel_size=3, stride=1, padding=1):
super(Block, self).__init__()
self.conv1 = fn(in_channels, out_channels, kernel_size, stride, padding)
self.conv_rest = fn(out_channels, out_channels, kernel_size, stride, padding)
self.bn = nn.BatchNorm2d(out_channels)
# following similar setup https://github.com/hysts/pytorch_resnet
self.identity = nn.Sequential() # identity
if in_channels != out_channels:
self.identity.add_module(
'conv',
nn.Conv2d(
in_channels,
out_channels,
kernel_size=1,
stride=stride, # downsample
padding=0,
bias=False))
self.identity.add_module('bn', nn.BatchNorm2d(out_channels)) # BN
def forward(self, x):
y = F.relu(self.bn(self.conv1(x)))
y = F.relu(self.bn(self.conv_rest(y)))
y = self.bn(self.conv_rest(y))
identity = self.identity(x)
y = F.relu(y + identity)
return y
class Im2Height(LightningModule):
""" Im2Height Fully Residual Convolutional-Deconvolutional Network
implementation based on https://arxiv.org/abs/1802.10249
"""
def __init__(self):
super(Im2Height, self).__init__()
# Convolutions
self.conv1 = Block(nn.Conv2d, 1, 64)
self.conv2 = Block(nn.Conv2d, 64, 128)
self.conv3 = Block(nn.Conv2d, 128, 256)
self.conv4 = Block(nn.Conv2d, 256, 512)
# Deconvolutions
self.deconv1 = Block(nn.ConvTranspose2d, 512, 256)
self.deconv2 = Block(nn.ConvTranspose2d, 256, 128)
self.deconv3 = Block(nn.ConvTranspose2d, 128, 64)
self.deconv4 = Block(nn.ConvTranspose2d, 128, 1) # note this is residual merge
self.pool = Pool(2, 2, return_indices=True)
self.unpool = Unpool(2, 2)
def forward(self, x):
# Convolve
x = self.conv1(x)
# Residual skip connection
x_conv_input = x.clone()
x, indices1, size1 = self.pool(x)
x, indices2, size2 = self.pool(self.conv2(x))
x, indices3, size3 = self.pool(self.conv3(x))
x, indices4, size4 = self.pool(self.conv4(x))
# Deconvolve
x = self.unpool(x, indices4, indices3.size())
x = self.deconv1(x)
x = self.unpool(x, indices3, indices2.size())
x = self.deconv2(x)
x = self.unpool(x, indices2, indices1.size())
x = self.deconv3(x)
x = self.unpool(x, indices1, x_conv_input.size())
# Concatenate with residual skip connection
x = torch.cat((x, x_conv_input), dim=1)
x = self.deconv4(x)
return x
# lightning implementations
def training_step(self, batch, batch_idx):
x, y = batch
y_pred = self(x)
l1loss = F.l1_loss(y_pred, y)
l2loss = F.mse_loss(y_pred, y)
tensorboard_logs = { 'l1loss': l1loss, 'l2loss': l2loss }
return { 'loss': l1loss, 'log': tensorboard_logs }
def configure_optimizers(self):
return Nadam(self.parameters(), lr=2e-5, schedule_decay=4e-3)
#return torch.optim.SGD(self.parameters(), lr=1e-3)
# validation
def validation_step(self, batch, batch_idx):
x, y = batch
y_pred = self(x)
l1loss = F.l1_loss(y_pred, y)
l2loss = F.mse_loss(y_pred, y)
ssim_loss = ssim(y_pred, y)
tensorboard_logs = { 'val_l1loss': l1loss, 'val_l2loss': l2loss, 'val_ssimloss': ssim_loss }
return tensorboard_logs
def validation_epoch_end(self, outputs):
avg_l1loss = torch.stack([x['val_l1loss'] for x in outputs]).mean()
avg_l2loss = torch.stack([x['val_l2loss'] for x in outputs]).mean()
avg_ssimloss = torch.stack([x['val_ssimloss'] for x in outputs]).mean()
tensorboard_logs = { 'val_l1loss': avg_l1loss, 'val_l2loss': avg_l2loss, 'val_ssimloss': avg_ssimloss }
return { 'val_l1loss': avg_l1loss, 'log': tensorboard_logs }
if __name__ == "__main__":
net = Im2Height()
print(net)