From bec5d644af1d7bbb495689cbab1d0c8fc6020b1d Mon Sep 17 00:00:00 2001 From: sermomon <59225676+sermomon@users.noreply.github.com> Date: Sun, 2 Jul 2023 12:20:19 +0200 Subject: [PATCH 1/3] Add files via upload --- geoinference.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 geoinference.py diff --git a/geoinference.py b/geoinference.py new file mode 100644 index 0000000..8573489 --- /dev/null +++ b/geoinference.py @@ -0,0 +1,86 @@ +''' Runs a georeferenced inference on a given GeoTIFF image. + +example: +$ python geoinference.py --checkpoint_path checkpoints/cp.135.ckpt \ + --image_path sample_data/sentinel2_example.tif --save_path water_map.tif +''' + +# Uncomment this to run inference on CPU if your GPU runs out of memory +# import os +# os.environ['CUDA_VISIBLE_DEVICES'] = '-1' + +import argparse +import deepwatermap +import tifffile as tiff +import numpy as np +import cv2 +import rasterio as rio + +def find_padding(v, divisor=32): + v_divisible = max(divisor, int(divisor * np.ceil( v / divisor ))) + total_pad = v_divisible - v + pad_1 = total_pad // 2 + pad_2 = total_pad - pad_1 + return pad_1, pad_2 + +def main(checkpoint_path, image_path, save_path): + # load the model + model = deepwatermap.model() + model.load_weights(checkpoint_path) + + # load and preprocess the input image + src = rio.open(image_path) + image = src.read() + image = np.moveaxis(image, [0,1,2], [2,0,1]) + + pad_r = find_padding(image.shape[0]) + pad_c = find_padding(image.shape[1]) + image = np.pad(image, ((pad_r[0], pad_r[1]), (pad_c[0], pad_c[1]), (0, 0)), 'reflect') + + # solve no-pad index issue after inference + if pad_r[1] == 0: + pad_r = (pad_r[0], 1) + if pad_c[1] == 0: + pad_c = (pad_c[0], 1) + + image = image.astype(np.float32) + + # remove nans (and infinity) - replace with 0s + image = np.nan_to_num(image, copy=False, nan=0.0, posinf=0.0, neginf=0.0) + + image = image - np.min(image) + image = image / np.maximum(np.max(image), 1) + + # run inference + image = np.expand_dims(image, axis=0) + dwm = model.predict(image) + dwm = np.squeeze(dwm) + dwm = dwm[pad_r[0]:-pad_r[1], pad_c[0]:-pad_c[1]] + + # soft threshold + dwm = 1./(1+np.exp(-(16*(dwm-0.5)))) + dwm = np.clip(dwm, 0, 1) + + dwm = np.expand_dims(dwm*255, axis=0) + + # get image metadata + metadata = src.meta().copy() + metadata.update({ + 'dtype': img.dtype, + 'count': img.shape[0], + 'compress': 'lzw'}) + + # save the output as GeoTIFF + with rio.open(save_path, 'w', **metadata) as dst: + dst.write(dwm) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('--checkpoint_path', type=str, + help="Path to the dir where the checkpoints are stored") + parser.add_argument('--image_path', type=str, help="Path to the input GeoTIFF image") + parser.add_argument('--save_path', type=str, help="Path where the output map will be saved") + args = parser.parse_args() + main(args.checkpoint_path, args.image_path, args.save_path) + From 3f2ccce55ec6c79868b489272cbe4e434641192c Mon Sep 17 00:00:00 2001 From: sermomon <59225676+sermomon@users.noreply.github.com> Date: Sun, 2 Jul 2023 12:29:24 +0200 Subject: [PATCH 2/3] Update geoinference.py --- geoinference.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/geoinference.py b/geoinference.py index 8573489..6ab566b 100644 --- a/geoinference.py +++ b/geoinference.py @@ -66,8 +66,8 @@ def main(checkpoint_path, image_path, save_path): # get image metadata metadata = src.meta().copy() metadata.update({ - 'dtype': img.dtype, - 'count': img.shape[0], + 'dtype': image.dtype, + 'count': image.shape[0], 'compress': 'lzw'}) # save the output as GeoTIFF From 68cebab732ad2889411b99085ec07bf291f44ade Mon Sep 17 00:00:00 2001 From: sermomon <59225676+sermomon@users.noreply.github.com> Date: Sun, 2 Jul 2023 19:03:15 +0200 Subject: [PATCH 3/3] Actualizar geoinference.py --- geoinference.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geoinference.py b/geoinference.py index 6ab566b..1ab8d95 100644 --- a/geoinference.py +++ b/geoinference.py @@ -64,7 +64,7 @@ def main(checkpoint_path, image_path, save_path): dwm = np.expand_dims(dwm*255, axis=0) # get image metadata - metadata = src.meta().copy() + metadata = src.meta metadata.update({ 'dtype': image.dtype, 'count': image.shape[0],