Exemple #1
0
def train():
    tf.keras.utils.get_file(
        fname='cec-videos.tar.gz',
        origin=
        'https://unir-tfm-cec.s3.us-east-2.amazonaws.com/cec-videos.tar.gz',
        extract=True)

    data = pandas.DataFrame(None,
                            columns=[
                                'trial', 'batch_size', 'time_steps', 'cycle',
                                'files', 'sequences'
                            ])
    data['trial'] = TRIAL

    for batch_size in BATCH_SIZE:
        for time_steps in TIME_STEPS:
            path = TRL_PATH + f'/{batch_size}/{time_steps}'
            os.makedirs(path, exist_ok=True)

            # Build and compile the model.
            model = build_model(time_steps, len(CLASSES))

            data_aug = tf.keras.preprocessing.image.ImageDataGenerator(
                zoom_range=.1,
                horizontal_flip=True,
                rotation_range=8,
                width_shift_range=.2,
                height_shift_range=.2,
                preprocessing_function=tf.keras.applications.mobilenet_v2.
                preprocess_input)

            train_idg = SlidingFrameGenerator(classes=CLASSES,
                                              glob_pattern=common.VIDEOS_PATH,
                                              nb_frames=time_steps,
                                              split_val=.2,
                                              shuffle=True,
                                              batch_size=batch_size,
                                              target_shape=(224, 224),
                                              nb_channel=3,
                                              transformation=data_aug,
                                              use_frame_cache=False)

            keras_video.utils.show_sample(train_idg)

            validation_idg = train_idg.get_validation_generator()

            row = {
                'trial': TRIAL,
                'batch_size': batch_size,
                'cycle': 'training',
                'time_steps': time_steps,
                'files': train_idg.files_count,
                'sequences': len(train_idg.vid_info)
            }
            data = data.append(row, ignore_index=True)

            row = {
                'trial': TRIAL,
                'batch_size': batch_size,
                'cycle': 'validation',
                'time_steps': time_steps,
                'files': validation_idg.files_count,
                'sequences': len(validation_idg.vid_info)
            }
            data = data.append(row, ignore_index=True)

            # Configure callbacks
            callbacks = [
                tf.keras.callbacks.ModelCheckpoint(
                    filepath=path + '/model',
                    monitor='val_accuracy',
                    mode='max',
                    save_best_only=True,
                    verbose=1,
                ),
                tf.keras.callbacks.EarlyStopping(monitor='val_loss',
                                                 mode='min',
                                                 verbose=1,
                                                 patience=int(EPOCHS * .01)),
                tf.keras.callbacks.CSVLogger(filename=path + '/log.csv'),
                tf.keras.callbacks.TensorBoard(log_dir=path + '/tb',
                                               histogram_freq=1),
                tf.keras.callbacks.ReduceLROnPlateau(verbose=1),
            ]

            history = model.fit(
                train_idg,
                validation_data=validation_idg,
                callbacks=callbacks,
                epochs=EPOCHS,
            )

            common.plot_acc_loss(history, path + '/plot.png')

    data.to_csv('trial_01.csv')
Exemple #2
0
def train():
    tf.keras.utils.get_file(
        fname='cec-videos-augmented.tar.gz',
        origin='https://unir-tfm-cec.s3.us-east-2.amazonaws.com/cec-videos-augmented.tar.gz',
        extract=True
    )

    for batch_size in BATCH_SIZE:
        for time_steps in TIME_STEPS:
            path = TRL_PATH + f'/{batch_size}/{time_steps}'
            os.makedirs(path, exist_ok=True)

            # Build and compile the model.
            model = build_model(time_steps, len(CLASSES))

            data_aug = tf.keras.preprocessing.image.ImageDataGenerator(
                preprocessing_function=tf.keras.applications.mobilenet.preprocess_input
            )

            train_idg = SlidingFrameGenerator(
                classes=CLASSES,
                glob_pattern=common.HOME + '/.keras/datasets/cec-videos-augmented/{classname}/*.avi',
                nb_frames=time_steps,
                split_val=.2,
                shuffle=True,
                batch_size=batch_size,
                target_shape=(224, 224),
                nb_channel=3,
                transformation=data_aug,
                use_frame_cache=False
            )

            validation_idg = train_idg.get_validation_generator()

            # Configure callbacks
            callbacks = [
                tf.keras.callbacks.ModelCheckpoint(
                    filepath=path + '/model',
                    monitor='val_accuracy',
                    mode='max',
                    save_best_only=True,
                    verbose=1,
                ),
                tf.keras.callbacks.EarlyStopping(
                    monitor='val_loss',
                    mode='min',
                    verbose=1,
                    patience=int(EPOCHS * .01)
                ),
                tf.keras.callbacks.CSVLogger(
                    filename=path + '/log.csv'
                ),
                tf.keras.callbacks.TensorBoard(
                    log_dir=path + '/tb',
                    histogram_freq=1
                ),
                tf.keras.callbacks.ReduceLROnPlateau(
                    verbose=1
                ),
            ]

            history = model.fit(
                train_idg,
                validation_data=validation_idg,
                callbacks=callbacks,
                epochs=EPOCHS,
            )

            common.plot_acc_loss(history, path + '/plot.png')
Exemple #3
0
#         more, frame = cap.read()
#
#         if more:
#             frame = cv2.resize(frame, (224, 224))
#             frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
#             frames.append(frame)
#     cap.release()
#
#     x = np.zeros((1, 0, 224, 224, 3), dtype=int)
#
#     for i in range(0, int(len(frames) / TIME_STEPS) * TIME_STEPS, int(len(frames) / TIME_STEPS)):
#         f = frames[i].reshape(1, 1, 224, 224, 3)
#         x = np.append(x, f, axis=1)

data_aug = tf.keras.preprocessing.image.ImageDataGenerator(
    preprocessing_function=tf.keras.applications.mobilenet.preprocess_input)

x = SlidingFrameGenerator(classes=CLASSES,
                          glob_pattern=common.HOME +
                          '/.keras/datasets/cec-videos-test/{classname}/*.avi',
                          nb_frames=TIME_STEPS,
                          split_val=None,
                          shuffle=True,
                          batch_size=32,
                          target_shape=(224, 224),
                          nb_channel=3,
                          transformation=data_aug,
                          use_frame_cache=False)

p = model.evaluate(x, verbose=1)