forked from harshitbansal05/Image-Colorization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
164 lines (142 loc) · 5.22 KB
/
Copy pathmodel.py
File metadata and controls
164 lines (142 loc) · 5.22 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
import torch
import torch.nn as nn
import torch.nn.functional as F
class UNet(nn.Module):
def unet_conv(self, ch_in, ch_out, is_leaky):
"""
Construct a convolutional unit comprising of two conv layers
followed by a batch normalisation layer and ReLU/Leaky ReLU.
"""
if is_leaky:
return nn.Sequential(
nn.Conv2d(ch_in, ch_out, 3, padding=1),
nn.BatchNorm2d(ch_out),
nn.LeakyReLU(0.2),
nn.Conv2d(ch_out, ch_out, 3, padding=1),
nn.BatchNorm2d(ch_out),
nn.LeakyReLU(0.2)
)
else:
return nn.Sequential(
nn.Conv2d(ch_in, ch_out, 3, padding=1),
nn.BatchNorm2d(ch_out),
nn.ReLU(),
nn.Conv2d(ch_out, ch_out, 3, padding=1),
nn.BatchNorm2d(ch_out),
nn.ReLU()
)
def up(self, ch_in, ch_out):
"""
Applies a 2D bilinear upsampling to the input image which scales
the image 2x times, followed by a convolution with a 1x1 kernel.
"""
return nn.Sequential(
nn.ConvTranspose2d(ch_in, ch_out, 3, 2. 1, 1),
nn.ReLU()
)
def __init__(self, is_leaky):
"""
In the constructer, all the convolutional, upsampling and max pooling
units are instantiated and assigned as member variables.
"""
super(UNet, self).__init__()
# First encoding layer
self.conv1 = self.unet_conv(1, 64, is_leaky)
# Second encoding layer
self.conv2 = self.unet_conv(64, 128, is_leaky)
# Third encoding layer
self.conv3 = self.unet_conv(128, 256, is_leaky)
# Fourth encoding layer
self.conv4 = self.unet_conv(256, 512, is_leaky)
# Fifth encoding layer
self.conv5 = self.unet_conv(512, 1024, is_leaky)
# Pooling layer
self.pool = nn.MaxPool2d(2)
# First Upsampling layer
self.up1 = self.up(1024, 512)
# Second Upsampling layer
self.up2 = self.up(512, 256)
# Third Upsampling layer
self.up3 = self.up(256, 128)
# Fourth Upsampling layer
self.up4 = self.up(128, 64)
# First decoding layer
self.conv6 = self.unet_conv(1024, 512, False)
# Second decoding layer
self.conv7 = self.unet_conv(512, 256, False)
# Third decoding layer
self.conv8 = self.unet_conv(256, 128, False)
# Fourth decoding layer
self.conv9 = self.unet_conv(128, 64, False)
# Last layer
self.conv10 = nn.Conv2d(64, 2, 1)
def forward(self, x):
"""
An input tensor of a black and white image is accepted and
passed through the U-Net model. A colored image in CieLAB color
space is returned as the result.
"""
# Encoding path
x1 = self.conv1(x)
x2 = self.conv2(self.pool(x1))
x3 = self.conv3(self.pool(x2))
x4 = self.conv4(self.pool(x3))
x5 = self.conv5(self.pool(x4))
# Decoding path
x = self.conv6(torch.cat((x4, self.up1(x5)), 1))
x = self.conv7(torch.cat((x3, self.up2(x)), 1))
x = self.conv8(torch.cat((x2, self.up3(x)), 1))
x = self.conv9(torch.cat((x1, self.up4(x)), 1))
x = self.conv10(x)
m = nn.Tanh()
x = m(x)
return x
class DNet(nn.Module):
def unet_conv(self, ch_in, ch_out):
"""
Construct a convolutional unit comprising of two conv layers
followed by a batch normalisation layer and Leaky ReLU.
"""
return nn.Sequential(
nn.Conv2d(ch_in, ch_out, 3, padding=1),
nn.BatchNorm2d(ch_out),
nn.LeakyReLU(0.2),
nn.Conv2d(ch_out, ch_out, 3, padding=1),
nn.BatchNorm2d(ch_out),
nn.LeakyReLU(0.2)
)
def __init__(self):
super(DNet, self).__init__()
"""
In the constructer, all the convolutional and max pooling units
are instantiated and assigned as member variables.
"""
# First layer
self.conv1 = self.unet_conv(3, 64)
# Second layer
self.conv2 = self.unet_conv(64, 128)
# Third layer
self.conv3 = self.unet_conv(128, 256)
# Fourth layer
self.conv4 = self.unet_conv(256, 512)
# Fifth layer
self.conv5 = self.unet_conv(512, 1024)
# Pooling layer
self.pool = nn.MaxPool2d(2)
# Last layer
self.conv6 = nn.Linear(2 * 2 * 1024, 1)
def forward(self, x):
"""
An input tensor of a colored image from either the generator or source
is accepted and passed through the model. The probability of the image
belonging to the source domain is returned as the result.
"""
x1 = self.conv1(x)
x2 = self.conv2(self.pool(x1))
x3 = self.conv3(self.pool(x2))
x4 = self.conv4(self.pool(x3))
x5 = self.conv5(self.pool(x4))
x6 = x5.view(-1, 2 * 2 * 1024)
m = nn.Sigmoid()
x = m(self.conv6(x6))
return x