def gtiff_dsm_read_then_create_from_numpy_array(): """ This demonstrates how to read a geotiff file in, add a bias term, update the nodata values, and write out to disk """ print("") print("CREATE GEOTIFF FROM NUMPY ARRAY DEMO") file_utils.make_dir_if_not_exists(save_dir) gtiff_image_object = ImageFactory.geotiff.from_file(gtiff_full_path) save_fname = "gtiff_dsm_ortho_from_numpy.tif" numpy_save_fullpath = os.path.join(save_dir, save_fname) gtiff_from_numpy = ImageFactory.geotiff.from_numpy_array(gtiff_image_object.read_all_image_data_from_disk(), gtiff_image_object.get_point_calculator().get_geot(), gtiff_image_object.get_point_calculator().get_projection(), gtiff_image_object.get_metadata().get_nodata_val()) gtiff_from_numpy.write_to_disk(numpy_save_fullpath) numpy_saved_image = ImageFactory.geotiff.from_file(numpy_save_fullpath) data = numpy_saved_image.read_all_image_data_from_disk() val_to_add = 10 data = data + val_to_add save_fname = "gtiff_dsm_ortho_from_numpy_plus_ten.tif" numpy_save_fullpath = os.path.join(save_dir, save_fname) gtiff_from_numpy.set_image_data(data) gtiff_from_numpy.get_metadata().set_nodata_val(gtiff_image_object.get_metadata().get_nodata_val() + val_to_add) gtiff_from_numpy.write_to_disk(numpy_save_fullpath) print("geotiff written to: " + numpy_save_fullpath)
def gtiff_dsm_image_object_save(): print("") print("GTIFF DSM IMAGE OBJECT SAVE DEMO") file_utils.make_dir_if_not_exists(save_dir) gtiff_image_object = ImageFactory.geotiff.from_file(gtiff_full_path) save_fname = "gtiff_dsm_write_to_disk.tif" save_fullpath = os.path.join(save_dir, save_fname) gtiff_image_object.write_to_disk(save_fullpath) print("geotiff written to: " + save_fullpath)
def train_model( base_model, # type: Model training_generator, # type: training_generator validation_generator, # type: training_generator output_dir, # type: str final_model_layers=None, # type: Tensor model_weights_file=None, # type: str model_output_fname="trained_model.h5", # type: str n_epochs=20, # type: int loss="categorical_crossentropy", # type: str optimizer=optimizers.Adam(), # type: Optimizer metrics=["accuracy"] # type: list ): # type: (...) -> None tensorboard_log_dir = os.path.join(output_dir, "logs") tensorboard_log_dir = os.path.join(tensorboard_log_dir, "{}".format(time())) file_utils.make_dir_if_not_exists(tensorboard_log_dir) tensorboard = TensorBoard(log_dir=tensorboard_log_dir) filepath = os.path.join(output_dir, model_output_fname) labels_fname = os.path.join(output_dir, "labels.txt") sorted_class_labels = sorted(list(training_generator.class_indices.keys())) class_labels_fname = os.path.join(output_dir, "labels.txt") file_utils.write_text_list_to_file(sorted_class_labels, class_labels_fname) checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='min', period=1) callbacks_list = [checkpoint, tensorboard] if final_model_layers is None: model = base_model else: model = Model(inputs=base_model.input, outputs=final_model_layers) if model_weights_file is not None: model.load_weights(model_weights_file) model.compile(loss=loss, optimizer=optimizer, metrics=metrics) num_training_img = len(training_generator.filenames) num_validation_img = len(validation_generator.filenames) steps_per_epoch = num_training_img / training_generator.batch_size validation_steps = num_validation_img / validation_generator.batch_size model.fit_generator(training_generator, steps_per_epoch=steps_per_epoch, epochs=n_epochs, callbacks=callbacks_list, validation_data=validation_generator, validation_steps=validation_steps)
reflectance_image_data = reflectance_envi_image.read_all_image_data_from_disk() reflectance_image_data = reflectance_image_data.astype(float) * 0.01 ny, nx, nbands = reflectance_image_data.shape seaborn_color_palette = seaborn.color_palette("RdBu_r") test_spectrum_fname = file_utils.get_path_from_subdirs(demo_data_base_dir, [ "image_data", "hyperspectral", "cooke_city", "self_test", "SPL", "F1", "F1_l.txt" ]) spectrum = SpectrumFactory.envi.ascii.EnviAsciiPlotFileFactory.from_ascii_file( test_spectrum_fname) save_dir = os.path.join(demo_data_save_dir, "spectral_target_detection_demos") file_utils.make_dir_if_not_exists(save_dir) def save_rgb(): print("") print("SAVE RGB DEMO") red_band = 16 green_band = 6 blue_band = 1 save_image_fname = os.path.join(save_dir, "cooke_city_rgb.png") rgb_image = np.zeros((ny, nx, 3)) rgb_image[:, :, 0] = reflectance_image_data[:, :, red_band] rgb_image[:, :, 1] = reflectance_image_data[:, :, green_band] rgb_image[:, :, 2] = reflectance_image_data[:, :, blue_band] imageio.imwrite(save_image_fname, rgb_image)
def create_maptiles(): print("") print("CREATE MAPTILES DEMO") file_utils.make_dir_if_not_exists(save_dir) gtiff_image_object = ImageFactory.geotiff.from_file(gtiff_full_path) gtiff_image_object.create_map_tiles("/tmp/")
def create_vehicle_training_chips(): print("CHIPPING OUT VEHICLES FROM TRAINING IMAGE, THIS MIGHT TAKE A WHILE") print("save dir = " + save_dir_base) chipper_ny = 50 chipper_nx = 50 output_chip_ny = 244 output_chip_nx = 244 vehicle_mask = imread(png_positive_mask_fullpath) not_vehicle_mask = imread(png_negative_mask_fullpath) image_data = imread(png_fullpath) vehicle_locs_y, vehicle_locs_x = image_mask_utils.rgb_image_point_mask_to_pixel_locations( vehicle_mask) # remove chips that aren't very vehicle like, or have vehicles that are badly obscured. vehicle_yx_to_remove = [(1547, 524), (1813, 725), (1841, 708), (2135, 674)] vehicle_loc_tuples = numpy_and_array_utils.separate_lists_to_list_of_tuples( [vehicle_locs_y, vehicle_locs_x]) for yx_to_remove in vehicle_yx_to_remove: vehicle_loc_tuples.remove(yx_to_remove) vehicle_locs_y, vehicle_locs_x = numpy_and_array_utils.list_of_tuples_to_separate_lists( vehicle_loc_tuples) # chip out the vehicles vehicle_chips, vehicle_upper_lefts = image_chipper.\ chip_images_by_pixel_centers(image_data, vehicle_locs_y, vehicle_locs_x, chip_ny_pixels=chipper_ny, chip_nx_pixels=chipper_nx) not_vehicle_locs_y, not_vehicle_locs_x = image_mask_utils.rgb_image_point_mask_to_pixel_locations( not_vehicle_mask) # remove chips that do actually contain vehicles. not_vehicle_yx_to_remove = [(431, 1811), (1039, 377), (1143, 415), (1186, 192), (1186, 193), (1190, 150)] not_vehicle_loc_tuples = numpy_and_array_utils.separate_lists_to_list_of_tuples( [not_vehicle_locs_y, not_vehicle_locs_x]) for yx_to_remove in not_vehicle_yx_to_remove: not_vehicle_loc_tuples.remove(yx_to_remove) not_vehicle_locs_y, not_vehicle_locs_x = numpy_and_array_utils.list_of_tuples_to_separate_lists( not_vehicle_loc_tuples) not_vehicle_chips, not_vehicle_upper_lefts = image_chipper.\ chip_images_by_pixel_centers(image_data, not_vehicle_locs_y, not_vehicle_locs_x, chip_ny_pixels=chipper_ny, chip_nx_pixels=chipper_nx) vehicles_save_dir = os.path.join(save_dir_base, "vehicle") not_vehicles_save_dir = os.path.join(save_dir_base, "not_vehicle") file_utils.make_dir_if_not_exists(vehicles_save_dir) file_utils.make_dir_if_not_exists(not_vehicles_save_dir) # create filenames for vehicles and non-vehicles based on original center locations. This will let us # look through our chips for false positives and remove them from the masks vehicle_chips_fnames_list = [ "vehicle_center_y_" + str(vehicle_locs_y[i]) + "_center_x_" + str(vehicle_locs_x[i]) for i in range(len(vehicle_locs_x)) ] not_vehicle_chips_fnames_list = [ "not_vehicle_center_y_" + str(not_vehicle_locs_y[i]) + "_center_x_" + str(not_vehicle_locs_x[i]) for i in range(len(not_vehicle_locs_x)) ] image_chipper.write_chips_to_disk(vehicle_chips, vehicles_save_dir, fnames_list=vehicle_chips_fnames_list, output_chip_ny=output_chip_ny, output_chip_nx=output_chip_nx) image_chipper.write_chips_to_disk( not_vehicle_chips, not_vehicles_save_dir, fnames_list=not_vehicle_chips_fnames_list, output_chip_ny=output_chip_ny, output_chip_nx=output_chip_nx) print("DONE CHIPPING OUT IMAGES")
def create_vehicle_training_chips(): print("CHIPPING OUT VEHICLES FROM EVALUATION IMAGE, THIS MIGHT TAKE A WHILE") print("save dir = " + save_dir_base) chipper_ny = 50 chipper_nx = 50 output_chip_ny = 244 output_chip_nx = 244 vehicle_mask = imread(png_positive_mask_fullpath) not_vehicle_mask = imread(png_negative_mask_fullpath) image_data = imread(png_fullpath) vehicle_locs_y, vehicle_locs_x = image_mask_utils.rgb_image_point_mask_to_pixel_locations(vehicle_mask) # remove chips that aren't very vehicle like, or have vehicles that are badly obscured. vehicle_yx_to_remove = [(1583, 1650), (1599, 1217), (2094, 122), (2106, 160)] vehicle_loc_tuples = numpy_and_array_utils.separate_lists_to_list_of_tuples([vehicle_locs_y, vehicle_locs_x]) for yx_to_remove in vehicle_yx_to_remove: vehicle_loc_tuples.remove(yx_to_remove) vehicle_locs_y, vehicle_locs_x = numpy_and_array_utils.list_of_tuples_to_separate_lists(vehicle_loc_tuples) # chip out the vehicles vehicle_chips, vehicle_upper_lefts = image_chipper.\ chip_images_by_pixel_centers(image_data, vehicle_locs_y, vehicle_locs_x, chip_ny_pixels=chipper_ny, chip_nx_pixels=chipper_nx) not_vehicle_locs_y, not_vehicle_locs_x = image_mask_utils.rgb_image_point_mask_to_pixel_locations(not_vehicle_mask) # remove chips that do actually contain vehicles. not_vehicle_yx_to_remove = [(489, 1124), (584, 1186), (1222, 84), (1271, 1409), (1278, 1421), (1487, 1497), (1499, 1883), (1531, 1444), (1597, 1820), (1601, 1766), (1659, 922), (1677, 811), (1694, 780)] not_vehicle_loc_tuples = numpy_and_array_utils.separate_lists_to_list_of_tuples([not_vehicle_locs_y, not_vehicle_locs_x]) for yx_to_remove in not_vehicle_yx_to_remove: not_vehicle_loc_tuples.remove(yx_to_remove) not_vehicle_locs_y, not_vehicle_locs_x = numpy_and_array_utils.list_of_tuples_to_separate_lists(not_vehicle_loc_tuples) not_vehicle_chips, not_vehicle_upper_lefts = image_chipper.\ chip_images_by_pixel_centers(image_data, not_vehicle_locs_y, not_vehicle_locs_x, chip_ny_pixels=chipper_ny, chip_nx_pixels=chipper_nx) vehicles_save_dir = os.path.join(save_dir_base, "vehicle") not_vehicles_save_dir = os.path.join(save_dir_base, "not_vehicle") file_utils.make_dir_if_not_exists(vehicles_save_dir) file_utils.make_dir_if_not_exists(not_vehicles_save_dir) # create filenames for vehicles and non-vehicles based on original center locations. This will let us # look through our chips for false positives and remove them from the masks vehicle_chips_fnames_list = ["vehicle_center_y_" + str(vehicle_locs_y[i]) + "_center_x_" + str(vehicle_locs_x[i]) for i in range(len(vehicle_locs_x))] not_vehicle_chips_fnames_list = ["not_vehicle_center_y_" + str(not_vehicle_locs_y[i]) + "_center_x_" + str(not_vehicle_locs_x[i]) for i in range(len(not_vehicle_locs_x))] image_chipper.write_chips_to_disk(vehicle_chips, vehicles_save_dir, fnames_list=vehicle_chips_fnames_list, output_chip_ny=output_chip_ny, output_chip_nx=output_chip_nx) image_chipper.write_chips_to_disk(not_vehicle_chips, not_vehicles_save_dir, fnames_list=not_vehicle_chips_fnames_list, output_chip_ny=output_chip_ny, output_chip_nx=output_chip_nx) print("DONE CHIPPING OUT IMAGES")