Пример #1
0
def combine_patches(patches,
                    patches_gt,
                    test,
                    test_gt,
                    neighborhood_size: int = 1):
    data = []
    for patch, gt in zip(patches, patches_gt):
        data.append(HyperspectralDataset(patch, gt, neighborhood_size))
    test_set = HyperspectralDataset(test, test_gt, neighborhood_size)
    return ConcatDataset(data), test_set
Пример #2
0
def load_patches(directory: os.PathLike, neighborhood_size: int = 1):
    patches_paths = [
        x for x in os.listdir(directory) if 'gt' not in x and 'patch' in x
    ]
    gt_paths = [x for x in os.listdir(directory) if 'gt' in x and 'patch' in x]
    test_paths = [
        x for x in os.listdir(directory) if 'test' in x and '.npy' in x
    ]
    patches_paths = sorted_alphanumeric(patches_paths)
    gt_paths = sorted_alphanumeric(gt_paths)
    data = []
    for patch_path, gt_path in zip(patches_paths, gt_paths):
        data.append(
            HyperspectralDataset(os.path.join(directory, patch_path),
                                 os.path.join(directory, gt_path),
                                 neighborhood_size))
    test_data = HyperspectralDataset(os.path.join(directory, test_paths[0]),
                                     os.path.join(directory, test_paths[1]),
                                     neighborhood_size)
    return ConcatDataset(data), test_data
Пример #3
0
def generate_samples(args: argparse.Namespace) -> List:
    """
    Generate samples and normalize them.

    :param args: Parsed arguments.
    :return: List of samples
    """
    samples = HyperspectralDataset(dataset=args.data_path, ground_truth=args.labels_path,
                                   neighbourhood_size=args.neighborhood_size)
    samples.normalize_min_max()
    samples.normalize_labels()
    data = samples.get_data()
    if args.cont is not None:
        data = attention_selection(data=data, args=args)
    labels = samples.get_labels()
    samples = [x for x in zip(data, labels)]
    return samples
Пример #4
0
parser.add_argument('--learning_rate',
                    type=float,
                    default=0.00001,
                    help="Learning rate used for optimizing the parameters")
parser.add_argument('--lambda_gp', type=int, default=10)
parser.add_argument('--b1', type=float, default=0)
parser.add_argument('--b2', type=float, default=0.9)
args = parser.parse_args()
if args.verbose:
    print(args)

os.makedirs(args.artifacts_path, exist_ok=True)

cuda = True if torch.cuda.is_available() else False

transformed_dataset = HyperspectralDataset(args.dataset_path, args.gt_path)
transformed_dataset.normalize_min_max()
dataloader = DataLoader(transformed_dataset,
                        batch_size=args.batch_size,
                        shuffle=True,
                        drop_last=True)

input_shape = bands_count = transformed_dataset.get_data().shape[-1]
if args.classes_count == 0:
    classes_count = len(np.unique(transformed_dataset.get_labels()))
else:
    classes_count = args.classes_count

classifier_criterion = nn.CrossEntropyLoss()
# Initialize generator, discriminator and classifier
generator = Generator(input_shape, classes_count)
Пример #5
0
def main(args):
    os.makedirs(os.path.join(args.artifacts_path), exist_ok=True)
    # Init data
    test_data = HyperspectralDataset(args.dataset_path,
                                     args.gt_path,
                                     neighborhood_size=args.pixel_neighborhood)
    test_data.normalize_labels()
    if args.pixel_neighborhood == 1:
        test_data.expand_dims(axis=-1)
    if args.balanced == 1:
        train_data = BalancedSubset(test_data, args.train_samples)
        val_data = BalancedSubset(train_data, args.val_set_part)
    elif args.balanced == 0:
        train_data = ImbalancedSubset(test_data, args.train_samples)
        val_data = ImbalancedSubset(train_data, args.val_set_part)
    elif args.balanced == 2:  # Case for balanced indiana
        train_data = CustomSizeSubset(test_data, [
            30, 250, 250, 150, 250, 250, 20, 250, 15, 250, 250, 250, 150, 250,
            50, 50
        ])
        val_data = BalancedSubset(train_data, args.val_set_part)
    # Callbacks
    early = EarlyStopping(patience=args.patience)
    logger = CSVLogger(
        os.path.join(args.artifacts_path, args.output_file) + ".csv")
    checkpoint = ModelCheckpoint(
        os.path.join(args.artifacts_path, args.output_file) + "_model",
        save_best_only=True)
    timer = TimeHistory()

    # Normalize data
    max_ = train_data.max if train_data.max > val_data.max else val_data.max
    min_ = train_data.min if train_data.min < val_data.min else val_data.min
    train_data.normalize_min_max(min_=min_, max_=max_)
    val_data.normalize_min_max(min_=min_, max_=max_)
    test_data.normalize_min_max(min_=min_, max_=max_)

    if args.classes_count == 0:
        args.classes_count = len(np.unique(test_data.get_labels()))

    # Build model
    if args.pixel_neighborhood == 1:
        model = build_1d_model((test_data.shape[1:]), args.kernels,
                               args.kernel_size, args.classes_count)
    else:
        settings = build_settings_for_dataset(
            (args.pixel_neighborhood, args.pixel_neighborhood))
        model = build_3d_model(settings, args.classes_count,
                               test_data.shape[-1])
    print(model.summary())
    # Train model
    history = model.fit(x=train_data.get_data(),
                        y=train_data.get_one_hot_labels(args.classes_count),
                        batch_size=args.batch_size,
                        epochs=args.epochs,
                        verbose=args.verbose,
                        callbacks=[early, logger, checkpoint, timer],
                        validation_data=(val_data.get_data(),
                                         val_data.get_one_hot_labels(
                                             args.classes_count)))

    # Load best model
    model = load_model(
        os.path.join(args.artifacts_path, args.output_file) + "_model")

    # Calculate test set score
    test_score = model.evaluate(x=test_data.get_data(),
                                y=test_data.get_one_hot_labels(
                                    args.classes_count))
    # Calculate accuracy for each class
    predictions = model.predict(x=test_data.get_data())
    predictions = np.argmax(predictions, axis=1)
    class_accuracy = calculate_class_accuracy(predictions,
                                              test_data.get_labels(),
                                              args.classes_count)
    # Collect metrics
    train_score = max(history.history['acc'])
    val_score = max(history.history['val_acc'])
    times = timer.times
    training_time = times[-1]
    avg_epoch_time = np.average(np.array(timer.average))
    epochs = len(history.epoch)
    kappa = cohen_kappa_score(predictions, test_data.get_labels())

    # Save metrics
    metrics_path = os.path.join(args.artifacts_path, "metrics.csv")
    kappa_path = os.path.join(args.artifacts_path, "kappa.csv")
    save_to_csv(metrics_path, [
        train_score, val_score, test_score[1], training_time, epochs,
        avg_epoch_time
    ])
    class_accuracy_path = os.path.join(args.artifacts_path,
                                       "class_accuracy.csv")
    save_to_csv(class_accuracy_path, class_accuracy)
    save_to_csv(kappa_path, [kappa])
    np.savetxt(os.path.join(args.artifacts_path, args.output_file) +
               "_times.csv",
               times,
               fmt="%1.4f")
Пример #6
0
def main(args):
    os.makedirs(os.path.join(args.artifacts_path), exist_ok=True)
    # Init data
    test_data = HyperspectralDataset(args.dataset_path,
                                     args.gt_path,
                                     neighborhood_size=args.pixel_neighborhood)
    test_data.normalize_labels()
    if args.pixel_neighborhood == 1:
        test_data.expand_dims(axis=-1)
    if args.balanced:
        train_data = BalancedSubset(test_data, args.train_samples)
        val_data = BalancedSubset(train_data, args.val_set_part)
    elif args.balanced == 0:
        train_data = ImbalancedSubset(test_data, args.train_samples)
        val_data = ImbalancedSubset(train_data, args.val_set_part)
    elif args.balanced == 2:  # Case for balanced indiana
        train_data = CustomSizeSubset(test_data, [
            30, 250, 250, 150, 250, 250, 20, 250, 15, 250, 250, 250, 150, 250,
            50, 50
        ])
        val_data = BalancedSubset(train_data, args.val_set_part)
    # Callbacks
    early = EarlyStopping(patience=args.patience)
    logger = CSVLogger(
        os.path.join(args.artifacts_path, args.output_file) + ".csv")
    checkpoint = ModelCheckpoint(
        os.path.join(args.artifacts_path, args.output_file) + "_model",
        save_best_only=True)
    timer = TimeHistory()

    # Normalize data
    max_ = train_data.max if train_data.max > val_data.max else val_data.max
    min_ = train_data.min if train_data.min < val_data.min else val_data.min
    train_data.normalize_min_max(min_=min_, max_=max_)
    val_data.normalize_min_max(min_=min_, max_=max_)
    test_data.normalize_min_max(min_=min_, max_=max_)

    if args.pixel_neighbourhood == 1:
        test_data.expand_dims(axis=-1)
        train_data.expand_dims(axis=-1)
        val_data.expand_dims(axis=-1)

    if args.classes_count == 0:
        args.classes_count = len(np.unique(test_data.get_labels()))

    # Build model
    if args.pixel_neighborhood == 1:
        model = build_1d_model((test_data.shape[1:]), args.kernels,
                               args.kernel_size, args.classes_count)
    else:
        settings = build_settings_for_dataset(
            (args.pixel_neighborhood, args.pixel_neighborhood))
        model = build_3d_model(settings, args.classes_count,
                               test_data.shape[-1])

    # Train model
    history = model.fit(x=train_data.get_data(),
                        y=train_data.get_one_hot_labels(args.classes_count),
                        batch_size=args.batch_size,
                        epochs=args.epochs,
                        verbose=args.verbose,
                        callbacks=[early, logger, checkpoint, timer],
                        validation_data=(val_data.get_data(),
                                         val_data.get_one_hot_labels(
                                             args.classes_count)))

    # Load best model
    model = load_model(
        os.path.join(args.artifacts_path, args.output_file) + "_model")
    # Remove last dimension
    train_data.data = train_data.get_data()[:, :, 0]
    test_data.data = test_data.get_data()[:, :, 0]
    from time import time
    transformation = OnlineLightenTransform(scaling=[0.05, 0.1])
    start_online = time()
    transformation.fit(train_data.get_data())
    augmenter = OnlineAugmenter()
    test_score, class_accuracy = augmenter.evaluate(model, test_data,
                                                    transformation)
    online_time = time() - start_online
    # Collect metrics
    train_score = max(history.history['acc'])
    val_score = max(history.history['val_acc'])
    times = timer.times
    time_ = times[-1]
    avg_epoch_time = np.average(np.array(timer.average))
    epochs = len(history.epoch)

    # Save metrics
    metrics_path = os.path.join(args.artifacts_path, "metrics.csv")
    time_path = os.path.join(args.artifacts_path, "inference_time.csv")
    save_to_csv(
        metrics_path,
        [train_score, val_score, test_score, time_, epochs, avg_epoch_time])
    class_accuracy_path = os.path.join(args.artifacts_path,
                                       "class_accuracy.csv")
    save_to_csv(class_accuracy_path, class_accuracy)
    save_to_csv(time_path, [online_time])
    np.savetxt(os.path.join(args.artifacts_path, args.output_file) +
               "_times.csv",
               times,
               fmt="%1.4f")
Пример #7
0
def main(args):
    os.makedirs(os.path.join(args.artifacts_path), exist_ok=True)
    # Init data
    test_data = HyperspectralDataset(args.dataset_path, args.gt_path,
                                     neighbourhood_size=args.pixel_neighbourhood)
    test_data.normalize_labels()
    if args.balanced == 1:
        train_data = BalancedSubset(test_data, args.train_samples)
        val_data = BalancedSubset(train_data, args.val_set_part)
    elif args.balanced == 0:
        train_data = ImbalancedSubset(test_data, args.train_samples)
        val_data = ImbalancedSubset(train_data, args.val_set_part)
    elif args.balanced == 2:  # Case for balanced indiana
        train_data = CustomSizeSubset(test_data, [30, 250, 250, 150, 250, 250,
                                                  20, 250, 15, 250, 250, 250,
                                                  150, 250, 50, 50])
        val_data = BalancedSubset(train_data, args.val_set_part)
    # Normalize data
    max_ = train_data.max if train_data.max > val_data.max else val_data.max
    min_ = train_data.min if train_data.min < val_data.min else val_data.min
    train_data.normalize_min_max(min_=min_, max_=max_)
    val_data.normalize_min_max(min_=min_, max_=max_)
    test_data.normalize_min_max(min_=min_, max_=max_)

    custom_data_loader = OrderedDataLoader(train_data, args.batch_size)
    data_loader = DataLoader(train_data, batch_size=args.batch_size,
                             shuffle=True, drop_last=True)

    cuda = True if torch.cuda.is_available() else False
    input_shape = bands_count = train_data.shape[-1]
    if args.classes_count == 0:
        args.classes_count = len(np.unique(train_data.get_labels()))

    classifier_criterion = nn.CrossEntropyLoss()
    # Initialize generator, discriminator and classifier
    generator = Generator(input_shape, args.classes_count)
    discriminator = Discriminator(input_shape)
    classifier = Classifier(classifier_criterion, input_shape, args.classes_count,
                            use_cuda=cuda, patience=args.classifier_patience)

    # Optimizers
    optimizer_G = torch.optim.Adam(generator.parameters(),
                                   lr=args.learning_rate,
                                   betas=(args.b1, args.b2))
    optimizer_D = torch.optim.Adam(discriminator.parameters(),
                                   lr=args.learning_rate,
                                   betas=(args.b1, args.b2))
    optimizer_C = torch.optim.Adam(classifier.parameters(),
                                   lr=args.learning_rate,
                                   betas=(args.b1, args.b2))

    if cuda:
        generator = generator.cuda()
        discriminator = discriminator.cuda()
        classifier = classifier.cuda()
        classifier_criterion = classifier_criterion.cuda()

    # Train classifier
    classifier.train_(data_loader, optimizer_C, args.n_epochs_gan)

    gan = WGAN(generator, discriminator, classifier, optimizer_G, optimizer_D,
               use_cuda=cuda, lambda_gp=args.lambda_gp, critic_iters=args.n_critic,
               patience=args.patience_gan, summary_writer=SummaryWriter(args.artifacts_path),
               generator_checkout=args.generator_checkout)
    # Train GAN
    gan.train(custom_data_loader, args.n_epochs_gan, bands_count,
              args.batch_size, args.classes_count,
              os.path.join(args.artifacts_path, args.output_file) + "_generator_model")

    # Generate samples using trained Generator
    generator = Generator(input_shape, args.classes_count)
    generator_path = os.path.join(args.artifacts_path, args.output_file + "_generator_model")
    generator.load_state_dict(torch.load(generator_path))
    if cuda:
        generator = generator.cuda()
    train_data.convert_to_numpy()

    device = 'gpu' if cuda is True else 'cpu'
    samples_generator = SamplesGenerator(device=device)
    generated_x, generated_y = samples_generator.generate(train_data,
                                                          generator)
    generated_x = np.reshape(generated_x.detach().cpu().numpy(),
                             generated_x.shape + (1, ))

    train_data.expand_dims(axis=-1)
    test_data.expand_dims(axis=-1)
    val_data.expand_dims(axis=-1)

    train_data.vstack(generated_x)
    train_data.hstack(generated_y)

    # Callbacks
    early = EarlyStopping(patience=args.patience)
    logger = CSVLogger(os.path.join(args.artifacts_path, args.output_file) + ".csv")
    checkpoint = ModelCheckpoint(os.path.join(args.artifacts_path,
                                              args.output_file) + "_model",
                                 save_best_only=True)
    timer = TimeHistory()

    # Build model
    model = build_1d_model((test_data.shape[1:]), args.kernels,
                            args.kernel_size, args.classes_count)

    # Train model
    history = model.fit(x=train_data.get_data(),
                        y=train_data.get_one_hot_labels(args.classes_count),
                        batch_size=args.batch_size,
                        epochs=args.epochs,
                        verbose=args.verbose,
                        callbacks=[early, logger, checkpoint, timer],
                        validation_data=(val_data.get_data(),
                                         val_data.get_one_hot_labels(args.classes_count)))

    # Load best model
    model = load_model(os.path.join(args.artifacts_path, args.output_file) + "_model")

    # Calculate test set score
    test_score = model.evaluate(x=test_data.get_data(),
                                y=test_data.get_one_hot_labels(
                                    args.classes_count))

    # Calculate accuracy for each class
    predictions = model.predict(x=test_data.get_data())
    predictions = np.argmax(predictions, axis=1)
    class_accuracy = calculate_class_accuracy(predictions,
                                              test_data.get_labels(),
                                              args.classes_count)
    # Collect metrics
    train_score = max(history.history['acc'])
    val_score = max(history.history['val_acc'])
    times = timer.times
    time = times[-1]
    avg_epoch_time = np.average(np.array(timer.average))
    epochs = len(history.epoch)

    # Save metrics
    metrics_path = os.path.join(args.artifacts_path, "metrics.csv")
    save_to_csv(metrics_path, [train_score, val_score,
                               test_score[1], time, epochs, avg_epoch_time])
    class_accuracy_path = os.path.join(args.artifacts_path,
                                       "class_accuracy.csv")
    save_to_csv(class_accuracy_path, class_accuracy)
    np.savetxt(os.path.join(args.artifacts_path, args.output_file) +
               "_times.csv", times, fmt="%1.4f")
Пример #8
0
def main(args):
    os.makedirs(os.path.join(args.artifacts_path), exist_ok=True)
    # Init data
    test_data = HyperspectralDataset(args.dataset_path, args.gt_path,
                                     neighbourhood_size=args.pixel_neighbourhood)
    mapper = BandMapper()
    test_data.data = mapper.map(test_data.get_data(), args.bands)
    test_data.normalize_labels()
    if args.balanced == 1:
        train_data = BalancedSubset(test_data, args.train_samples)
        val_data = BalancedSubset(train_data, args.val_set_part)
    elif args.balanced == 0:
        train_data = ImbalancedSubset(test_data, args.train_samples)
        val_data = ImbalancedSubset(train_data, args.val_set_part)
    elif args.balanced == 2:  # Case for balanced indiana
        train_data = CustomSizeSubset(test_data, [30, 250, 250, 150, 250, 250,
                                                  20, 250, 15, 250, 250, 250,
                                                  150, 250, 50, 50])
        val_data = BalancedSubset(train_data, args.val_set_part)

    # Callbacks
    early = EarlyStopping(patience=args.patience)
    logger = CSVLogger(os.path.join(args.artifacts_path, args.output_file) + ".csv")
    checkpoint = ModelCheckpoint(os.path.join(args.artifacts_path, args.output_file) + "_model",
                                 save_best_only=True)
    timer = TimeHistory()
    # Normalize data
    max_ = train_data.max if train_data.max > val_data.max else val_data.max
    min_ = train_data.min if train_data.min < val_data.min else val_data.min
    train_data.normalize_min_max(min_=min_, max_=max_)
    val_data.normalize_min_max(min_=min_, max_=max_)
    test_data.normalize_min_max(min_=min_, max_=max_)

    if args.pixel_neighbourhood == 1:
        test_data.expand_dims(axis=-1)
        train_data.expand_dims(axis=-1)
        val_data.expand_dims(axis=-1)

    if args.classes_count == 0:
        args.classes_count = len(np.unique(test_data.get_labels()))

    # Load model
    model = load_model(os.path.join(args.models_dir, args.output_file + "_model"))

    model.pop()
    model.pop()
    model.pop()

    first = Dense(units=20, activation='relu')(model.output)
    second = Dense(units=20, activation='relu')(first)
    new_layer = Dense(units=args.classes_count, activation='softmax')(second)

    model = Model(inputs=model.input, outputs=new_layer)

    # for layer in model.layers[0:7]:
    #     layer.trainable = False
    if args.blocks >= 1:
        model.layers[1].trainable = False
    if args.blocks >= 2:
        model.layers[3].trainable = False
    if args.blocks >= 3:
        model.layers[5].trainable = False

    optimizer = Adam(lr=0.0001)
    model.compile(optimizer=optimizer,
                  metrics=['accuracy'],
                  loss='categorical_crossentropy')
    print(model.summary())
    history = model.fit(x=train_data.get_data(),
                        y=train_data.get_one_hot_labels(args.classes_count),
                        batch_size=args.batch_size,
                        epochs=args.epochs,
                        verbose=args.verbose,
                        callbacks=[early, logger, checkpoint, timer],
                        validation_data=(val_data.get_data(),
                                         val_data.get_one_hot_labels(args.classes_count)))

    # Load best model
    model = load_model(os.path.join(args.artifacts_path, args.output_file + "_model"))

    # Calculate test set score
    test_score = model.evaluate(x=test_data.get_data(),
                                y=test_data.get_one_hot_labels(args.classes_count))

    # Calculate accuracy for each class
    predictions = model.predict(x=test_data.get_data())
    predictions = np.argmax(predictions, axis=1)
    class_accuracy = calculate_class_accuracy(predictions,
                                              test_data.get_labels(),
                                              args.classes_count)
    # Collect metrics
    train_score = max(history.history['acc'])
    val_score = max(history.history['val_acc'])
    times = timer.times
    time = times[-1]
    avg_epoch_time = np.average(np.array(timer.average))
    epochs = len(history.epoch)

    # Save metrics
    metrics_path = os.path.join(args.artifacts_path, "metrics.csv")
    save_to_csv(metrics_path, [train_score, val_score,
                               test_score[1], time, epochs, avg_epoch_time])
    class_accuracy_path = os.path.join(args.artifacts_path, "class_accuracy.csv")
    save_to_csv(class_accuracy_path, class_accuracy)
    np.savetxt(os.path.join(args.artifacts_path, args.output_file) +
               "_times.csv", times, fmt="%1.4f")