File size: 9,721 Bytes
69c590e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from tensorflow.keras.layers import *
from tensorflow.keras.models import Model
from tensorflow_addons.layers import InstanceNormalization
from networks.layers import AdaIN, AdaptiveAttention

import numpy as np


def residual_down_block(inputs, filters, resample=True):
    x = inputs

    r = Conv2D(filters=filters, kernel_size=1, strides=1, padding='same')(x)
    if resample:
        r = AveragePooling2D()(r)

    x = InstanceNormalization()(x)
    x = LeakyReLU(0.2)(x)
    x = Conv2D(filters=filters, kernel_size=3, strides=1, padding='same')(x)

    if resample:
        x = AveragePooling2D()(x)

    x = Add()([x, r])

    return x


def residual_up_block(inputs, filters, resample=True, name=None):
    x, z_id = inputs

    r = Conv2D(filters=filters, kernel_size=1, strides=1, padding='same')(x)
    if resample:
        r = UpSampling2D(interpolation='bilinear')(r)

    x = InstanceNormalization()(x)
    x = AdaIN()([x, z_id])
    x = LeakyReLU(0.2)(x)
    x = Conv2D(filters=filters, kernel_size=3, strides=1, padding='same')(x)

    if resample:
        x = UpSampling2D(interpolation='bilinear')(x)

    x = Add()([x, r])

    return x


def adaptive_attention(inputs, filters, name=None):
    x_t, x_s = inputs

    m = Concatenate(axis=-1)([x_t, x_s])
    m = Conv2D(filters=filters // 4, kernel_size=3, strides=1, padding='same')(m)
    m = LeakyReLU(0.2)(m)
    m = InstanceNormalization()(m)
    m = Conv2D(filters=filters, kernel_size=1, strides=1, padding='same', activation='sigmoid', name=name)(m)

    x = AdaptiveAttention()([m, x_t, x_s])

    return x


def adaptive_fusion_up_block(inputs, filters, resample=True, name=None):
    x_t, x_s, z_id = inputs

    x = adaptive_attention([x_t, x_s], x_t.shape[-1], name=name)

    r = Conv2D(filters=filters, kernel_size=1, strides=1, padding='same')(x)
    if resample:
        r = UpSampling2D(interpolation='bilinear')(r)

    x = InstanceNormalization()(x)
    x = AdaIN()([x, z_id])
    x = LeakyReLU(0.2)(x)
    x = Conv2D(filters=filters, kernel_size=3, strides=1, padding='same')(x)

    if resample:
        x = UpSampling2D(interpolation='bilinear')(x)

    x = Add()([x, r])

    return x


def dual_adaptive_fusion_up_block(inputs, filters, resample=True, name=None):
    x_t, x_s, z_id = inputs

    x = adaptive_attention([x_t, x_s], x_t.shape[-1], name=name + '_0')
    x = adaptive_attention([x_t, x], x_t.shape[-1], name=name + '_1')

    r = Conv2D(filters=filters, kernel_size=1, strides=1, padding='same')(x)
    if resample:
        r = UpSampling2D(interpolation='bilinear')(r)

    x = InstanceNormalization()(x)
    x = AdaIN()([x, z_id])
    x = LeakyReLU(0.2)(x)
    x = Conv2D(filters=filters, kernel_size=3, strides=1, padding='same')(x)

    if resample:
        x = UpSampling2D(interpolation='bilinear')(x)

    x = Add()([x, r])

    return x


def adaptive_fusion_up_block_no_add(inputs, filters, resample=True, name=None):
    x_t, x_s, z_id = inputs

    x = adaptive_attention([x_t, x_s], x_t.shape[-1], name=name)

    x = InstanceNormalization()(x)
    x = AdaIN()([x, z_id])
    x = LeakyReLU(0.2)(x)
    x = Conv2D(filters=filters, kernel_size=3, strides=1, padding='same')(x)

    return x


def adaptive_fusion_up_block_concat_baseline(inputs, filters, resample=True, name=None):
    x_t, x_s, z_id = inputs

    x = Concatenate(axis=-1)([x_t, x_s])

    r = Conv2D(filters=filters, kernel_size=1, strides=1, padding='same')(x)
    if resample:
        r = UpSampling2D(interpolation='bilinear')(r)

    x = InstanceNormalization()(x)
    x = AdaIN()([x, z_id])
    x = LeakyReLU(0.2)(x)
    x = Conv2D(filters=filters, kernel_size=3, strides=1, padding='same')(x)

    if resample:
        x = UpSampling2D(interpolation='bilinear')(x)

    x = Add(name=name if name == 'final' else None)([x, r])

    return x


def adaptive_fusion_up_block_add_baseline(inputs, filters, resample=True, name=None):
    x_t, x_s, z_id = inputs

    x = Add()([x_t, x_s])

    r = Conv2D(filters=filters, kernel_size=1, strides=1, padding='same')(x)
    if resample:
        r = UpSampling2D(interpolation='bilinear')(r)

    x = InstanceNormalization()(x)
    x = AdaIN()([x, z_id])
    x = LeakyReLU(0.2)(x)
    x = Conv2D(filters=filters, kernel_size=3, strides=1, padding='same')(x)

    if resample:
        x = UpSampling2D(interpolation='bilinear')(x)

    x = Add()([x, r])

    return x


def get_generator_original(mapping_depth=4, mapping_size=256):
    x_target = Input(shape=(256, 256, 3))
    z_source = Input(shape=(512,))

    z_id = z_source
    for m in range(np.max([mapping_depth - 1, 0])):
        z_id = Dense(mapping_size)(z_id)
        z_id = LeakyReLU(0.2)(z_id)
    if mapping_depth >= 1:
        z_id = Dense(mapping_size)(z_id)

    x_0 = Conv2D(filters=64, kernel_size=3, strides=1, padding='same')(x_target)    # 256

    x_1 = residual_down_block(x_0, 128)                                             # 128

    x_2 = residual_down_block(x_1, 256)                                             # 64

    x_3 = residual_down_block(x_2, 512)

    x_4 = residual_down_block(x_3, 512)

    x_5 = residual_down_block(x_4, 512)

    x_6 = residual_down_block(x_5, 512, resample=False)

    u_5 = residual_up_block([x_6, z_id], 512, resample=False)

    u_4 = residual_up_block([u_5, z_id], 512)

    u_3 = residual_up_block([u_4, z_id], 512)

    u_2 = residual_up_block([u_3, z_id], 256)                   # 64

    u_1 = adaptive_fusion_up_block([x_2, u_2, z_id], 128, name='aff_attention_64x64')       # 128

    u_0 = adaptive_fusion_up_block([x_1, u_1, z_id], 64, name='aff_attention_128x128')        # 256

    out = adaptive_fusion_up_block([x_0, u_0, z_id], 3, resample=False, name='aff_attention_256x256')

    gen_model = Model([x_target, z_source], out)
    gen_model.summary()

    return gen_model


def make_layer(l_type, inputs, filters, resample, name=None):
    if l_type == 'affa':
        return adaptive_fusion_up_block(inputs, filters, resample=resample, name=name)
    if l_type == 'd_affa':
        return dual_adaptive_fusion_up_block(inputs, filters, resample=resample, name=name)
    elif l_type == 'concat':
        return adaptive_fusion_up_block_concat_baseline(inputs, filters, resample=resample, name=name)
    elif l_type == 'no_skip':
        return residual_up_block(inputs[1:], filters, resample=resample)


def get_generator(up_types=None, mapping_depth=4, mapping_size=256):

    if up_types is None:
        up_types = ['no_skip', 'no_skip', 'd_affa', 'd_affa', 'd_affa', 'concat']

    x_target = Input(shape=(256, 256, 3))
    z_source = Input(shape=(512,))

    z_id = z_source
    for m in range(np.max([mapping_depth - 1, 0])):
        z_id = Dense(mapping_size)(z_id)
        z_id = LeakyReLU(0.2)(z_id)
    if mapping_depth >= 1:
        z_id = Dense(mapping_size)(z_id)

    x_0 = Conv2D(filters=64, kernel_size=3, strides=1, padding='same')(x_target)    # 256

    x_1 = residual_down_block(x_0, 128)                                             # 128

    x_2 = residual_down_block(x_1, 256)                                             # 64

    x_3 = residual_down_block(x_2, 512)

    x_4 = residual_down_block(x_3, 512)

    x_5 = residual_down_block(x_4, 512)

    x_6 = residual_down_block(x_5, 512, resample=False)

    u_5 = residual_up_block([x_6, z_id], 512, resample=False)

    u_4 = make_layer(up_types[0], [x_5, u_5, z_id], 512, resample=True, name='16x16')

    u_3 = make_layer(up_types[1], [x_4, u_4, z_id], 512, resample=True, name='32x32')

    u_2 = make_layer(up_types[2], [x_3, u_3, z_id], 256, resample=True, name='64x64')

    u_1 = make_layer(up_types[3], [x_2, u_2, z_id], 128, resample=True, name='128x128')

    u_0 = make_layer(up_types[4], [x_1, u_1, z_id], 64, resample=True, name='256x256')

    out = make_layer(up_types[5], [x_0, u_0, z_id], 3, resample=False, name='final')

    gen_model = Model([x_target, z_source], out)
    gen_model.summary()

    return gen_model


def get_generator_large(up_types=None, mapping_depth=4, mapping_size=512):

    if up_types is None:
        up_types = ['no_skip', 'no_skip', 'affa', 'affa', 'affa', 'concat']

    x_target = Input(shape=(256, 256, 3))
    z_source = Input(shape=(512,))

    z_id = z_source
    for m in range(np.max([mapping_depth - 1, 0])):
        z_id = Dense(mapping_size)(z_id)
        z_id = LeakyReLU(0.2)(z_id)
    if mapping_depth >= 1:
        z_id = Dense(mapping_size)(z_id)

    x_0 = Conv2D(filters=64, kernel_size=3, strides=1, padding='same')(x_target)    # 256

    x_1 = residual_down_block(x_0, 128)                                             # 128

    x_2 = residual_down_block(x_1, 256)                                             # 64

    x_3 = residual_down_block(x_2, 512)

    x_4 = residual_down_block(x_3, 512)

    x_5 = residual_down_block(x_4, 512)

    b_0 = residual_up_block([x_5, z_id], 512, resample=False)

    b_1 = residual_up_block([b_0, z_id], 512, resample=False)

    b_2 = residual_up_block([b_1, z_id], 512, resample=False)

    u_5 = residual_up_block([b_2, z_id], 512, resample=False)

    u_4 = make_layer(up_types[0], [x_5, u_5, z_id], 512, resample=True, name='16x16')

    u_3 = make_layer(up_types[1], [x_4, u_4, z_id], 512, resample=True, name='32x32')

    u_2 = make_layer(up_types[2], [x_3, u_3, z_id], 256, resample=True, name='64x64')

    u_1 = make_layer(up_types[3], [x_2, u_2, z_id], 128, resample=True, name='128x128')

    u_0 = make_layer(up_types[4], [x_1, u_1, z_id], 64, resample=True, name='256x256')

    out = make_layer(up_types[5], [x_0, u_0, z_id], 3, resample=False, name='final')

    gen_model = Model([x_target, z_source], out)
    gen_model.summary()

    return gen_model