Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notebook_tutorial for prediction with any video #128

Open
wants to merge 6 commits into
base: add-demo-script
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 35 additions & 29 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,41 +99,47 @@ def lrcn(self):
Also known as an LRCN:
https://arxiv.org/pdf/1411.4389.pdf
"""
def add_default_block(model, kernel_filters, init, reg_lambda):

# conv
model.add(TimeDistributed(Conv2D(kernel_filters, (3, 3), padding='same',
kernel_initializer=init, kernel_regularizer=L2_reg(l=reg_lambda))))
model.add(TimeDistributed(BatchNormalization()))
model.add(TimeDistributed(Activation('relu')))
# conv
model.add(TimeDistributed(Conv2D(kernel_filters, (3, 3), padding='same',
kernel_initializer=init, kernel_regularizer=L2_reg(l=reg_lambda))))
model.add(TimeDistributed(BatchNormalization()))
model.add(TimeDistributed(Activation('relu')))
# max pool
model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))

return model

initialiser = 'glorot_uniform'
reg_lambda = 0.001

model = Sequential()

model.add(TimeDistributed(Conv2D(32, (7, 7), strides=(2, 2),
activation='relu', padding='same'), input_shape=self.input_shape))
model.add(TimeDistributed(Conv2D(32, (3,3),
kernel_initializer="he_normal", activation='relu')))
# first (non-default) block
model.add(TimeDistributed(Conv2D(32, (7, 7), strides=(2, 2), padding='same',
kernel_initializer=initialiser, kernel_regularizer=L2_reg(l=reg_lambda)),
input_shape=self.input_shape))
model.add(TimeDistributed(BatchNormalization()))
model.add(TimeDistributed(Activation('relu')))
model.add(TimeDistributed(Conv2D(32, (3,3), kernel_initializer=initialiser, kernel_regularizer=L2_reg(l=reg_lambda))))
model.add(TimeDistributed(BatchNormalization()))
model.add(TimeDistributed(Activation('relu')))
model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))

model.add(TimeDistributed(Conv2D(64, (3,3),
padding='same', activation='relu')))
model.add(TimeDistributed(Conv2D(64, (3,3),
padding='same', activation='relu')))
model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))

model.add(TimeDistributed(Conv2D(128, (3,3),
padding='same', activation='relu')))
model.add(TimeDistributed(Conv2D(128, (3,3),
padding='same', activation='relu')))
model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))

model.add(TimeDistributed(Conv2D(256, (3,3),
padding='same', activation='relu')))
model.add(TimeDistributed(Conv2D(256, (3,3),
padding='same', activation='relu')))
model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))

model.add(TimeDistributed(Conv2D(512, (3,3),
padding='same', activation='relu')))
model.add(TimeDistributed(Conv2D(512, (3,3),
padding='same', activation='relu')))
model.add(TimeDistributed(MaxPooling2D((2, 2), strides=(2, 2))))
# 2nd-5th (default) blocks
model = add_default_block(model, 64, init=initialiser, reg_lambda=reg_lambda)
model = add_default_block(model, 128, init=initialiser, reg_lambda=reg_lambda)
model = add_default_block(model, 256, init=initialiser, reg_lambda=reg_lambda)
model = add_default_block(model, 512, init=initialiser, reg_lambda=reg_lambda)

# LSTM output head
model.add(TimeDistributed(Flatten()))

model.add(Dropout(0.5))
model.add(LSTM(256, return_sequences=False, dropout=0.5))
model.add(Dense(self.nb_classes, activation='softmax'))

Expand Down
2 changes: 1 addition & 1 deletion train_cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# Helper: Save the model.
checkpointer = ModelCheckpoint(
filepath=os.path.join('data', 'checkpoints', 'inception.{epoch:03d}-{val_loss:.2f}.hdf5',)
filepath=os.path.join('data', 'checkpoints', 'inception.{epoch:03d}-{val_loss:.2f}.hdf5'),
verbose=1,
save_best_only=True)

Expand Down
6 changes: 3 additions & 3 deletions validate_rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def validate(data_type, model, seq_length=40, saved_model=None,
print(rm.model.metrics_names)

def main():
model = 'mlp'
saved_model = 'data/ucf101/checkpoints/mlp-features.023-0.926.hdf5'
model = 'lstm'
saved_model = 'data/checkpoints/lstm-features.026-0.239.hdf5'

if model == 'conv_3d' or model == 'lrcn':
data_type = 'images'
Expand All @@ -48,7 +48,7 @@ def main():
image_shape = None

validate(data_type, model, saved_model=saved_model,
image_shape=image_shape)
image_shape=image_shape, class_limit=4)

if __name__ == '__main__':
main()