-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3DInceptionV3.py
194 lines (144 loc) · 10.3 KB
/
3DInceptionV3.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
from tensorflow.keras.layers import Conv3D, MaxPool3D, AveragePooling3D, GlobalAveragePooling3D, GlobalMaxPool3D
def Inception3D_v3(classes, shape, input_layer=None, include_top=True, pooling=None):
if K.image_data_format() == 'channels_first':
channel_axis = 1
else:
channel_axis = 4 #Because now 4 dimensions besides batch
# Ensure that the model takes into account
# any potential predecessors of `input_layer`.
if input_layer is not None:
inputLayer = input_layer
else:
inputLayer = Input(shape=shape) #4 dimensions channels last
model = Conv3D(32, kernel_size=(3, 3, 3), padding='valid')(inputLayer)
model = Conv3D(64, kernel_size=(3, 3, 3), padding='valid')(model)
model = MaxPool3D((3, 3, 3), strides=(2, 2, 2))(model)
model = Conv3D(80, kernel_size=(1, 1, 1), padding='valid')(model)
model = Conv3D(192, kernel_size=(3, 3, 3), padding='valid')(model)
model = MaxPool3D((3, 3, 3), strides=(2, 2, 2))(model)
# mixed 0, 1, 2: 35 x 35 x 256
branch1x1 = Conv3D(64, kernel_size=(1, 1, 1))(model)
branch5x5 = Conv3D(48, kernel_size=(1, 1, 1))(model)
branch5x5 = Conv3D(64, kernel_size=(5, 5, 5), padding='same')(branch5x5) #Added same padding
branch3x3dbl = Conv3D(64, kernel_size=(1, 1, 1))(model)
branch3x3dbl = Conv3D(96, kernel_size=(3, 3, 3), padding='same')(branch3x3dbl) #Added same padding
branch3x3dbl = Conv3D(96, kernel_size=(3, 3, 3), padding='same')(branch3x3dbl) #Added same padding
branch_pool = AveragePooling3D((3, 3, 3), strides=(1, 1, 1), padding='same')(model)
branch_pool = Conv3D(32, kernel_size=(1, 1, 1))(branch_pool)
#problem with dimensions unless same padding is added as is above
model = Concatenate(axis=channel_axis, name='mixed')([branch1x1, branch5x5, branch3x3dbl, branch_pool])
# mixed 1: 35 x 35 x 256
branch1x1 = Conv3D(64, kernel_size=(1, 1, 1))(model)
branch5x5 = Conv3D(48, kernel_size=(1, 1, 1))(model)
branch5x5 = Conv3D(64, kernel_size=(5, 5, 5), padding='same')(branch5x5) #Added same padding
branch3x3dbl = Conv3D(64, kernel_size=(1, 1, 1))(model)
branch3x3dbl = Conv3D(96, kernel_size=(3, 3, 3), padding='same')(branch3x3dbl) #Added same padding
branch3x3dbl = Conv3D(96, kernel_size=(3, 3, 3), padding='same')(branch3x3dbl) #Added same padding
branch_pool = AveragePooling3D((3, 3, 3), strides=(1, 1, 1), padding='same')(model)
branch_pool = Conv3D(64, kernel_size=(1, 1, 1))(branch_pool)
#problem with dimensions unless same padding is added as is above
model = Concatenate(axis=channel_axis, name='mixed1')([branch1x1, branch5x5, branch3x3dbl, branch_pool])
# mixed 2: 35 x 35 x 256
branch1x1 = Conv3D(64, kernel_size=(1, 1, 1))(model)
branch5x5 = Conv3D(48, kernel_size=(1, 1, 1))(model)
branch5x5 = Conv3D(64, kernel_size=(5, 5, 5), padding='same')(branch5x5) #Added same padding
branch3x3dbl = Conv3D(64, kernel_size=(1, 1, 1))(model)
branch3x3dbl = Conv3D(96, kernel_size=(3, 3, 3), padding='same')(branch3x3dbl) #Added same padding
branch3x3dbl = Conv3D(96, kernel_size=(3, 3, 3), padding='same')(branch3x3dbl) #Added same padding
branch_pool = AveragePooling3D((3, 3, 3), strides=(1, 1, 1), padding='same')(model)
branch_pool = Conv3D(64, kernel_size=(1, 1, 1))(branch_pool)
#problem with dimensions unless added same padding
model = Concatenate(axis=channel_axis, name='mixed2')([branch1x1, branch5x5, branch3x3dbl, branch_pool])
# mixed 3: 17 x 17 x 768
branch3x3 = Conv3D(384, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding='valid')(model)
branch3x3dbl = Conv3D(64, kernel_size=(1, 1, 1))(model)
branch3x3dbl = Conv3D(96, kernel_size=(3, 3, 3), padding='same')(branch3x3dbl) #Added same padding
branch3x3dbl = Conv3D(96, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding='valid')(branch3x3dbl)
branch_pool = MaxPool3D((3, 3, 3), strides=(2, 2, 2))(model)
#problem with dimensions without same padding
model = Concatenate(axis=channel_axis, name='mixed3')([branch3x3, branch3x3dbl, branch_pool])
# mixed 4: 17 x 17 x 768
branch1x1 = Conv3D(192, kernel_size=(1, 1, 1))(model)
branch7x7 = Conv3D(128, kernel_size=(1, 1, 1))(model) #Unsure on transformations from here because 3d
branch7x7 = Conv3D(128, kernel_size=(1, 7, 1), padding='same')(branch7x7) #Added same padding
branch7x7 = Conv3D(192, kernel_size=(7, 1, 1), padding='same')(branch7x7) #Added same padding
branch7x7dbl = Conv3D(128, kernel_size=(1, 1, 1))(model)
branch7x7dbl = Conv3D(128, kernel_size=(7, 1, 1), padding='same')(branch7x7dbl) #Added same padding
branch7x7dbl = Conv3D(128, kernel_size=(1, 7, 1), padding='same')(branch7x7dbl) #Added same padding
branch7x7dbl = Conv3D(128, kernel_size=(7, 1, 1), padding='same')(branch7x7dbl) #Added same padding
branch7x7dbl = Conv3D(192, kernel_size=(1, 7, 1), padding='same')(branch7x7dbl) #Added same padding
branch_pool = AveragePooling3D((3, 3, 3), strides=(1, 1, 1), padding='same')(model)
branch_pool = Conv3D(192, kernel_size=(1, 1, 1))(branch_pool)
#problem with dimensions without same padding
model = Concatenate(axis=channel_axis, name='mixed4')([branch1x1, branch7x7, branch7x7dbl, branch_pool])
# mixed 5, 6: 17 x 17 x 768
for i in range(2):
branch1x1 = Conv3D(192, kernel_size=(1, 1, 1))(model)
branch7x7 = Conv3D(160, kernel_size=(1, 1, 1))(model)
branch7x7 = Conv3D(160, kernel_size=(1, 7, 1), padding='same')(branch7x7) #Added same padding
branch7x7 = Conv3D(192, kernel_size=(7, 1, 1), padding='same')(branch7x7) #Added same padding
branch7x7dbl = Conv3D(160, kernel_size=(1, 1, 1))(model)
branch7x7dbl = Conv3D(160, kernel_size=(7, 1, 1), padding='same')(branch7x7dbl) #Added same padding
branch7x7dbl = Conv3D(160, kernel_size=(1, 7, 1), padding='same')(branch7x7dbl) #Added same padding
branch7x7dbl = Conv3D(160, kernel_size=(7, 1, 1), padding='same')(branch7x7dbl) #Added same padding
branch7x7dbl = Conv3D(192, kernel_size=(1, 7, 1), padding='same')(branch7x7dbl) #Added same padding
branch_pool = AveragePooling3D((3, 3, 3), strides=(1, 1, 1), padding='same')(model)
branch_pool = Conv3D(192, kernel_size=(1, 1, 1))(branch_pool)
#problem with dimensons without same padding
model = Concatenate(axis=channel_axis, name=('mixed'+str(5 + i)))([branch1x1, branch7x7, branch7x7dbl, branch_pool])
#model = concatenate(inputs=[branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed'+str(5 + i)) #([branch1x1, branch7x7, branch7x7dbl, branch_pool])
# mixed 7: 17 x 17 x 768
branch1x1 = Conv3D(192, kernel_size=(1, 1, 1))(model)
branch7x7 = Conv3D(192, kernel_size=(1, 1, 1))(model)
branch7x7 = Conv3D(192, kernel_size=(1, 7, 1), padding='same')(branch7x7) #Added same padding
branch7x7 = Conv3D(192, kernel_size=(7, 1, 1), padding='same')(branch7x7) #Added same padding
branch7x7dbl = Conv3D(192, kernel_size=(1, 1, 1))(model)
branch7x7dbl = Conv3D(192, kernel_size=(7, 1, 1), padding='same')(branch7x7dbl) #Added same padding
branch7x7dbl = Conv3D(192, kernel_size=(1, 7, 1), padding='same')(branch7x7dbl) #Added same padding
branch7x7dbl = Conv3D(192, kernel_size=(7, 1, 1), padding='same')(branch7x7dbl) #Added same padding
branch7x7dbl = Conv3D(192, kernel_size=(1, 7, 1), padding='same')(branch7x7dbl) #Added same padding
branch_pool = AveragePooling3D((3, 3, 3), strides=(1, 1, 1), padding='same')(model)
branch_pool = Conv3D(192, kernel_size=(1, 1, 1))(branch_pool)
#problem with dimension without same padding
model = Concatenate(axis=channel_axis, name='mixed7')([branch1x1, branch7x7, branch7x7dbl, branch_pool])
# mixed 8: 8 x 8 x 1280
branch3x3 = Conv3D(192, kernel_size=(1, 1, 1))(model)
branch3x3 = Conv3D(320, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding='valid')(branch3x3)
branch7x7x3 = Conv3D(192, kernel_size=(1, 1, 1))(model)
branch7x7x3 = Conv3D(192, kernel_size=(1, 7, 1), padding='same')(branch7x7x3) #Added same padding
branch7x7x3 = Conv3D(192, kernel_size=(7, 1, 1), padding='same')(branch7x7x3) #Added same padding
branch7x7x3 = Conv3D(192, kernel_size=(3, 3, 3), strides=(2, 2, 2), padding='valid')(branch7x7x3)
branch_pool = MaxPool3D((3, 3, 3), strides=(2, 2, 2))(model)
#problem with dimesions without same padding
model = Concatenate(axis=channel_axis, name='mixed8')([branch3x3, branch7x7x3, branch_pool])
# mixed 9: 8 x 8 x 2048
for i in range(2):
branch1x1 = Conv3D(320, kernel_size=(1, 1, 1))(model)
branch3x3 = Conv3D(384, kernel_size=(1, 1, 1))(model)
branch3x3_1 = Conv3D(384, kernel_size=(1, 3, 1), padding='same')(branch3x3) #Added same padding
branch3x3_2 = Conv3D(384, kernel_size=(3, 1, 1), padding='same')(branch3x3) #Added same padding
#problem with dimension without same
branch3x3 = Concatenate(axis=channel_axis, name='mixed9_' + str(i))([branch3x3_1, branch3x3_2])
branch3x3dbl = Conv3D(448, kernel_size=(1, 1, 1))(model)
branch3x3dbl = Conv3D(384, kernel_size=(3, 3, 3), padding='same')(branch3x3dbl) #Added same padding
branch3x3dbl_1 = Conv3D(384, kernel_size=(1, 3, 1), padding='same')(branch3x3dbl) #Added same padding
branch3x3dbl_2 = Conv3D(384, kernel_size=(3, 1, 1), padding='same')(branch3x3dbl) #Added same padding
#problem with dimension without same
branch3x3dbl = Concatenate(axis=channel_axis)([branch3x3dbl_1, branch3x3dbl_2])
branch_pool = AveragePooling3D((3, 3, 3), strides=(1, 1, 1), padding='same')(model)
branch_pool = Conv3D(192, kernel_size=(1, 1, 1))(branch_pool)
#problem with dimension without same
model = Concatenate(axis=channel_axis, name='mixed' + str(9 + i))([branch1x1, branch3x3, branch3x3dbl, branch_pool])
if include_top:
# Classification block
model = GlobalAveragePooling3D(name='avg_pool')(model)
print(model)
model = Dense(classes, activation='softmax', name='predictions')(model)
else:
if pooling == 'avg':
model = GlobalAveragePooling3D()(model)
elif pooling == 'max':
model = GlobalMaxPool3D()(model)
# Create model.
model = Model(inputs=inputLayer, outputs=model, name='inception_v3_lite')
return model