-
Notifications
You must be signed in to change notification settings - Fork 7
/
models.py
227 lines (182 loc) · 8.56 KB
/
models.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
from keras.models import Sequential, load_model
from keras.optimizers import Adam
from keras.layers import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv3D, MaxPooling3D
class ModelLoader():
def __init__(self, n_labels, seq_length, model_name,
saved_weights=None, optimizer=None, image_size=(100, 176)):
self.n_labels = n_labels
self.load_model = load_model
self.saved_weights = saved_weights
self.model_name = model_name
# Loads the specified model
if self.model_name == 'small_c3d':
print('Loading Small C3D model')
self.input_shape = ((seq_length,) + image_size + (3,) )
self.model = self.small_c3d()
elif self.model_name == 'smaller_c3d':
print('Loading Smaller C3D model')
self.input_shape = ((seq_length,) + image_size + (3,) )
self.model = self.smaller_c3d()
elif self.model_name == "c3d":
print('Loading C3D model')
self.input_shape = ((seq_length,) + image_size + (3,) )
self.model = self.c3d()
elif self.model_name == "big_c3d":
print('loading Big C3D model')
self.input_shape = ((seq_length,) + image_size + (3,) )
self.model = self.big_c3d()
elif self.model_name == "small_c3dv2":
print('loading Smaller C3D model v2')
self.input_shape = ((seq_length,) + image_size + (3,) )
self.model = self.small_c3dv2()
else:
raise Exception('No model with name {} found!'.format(model_name))
# Define metrics
metrics = ['accuracy', 'top_k_categorical_accuracy']
# If no optimizer is given, use Adam as default
if not optimizer:
optimizer = Adam()
self.model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=metrics)
print(self.model.summary())
def small_c3dv2(self):
"""See: 'https://arxiv.org/pdf/1412.0767.pdf' """
# Tunable parameters
kernel_size = (3, 3, 3)
strides = (1, 1, 1)
extra_conv_blocks = 1
model = Sequential()
# Conv Block 1
model.add(Conv3D(32, (7,7,5), strides=strides, activation='relu',
padding='same', input_shape=self.input_shape))
model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)))
# Conv Block 2
model.add(Conv3D(64, (5,5,3), strides=strides, activation='relu',
padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
# Conv Block 3
model.add(Conv3D(128, (5,5,3), strides=strides, activation='relu',
padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
#model.add(Dropout(0.25))
# Conv Block 4
model.add(Conv3D(128, (3,5,3), strides=strides, activation='relu',
padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
model.add(Dropout(0.25))
# Dense Block
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(self.n_labels, activation='softmax'))
return model
def small_c3d(self):
"""See: 'https://arxiv.org/pdf/1412.0767.pdf' """
# Tunable parameters
kernel_size = (3, 3, 3)
strides = (1, 1, 1)
extra_conv_blocks = 1
model = Sequential()
# Conv Block 1
model.add(Conv3D(32, kernel_size, strides=strides, activation='relu',
padding='same', input_shape=self.input_shape))
model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)))
# Conv Block 2
model.add(Conv3D(64, kernel_size, strides=strides, activation='relu',
padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
# Conv Block 3
model.add(Conv3D(128, kernel_size, strides=strides, activation='relu',
padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
# Conv Block 4
model.add(Conv3D(128, kernel_size, strides=strides, activation='relu',
padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
# Dense Block
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(self.n_labels, activation='softmax'))
return model
def big_c3d(self):
kernel_size = (3, 3, 3)
strides = (1, 1, 1)
model = Sequential()
# Conv Block 1
model.add(Conv3D(64, kernel_size, strides=strides, activation='relu',
padding='same', input_shape=self.input_shape))
model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)))
# Conv Block 2
model.add(Conv3D(128, kernel_size, strides=strides, activation='relu',
padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
# Conv Block 3
model.add(Conv3D(256, kernel_size, strides=strides, activation='relu',
padding='same'))
#model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
model.add(Conv3D(256, kernel_size, strides=strides, activation='relu',
padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
# Conv Block 4
model.add(Conv3D(512, kernel_size, strides=strides, activation='relu',
padding='same'))
model.add(Conv3D(512, kernel_size, strides=strides, activation='relu',
padding='same'))# Dense Block
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
#Conv Block 5
model.add(Conv3D(512, kernel_size, strides=strides, activation='relu',
padding='same'))
model.add(Conv3D(512, kernel_size, strides=strides, activation='relu',
padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
model.add(Flatten())
model.add(Dense(2048, activation='relu'))
model.add(Dense(2048, activation='relu'))
model.add(Dense(self.n_labels, activation='softmax'))
return model
def c3d(self):
"""See: 'https://arxiv.org/pdf/1412.0767.pdf' """
# Tunable parameters
strides = (1, 1, 1)
model = Sequential()
model.add(Conv3D(32, kernel_size=(3, 3, 3), strides=strides,input_shape=(
self.input_shape), border_mode='same', activation='relu'))
#model.add(Activation('relu'))
model.add(Conv3D(32, kernel_size=(3, 3, 3), strides=strides,padding='same', activation='softmax'))
#model.add(Activation('softmax'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), padding='same'))
model.add(Dropout(0.25))
model.add(Conv3D(64, kernel_size=(3, 3, 3),strides=strides, padding='same', activation='relu'))
#model.add(Activation('relu'))
model.add(Conv3D(64, kernel_size=(3, 3, 3),strides=strides, padding='same', activation='softmax'))
#model.add(Activation('softmax'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), padding='same'))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512, activation='sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(self.n_labels, activation='softmax'))
return model
def smaller_c3d(self):
model = Sequential()
model.add(Conv3D(
32, (7,7,7), activation='relu', input_shape=self.input_shape
))
model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)))
model.add(Conv3D(64, (3,3,3), activation='relu'))
model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)))
model.add(Conv3D(128, (2,2,2), activation='relu'))
model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)))
model.add(Flatten())
model.add(Dense(256))
model.add(Dropout(0.2))
model.add(Dense(256))
model.add(Dropout(0.2))
model.add(Dense(self.n_labels, activation='softmax'))
return model