-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathimage_inpainting.py
191 lines (158 loc) · 7.51 KB
/
image_inpainting.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import torch
from torch import nn
from models.BaseModels import BaseModule
from models.MobileNetV2 import PartialInvertedResidual
from models.partial_convolution import partial_convolution_block, DoubleUpSample
class ImageFill(BaseModule):
def __init__(self):
super(ImageFill, self).__init__()
self.act_fn = nn.LeakyReLU(0.3)
# up-sampling must be nearest s.t. masks are either 0 or 1
self.double_upscale = DoubleUpSample(scale_factor=2, mode='nearest')
encoder = [
# i, o, k, s, p, d, t, n
[64, 128, 3, 2, 1, 1, 2, 3],
[128, 256, 3, 2, 1, 1, 2, 3],
[256, 256, 3, 2, 1, 1, 2, 2],
]
self.encoder = nn.Sequential(
partial_convolution_block(3, 64, 7, 2, 3, 1, bias=True, BN=False, activation=self.act_fn),
*self.make_layers(encoder, use_1_conv=True))
# With large dilation rate, convolutions can easily overlap holes
feature_pooling = [
# in_c, out_c, k, s, p, d, t, n
[256, 256, 3, 1, 4, 4, 4, 1],
[256, 256, 3, 1, 7, 7, 4, 1],
[256, 256, 3, 1, 15, 15, 4, 1]
]
self.feature_pooling = nn.Sequential(*self.make_layers(feature_pooling, use_1_conv=True))
# 3x3 convolution is used to reduce gridding artifact. See "Dilated Residual Networks"
self.feature_cat = nn.Sequential(*self.make_layers([[256 * 4, 256, 5, 1, 2, 1, 1, 1]]))
decoder = [
# in_c, out_c, k, s, p, d, t, n
[256 + 256, 256, 3, 1, 1, 1, 1, 2],
[256 + 128, 128, 3, 1, 1, 1, 1, 2],
[128 + 64, 32, 3, 1, 1, 1, 1, 2],
]
self.decoder = nn.Sequential(
*self.make_layers(decoder, drop_first_1_conv=True),
partial_convolution_block(32 + 3, 3, 3, 1, 1, 1, bias=True, BN=False, activation=False))
def make_layers(self, settings, use_1_conv=False, drop_first_1_conv=False):
# similar to mobile net v2's inverted residual block
m = []
for in_c, out_c, k, s, p, d, t, n in settings:
layer = []
for i in range(n):
if i == 0:
layer.append(
PartialInvertedResidual(in_c, out_c, k, s, p, d, t, bias=False, BN=True, activation=self.act_fn,
use_1_conv=use_1_conv, drop_first_1_conv=drop_first_1_conv))
else:
layer.append(
PartialInvertedResidual(in_c, out_c, k, 1, p, d, t, bias=False, BN=True, activation=self.act_fn,
use_1_conv=use_1_conv, drop_first_1_conv=drop_first_1_conv))
in_c = out_c
m.append(nn.Sequential(*layer))
return m
def forward(self, args):
# mask: 1: ground truth, 0: holes
x, mask = args
feature_x, feature_mask = [x], [mask]
for index, layer in enumerate(self.encoder):
x, mask = layer((x, mask))
feature_x.append(x)
feature_mask.append(mask)
# unique, count = np.unique(mask.numpy(), return_counts=True)
# print(f"Unique number {unique} with counts: {count}")
# print('end of encoder')
feature_pool_x = [feature_x.pop(-1)]
feature_pool_m = [feature_mask.pop(-1)]
for layer in self.feature_pooling:
x_p, mask_p = layer((x, mask))
feature_pool_x.append(x_p)
feature_pool_m.append(mask_p)
# unique, count = np.unique(mask.numpy(), return_counts=True)
# print(f"Feature Pooling Unique number {unique} with counts: {count}")
# print('end of feature pool')
x = torch.cat(feature_pool_x, dim=1)
mask = torch.cat(feature_pool_m, dim=1)
x, mask = self.feature_cat((x, mask))
# unique, count = np.unique(mask.numpy(), return_counts=True)
# print(f"Unique number {unique} with counts: {count}")
# raise NotImplemented
# del feature_pool_m
# del feature_pool_x
for layer in self.decoder:
x_up, mask_up = self.double_upscale((x, mask))
x_h = torch.cat([x_up, feature_x.pop(-1)], dim=1)
mask_h = torch.cat([mask_up, feature_mask.pop(-1)], dim=1)
x, mask = layer((x_h, mask_h))
# unique, count = np.unique(mask.numpy(), return_counts=True)
# print(f"Unique number {unique} with counts: {count}")
return x
class ImageFillOrigin(BaseModule):
def __init__(self):
super(ImageFillOrigin, self).__init__()
self.act_fn = nn.LeakyReLU(0.3)
# up-sampling must be nearest s.t. masks are either 0 or 1
self.double_upscale = DoubleUpSample(scale_factor=2, mode='nearest')
encoder = [
# i, o, k, s, p, d, t, n
[64, 128, 5, 2, 2, 1, 1, 1],
[128, 256, 5, 2, 2, 1, 1, 1],
[256, 256, 3, 2, 1, 1, 1, 1],
[256, 256, 3, 2, 1, 1, 1, 1],
[256, 256, 3, 2, 1, 1, 1, 1],
[256, 256, 3, 2, 1, 1, 1, 1]
]
# self.encoder = nn.Sequential(
# partial_convolution_block(3, 64, 7, 2, 3, 1, bias=True, BN=False, activation=self.act_fn),
# *self.make_layers(encoder, use_1_conv=True))
self.encoder = nn.Sequential(
partial_convolution_block(3, 64, 7, 2, 3, 1, bias=True, BN=False, activation=self.act_fn),
*self.make_layer_v2(encoder))
decoder = [
# in_c, out_c, k, s, p, d, t, n
[256 + 256, 256, 3, 1, 1, 1, 1, 1],
[256 + 256, 256, 3, 1, 1, 1, 1, 1],
[256 + 256, 256, 3, 1, 1, 1, 1, 1],
[256 + 256, 256, 3, 1, 1, 1, 1, 1],
[256 + 128, 128, 3, 1, 1, 1, 1, 1],
[128 + 64, 32, 3, 1, 1, 1, 1, 1],
]
# self.decoder = nn.Sequential(
# *self.make_layers(decoder, drop_first_1_conv=True),
# partial_convolution_block(32 + 3, 3, 3, 1, 1, 1, bias=True, BN=False, activation=False))
self.decoder = nn.Sequential(
*self.make_layer_v2(decoder),
partial_convolution_block(32 + 3, 3, 3, 1, 1, 1, bias=True, BN=False, activation=False))
def make_layer_v2(self, settings):
m = []
for in_c, out_c, k, s, p, d, t, n in settings:
layer = partial_convolution_block(in_c, out_c, k, s, p, d,
groups=1, BN=True, activation=self.act_fn, bias=False)
m.append(nn.Sequential(layer))
return m
def forward(self, args):
# mask: 1: ground truth, 0: holes
x, mask = args
feature_x, feature_mask = [x], [mask]
for index, layer in enumerate(self.encoder):
x, mask = layer((x, mask))
feature_x.append(x)
feature_mask.append(mask)
# unique, count = np.unique(mask.numpy(), return_counts=True)
# print(f"Unique number {unique} with counts: {count}")
# raise NotImplemented
# del feature_pool_m
# del feature_pool_x
feature_x = feature_x[:-1]
feature_mask = feature_mask[:-1]
for layer in self.decoder:
x_up, mask_up = self.double_upscale((x, mask))
x_h = torch.cat([x_up, feature_x.pop(-1)], dim=1)
mask_h = torch.cat([mask_up, feature_mask.pop(-1)], dim=1)
x, mask = layer((x_h, mask_h))
# unique, count = np.unique(mask.numpy(), return_counts=True)
# print(f"Unique number {unique} with counts: {count}")
return x