Ejemplo n.º 1
0
def main():
    print(
        "****** Run short training with InceptionV3 and save results. ******")
    num_classes = 10
    config_dict = {
        'base_model': 'InceptionV3',
        'num_dense_layers': 3,
        'num_dense_units_0': 500,
        'num_dense_units_1': 250,
        'num_dense_units_2': 50,
        'activation': 'relu',
        'dropout': True,
        'dropout_0': 0.5,
        'dropout_1': 0.5,
        'dropout_2': 0.5,
        'optimizer': "SGD",
        'learning_rate': 0.001,
        'cnn_unlock_epoch': 8,
        'unfreeze_percentage': 0.1,
        'batch_size': 16
    }
    _, _, histories = train(config_dict, epochs=16, num_classes=num_classes)
    print("HISTORIES:")
    print(histories)
    run_name = tools.get_run_name()
    tools.save_learning_curves(histories, run_name)
    csv_path = os.path.join("plots/", run_name, "data.csv")
    ut.write_csv_dict(histories,
                      keys=['loss', 'acc', 'val_loss', 'val_acc'],
                      filename=csv_path)
Ejemplo n.º 2
0
def main():
    print("Run short training with InceptionV3 and save results.")
    num_classes = 10
    model = create_pretrained_model(num_classes=num_classes)
    histories = train(model, num_classes, epochs=2, cnn_epochs=2)
    print("HISTORIES:")
    print(histories)
    run_name = tools.get_run_name()
    tools.save_learning_curves(histories, run_name)
    csv_path = os.path.join("plots/", run_name, "data.csv")
    ut.write_csv_dict(histories,
                      keys=['loss', 'acc', 'val_loss', 'val_acc'],
                      filename=csv_path)
Ejemplo n.º 3
0
def train(config_dict, 
          epochs,
          model=None,
          num_classes=10,
          save_model_path=None,
          save_data_path="plots",
          train_dir="data/model_train",
          valid_dir="data/model_valid",
          train_valid_split=0.7):

    start_time = time.time()
    #
    # extract relevant parts of configuration
    #
    cnn_unlock_epoch = config_dict["cnn_unlock_epoch"]
    unfreeze_percentage = config_dict["unfreeze_percentage"]
    batch_size = config_dict['batch_size']
    
    #
    # get model to train, determine training times
    #
    if model is None:
        model = _create_pretrained_model(config_dict, num_classes)
    
    if epochs <= cnn_unlock_epoch:
        training_epochs_dense = epochs
        training_epochs_wholemodel = 0
    else:
        training_epochs_dense = cnn_unlock_epoch
        training_epochs_wholemodel = epochs - cnn_unlock_epoch
    
    if model.name == 'InceptionV3' or model.name == 'Xception' or model.name == 'InceptionResNetV2':
        target_size = (299, 299)
    elif model.name == 'ResNet50' or model.name == 'MobileNet':
        target_size = (224, 224)
    else:
        print("invalid model: ", model.name)
    print("training model", model.name)    

    #
    # prepare training data
    #
    
    # create environment on filesystem with new random train/valid split
    num_train_imgs, num_valid_imgs = ut.create_small_case(
        sel_whales=np.arange(1, num_classes+1),
        train_dir=train_dir,
        valid_dir=valid_dir,
        train_valid=train_valid_split,
        sub_dirs=True)
    
    train_gen = image.ImageDataGenerator(
        # featurewise_center=True,
        # featurewise_std_normalization=True,
        rescale=1./255,   # redundant with featurewise_center ? 
        # preprocessing_function=preprocess_input, not used in most examples
        # horizontal_flip = True,    # no, as individual shapes are looked for
        fill_mode="nearest",
        zoom_range=0.3,
        width_shift_range=0.3,
        height_shift_range=0.3,
        rotation_range=30)
    
    train_flow = train_gen.flow_from_directory(
        train_dir,
        # save_to_dir="data/model_train/augmented",    
        # color_mode="grayscale",
        target_size=target_size,
        batch_size=batch_size, 
        class_mode="categorical")
    
    valid_gen = image.ImageDataGenerator(
        rescale=1./255,
        fill_mode="nearest")
    
    valid_flow = valid_gen.flow_from_directory(
        valid_dir,
        target_size=target_size,
        class_mode="categorical") 

    #
    # train fully connected part
    #
    hist_dense = model.fit_generator(
        train_flow, 
        steps_per_epoch=num_train_imgs//batch_size,
        verbose=2, 
        validation_data=valid_flow,
        validation_steps=num_valid_imgs//batch_size,
        epochs=training_epochs_dense)
    histories = hist_dense.history
    #
    # train the whole model with parts of the cnn unlocked (fixed optimizer!)
    #
    if training_epochs_wholemodel > 0:
        model = _unfreeze_cnn_layers(model, config_dict)
        hist_wholemodel = model.fit_generator(
            train_flow, 
            steps_per_epoch = num_train_imgs//batch_size,
            verbose = 2, 
            validation_data = valid_flow,
            validation_steps = num_valid_imgs//batch_size,
            epochs=training_epochs_wholemodel)
        # concatenate training history
        for key in histories.keys():
            if type(histories[key]) == list:
                histories[key].extend(hist_wholemodel.history[key])
    
    #
    # do final cleanup
    #
    if save_model_path is not None:
        model.save(save_model_path)

    if save_data_path is not None:
        run_name = tools.get_run_name()
        tools.save_learning_curves(histories, run_name, base_path=save_data_path)
        csv_path = os.path.join(save_data_path, run_name, run_name + ".csv")
        ut.write_csv_dict(histories,
                          keys=['loss', 'acc', 'val_loss', 'val_acc'],
                          filename=csv_path)
        config_file_path = os.path.join(save_data_path, run_name, "config.txt")
        ut.append_to_file("configuration of this run:", config_file_path)
        ut.append_to_file(config_dict, config_file_path)
        ut.append_to_file("epochs=" + str(epochs), config_file_path)
        ut.append_to_file("num_classes=" + str(num_classes), config_file_path)
        ut.append_to_file("train_valid_split=" + str(train_valid_split), config_file_path)

    hpbandster_loss = 1.0 - histories['val_acc'][-1]
    runtime = time.time() - start_time
    return (hpbandster_loss, runtime, histories)