예제 #1
0
def train_and_test():
    # load network
    model = vgg11(365, 1)

    model.compile(
        loss='categorical_crossentropy',
        # Learning rate will be set by lr_finder
        optimizer=SGD(lr=0.0, momentum=0.9),
        metrics=['accuracy', top_5])

    # load data
    img_size = (224, 224)
    color_mode = 'grayscale'
    batch_size = 64
    train_dir = '/usr/local/data/gabriel/places365_line_drawings/train'
    test_dir = '/usr/local/data/gabriel/places365_line_drawings/val'

    # fixed for places365
    nb_train_samples = 1803460.
    nb_test_samples = 36500.

    train_datagen = ImageDataGenerator(rescale=1. / 255,
                                       shear_range=0.2,
                                       zoom_range=0.2,
                                       horizontal_flip=True)

    # no test data for now
    #  test_datagen = ImageDataGenerator(rescale=1. / 255)

    train_gen = train_datagen.flow_from_directory(train_dir,
                                                  target_size=img_size,
                                                  batch_size=batch_size,
                                                  class_mode='categorical',
                                                  color_mode=color_mode)

    # no test data for now
    #  test_gen = test_datagen.flow_from_directory(
    #  test_dir,
    #  target_size = img_size,
    #  batch_size = batch_size,
    #  class_mode = 'categorical',
    #  color_mode = color_mode
    #  )

    # find best learning rate
    lr_finder = LRFinder(min_lr=1e-5,
                         max_lr=1e-2,
                         steps_per_epoch=np.ceil(nb_train_samples /
                                                 batch_size),
                         epochs=4)

    model.fit_generator(train_gen,
                        steps_per_epoch=np.ceil(nb_train_samples / batch_size),
                        epochs=4,
                        callbacks=[lr_finder])

    # save loss and learning rate plots to files
    lr_finder.plot_loss('loss.png')
    lr_finder.plot_lr('lr.png')
예제 #2
0
def _main():
    annotation_path = 'train.txt'
    classes_path = 'model_data/openimgs_classes.txt'
    anchors_path = 'model_data/yolo_anchors.txt'
    class_names = get_classes(classes_path)
    num_classes = len(class_names)
    anchors = get_anchors(anchors_path)

    input_shape = (416, 416)  # multiple of 32, hw

    # use darknet53 weights
    #model = create_model(input_shape, anchors, num_classes,
    #        freeze_body=2, weights_path='model_data/darknet53_weights.h5')
    model = create_model(input_shape,
                         anchors,
                         num_classes,
                         freeze_body=0,
                         weights_path='logs/001/trained_weights_stage_2.h5')

    val_split = 0.1
    with open(annotation_path) as f:
        lines = f.readlines()
    #np.random.seed(10101)
    np.random.shuffle(lines)
    #np.random.seed(None)
    num_val = int(len(lines) * val_split)
    num_val = 10000 if num_val > 10000 else num_val
    num_train = len(lines) - num_val

    if True:
        batch_size = 6
        lr_finder = LRFinder(min_lr=1e-10,
                             max_lr=2e-2,
                             steps_per_epoch=TOTAL_ITERATIONS,
                             epochs=1)
        for i in range(len(model.layers)):
            model.layers[i].trainable = True
        model.compile(optimizer=SGD(lr=1e-8),
                      loss={
                          'yolo_loss': lambda y_true, y_pred: y_pred
                      })

        print('train on {} samples, val on {} samples, with batch size {}.'.
              format(num_train, num_val, batch_size))
        model.fit_generator(data_generator_wrapper(lines[:num_train],
                                                   batch_size, input_shape,
                                                   anchors, num_classes),
                            steps_per_epoch=TOTAL_ITERATIONS,
                            validation_data=data_generator_wrapper(
                                lines[num_train:], batch_size, input_shape,
                                anchors, num_classes),
                            validation_steps=1,
                            epochs=1,
                            initial_epoch=0,
                            callbacks=[lr_finder])

        lr_finder.save_history('lr_finder_loss.csv')
        lr_finder.plot_loss('lr_finder_loss.png')
예제 #3
0
                              min_lr=0.000001,
                              cooldown=3,
                              verbose=1)

stop_on_nan = keras.callbacks.TerminateOnNaN()

# LR finder
if opt.find_lr:
    # pre-train to avoid model being too far away from interesting range
    history = model.fit_generator(gen_x_train,
                                  epochs=2,
                                  verbose=1,
                                  callbacks=[clr])
    lr_finder = LRFinder(model)
    lr_finder.find_generator(gen_x_train, 0.00001, 1.0, 5)
    lr_finder.plot_loss()
    import pdb
    pdb.set_trace()

# Run training
if not opt.notrain:
    # Train classifier
    history = model.fit_generator(
        gen_x_train,
        epochs=epochs,
        verbose=1,  # switch to 1 for more verbosity
        callbacks=[early_stopping, clr,
                   stop_on_nan],  #, reduce_lr], #, lr,   reduce_lr],
        # callbacks=[early_stopping, reduce_lr], #, lr, reduce_lr],
        use_multiprocessing=True,
        workers=8,
예제 #4
0
def main(batch_size: int = 24,
         epochs: int = 384,
         train_path: str = 'train',
         val_path: str = 'val',
         multi_gpu_weights=None,
         weights=None,
         workers: int = 8,
         find_lr: bool = False):

    keras_model = MobileDetectNetModel.complete_model()
    keras_model.summary()

    if weights is not None:
        keras_model.load_weights(weights, by_name=True)

    train_seq = MobileDetectNetSequence(train_path,
                                        stage="train",
                                        batch_size=batch_size)
    val_seq = MobileDetectNetSequence(val_path,
                                      stage="val",
                                      batch_size=batch_size)

    keras_model = keras.utils.multi_gpu_model(keras_model,
                                              gpus=[0, 1],
                                              cpu_merge=True,
                                              cpu_relocation=False)
    if multi_gpu_weights is not None:
        keras_model.load_weights(multi_gpu_weights, by_name=True)

    callbacks = []

    def region_loss(classes):
        def loss_fn(y_true, y_pred):
            # Don't penalize bounding box errors when there is no object present
            return 10 * classes * K.abs(y_pred - y_true)

        return loss_fn

    keras_model.compile(optimizer=SGD(),
                        loss=[
                            'mean_absolute_error',
                            region_loss(
                                keras_model.get_layer('classes').output),
                            'binary_crossentropy'
                        ])

    if find_lr:
        from lr_finder import LRFinder
        lr_finder = LRFinder(keras_model)
        lr_finder.find_generator(train_seq,
                                 start_lr=0.000001,
                                 end_lr=1,
                                 epochs=5)
        lr_finder.plot_loss()
        return

    filepath = "weights-{epoch:02d}-{val_loss:.4f}-multi-gpu.hdf5"
    checkpoint = ModelCheckpoint(filepath,
                                 monitor='val_loss',
                                 verbose=1,
                                 save_best_only=True,
                                 mode='min')
    callbacks.append(checkpoint)

    sgdr_sched = SGDRScheduler(0.00001,
                               0.01,
                               steps_per_epoch=np.ceil(
                                   len(train_seq) / batch_size),
                               mult_factor=1.5)
    callbacks.append(sgdr_sched)

    keras_model.fit_generator(
        train_seq,
        validation_data=val_seq,
        epochs=epochs,
        steps_per_epoch=np.ceil(len(train_seq) / batch_size),
        validation_steps=np.ceil(len(val_seq) / batch_size),
        callbacks=callbacks,
        use_multiprocessing=True,
        workers=workers,
        shuffle=True)