forked from farazBhatti/DRAEM-Tensoflow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
232 lines (225 loc) · 11.1 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
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
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from tensorflow.keras.layers import *
from keras.layers import Input
from tensorflow.keras.models import Model
from keras.layers.pooling.max_pooling2d import MaxPool2D
from keras.layers.core.activation import Activation
from keras.layers.normalization.batch_normalization_v1 import BatchNormalization
from numpy.core.shape_base import block
from keras.layers.convolutional.conv2d_transpose import Conv2D
def DiscriminativeSubNetwork(input_shape):
# Initialize model
base_channels = 64
kernel_size = 3
inputs = Input(shape=input_shape)
#Encoder Block
# block 1
block_1 = Conv2D(filters=base_channels, kernel_size = kernel_size, input_shape=input_shape[1:],padding='same')(inputs)
block_1 = BatchNormalization()(block_1)
block_1 = Activation('relu')(block_1)
block_1 = Conv2D(filters=base_channels, kernel_size = kernel_size, padding='same')(block_1)
block_1 = BatchNormalization()(block_1)
block_1 = Activation('relu')(block_1)
block_1_mp = MaxPool2D(pool_size=(2, 2))(block_1)
#block_2
block_2 = Conv2D(filters=base_channels*2, kernel_size = kernel_size, padding='same')(block_1_mp)
block_2 = BatchNormalization()(block_2)
block_2 = Activation('relu')(block_2)
block_2 = Conv2D(filters=base_channels*2, kernel_size = kernel_size, padding='same')(block_2)
block_2 = BatchNormalization()(block_2)
block_2 = Activation('relu')(block_2)
block_2_mp = MaxPool2D(pool_size=(2, 2))(block_2)
#block_3
block_3 = Conv2D(filters=base_channels*4, kernel_size = kernel_size, padding='same')(block_2_mp)
block_3 = BatchNormalization()(block_3)
block_3 = Activation('relu')(block_3)
block_3 = Conv2D(filters=base_channels*4, kernel_size = kernel_size, padding='same')(block_3)
block_3 = BatchNormalization()(block_3)
block_3 = Activation('relu')(block_3)
block_3_mp = MaxPool2D(pool_size=(2, 2))(block_3)
#block_4
block_4 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_3_mp)
block_4 = BatchNormalization()(block_4)
block_4 = Activation('relu')(block_4)
block_4 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_4)
block_4 = BatchNormalization()(block_4)
block_4 = Activation('relu')(block_4)
block_4_mp = MaxPool2D(pool_size=(2, 2))(block_4)
# block_5
block_5 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_4_mp)
block_5 = BatchNormalization()(block_5)
block_5 = Activation('relu')(block_5)
block_5 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_5)
block_5 = BatchNormalization()(block_5)
block_5 = Activation('relu')(block_5)
block_5_mp = MaxPool2D(pool_size=(2, 2))(block_5)
# block_6
block_6 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_5_mp)
block_6 = BatchNormalization()(block_6)
block_6 = Activation('relu')(block_6)
block_6 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_6)
block_6 = BatchNormalization()(block_6)
#decoder Block
block_7 = UpSampling2D(size = 2,)(block_6)
block_7 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_7)
block_7 = BatchNormalization()(block_7)
block_7 = Activation('relu')(block_7)
block_7 = Concatenate(axis=3)([block_7, block_5])
block_7 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_7)
block_7 = BatchNormalization()(block_7)
block_7 = Activation('relu')(block_7)
block_7 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_7)
block_7 = BatchNormalization()(block_7)
block_7 = Activation('relu')(block_7)
#block_8
block_8 = UpSampling2D(size = 2,)(block_7)
block_8 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_8)
block_8 = BatchNormalization()(block_8)
block_8 = Activation('relu')(block_8)
block_8 = Concatenate(axis=3)([block_8, block_4])
block_8 = Conv2D(filters=base_channels*4, kernel_size = kernel_size, padding='same')(block_8)
block_8 = BatchNormalization()(block_8)
block_8 = Activation('relu')(block_8)
block_8 = Conv2D(filters=base_channels*4, kernel_size = kernel_size, padding='same')(block_8)
block_8 = BatchNormalization()(block_8)
block_8 = Activation('relu')(block_8)
#block_9
block_9 = UpSampling2D(size = 2,)(block_8)
block_9 = Conv2D(filters=base_channels*4, kernel_size = kernel_size, padding='same')(block_9)
block_9 = BatchNormalization()(block_9)
block_9 = Activation('relu')(block_9)
block_9 = Concatenate(axis=3)([block_9, block_3])
block_9 = Conv2D(filters=base_channels*2, kernel_size = kernel_size, padding='same')(block_9)
block_9 = BatchNormalization()(block_9)
block_9 = Activation('relu')(block_9)
block_9 = Conv2D(filters=base_channels*2, kernel_size = kernel_size, padding='same')(block_9)
block_9 = BatchNormalization()(block_9)
block_9 = Activation('relu')(block_9)
#block_10
block_10 = UpSampling2D(size = 2,)(block_9)
block_10 = Conv2D(filters=base_channels*2, kernel_size = kernel_size, padding='same')(block_10)
block_10 = BatchNormalization()(block_10)
block_10 = Activation('relu')(block_10)
block_10 = Concatenate(axis=3)([block_10, block_2])
block_10 = Conv2D(filters=base_channels, kernel_size = kernel_size, padding='same')(block_10)
block_10 = BatchNormalization()(block_10)
block_10 = Activation('relu')(block_10)
block_10 = Conv2D(filters=base_channels, kernel_size = kernel_size, padding='same')(block_10)
block_10 = BatchNormalization()(block_10)
block_10 = Activation('relu')(block_10)
#block_11
block_11 = UpSampling2D(size = 2,)(block_10)
block_11 = Conv2D(filters=base_channels, kernel_size = kernel_size, padding='same')(block_11)
block_11 = BatchNormalization()(block_11)
block_11 = Activation('relu')(block_11)
block_11 = Concatenate(axis=3)([block_11, block_1])
block_11 = Conv2D(filters=base_channels, kernel_size = kernel_size, padding='same')(block_11)
block_11 = BatchNormalization()(block_11)
block_11 = Activation('relu')(block_11)
block_11 = Conv2D(filters=base_channels, kernel_size = kernel_size, padding='same')(block_11)
block_11 = BatchNormalization()(block_11)
block_11 = Activation('relu')(block_11)
#output
outputs = Conv2D(filters=2, kernel_size = kernel_size, padding='same')(block_11)
model = Model(inputs=[inputs], outputs=[outputs])
return model
def ReconstructiveSubNetwork(input_shape):
# Initialize model
base_channels = 128
kernel_size = 3
inputs = Input(shape=input_shape)
#Encoder Block
# block 1
block_1 = Conv2D(filters=base_channels, kernel_size = kernel_size, input_shape=input_shape,padding='same')(inputs)
block_1 = BatchNormalization()(block_1)
block_1 = Activation('relu')(block_1)
block_1 = Conv2D(filters=base_channels, kernel_size = kernel_size, padding='same')(block_1)
block_1 = BatchNormalization()(block_1)
block_1 = Activation('relu')(block_1)
block_1_mp = MaxPool2D(pool_size=(2, 2))(block_1)
#block_2
block_2 = Conv2D(filters=base_channels*2, kernel_size = kernel_size, padding='same')(block_1_mp)
block_2 = BatchNormalization()(block_2)
block_2 = Activation('relu')(block_2)
block_2 = Conv2D(filters=base_channels*2, kernel_size = kernel_size, padding='same')(block_2)
block_2 = BatchNormalization()(block_2)
block_2 = Activation('relu')(block_2)
block_2_mp = MaxPool2D(pool_size=(2, 2))(block_2)
#block_3
block_3 = Conv2D(filters=base_channels*4, kernel_size = kernel_size, padding='same')(block_2_mp)
block_3 = BatchNormalization()(block_3)
block_3 = Activation('relu')(block_3)
block_3 = Conv2D(filters=base_channels*4, kernel_size = kernel_size, padding='same')(block_3)
block_3 = BatchNormalization()(block_3)
block_3 = Activation('relu')(block_3)
block_3_mp = MaxPool2D(pool_size=(2, 2))(block_3)
#block_4
block_4 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_3_mp)
block_4 = BatchNormalization()(block_4)
block_4 = Activation('relu')(block_4)
block_4 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_4)
block_4 = BatchNormalization()(block_4)
block_4 = Activation('relu')(block_4)
block_4_mp = MaxPool2D(pool_size=(2, 2))(block_4)
# block_5
block_5 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_4_mp)
block_5 = BatchNormalization()(block_5)
block_5 = Activation('relu')(block_5)
block_5 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_5)
block_5 = BatchNormalization()(block_5)
block_5 = Activation('relu')(block_5)
#block_6
block_6 = UpSampling2D(size = 2,)(block_5)
block_6 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_6)
block_6 = BatchNormalization()(block_6)
block_6 = Activation('relu')(block_6)
block_6 = Conv2D(filters=base_channels*8, kernel_size = kernel_size, padding='same')(block_6)
block_6 = BatchNormalization()(block_6)
block_6 = Activation('relu')(block_6)
block_6 = Conv2D(filters=base_channels*4, kernel_size = kernel_size, padding='same')(block_6)
block_6 = BatchNormalization()(block_6)
block_6 = Activation('relu')(block_6)
#block_7
block_7 = UpSampling2D(size = 2,)(block_6)
block_7 = Conv2D(filters=base_channels*4, kernel_size = kernel_size, padding='same')(block_7)
block_7 = BatchNormalization()(block_7)
block_7 = Activation('relu')(block_7)
block_7 = Conv2D(filters=base_channels*4, kernel_size = kernel_size, padding='same')(block_7)
block_7 = BatchNormalization()(block_7)
block_7 = Activation('relu')(block_7)
block_7 = Conv2D(filters=base_channels*2, kernel_size = kernel_size, padding='same')(block_7)
block_7 = BatchNormalization()(block_7)
block_7 = Activation('relu')(block_7)
#block_8
block_8 = UpSampling2D(size = 2,)(block_7)
block_8 = Conv2D(filters=base_channels*2, kernel_size = kernel_size, padding='same')(block_8)
block_8 = BatchNormalization()(block_8)
block_8 = Activation('relu')(block_8)
block_8 = Conv2D(filters=base_channels*2, kernel_size = kernel_size, padding='same')(block_8)
block_8 = BatchNormalization()(block_8)
block_8 = Activation('relu')(block_8)
block_8 = Conv2D(filters=base_channels, kernel_size = kernel_size, padding='same')(block_8)
block_8 = BatchNormalization()(block_8)
block_8 = Activation('relu')(block_8)
#block_9
block_9 = UpSampling2D(size = 2,)(block_8)
block_9 = Conv2D(filters=base_channels, kernel_size = kernel_size, padding='same')(block_9)
block_9 = BatchNormalization()(block_9)
block_9 = Activation('relu')(block_9)
block_9 = Conv2D(filters=base_channels, kernel_size = kernel_size, padding='same')(block_9)
block_9 = BatchNormalization()(block_9)
block_9 = Activation('relu')(block_9)
block_9 = Conv2D(filters=base_channels, kernel_size = kernel_size, padding='same')(block_9)
block_9 = BatchNormalization()(block_9)
block_9 = Activation('relu')(block_9)
#output
outputs = Conv2D(filters=3, kernel_size = kernel_size, padding='same')(block_9)
model = Model(inputs=[inputs], outputs=[outputs])
return model
# input_shape = (256,256, 3)
# model = ReconstructiveSubNetwork(input_shape)
#
# # model.build(input_shape)
# model.summary()