-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
201 lines (177 loc) · 8.75 KB
/
model.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
192
193
194
195
196
197
198
199
200
201
from tensorflow.keras.layers import BatchNormalization as BatchNorm
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.layers import Input, Reshape, Activation
from tensorflow.keras.layers import Conv2D, Conv2DTranspose, UpSampling2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Concatenate
from tensorflow.keras import backend as K
from tensorflow.keras import layers
from tensorflow.keras import regularizers
import tensorflow as tf
def extract_first_features(filters, size, apply_batchnorm=True):
initializer = tf.keras.initializers.he_normal(seed=None)
result = tf.keras.Sequential()
result.add(tf.keras.layers.Conv2D(filters, size, padding='valid',
kernel_initializer=initializer, use_bias=False,
kernel_regularizer=tf.keras.regularizers.l2(0.001)))
if apply_batchnorm:
result.add(tf.keras.layers.BatchNormalization())
#result.add(tfa.layers.InstanceNormalization())
result.add(tf.keras.layers.ReLU())
return result
def NIR_domain_matching(input_x1, input_x2):
x_1 = input_x1
x_2 = input_x2
# for x_1
layer1 = extract_first_features(32, 3, True)
layer2 = extract_first_features(64, 3, True)
layer3 = extract_first_features(128, 3, True)
layer4 = extract_first_features(128, 3, True)
layer5 = extract_first_features(256, 3, True)
layer6 = extract_first_features(256, 3, True)
#layer7 = extract_first_features(256, 3, 1, True)
#layer8 = extract_first_features(256, 5, 2, True)
# for x_1
x_1 = layer1(x_1)
x_1 = layer2(x_1)
x_1 = layer3(x_1)
x_1 = layer4(x_1)
x_1 = layer5(x_1)
x_1 = layer6(x_1)
#x_1 = layer7(x_1)
#x_1 = layer8(x_1)
x_1 = layers.Flatten()(x_1)
# for x_2
x_2 = layer1(x_2)
x_2 = layer2(x_2)
x_2 = layer3(x_2)
x_2 = layer4(x_2)
x_2 = layer5(x_2)
x_2 = layer6(x_2)
#x_2 = layer7(x_2)
#x_2 = layer8(x_2)
x_2 = layers.Flatten()(x_2)
x = tf.abs(x_1 - x_2)
#x = tf.concat([x_1, x_2, x], 1)
#x = tf.reduce_sum(tf.multiply(x_1, x_2), axis=3, name='map_inner_product') # Batch x 1 x 201
return x
def RGB_domain_matching(input_x1, input_x2):
x_1 = input_x1
x_2 = input_x2
# for x_1
layer1 = extract_first_features(32, 3, True)
layer2 = extract_first_features(64, 3, True)
layer3 = extract_first_features(128, 3, True)
layer4 = extract_first_features(128, 3, True)
layer5 = extract_first_features(256, 3, True)
layer6 = extract_first_features(256, 3, True)
#layer7 = extract_first_features(256, 3, 1, True)
#layer8 = extract_first_features(256, 5, 2, True)
# for x_1
x_1 = layer1(x_1)
x_1 = layer2(x_1)
x_1 = layer3(x_1)
x_1 = layer4(x_1)
x_1 = layer5(x_1)
x_1 = layer6(x_1)
#x_1 = layer7(x_1)
#x_1 = layer8(x_1)
x_1 = layers.Flatten()(x_1)
# for x_2
x_2 = layer1(x_2)
x_2 = layer2(x_2)
x_2 = layer3(x_2)
x_2 = layer4(x_2)
x_2 = layer5(x_2)
x_2 = layer6(x_2)
#x_2 = layer7(x_2)
#x_2 = layer8(x_2)
x_2 = layers.Flatten()(x_2)
x = tf.abs(x_1 - x_2)
#x = tf.concat([x_1, x_2, x], 1)
#x = tf.reduce_sum(tf.multiply(x_1, x_2), axis=3, name='map_inner_product') # Batch x 1 x 201
return x
def downsampling(x, level, filters, kernel_size, num_convs, conv_strides=1, activation = 'relu', batch_norm = False, pool_size = 2, pool_strides = 2, regularizer = None, regularizer_param = 0.001,input_name=None):
if regularizer is not None:
if regularizer == 'l2':
reg = regularizers.l2(regularizer_param)
elif regularizer == 'l1':
reg = regularizers.l1(regularizer_param)
else:
reg = None
for i in range(num_convs):
x = Conv2D(filters=filters, kernel_size=kernel_size, strides=conv_strides, padding='same', kernel_regularizer=reg, bias_regularizer=reg, name = input_name + 'downsampling_' + str(level) + '_conv_' + str(i))(x)
if batch_norm:
x = BatchNorm(name = input_name + 'downsampling_' + str(level) + '_batchnorm_' + str(i))(x)
x = Activation(activation, name = input_name + 'downsampling_' + str(level) + '_activation_' + str(i))(x)
skip = x
x = MaxPooling2D(pool_size=2, strides=2)(x)
return x, skip
def bottleneck_dilated(x, filters, kernel_size, num_convs = 6, activation = 'relu', batch_norm = False, last_activation = False, regularizer = None, regularizer_param = 0.001,input_name=None):
# assert num_convs == len(conv_strides)
if regularizer is not None:
if regularizer == 'l2':
reg = regularizers.l2(regularizer_param)
elif regularizer == 'l1':
reg = regularizers.l1(regularizer_param)
else:
reg = None
skips = []
for i in range(num_convs):
x = Conv2D(filters=filters, kernel_size=kernel_size, strides=1, dilation_rate = 2 ** i, activation='relu', padding='same', kernel_regularizer=reg, bias_regularizer=reg, name = input_name + 'bottleneck_skip_' + str(i))(x)
skips.append(x)
x = layers.add(skips)
if last_activation:
x = Activation('relu')(x)
return x
def bottleneck(x, filters, kernel_size, num_convs, conv_strides=1, activation = 'relu', batch_norm = False, pool_size = 2, pool_strides = 2, regularizer = None, regularizer_param = 0.001,input_name = None):
if regularizer is not None:
if regularizer == 'l2':
reg = regularizers.l2(regularizer_param)
elif regularizer == 'l1':
reg = regularizers.l1(regularizer_param)
else:
reg = None
for i in range(num_convs):
x = Conv2D(filters=filters, kernel_size=3, strides=1, padding='same', kernel_regularizer=reg, bias_regularizer=reg, name = input_name + 'bottleneck_' + str(i))(x)
if batch_norm:
x = BatchNorm()(x)
x = Activation(activation)(x)
return x
def upsampling(x, level, skip, filters, kernel_size, num_convs, conv_strides=1, activation = 'relu', batch_norm = False, conv_transpose = True, upsampling_size = 2, upsampling_strides = 2, regularizer = None, regularizer_param = 0.001,input_name=None):
if regularizer is not None:
if regularizer == 'l2':
reg = regularizers.l2(regularizer_param)
elif regularizer == 'l1':
reg = regularizers.l1(regularizer_param)
else:
reg = None
if conv_transpose:
x = Conv2DTranspose(filters=filters, kernel_size = upsampling_size, strides=upsampling_strides, name = input_name + 'upsampling_' + str(level) + '_conv_trans_' + str(level))(x)
else:
x = UpSampling2D((upsampling_size), name = input_name + 'upsampling_' + str(level) + '_ups_' + str(i))(x)
x = Concatenate()([x, skip])
for i in range(num_convs):
x = Conv2D(filters=filters, kernel_size=3, strides=1, padding='same', kernel_regularizer=reg, bias_regularizer=reg, name = input_name + 'upsampling_' + str(level) + '_conv_' + str(i))(x)
if batch_norm:
x = BatchNorm(name = input_name + 'upsampling_' + str(level) + '_batchnorm_' + str(i))(x)
x = Activation(activation, name = input_name + 'upsampling_' + str(level) + '_activation_' + str(i))(x)
return x
def model_simple_unet_initializer(input=None,num_levels = 4, num_layers = 2, num_bottleneck = 2, filter_size_start = 32, kernel_size = 3, bottleneck_dilation = True, bottleneck_sum_activation = False, regularizer = 'l2', regularizer_param = 0.001,output_channels=3,input_name=None):
inputs = input
x = inputs
skips = []
for i in range(num_levels):
x, skip = downsampling(x, i, filter_size_start * (2 ** i), kernel_size, num_layers, batch_norm=True, regularizer= regularizer, regularizer_param=regularizer_param,input_name=input_name)
skips.append(skip)
if bottleneck_dilation:
x = bottleneck_dilated(x, filter_size_start * (2 ** num_levels), kernel_size, num_bottleneck, batch_norm=True, last_activation=bottleneck_sum_activation, regularizer= regularizer, regularizer_param=regularizer_param,input_name=input_name)
else:
x = bottleneck(x, filter_size_start * (2 ** num_levels), kernel_size, num_bottleneck, batch_norm=True, regularizer=regularizer, regularizer_param=regularizer_param,input_name=input_name)
for j in range(num_levels):
last_layer = num_levels - 1
if j < last_layer:
x = upsampling(x, j, skips[num_levels - j - 1], filter_size_start * (2 ** (num_levels - j - 1)), kernel_size, num_layers, batch_norm=True, regularizer= regularizer, regularizer_param=regularizer_param,input_name=input_name)
if j == last_layer:
x = upsampling(x, j, skips[num_levels - j - 1], output_channels, kernel_size, num_layers, batch_norm=True, regularizer= regularizer, regularizer_param=regularizer_param,input_name=input_name)
return x