# Calculate statistics for measuring the goodness of the prediction.
    stats_temp = lib.calculate_goodness_of_classification(labels_true=labels_true_cropped,
                                                          labels_predicted=labels_predicted,
                                                          labels_dict=labels_dict)

    # Cast dict objects 'stats_temp' to a pd.DataFrame object and append it
    # to 'stats_all' that stores statistics for all images of this test round.
    stats_all = stats_all.append(pd.DataFrame.from_dict(stats_temp, orient='index').T)

    # Plot predicted class labels over aerial view image.
    lib.plot_sat_image_and_labels(img=img_cropped,
                                  lab=labels_predicted,
                                  alpha=0.5,
                                  plot_img=False,
                                  store_img=True,
                                  plot_with='Image',
                                  dir_name=RESULTS_ROOT,
                                  file_name='test_image_' + area_name + RUN_NAME + str(number_of_training_steps),
                                  cmap=cmap1)

# Store classification statistics calculated from these test images.
with open(RESULTS_ROOT + 'stats_all_after_training_' + str(number_of_training_steps) + '.pickle', 'w') as f:
    pickle.dump(stats_all, f)
    f.close()

# Calculate average statistics for classifications based on
# all images that have been used in this test round.
stats_average = stats_all.T.mean(axis=1)
print "Statistics for all labels:"
lib.print_statistics_to_console(stats_average, 3)
for dir_content in dir_contents:
    if 'top_potsdam' in dir_content:
        area_names.append(dir_content)
area_names.sort()

for i in range(len(area_names)):

    area_name = area_names[i].replace('_RGB', '')

    print "Processing area " + str(i + 1) + ' (' + str(area_name) + ") of " + str(len(area_names)) + " areas ..."

    directory_name = STORE_DATA_TO_ROOT + area_name + '_RGB' + '/'
    if not os.path.exists(directory_name):
        os.makedirs(directory_name)

    # Load aerial image.
    img = Image.open(LOAD_IMAGES_FROM_ROOT + area_name + '/' + area_name + '_wholeSatImage_cropped_rotated_small.png')

    # Load labels.
    labels = np.load(LOAD_LABELS_FROM_ROOT + area_name + '_RGB/' + area_name + '_labels.npy')

    # Overlay aerial image and labels for control and save.
    lib.plot_sat_image_and_labels(img=np.asarray(img),
                                  lab=labels,
                                  cmap=cmap1,
                                  plot_img=False,
                                  store_img=True,
                                  plot_with='Image',
                                  file_name= area_name + '_w_B+H',
                                  dir_name=directory_name)
                                                          return_mean_iu=False,
                                                          return_frequency_weighted_iu=False)

    # Cast dict objects 'stats_temp' to a pd.DataFrame object and append it
    # to 'stats_all' that stores statistics for all images of this test round.
    stats_temp_Data_Frame = pd.DataFrame(stats_temp.values()).T
    stats_temp_Data_Frame.columns = stats_temp.keys()
    stats_all = stats_all.append(stats_temp_Data_Frame)

    # Plot predicted class labels over aerial view image.
    lib.plot_sat_image_and_labels(img=img_cropped,
                                  lab_1=labels_predicted,
                                  lab_2=None,
                                  alpha_1=0.5,
                                  alpha_2=0.25,
                                  alpha_3=0.25,
                                  plot_img=False,
                                  store_img=True,
                                  plot_with='Image',
                                  dir_name=RESULTS_ROOT,
                                  file_name='test_image_' + area_name + '_fcn-8s_p2_' + str(number_of_training_steps))

# Store classification statistics calculated from these test images.
with open(RESULTS_ROOT + 'stats_all_after_training_' + str(number_of_training_steps) + '.pickle', 'w') as f:
    pickle.dump(stats_all, f)

# Calculate average statistics for classifications based on
# all images that have been used in this test round.
stats_average = stats_all.T.mean(axis=1)
lib.print_statistics_to_console(stats_average, 3)
# Choose randomly some tile images and overlay them with their corresponding tile label images.
# We do this to check whether tile images and tile label images correspond to each other.
tile_number = '0000101'

for idx in range(len(names_tiles_img)):
    if names_tiles_img[idx].startswith(tile_number):
        name_tile_img = names_tiles_img[idx]
        name_tile_labels = names_tiles_labels[idx]

# Load tile image and corresponding tile label image.
img = np.load(TILES_ROOT + 'img_npy/' + name_tile_img)
labels = np.load(TILES_ROOT + 'labels_npy/' + name_tile_labels)


# Overlay tile image and tile labels.
lib.plot_sat_image_and_labels(img, labels)

# Specify which large aerial images should be used for training
# and which should be used for testing.
areas_all_numbers = []
for i in range(2, 8):
    for j in range(10, 13):
        areas_all_numbers.append(str(i) + '_' + str(j))
for i in range(6, 8):
    for j in range(7, 10):
        areas_all_numbers.append(str(i) + '_' + str(j))

areas_testing_numbers = np.asarray(['2_12', '6_10', '7_11'])
areas_training_numbers = np.setdiff1d(areas_all_numbers, areas_testing_numbers)
areas_training_names = []
for area in areas_training_numbers:
    # Calculate statistics for measuring the goodness of the prediction.
    stats_temp = lib.calculate_goodness_of_classification(
        labels_true=labels_true_cropped, labels_predicted=labels_predicted, labels_dict=labels_dict
    )

    # Cast dict objects 'stats_temp' to a pd.DataFrame object and append it
    # to 'stats_all' that stores statistics for all images of this test round.
    stats_all = stats_all.append(pd.DataFrame.from_dict(stats_temp, orient="index").T)

    # Plot predicted class labels over aerial view image.
    lib.plot_sat_image_and_labels(
        img=img_cropped,
        lab=labels_predicted,
        alpha=0.5,
        plot_img=False,
        store_img=True,
        plot_with="Image",
        dir_name=RESULTS_ROOT,
        file_name="test_image_" + area_name + RUN_NAME + ADDITIONAL_INFO + str(number_of_training_steps),
        cmap=cmap1,
    )

# Store classification statistics calculated from these test images.
with open(
    RESULTS_ROOT + "stats_all_after_training_" + RUN_NAME + ADDITIONAL_INFO + str(number_of_training_steps) + ".pickle",
    "w",
) as f:
    pickle.dump(stats_all, f)
    f.close()

# Calculate average statistics for classifications based on
    labels_out = np.copy(isprs_labels)

    # Add additional road labels.
    if os.path.exists(road_add_labels_name):
        labels_out[road_add_labels[:,:,0] != 255] = 2

    # Subract road labels that should be removed. Note that we only remove road labels
    # from pixels that were previously labeled as roads.
    road_labels = (isprs_labels == 2)
    if os.path.exists(road_subtract_labels_name):
        road_labels_to_subtract_1 = (road_subtract_labels[:,:,0] != 255)
        road_labels_to_subtract_2 = (road_labels_to_subtract_1 & road_labels)
    labels_out[road_labels_to_subtract_2] = 0

    # Store 'labels_out'
    out_name_1 = area_name + '_label_ETH_transformed_3.npy'
    np.save(STORE_DATA_ROOT + out_name_1, labels_out)

    # Overlay 'labels_out' with aerial image.
    out_name_2 = area_name + '_w_B+H_mod_2'
    cmap = colors.ListedColormap(['white', 'red', 'blue'])
    lib.plot_sat_image_and_labels(img=img,
                                  lab=labels_out,
                                  cmap=cmap,
                                  plot_img=False,
                                  store_img=True,
                                  plot_with='Image',
                                  dir_name=STORE_DATA_ROOT,
                                  file_name=out_name_2
                                  )
Esempio n. 7
0
LOAD_LABELS_FROM = LOAD_IMAGE_FROM
STORE_DATA_ROOT = LOAD_IMAGE_FROM

# Set color map.
cmap1 = colors.ListedColormap(['white', 'red', 'blue'])

# Specify names of areas.
area_names = os.listdir(LOAD_IMAGE_FROM)

for i in range(len(area_names)):
    print "Processing area " + str(i + 1) + " of " + str(len(area_names)) + " areas ..."
    area_name = area_names[i]
    image = np.asarray(Image.open(LOAD_IMAGE_FROM + area_name + '/' + area_name +
                                  "_RGB.png"))
    labels = np.load(LOAD_LABELS_FROM + area_name + '/' + area_name + "_labels.npy")

    labels_3d = np.full(shape=labels.shape + (3,), fill_value=255).astype(np.uint8)
    labels_3d[(labels == 1), :] = (255, 0, 0)
    labels_3d[(labels == 2), :] = (0, 0, 255)

    labels_out = Image.fromarray(labels_3d, 'RGB')
    labels_out.save(STORE_DATA_ROOT + area_name + '/' + area_name + "_RGB_wholeImage_o_B+H.png")

    lib.plot_sat_image_and_labels(img=image,
                                  lab=labels,
                                  cmap=cmap1,
                                  plot_with='Image',
                                  plot_img=False,
                                  store_img=True,
                                  dir_name=STORE_DATA_ROOT + area_name + '/',
                                  file_name=area_name + '_wholeSatImage_cropped_rotated_small_w_B+H')
        img_google_np = np.asarray(img_google)
        img_google_np_cropped = img_google_np[crop_ind_left:crop_ind_right, crop_ind_top:crop_ind_bottom]
        labels_isprs_new_rotated_reshaped_np_2d_cropped = \
            labels_isprs_new_rotated_reshaped_np_2d[crop_ind_left:crop_ind_right, crop_ind_top:crop_ind_bottom]

        # Cast google aerial image back from numpy to RGB.
        img_google_cropped = Image.fromarray(img_google_np_cropped)
        img_google_np_cropped = None

        # Save cropped google aerial image as PNG.
        img_google_cropped.save(STORE_DATA_TO_DIR + area_name + '/transform_out/' + area_name + '_wholeSatImage_cropped.png')

        # Overlay the Google Maps image and the ISPRS pixel-wise object labels.
        lib.plot_sat_image_and_labels(img=np.asarray(img_google_cropped),
                                      lab=labels_isprs_new_rotated_reshaped_np_2d_cropped,
                                      cmap=color_map,
                                      plot_img=False,
                                      store_img=True,
                                      plot_with='Image',
                                      dir_name=STORE_DATA_TO_DIR + area_name + '/transform_out/',
                                      file_name=area_name + '_overlay_google_img_isprs_labels')

        # Overlay the Google Maps image and the ISPRS image.
        img_isprs_rotated_reshaped = img_isprs_rotated.copy()
        img_isprs_rotated_reshaped = img_isprs_rotated_reshaped.resize(google_img_shape, Image.ANTIALIAS)
        img_google_isprs_overlay = Image.blend(img_google, img_isprs_rotated_reshaped, 0.5)
        img_google_isprs_overlay.save(STORE_DATA_TO_DIR + area_name + '/transform_out/' + area_name + '_overlay_google_isprs_images.png')

        # Store the rotated and cropped, pixel-wise ISPRS labels as numpy file
        np.save(STORE_DATA_TO_DIR + area_name + '/transform_out/' + area_name + '_ground_truth_labels.npy', labels_isprs_new_rotated_reshaped_np_2d_cropped)
    )

    # Cast dict objects 'stats_temp' to a pd.DataFrame object and append it
    # to 'stats_all' that stores statistics for all images of this test round.
    stats_temp_Data_Frame = pd.DataFrame(stats_temp.values()).T
    stats_temp_Data_Frame.columns = stats_temp.keys()
    stats_all = stats_all.append(stats_temp_Data_Frame)

    # Plot predicted class labels over aerial view image.
    lib.plot_sat_image_and_labels(
        img=img_cropped,
        lab_1=labels_predicted,
        lab_2=None,
        alpha_1=0.5,
        alpha_2=0.25,
        alpha_3=0.25,
        plot_img=False,
        store_img=True,
        plot_with="Image",
        dir_name=RESULTS_ROOT,
        file_name="test_image_" + area_name + "_" + str(number_of_training_steps),
    )

# Store classification statistics calculated from these test images.
with open(RESULTS_ROOT + "stats_all_after_training_" + str(number_of_training_steps) + ".pickle", "w") as f:
    pickle.dump(stats_all, f)

# Calculate average statistics for classifications based on
# all images that have been used in this test round.
stats_average = stats_all.T.mean(axis=1)
lib.print_statistics_to_console(stats_average, 3)
    print "Processing area " + str(i + 1) + ' (' + str(area_name) + ") of " + str(len(area_names)) + " areas ..."

    directory_name = STORE_DATA_TO_ROOT + area_name + '/'
    if not os.path.exists(directory_name):
        os.makedirs(directory_name)

    # Load aerial image.
    img = Image.open(LOAD_IMAGES_FROM_ROOT + area_name + '.png')

    # Load label.
    labels = Image.fromarray(np.load(LOAD_LABELS_FROM_ROOT + area_name.replace('RGB', 'label_ETH_transformed_3.npy')))

    # Shrink aerial image and labels.
    img_shrunken = img.resize((wdt_new, hgh_new), PIL.Image.ANTIALIAS)
    labels_shrunken = np.asarray(labels.resize((wdt_new, hgh_new), resample=Image.NEAREST)).astype(np.uint8)

    # Store aerial image and labels.
    img_shrunken.save(STORE_DATA_TO_ROOT + area_name + '/' + area_name + '.png')
    np.save(STORE_DATA_TO_ROOT + area_name + '/' + area_name.replace('RGB', 'labels') , labels_shrunken)

    # Overlay aerial image and labels for control and store.
    lib.plot_sat_image_and_labels(img=np.asarray(img_shrunken),
                                  lab=labels_shrunken,
                                  cmap=cmap1,
                                  plot_img=False,
                                  store_img=True,
                                  plot_with='Image',
                                  file_name= area_name + '_w_B+H',
                                  dir_name=STORE_DATA_TO_ROOT + area_name + '/')