-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
55 lines (46 loc) · 1.87 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
from keras.layers import Dense, Flatten, Dropout, ZeroPadding3D
from keras.layers.recurrent import LSTM
from keras.models import Sequential, load_model
from keras.optimizers import Adam, RMSprop
from keras.layers.wrappers import TimeDistributed
from keras.layers.convolutional import (Conv2D, MaxPooling3D, Conv3D,
MaxPooling2D)
from collections import deque
import sys
class ResearchModels():
def __init__(self, nb_classes, model, seq_length,
saved_model=None, features_length=2048):
# Set defaults.
self.seq_length = seq_length
self.load_model = load_model
self.saved_model = saved_model
self.nb_classes = nb_classes
self.feature_queue = deque()
# Set the metrics. Only use top k if there's a need.
metrics = ['accuracy']
#if self.nb_classes >= 10:
# metrics.append('top_k_categorical_accuracy')
# Get the appropriate model.
print("Loading LSTM model.")
self.input_shape = (seq_length, features_length)
self.model = self.lstm()
# Now compile the network.
optimizer = Adam(lr=0.00005)
self.model.compile(loss='categorical_crossentropy', optimizer=optimizer,
metrics=metrics)
print(self.model.summary())
def lstm(self):
"""Build a simple LSTM network. We pass the extracted features from
our CNN to this model predomenently."""
# Model.
print self.input_shape
model = Sequential()
model.add(LSTM(2048, return_sequences=True,
input_shape=self.input_shape,
dropout=0.5))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(self.nb_classes, activation='softmax'))
return model