Пример #1
0
def main():
    img_path = '/home/shixi/C3D-keras/datasets/ucfimgs/'
    train_file = 'train_list.txt'
    test_file = 'test_list.txt'
    f1 = open(train_file, 'r')
    f2 = open(test_file, 'r')
    lines = f1.readlines()
    f1.close()
    train_samples = len(lines)
    lines = f2.readlines()
    f2.close()
    val_samples = len(lines)

    num_classes = 2
    batch_size = 16
    epochs = 16

    model = c3d_model()
    lr = 0.005
    sgd = SGD(lr=lr, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
    model.summary()
    history = model.fit_generator(generator_train_batch(train_file, batch_size, num_classes,img_path),
                                  steps_per_epoch=train_samples // batch_size,
                                  epochs=epochs,
                                  callbacks=[onetenth_4_8_12(lr)],
                                  validation_data=generator_val_batch(test_file,
                                        batch_size,num_classes,img_path),
                                  validation_steps=val_samples // batch_size,
                                  verbose=1)
    if not os.path.exists('results/'):
        os.mkdir('results/')
    plot_history(history, 'results/')
    save_history(history, 'results/')
    model.save_weights('results/weights_c3d.h5')
Пример #2
0
def main():
    img_path = '/home/pirl/PycharmProjects/cnnTest/FrameImg/'
    train_file = 'newTrainlist.txt'
    test_file = 'newTestlist.txt'
    f1 = open(train_file, 'r')
    f2 = open(test_file, 'r')
    lines = f1.readlines()
    f1.close()
    train_samples = len(lines)
    lines = f2.readlines()
    f2.close()
    val_samples = len(lines)     # Confusing name : why val?

    # hyper parameter
    num_classes = 2
    batch_size = 16
    epochs = 14
    lr = 0.01

    model = c3d_model(True) # train mode: True, test mode: False
    model.summary()

    # gpu set
    modelFromGpu = multi_gpu_model(model, gpus=2)

    # optimizer Adam
    optimizer = Adam(lr=lr, beta_1=0.9, beta_2=0.999)

    modelFromGpu.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
    history = modelFromGpu.fit_generator(generator_train_batch(train_file, batch_size, num_classes,img_path),
                                  steps_per_epoch=train_samples // batch_size,
                                  epochs=epochs,
                                  callbacks=[onetenth_4_8_12(lr)],
                                  validation_data=generator_val_batch(test_file,
                                        batch_size,num_classes,img_path),
                                  validation_steps=val_samples // batch_size,
                                  verbose=1)
    if not os.path.exists('results/'):
        os.mkdir('results/')
    plot_history(history, 'results/')
    save_history(history, 'results/')

    model.save_weights('results/weights_c3d_lr001.h5')
Пример #3
0
def main(_mean, _std, _resize_img, _img_path, _train_file, _test_file, _num_classes, _batch_size, _epochs,
         _input_shape, weights_to_load, results_dir):
    '''
    Notes:
        1- make sure the labels are 0 based or 1 based: >> Line 64 in this file <<
        2- adjut the parameters/values in the main method
    :return:
    '''
    global mean, std, resize_img

    mean = _mean
    std = _std
    resize_img = _resize_img

    f1 = open(_train_file, 'r')
    f2 = open(_test_file, 'r')
    lines = f1.readlines()
    f1.close()
    train_samples = len(lines)
    lines = f2.readlines()
    f2.close()
    val_samples = len(lines)

    # model = c3d_model()
    lr = 0.005
    sgd = SGD(lr=lr, momentum=0.9, nesterov=True)
    # model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
    # model.summary()

    pretrained_model = c3d_model.c3d_model(_input_shape, 0.005, 11)
    pretrained_model.load_weights(weights_to_load)
    print("pretrained model")
    pretrained_model.summary()

    my_model = Model(input=pretrained_model.input,
                     output=c3d_model.c3d_classifier_layer(pretrained_model.layers[-3].output, 0.005, _num_classes, 512))
    print("\n\nfinal model")

    # for layer in my_model.layers:
    #     layer.trainable = False
    # my_model.layers[1].trainable=False
    # my_model.layers[3].trainable=False
    # my_model.layers[5].trainable=False
    # my_model.layers[7].trainable=False

    my_model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
    my_model.summary()

    history = my_model.fit_generator(generator_train_batch(_train_file, _batch_size, _num_classes, _img_path),
                                     steps_per_epoch=train_samples // _batch_size,
                                     epochs=_epochs,
                                     callbacks=[onetenth_4_8_12(lr)],
                                     validation_data=generator_val_batch(_test_file, _batch_size, _num_classes, _img_path),
                                     validation_steps=val_samples // _batch_size,
                                     verbose=1)
    if not os.path.exists(results_dir):
        os.mkdir(results_dir)

    plot_history(history, results_dir)
    save_history(history, results_dir)

    my_model.save_weights(results_dir+'/oa18_weights_c3d.h5')
Пример #4
0
def main(_mean,
         _std,
         _resize_img,
         _img_path,
         _train_file,
         _test_file,
         _num_classes,
         _batch_size,
         _epochs,
         _input_shape,
         stack_size,
         results_dir='results/',
         use_step_size=True):
    '''
    Notes:
        1- make sure the labels are 0 based or 1 based: >> Line 64 in this file <<
        2- adjut the parameters/values in the main method
    :return:
    '''
    global mean, std, resize_img
    mean = _mean
    std = _std
    resize_img = _resize_img

    f1 = open(_train_file, 'r')
    f2 = open(_test_file, 'r')
    lines = f1.readlines()

    f1.close()
    train_samples = len(lines)
    lines = f2.readlines()

    f2.close()
    val_samples = len(lines)

    model = c3d_model.c3d_model(_input_shape, 0.005, _num_classes)
    if not os.path.exists(results_dir):
        os.makedirs(results_dir)
    checkpoint = ModelCheckpoint(
        results_dir + 'model-{epoch:03d}-{acc:03f}-{val_acc:03f}.h5',
        verbose=1,
        monitor='val_loss',
        save_best_only=True,
        mode='auto')

    lr = 0.005
    sgd = SGD(lr=lr, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy',
                  optimizer=sgd,
                  metrics=['accuracy'])
    model.summary()

    history = model.fit_generator(
        generator_train_batch(_train_file, _batch_size, _num_classes,
                              _img_path, stack_size, use_step_size,
                              _input_shape),
        steps_per_epoch=train_samples // _batch_size,
        epochs=_epochs,
        callbacks=[onetenth_4_8_12(lr), checkpoint],
        validation_data=generator_val_batch(_test_file, _batch_size,
                                            _num_classes, _img_path,
                                            stack_size, use_step_size),
        validation_steps=val_samples // _batch_size,
        verbose=1)
    if not os.path.exists(results_dir):
        os.mkdir(results_dir)

    plot_history(history, results_dir)
    save_history(history, results_dir)
Пример #5
0
def main(id):
    img_path = '/media/lq/C13E-1ED0/dataset/UCF_Crimes/Imgs/RoadAccidents/'
    weights_dir = '/home/lq/Documents/Thesis/Thesis/weight'
    model_weight_filename = os.path.join(weights_dir, 'sports1M_weights_tf.h5')

    N_classes = 2
    batch_size = 24
    epochs = 32
    input_shape = (16, 120, 160, 3)
    windows_length = 16

    model = c3d_model.get_model()

    # Setting the Learning rate multipliers
    LR_mult_dict = {}
    LR_mult_dict['conv1'] = 1
    LR_mult_dict['conv2'] = 1
    LR_mult_dict['conv3a'] = 1
    LR_mult_dict['conv3b'] = 1
    LR_mult_dict['conv4a'] = 1
    LR_mult_dict['conv4b'] = 1
    LR_mult_dict['conv5a'] = 1
    LR_mult_dict['conv5b'] = 1
    LR_mult_dict['fc6'] = 1
    LR_mult_dict['fc7'] = 1
    LR_mult_dict['fc8'] = 10

    # Setting up optimizer
    base_lr = 0.00001
    adam = Adam(lr=base_lr, decay=0.00005, multipliers=LR_mult_dict)

    opt = adam
    convLayers = ['conv1', 'conv2', 'conv3a', 'conv3b', 'conv4a', 'conv4b']
    for layer in convLayers:
        model.get_layer(layer).trainable = False
    model.compile(loss='binary_crossentropy',
                  optimizer=opt,
                  metrics=['accuracy'])
    model.load_weights(model_weight_filename,
                       by_name=True,
                       skip_mismatch=True,
                       reshape=True)

    model.summary()

    from dataUtil import load_train_data, load_val_data
    train_AS_windows, train_A_windows, train_BG_windows = load_train_data(
    )  # load train data

    N_train_samples = len(train_A_windows)
    N_train_iterations = N_train_samples // batch_size

    val_AS_windows, val_A_windows, val_BG_windows = load_val_data(
    )  # load val data

    N_val_samples = len(val_A_windows) + len(val_AS_windows) + len(
        val_BG_windows)
    # N_val_samples = len(val_AS_windows) << 1
    N_val_iterations = N_val_samples // batch_size
    # ####################################
    print("--#train AS windows: " + str(len(train_AS_windows)) +
          " #train A windows: " + str(len(train_A_windows)) +
          " #train BG windows: " + str(len(train_BG_windows)))
    print("-N_val_samples:" + str(N_val_samples) + "\n--#val AS windows: " +
          str(len(val_AS_windows)) + " #val A windows: " +
          str(len(val_A_windows)) + " #val BG windows: " +
          str(len(val_BG_windows)))

    # ##################################
    result_dir = '/media/lq/C13E-1ED0/dataset/UCF_Crimes/results/adam_c3d_{}/'.format(
        id)
    best_weight_dir = result_dir + 'weights'
    best_weight_name = best_weight_dir + '/weights.{epoch:02d}-{val_loss:.3f}.hdf5'
    if not os.path.isdir(best_weight_dir):
        os.makedirs(best_weight_dir)
    if not os.path.exists(best_weight_dir):
        os.makedirs(result_dir)
    desp = result_dir + 'desp.txt'
    with open(desp, 'w') as f:
        f.write(
            'batch size: {}\nbase_lr: {} \ntrain_samples:{} \nval_samples:{}\n '
            .format(batch_size, base_lr, N_train_samples, N_val_samples))
        f.write('init_weiht: {}'.format(model_weight_filename))
        f.write('batch_generator: {}'.format(train_batch_generator))
    # callbacks
    csv_logger = CSVLogger(result_dir + '/log.csv', separator=',')
    checkpointer = ModelCheckpoint(filepath=best_weight_name,
                                   verbose=1,
                                   save_best_only=False,
                                   save_weights_only=True)
    # NAME = "THUMOS-{}".format(int(time.time()))
    log_dir = os.path.join(result_dir, 'log')
    tbCallBack = callbacks.TensorBoard(log_dir=log_dir,
                                       histogram_freq=0,
                                       write_graph=True,
                                       write_images=True)
    train_generator = train_batch_generator(train_A_windows, [],
                                            train_BG_windows, windows_length,
                                            batch_size, N_train_iterations,
                                            N_classes, img_path)
    val_generator = val_batch_generator(val_AS_windows, val_A_windows,
                                        val_BG_windows, windows_length,
                                        batch_size, N_val_iterations,
                                        N_classes, img_path)

    history = model.fit_generator(train_generator,
                                  steps_per_epoch=N_train_iterations,
                                  epochs=epochs,
                                  callbacks=[
                                      onetenth_4_8_12(base_lr), csv_logger,
                                      tbCallBack, checkpointer
                                  ],
                                  validation_data=val_generator,
                                  validation_steps=N_val_iterations,
                                  verbose=1)

    plot_history(history, result_dir)