-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
185 lines (142 loc) · 5.25 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
import keras
from keras.layers import Conv3D, MaxPool3D, Flatten, Dense, Activation, Dropout
from keras.layers.pooling import AvgPool3D
from keras.models import Sequential
from typing import Tuple
import os
import gesture
input_length = 32
input_width = 120
input_height = 90
"""
Hand Gesture Recognition with 3D Convolutional Neural Networks by Molchanov et al.
"""
class HRN(Sequential):
"""
High resolution network.
"""
def __init__(self):
# Construct base sequential model
super().__init__()
# Main model specification
# Convolutional layers
input_shape = [input_length, input_height, input_width, 2]
self.add(Conv3D(4, (5, 7, 7), input_shape=input_shape))
self.add(MaxPool3D())
self.add(Activation("relu"))
self.add(Conv3D(8, (3, 5, 5)))
self.add(MaxPool3D())
self.add(Activation("relu"))
self.add(Conv3D(32, (3, 5, 5)))
self.add(MaxPool3D(pool_size=(1, 2, 2)))
self.add(Activation("relu"))
self.add(Conv3D(64, (3, 3, 5)))
self.add(MaxPool3D(pool_size=(1, 2, 2)))
self.add(Activation("relu"))
# Fully-connected layers
self.add(Flatten())
self.add(Dense(512))
self.add(Activation("relu"))
self.add(Dropout(0.5))
self.add(Dense(256))
self.add(Activation("relu"))
self.add(Dropout(0.5))
self.add(Dense(len(gesture.category_names)))
self.add(Activation("softmax"))
# Compile models in constructor, since its compiling configuration is fixed
opt = keras.optimizers.SGD(learning_rate=0.005, momentum=0.9, nesterov=True)
self.compile(opt, loss="categorical_crossentropy", metrics=["accuracy"])
class LRN(Sequential):
"""
Low resolution network.
"""
def __init__(self):
super().__init__()
input_shape = [input_length, input_height, input_width, 2]
self.add(AvgPool3D(input_shape=input_shape, pool_size=(1, 2, 2)))
self.add(Conv3D(8, (5, 5, 5)))
self.add(MaxPool3D())
self.add(Activation("relu"))
self.add(Conv3D(32, (3, 5, 5)))
self.add(MaxPool3D())
self.add(Activation("relu"))
self.add(Conv3D(64, (3, 3, 5)))
self.add(MaxPool3D(pool_size=(2, 2, 4)))
self.add(Activation("relu"))
self.add(Flatten())
self.add(Dense(512))
self.add(Activation("relu"))
self.add(Dropout(0.5))
self.add(Dense(256))
self.add(Activation("relu"))
self.add(Dropout(0.5))
self.add(Dense(len(gesture.category_names)))
self.add(Activation("softmax"))
# Compile models in constructor, since its compiling configuration is fixed
opt = keras.optimizers.SGD(learning_rate=0.005, momentum=0.9, nesterov=True)
self.compile(opt, loss="categorical_crossentropy", metrics=["accuracy"])
class C3D(Sequential):
"""
Learning Spatialtemporal Features with 3D Convolutional Networks by Tran et.al.
"""
def __init__(self):
super().__init__()
input_shape = [input_length, input_height, input_width, 2]
self.add(Conv3D(64, 3, input_shape=input_shape, padding="same", activation="relu"))
self.add(MaxPool3D())
self.add(Conv3D(128, 3, padding="same", activation="relu"))
self.add(MaxPool3D())
self.add(Conv3D(256, 3, padding="same", activation="relu"))
self.add(Conv3D(256, 3, padding="same", activation="relu"))
self.add(MaxPool3D())
self.add(Conv3D(512, 3, padding="same", activation="relu"))
self.add(Conv3D(512, 3, padding="same", activation="relu"))
self.add(MaxPool3D())
self.add(Conv3D(512, 3, padding="same", activation="relu"))
self.add(Conv3D(512, 3, padding="same", activation="relu"))
self.add(MaxPool3D())
self.add(Flatten())
self.add(Dense(4096, activation='relu'))
self.add(Dropout(.5))
self.add(Dense(4096, activation='relu'))
self.add(Dropout(.5))
self.add(Dense(len(gesture.category_names), activation='softmax'))
opt = keras.optimizers.SGD(learning_rate=0.003, momentum=0.9, nesterov=True)
self.compile(opt, loss="categorical_crossentropy", metrics=["accuracy"])
network_spec = {
"hrn": {
"init": HRN,
"path": "weights/hrn.h5"
},
"lrn": {
"init": LRN,
"path": "weights/lrn.h5"
},
"c3d": {
"init": C3D,
"path": "weights/c3d.h5"
}
}
def load_model(name: str) -> Tuple[keras.Model]:
"""
Load one model from file.
"""
spec = network_spec[name]
model = spec["init"]()
if not os.path.exists(spec["path"]):
raise RuntimeError("Weight file of model %s is not found." % name)
model.load_weights(spec["path"])
return model
def load_two_models(m1_name: str, m2_name: str) -> Tuple[keras.Model, keras.Model]:
"""
Load two models from file at a time.
:param m1_name: name of first model to load
:param m2_name: name of second model to load
:return: (model_one, model_two)
"""
m1_model = load_model(m1_name)
if m1_name == m2_name :
return m1_model, m1_model
else:
m2_model = load_model(m2_name)
return m1_model, m2_model