def main(argv):
    data = get_data(FLAGS.train_data, FLAGS.num_classes)
    train_data = data.batch(16, drop_remainder=True)

    model = UNet(num_classes=FLAGS.num_classes)
    model.compile(optimizer=tf.keras.optimizers.Adam(),
                  loss='binary_crossentropy')
    model.fit(train_data, epochs=25)

    for index, (image, label) in enumerate(data.batch(1).take(5)):
        prediction = model.predict(image)
        plot_result(f'results/{index}.png', image, label, prediction)
Exemple #2
0
    lr_base = 0.01 * (float(batch_size) / 16)
    input_shape = (320, 320, 3)

    with tf.device("/cpu:0"):
        model = UNet(input_shape, weight_decay=3e-3, classes=21)

    if GPU_COUNT > 1:
        model.summary()
        from keras.utils.training_utils import multi_gpu_model
        model = multi_gpu_model(model, gpus=GPU_COUNT)

    model.compile(
        #        loss=crossentropy_without_ambiguous,
        loss=crossentropy_without_ambiguous,
        optimizer=Adam(lr=0.001),
        #        optimizer = SGD(lr=lr_base, momentum=0.9),
        metrics=[
            'accuracy', categorical_accuracy_without_ambiguous,
            categorical_accuracy_only_valid_classes
        ])

    model.summary()

    current_dir = os.getcwd()
    save_path = os.path.join(current_dir, 'output/unet')
    if not os.path.exists(save_path):
        os.mkdir(save_path)

    checkpoint_path = os.path.join(save_path, 'checkpoint_weights.hdf5')
    if RESUME:
        print('checkPoint file:{}'.format(checkpoint_path))
def train_model(name, experiment, image_size, training_data_list, training_mask_list,
                model_spec=[16, 32, 64, 128, 256], preprocess_list=None,
                preprocess_stretch=False, preprocess_mask=None,
                preprocess_fisher=False, keep_image=True,
                load_model=False, epochs=15):

    # make copies of the input array before shuffling
    training_data_list = list(training_data_list)
    training_mask_list = list(training_mask_list)

    random.Random(experiment*42).shuffle(training_data_list)
    random.Random(experiment*42).shuffle(training_mask_list)

    # we're augmenting data -- expand the list of training data
    train_input_img_paths = training_data_list[:-(test_samples
                                                  + val_samples)] * random_factor
    train_target_img_paths = training_mask_list[:-(test_samples
                                                   + val_samples)] * random_factor

    val_input_img_paths = training_data_list[-(
        test_samples + val_samples):-val_samples]
    val_target_img_paths = training_mask_list[-(
        test_samples + val_samples):-val_samples]

    test_input_img_paths = training_data_list[-test_samples:]
    test_target_img_paths = training_mask_list[-test_samples:]

    pp = None
    # Chain of preprocessing functions, first one added is performed first
    if preprocess_list is not None:
        # Instantiate data Sequences for each split
        if not preprocess_stretch:
            pp = ImagePreprocessGradient(preprocess_list, keep_image, pp)
        else:
            pp = ImagePreprocessStretchedGradient(preprocess_list, pp)

    if preprocess_mask is not None:
        # Apply mask after gradients - masking first only gets overwritten
        pp = ImagePreprocessMask(preprocess_mask, pp)

    if preprocess_fisher is True:
        pp = ImagePreprocessFisherize(pp)

    if pp is not None:
        # Instantiate pre-processed data sequences for each split
        train_gen = RoadSeq(batch_size, image_size,
                            train_input_img_paths, train_target_img_paths, augment_data=True,
                            preprocess_fn=pp.preprocess())
        val_gen = RoadSeq(batch_size, image_size,
                          val_input_img_paths, val_target_img_paths, augment_data=False,
                          preprocess_fn=pp.preprocess())
        test_gen = RoadSeq(len(test_input_img_paths), image_size,
                           test_input_img_paths, test_target_img_paths, augment_data=False,
                           preprocess_fn=pp.preprocess())

    else:
        # use the images as they are
        train_gen = RoadSeq(batch_size, image_size,
                            train_input_img_paths, train_target_img_paths, augment_data=True)
        val_gen = RoadSeq(batch_size, image_size,
                          val_input_img_paths, val_target_img_paths, augment_data=False)
        test_gen = RoadSeq(len(test_input_img_paths), image_size,
                           test_input_img_paths, test_target_img_paths, augment_data=False)

    model_name = name+'.'+str(experiment)+'.h5'
    model = UNet(image_size, model_spec)
    model.compile(optimizer="adam",
                  loss="binary_crossentropy", metrics=["acc"])
    if load_model:
        model.load_weights(model_name)
    model.summary()

    callbacks = [
        keras.callbacks.ModelCheckpoint(
            model_name, save_best_only=True)
    ]

    model.fit(train_gen, epochs=epochs, verbose=1,
              validation_data=val_gen, callbacks=callbacks)

    x, y = test_gen.__getitem__(0)
    start = timer()
    results = model.predict(x)
    end = timer()
    prediction_time = (end - start) / len(results)

    results = np.array(results > 0.5).astype(np.uint8)

    return calculate_error(results, test_target_img_paths) + (prediction_time,)
Exemple #4
0
    optimizer = tf.keras.optimizers.Adam(lr=config.lr)

    # compute class weights for the loss: inverse-frequency balanced
    # note: we set to 0 the weights for the classes "no_data"(0) and "clouds"(1) to ignore these
    class_weight = (1 / LCD.TRAIN_CLASS_COUNTS
                    ) * LCD.TRAIN_CLASS_COUNTS.sum() / (LCD.N_CLASSES)
    class_weight[LCD.IGNORED_CLASSES_IDX] = 0.
    print(f"Will use class weights: {class_weight}")

    #loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)
    loss = WeightedSparseCategoricalCrossEntropy()
    #loss = dice_loss()
    #loss = jaccard_loss()

    print("Compile model")
    model.compile(optimizer=optimizer, loss=loss, metrics=[])
    # metrics = [tf.keras.metrics.Precision(),
    #            tf.keras.metrics.Recall(),
    #            tf.keras.metrics.MeanIoU(num_classes=LCD.N_CLASSES)]) # TODO segmentation metrics

    # Launch training
    model.fit(
        train_dataset,
        epochs=config.epochs,
        callbacks=callbacks,
        steps_per_epoch=trainset_size // config.batch_size,
        validation_data=val_dataset,
        validation_steps=valset_size // config.batch_size,
    )
    model.save('/content/experiments/saved')
def run_demo(name,
             experiment,
             image_size,
             training_data_list,
             training_mask_list,
             model_spec=[16, 32, 64, 128, 256],
             preprocess_list=None,
             preprocess_stretch=False,
             preprocess_mask=None,
             keep_image=True):

    # make copies of the input array before shuffling
    training_data_list = list(training_data_list)
    random.Random(experiment * 42).shuffle(training_data_list)
    test_input_img_paths = training_data_list[-test_samples:]

    if training_mask_list is not None:
        training_mask_list = list(training_mask_list)
        random.Random(experiment * 42).shuffle(training_mask_list)
        test_target_img_paths = training_mask_list[-test_samples:]
    else:
        test_target_img_paths = None

    pp = None
    # Chain of preprocessing functions, first one added is performed first
    if preprocess_list is not None:
        # Instantiate data Sequences for each split
        if not preprocess_stretch:
            pp = ImagePreprocessGradient(preprocess_list, keep_image, pp)
        else:
            pp = ImagePreprocessStretchedGradient(preprocess_list, pp)

    if preprocess_mask is not None:
        # Apply mask after gradients - masking first only gets overwritten
        pp = ImagePreprocessMask(preprocess_mask, pp)

    if pp is not None:
        # Instantiate pre-processed data sequences for each split
        test_gen = RoadSeq(len(test_input_img_paths),
                           image_size,
                           test_input_img_paths,
                           test_target_img_paths,
                           augment_data=False,
                           preprocess_fn=pp.preprocess())

    else:
        # use the images as they are
        test_gen = RoadSeq(len(test_input_img_paths),
                           image_size,
                           test_input_img_paths,
                           test_target_img_paths,
                           augment_data=False)

    model_name = name + '.' + str(experiment) + '.h5'
    model = UNet(image_size, model_spec)
    model.compile(optimizer="adam",
                  loss="binary_crossentropy",
                  metrics=["acc"])
    model.load_weights(model_name)

    x, y = test_gen.__getitem__(0)
    results = model.predict(x)
    results = np.array(results > 0.5).astype(np.uint8)

    display_results(test_input_img_paths, test_target_img_paths, [results])

    return results