Ejemplo n.º 1
0
    os.makedirs(path_to_results)

n_epoch = 40
batch_size = 32
n_cv_runs = 10

for cv_run in tqdm(range(n_cv_runs)):
    data = np.load(path_to_data + 'train_test_data_{}.npz'.format(cv_run))

    train_images = data['train_images']
    train_labels = data['train_labels']

    test_images = data['test_images']
    test_labels = data['test_labels']

    cnn = cnn_adjust_lr(n_classes=train_labels.shape[1], input_shape=train_images[0].shape, lr=1e-4)

    train_accuracy = np.zeros((n_epoch,), dtype=np.float64)
    test_accuracy = np.zeros((n_epoch,), dtype=np.float64)

    for epoch in range(n_epoch):
        print('\tepoch %d...' % epoch)

        cnn.fit(train_images, train_labels, epochs=1, shuffle=True, batch_size=batch_size, verbose=0)

        train_prediction = cnn.predict(train_images)
        train_accuracy[epoch] = np.mean(np.argmax(train_prediction, axis=1) == np.argmax(train_labels, axis=1))
        print('\ttrain accuracy {}'.format(train_accuracy[epoch]))

        test_prediction = cnn.predict(test_images)
        test_accuracy[epoch] = np.mean(np.argmax(test_prediction, axis=1) == np.argmax(test_labels, axis=1))
Ejemplo n.º 2
0
                          'results_{}_run.npz'.format(run))

    test_accuracy = cur_results['test_accuracy'][-1]

    if test_accuracy > best_accuracy:
        best_accuracy = test_accuracy
        best_model_id = run

train_data = np.load(path_to_train_data +
                     'train_test_data_{}.npz'.format(best_model_id))
image_mean = train_data['image_mean']
image_std = train_data['image_std']
patch_width, patch_height, patch_channels = train_data['train_images'][0].shape

model = cnn_adjust_lr(n_classes=train_data['train_labels'].shape[1],
                      input_shape=(patch_width, patch_height, patch_channels),
                      lr=1e-4)
model.load_weights(path_to_train_results +
                   'trained_model/weights_{}_run.ckpt'.format(best_model_id))

writer = GdalIO()

for file in os.listdir(path_to_test_images):
    image = np.transpose(np.array(
        gdal.Open(path_to_test_images + file).ReadAsArray()),
                         axes=(1, 2, 0)).astype(np.float64)
    writer.parse_meta_with_gdal(path_to_test_images + file)

    pred_boxes, pred_probs = apply_for_sliding_window(
        model,
        image,