forked from ariesiitr/Lip-Reading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
84 lines (66 loc) · 4.2 KB
/
Copy pathmodel.py
File metadata and controls
84 lines (66 loc) · 4.2 KB
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
from keras.layers.convolutional import Conv3D, ZeroPadding3D
from keras.layers.pooling import MaxPooling3D
from keras.layers.core import Dense, Activation, SpatialDropout3D, Flatten
from keras.layers.wrappers import Bidirectional, TimeDistributed
from keras.layers.recurrent import GRU
from keras.layers.normalization import BatchNormalization
from keras.layers import Input,Lambda
from keras.models import Model
from keras import backend as K
def ctc_lambda_func(args):
y_pred, labels, input_length, label_length = args
y_pred = y_pred[:, :, :]
return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
def CTC(name, args):
return Lambda(ctc_lambda_func, output_shape=(1,), name=name)(args)
class LipNet(object):
def __init__(self, img_c=3, img_w=100, img_h=50, frames_n=75, absolute_max_string_len=32, output_size=28):
self.img_c = img_c
self.img_w = img_w
self.img_h = img_h
self.frames_n = frames_n
self.absolute_max_string_len = absolute_max_string_len
self.output_size = output_size
self.build()
def build(self):
if K.image_data_format() == 'channels_first':
input_shape = (self.img_c, self.frames_n, self.img_w, self.img_h)
else:
input_shape = (self.frames_n, self.img_w, self.img_h, self.img_c)
self.input_data = Input(name='the_input', shape=input_shape, dtype='float32')
self.zero1 = ZeroPadding3D(padding=(1, 2, 2), name='zero1')(self.input_data)
self.conv1 = Conv3D(32, (3, 5, 5), strides=(1, 2, 2), kernel_initializer='he_normal', name='conv1')(self.zero1)
self.batc1 = BatchNormalization(name='batc1')(self.conv1)
self.actv1 = Activation('relu', name='actv1')(self.batc1)
self.drop1 = SpatialDropout3D(0.5)(self.actv1)
self.maxp1 = MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2), name='max1')(self.drop1)
self.zero2 = ZeroPadding3D(padding=(1, 2, 2), name='zero2')(self.maxp1)
self.conv2 = Conv3D(64, (3, 5, 5), strides=(1, 1, 1), kernel_initializer='he_normal', name='conv2')(self.zero2)
self.batc2 = BatchNormalization(name='batc2')(self.conv2)
self.actv2 = Activation('relu', name='actv2')(self.batc2)
self.drop2 = SpatialDropout3D(0.5)(self.actv2)
self.maxp2 = MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2), name='max2')(self.drop2)
self.zero3 = ZeroPadding3D(padding=(1, 1, 1), name='zero3')(self.maxp2)
self.conv3 = Conv3D(96, (3, 3, 3), strides=(1, 1, 1), kernel_initializer='he_normal', name='conv3')(self.zero3)
self.batc3 = BatchNormalization(name='batc3')(self.conv3)
self.actv3 = Activation('relu', name='actv3')(self.batc3)
self.drop3 = SpatialDropout3D(0.5)(self.actv3)
self.maxp3 = MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2), name='max3')(self.drop3)
self.resh1 = TimeDistributed(Flatten())(self.maxp3)
self.gru_1 = Bidirectional(GRU(256, return_sequences=True, kernel_initializer='Orthogonal', name='gru1',reset_after=False), merge_mode='concat')(self.resh1)
self.gru_2 = Bidirectional(GRU(256, return_sequences=True, kernel_initializer='Orthogonal', name='gru2',reset_after=False), merge_mode='concat')(self.gru_1)
# transforms RNN output to character activations:
self.dense1 = Dense(self.output_size, kernel_initializer='he_normal', name='dense1')(self.gru_2)
self.y_pred = Activation('softmax', name='softmax')(self.dense1)
self.labels = Input(name='the_labels', shape=[self.absolute_max_string_len], dtype='float32')
self.input_length = Input(name='input_length', shape=[1], dtype='int64')
self.label_length = Input(name='label_length', shape=[1], dtype='int64')
self.loss_out = CTC('ctc', [self.y_pred, self.labels, self.input_length, self.label_length])
self.model = Model(inputs=self.input_data, outputs=self.y_pred)
def summary(self):
Model(inputs=self.input_data, outputs=self.y_pred).summary()
def predict(self, input_batch):
return self.test_function([input_batch, 0])[0]
@property
def test_function(self):
return K.function([self.input_data, K.learning_phase()], [self.y_pred, K.learning_phase()])