コード例 #1
0
def main():
    #project_path = r"/media/megatron/Projects/DeepSolaris"
    project_path = r"/media/tim/Data/Work/CBS/DeepSolaris"
    with Project(project_path=project_path) as project:
        dataset = project.dataset("Heerlen-HR")
        dataset.data = dataset.data[:, :, :, ::-1]

        train_dataset, test_dataset = dataset.split(test_size=0.2)

        train_generator = ImageGenerator(train_dataset)\
                          .with_rescale(1/255.)\
                          .with_rotation_range(30)\
                          .with_width_shift_range(0.1)\
                          .with_height_shift_range(0.1)\
                          .with_zoom_range(0.2)\
                          .with_shear_range(0.2)\
                          .with_horizontal_flip(True)\
                          .with_fill_mode("reflect")

        test_generator = ImageGenerator(test_dataset)\
                         .with_rescale(1/255.)

        image_shape = dataset.data[0].shape

        name = "vgg16_rmsprop_fine_tune_sgd"
        model = project.model(name)
        model.import_model(
            r"/media/tim/Data/Work/CBS/DeepSolaris/models/vgg16_full_fc512_fc512_fc1_aug_frozen_rms_prop/runs/2019/5/25/11:56:19/model.hdf5"
        )

        model.plot()
        name, cnn_model = train_model_until(model.model)
        cnn_model.summary()

        epochs = 15
        lr = 1e-2
        decay = lr / epochs
        #        .with_callbacks([EarlyStopping(patience=5)])\
        with model.run().with_epochs(epochs)\
                .with_batch_size(64)\
                .with_loss_function("binary_crossentropy")\
                .with_optimizer(SGDSettings(lr=lr, momentum=0.9, decay=decay, nesterov=False))\
                .with_metric_callbacks([ClassificationReportCallback(), ConfusionMatrixCallback(), PlotRocCallback()])\
                .with_class_weights(train_dataset.class_weights)\
                .with_train_dataset(train_generator)\
                .with_test_dataset(test_generator)\
                .with_evaluation_dataset(test_generator) as run:
            run.train()
            run.evaluate()
コード例 #2
0
def main():
    #project_path = r"/media/megatron/Projects/DeepSolaris"
    project_path = r"/media/tim/Data/Work/CBS/DeepSolaris"
    with Project(project_path=project_path) as project:
        dataset = project.dataset("Heerlen-HR")
        dataset.data = preprocess_input(dataset.data[:, :, :, ::-1])

        train_dataset, test_dataset = dataset.split(test_size=0.25)
        class_weights = train_dataset.class_weights

        train_dataset.labels = to_categorical(train_dataset.labels)
        test_dataset.labels = to_categorical(test_dataset.labels)

        train_generator = ImageGenerator(train_dataset)\
                          .with_seed(42)\
                          .with_rotation_range(30)\
                          .with_width_shift_range(0.1)\
                          .with_height_shift_range(0.1)\
                          .with_zoom_range(0.2)\
                          .with_shear_range(0.2)\
                          .with_horizontal_flip(True)\
                          .with_fill_mode("reflect")

        test_generator = ImageGenerator(test_dataset)\
                         .with_seed(84)

        image_shape = dataset.data[0].shape

        name, cnn_model = create_vgg16_model(input_shape=image_shape,
                                             layer_index=7)

        model = project.model(name)
        model.create_model(cnn_model)
        model.plot()
        cnn_model.summary()

        #        .with_callbacks([EarlyStopping(patience=5)])\
        with model.run().with_epochs(10)\
                .with_batch_size(64)\
                .with_loss_function("binary_crossentropy")\
                .with_optimizer(RMSPropSettings(lr=1e-5))\
                .with_metric_callbacks([ClassificationReportCallback(), ConfusionMatrixCallback(), PlotRocCallback()])\
                .with_class_weights(class_weights)\
                .with_train_dataset(train_generator)\
                .with_test_dataset(test_generator)\
                .with_evaluation_dataset(test_dataset) as run:
            run.train()
            run.evaluate()
コード例 #3
0
ファイル: test_dl.py プロジェクト: thinkpractice/DeepSolaris
                        .with_rotation_range(45)\
                        .with_width_shift_range(0.1)\
                        .with_height_shift_range(0.1)\
                        .with_shear_range(0.1)\
                        .with_zoom_range(0.1)\
                        .with_channel_shift_range(0.1)\
                        .with_horizontal_flip(0.1)\
                        .with_rescale(1./255)
    test_generator = ImageGenerator(test_dataset)\
                        .with_rescale(1./255)

    print("Number of training items: {}".format(len(train_dataset)))
    print("Number of testing items: {}".format(len(test_dataset)))

    model = project.model("vgg16_small")

    vgg16_small = vgg16_model()
    model.create_model(vgg16_small)

    run = model.run()\
        .with_epochs(5)\
        .with_loss_function("binary_crossentropy")\
        .with_sgd_optimizer(lr=0.0001, momentum=0.9, decay=0.0001 / 20, nesterov=True)\
        .with_metric_callbacks([ClassificationReportCallback(), ConfusionMatrixCallback(), PlotRocCallback()])\
        .with_train_dataset(train_generator)\
        .with_test_dataset(test_generator)\
        .with_evaluation_dataset(test_generator)

    run.train()
    run.evaluate()
コード例 #4
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-p",
                        "--project-path",
                        default=r"/media/megatron/Projects/DeepSolaris",
                        help="The project path to use")
    parser.add_argument("-d",
                        "--training-set",
                        required=True,
                        help="The dataset to train on")
    parser.add_argument("-t",
                        "--test-set",
                        required=True,
                        help="The dataset to test on")
    parser.add_argument("-v",
                        "--validation-set",
                        required=True,
                        help="The dataset to validate on")
    parser.add_argument("-s",
                        "--sample-size",
                        required=True,
                        type=float,
                        help="The sample fraction of the training set")
    parser.add_argument("-e",
                        "--epochs",
                        default=10,
                        type=int,
                        help="The number of epochs to train the model")
    parser.add_argument("-b",
                        "--batch_size",
                        default=32,
                        type=int,
                        help="The batch_size to train with")
    parser.add_argument("-l",
                        "--learning_rate",
                        default=1e-4,
                        type=float,
                        help="The learning rate to train with")
    #parser.add_argument("-m", "--model-filename", required=True, help="The model to evaluate")
    #parser.add_argument("-n", "--model-name", required=True, help="The name of the model to evaluate")
    args = vars(parser.parse_args())

    with Project(project_path=args["project_path"]) as project:
        dataset = project.dataset(args["training_set"])
        dataset.data = dataset.data[:, :, :, ::-1]
        train_dataset = dataset.sample(sample_size=args["sample_size"],
                                       random_state=42)
        test_dataset = project.dataset(args["test_set"])
        validation_dataset = project.dataset(args["validation_set"])

        train_generator = ImageGenerator(train_dataset)\
                          .with_rescale(1/255.)\
                          .with_seed(42)\
                          .with_rotation_range(30)\
                          .with_width_shift_range(0.1)\
                          .with_height_shift_range(0.1)\
                          .with_zoom_range(0.2)\
                          .with_shear_range(0.2)\
                          .with_horizontal_flip(True)\
                          .with_fill_mode("reflect")

        test_generator = ImageGenerator(test_dataset)\
                         .with_rescale(1/255.)\
                         .with_seed(84)

        validation_generator = ImageGenerator(validation_dataset)\
                                .with_rescale(1/255.)\
                                .with_seed(84)

        #model_name, cnn_model = load_model(args["model_filename"])
        model_name, cnn_model = create_vgg16_model(input_shape=(187, 187, 3))
        model = project.model("{}_{}".format(model_name,
                                             str(args["sample_size"])))
        model.create_model(cnn_model)
        model.plot()
        cnn_model.summary()

        with model.run().with_epochs(args["epochs"])\
                .with_batch_size(args["batch_size"])\
                .with_loss_function("binary_crossentropy")\
                .with_optimizer(RMSPropSettings(lr=args["learning_rate"]))\
                .with_metric_callbacks([ClassificationReportCallback(), ConfusionMatrixCallback(), PlotRocCallback()])\
                .with_class_weights(train_dataset.class_weights)\
                .with_train_dataset(train_generator)\
                .with_test_dataset(test_generator)\
                .with_evaluation_dataset(test_generator)\
                .with_evaluation_dataset(validation_generator) as run:
            run.train()
            run.evaluate()