Esempio n. 1
0
def fit_model(model, train_data, test_data, weights_dir, input_shape):
    try:
        if os.path.exists(weights_dir):
            model.load_weights(weights_dir)
            print('Load weights')
        train_generator = sequence_generator(train_data, BatchSize,
                                             input_shape, N_CLASSES)
        test_generator = sequence_generator(test_data, BatchSize, input_shape,
                                            N_CLASSES)
        print('Start fitting model')
        checkpointer = keras.callbacks.ModelCheckpoint(weights_dir,
                                                       save_best_only=True,
                                                       save_weights_only=True)
        adam = Adam()
        model.compile(loss='categorical_crossentropy',
                      optimizer=adam,
                      metrics=['accuracy'])
        model.fit_generator(train_generator,
                            steps_per_epoch=300,
                            epochs=200,
                            validation_data=test_generator,
                            validation_steps=100,
                            verbose=2,
                            callbacks=[checkpointer])
    except KeyboardInterrupt:
        print('Training time:')
Esempio n. 2
0
def fit_model(model,
              train_data,
              test_data,
              weights_dir,
              input_shape,
              optical_flow=False,
              project_dir='./'):
    try:
        # using sequence or image_from_sequnece generator
        if optical_flow:
            train_generator = sequence_generator(train_data, BatchSize,
                                                 input_shape, N_CLASSES)
            test_generator = sequence_generator(test_data, BatchSize,
                                                input_shape, N_CLASSES)
        else:
            train_generator = image_from_sequence_generator(
                train_data, BatchSize, input_shape, N_CLASSES)
            test_generator = image_from_sequence_generator(
                test_data, BatchSize, input_shape, N_CLASSES)

        # frames_dir = '/home/changan/ActionRecognition/data/flow_images'
        # train_generator = image_generator(train_data, frames_dir, BatchSize, input_shape, N_CLASSES, mean_sub=False,
        #                                   normalization=True, random_crop=True, horizontal_flip=True)
        # test_generator = image_generator(test_data, frames_dir, BatchSize, input_shape, N_CLASSES, mean_sub=False,
        #                                  normalization=True, random_crop=True, horizontal_flip=True)

        sgd = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
        model.compile(optimizer=sgd,
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])
        print(model.summary())

        print('Start fitting model')
        while True:
            checkpointer = keras.callbacks.ModelCheckpoint(
                weights_dir, save_best_only=True, save_weights_only=True)
            earlystopping = keras.callbacks.EarlyStopping(monitor='val_loss',
                                                          min_delta=0.001,
                                                          patience=20,
                                                          verbose=2,
                                                          mode='auto')
            tensorboard = keras.callbacks.TensorBoard(log_dir='./logs/try',
                                                      histogram_freq=0,
                                                      write_graph=True,
                                                      write_images=True)
            model.fit_generator(
                train_generator,
                steps_per_epoch=200,
                epochs=2000,
                validation_data=test_generator,
                validation_steps=100,
                verbose=2,
                callbacks=[checkpointer, tensorboard, earlystopping])
            data_dir = os.path.join(project_dir, 'data')
            list_dir = os.path.join(data_dir, 'ucfTrainTestlist')
            UCF_dir = os.path.join(data_dir, 'UCF-101')
            regenerate_data(data_dir, list_dir, UCF_dir)

    except KeyboardInterrupt:
        print('Training is interrupted')
Esempio n. 3
0
from utils.UCF_utils import sequence_generator, get_data_list
import os
import numpy as np
from train_CNN import LockedIterator
cwd = os.getcwd()
data_dir = os.path.join(cwd, 'data')
list_dir = os.path.join(data_dir, 'ucfTrainTestlist')
video_dir = os.path.join(data_dir, 'OF_data')
input_shape = (216, 216, 18)
N_CLASSES = 101
train_data, test_data, class_index = get_data_list(list_dir, video_dir)
c = LockedIterator(sequence_generator(train_data, 32, input_shape, N_CLASSES))
i = next(c)[0]
print("i shape = " + str(i.shape))
print(i)