Exemplo n.º 1
0
def test_resample(ground_truth_raster, training_raster, tmp_path):
    """Assert that resampled training raster has the shape of the ground truth raster"""
    src = rasterio.open(training_raster)
    data = src.read(1)
    data = np.expand_dims(data,0)
    filename = "{}/test.tif".format(tmp_path)
    resample.create_tif(source_tif=training_raster, filename=filename, numpy_array=data)    
    
    resampled_filename = resample.resample(filename)
    resampled_src = rasterio.open(resampled_filename)
    ground_truth_raster_src = rasterio.open(ground_truth_raster)
    
    resampled_data = resampled_src.read()
    assert ground_truth_raster_src.shape == resampled_src.shape
    assert ground_truth_raster_src.bounds == resampled_src.bounds
    
Exemplo n.º 2
0
#Predict
predict_tfrecords = glob.glob(
    "/orange/ewhite/b.weinstein/Houston2018/tfrecords/predict/*.tfrecord")
results = model.predict_raster(predict_tfrecords, batch_size=512)
#predicted classes
print(results.label.unique())

predicted_raster = visualize.create_raster(results)
print(np.unique(predicted_raster))
experiment.log_image(name="Prediction",
                     image_data=predicted_raster,
                     image_colormap=visualize.discrete_cmap(20,
                                                            base_cmap="jet"))

#Save as tif for resampling
prediction_path = os.path.join(save_dir, "prediction.tif")
predicted_raster = np.expand_dims(predicted_raster, 0)
resample.create_tif(
    "/home/b.weinstein/DeepTreeAttention/data/processed/20170218_UH_CASI_S4_NAD83.tif",
    filename=prediction_path,
    numpy_array=predicted_raster)
filename = resample.resample(prediction_path)
experiment.log_image(name="Resampled Prediction",
                     image_data=filename,
                     image_colormap=visualize.discrete_cmap(20,
                                                            base_cmap="jet"))

#Save model
model.model.save("{}/{}.h5".format(save_dir, timestamp))
    "transform": out_transform
})

with rasterio.open(destination_path, "w", **out_meta) as dest:
    dest.write(out_img)

####
#Don't crop, just grab bands
####
destination_path = "data/processed/20170218_UH_CASI_S4_NAD83.tif"

ground_truth_src = rasterio.open(ground_truth_path)
bounds = ground_truth_src.bounds

#Read sensor data and crop
sensor_src = rasterio.open(sensor_path)
out_img = sensor_src.read()

#Select first 48 bands as channels
out_img = out_img[:48, :, :]

# Copy the metadata and write
out_meta = sensor_src.meta.copy()
out_meta.update({"driver": "GTiff", "count": out_img.shape[0]})

with rasterio.open(destination_path, "w", **out_meta) as dest:
    dest.write(out_img)

#Resample ground truth raster to the correct size
resample.resample(ground_truth_path, upscale_factor=0.5)