GeoPatch enables the user to read, process and export GeoTIFFs in various patch sizes. The module is built on the Rasterio library but is much more convenient when it comes to reading and exporting GeoTIFs patches for training deep learning models.
Using this package user is able to feed satellite imagery and corresponding label data and exports patches in both Geotiff and Numpy array.
Any feedback from users is welcome and you can write to me at hejarshahabi@gmail.com in case of any contribution or suggestion.
pip install GeoPatch
from GeoPatch import TrainPatch
for image and label variables both string and array
can be passed and the function automatically process and read dataset.
patch= TrainPatch( image="xxx/image.tif", label="xxx/label.tif",
patch_size=128, stride=64, channel_first=True)
Using follwoing code the the shape and size of data can be displayed
patch.data_dimension()
To display the number of orginal image patches can be generated based on given patch size and stride values.
patch.patch_info()
To save image patches as Geotiff files in the current working directory with the given "folder_name", and if "only_label" pass as True, only patches will be save that has labelled data.
patch.save_Geotif(folder_name="tif", only_label=True)
Using this function image patches will be generated in Numpy format with data augmentation options. V_flip and H_flip are used to vertically and horizontally flip patches, respectively, and rotation is used to rotate patches in 90,180 and 270 degree.
patch.save_numpy(folder_name="npy", only_label=False, return_stacked=False, save_stack=False, V_flip=True, H_flip=True, Rotation=True)
#to return numpy patches as a stack file:
patch, label= patch.save_numpy(folder_name="npy", only_label=False, return_stacked=True, save_stack=False, V_flip=True, H_flip=True, Rotation=True)
Patches with their corresponding labels can be displayed using this line of code.
In "folder_name" the exact name of the folder that patches are located in should be passed.
patch.visualize(folder_name='npy',patches_to_show=2,band_num=1,
fig_size=(10, 20),dpi=96)
Using the following line of codes prediction patches can be generated.
from GeoPatch import PredictionPatch
Prediction= PredictionPatch( image="xxx/test_image.tif", patch_size=128, stride=128, channel_first=True)
Prediction.data_dimension()
Prediction.patch_info()
Using the following line of code prediction patches can be saved as GeoTIF format in the provided folder.
Prediction.save_Geotif('folder_name')