Package: keras-unet
Share:AboutHelper package with multiple U-Net implementations in Keras as well as useful utility tools helpful when working with image segmentation tasksFeatures:U-Net models implemented in KerasVanilla U-Net implementation based onthe original paperCustomizable U-NetU-Net optimized for satellite images based onDeepSense.AI Kaggle competition entryUtility functions:Plotting images and masks with overlayPlotting images masks and predictions with overlay (prediction on top of original image)Plotting training history for metrics and lossesCropping smaller patches out of bigger image (e.g. satellite imagery) using sliding window technique (also with overlap if needed)Plotting smaller patches to visualize the cropped big imageReconstructing smaller patches back to a big imageData augmentation helper functionNotebooks (examples):Training custom U-Net for whale tails segmentationSemantic segmentation for satellite imagesSemantic segmentation for medical imagesISBI challenge 2015Installation:pipinstallgit+https://github.com/karolzak/keras-unetorpipinstallkeras-unetUsage examples:U-Net implementations in Keras:Vanilla U-NetCustomizable U-NetU-Net for satellite imagesUtils:Plot training historyPlot images and segmentation masksGet smaller patches/crops from bigger imagePlot small patches into single big imageReconstruct a bigger image from smaller patches/cropsVanilla U-NetModel scheme can be viewed herefromkeras_unet.modelsimportvanilla_unetmodel=vanilla_unet(input_shape=(512,512,3))[back to usage examples]Customizable U-NetModel scheme can be viewed herefromkeras_unet.modelsimportcustom_unetmodel=custom_unet(input_shape=(512,512,3),use_batch_norm=False,num_classes=1,filters=64,dropout=0.2,output_activation='sigmoid')[back to usage examples]U-Net for satellite imagesModel scheme can be viewed herefromkeras_unet.modelsimportsatellite_unetmodel=satellite_unet(input_shape=(512,512,3))[back to usage examples]Plot training historyhistory=model.fit_generator(...)fromkeras_unet.utilsimportplot_segm_historyplot_segm_history(history,# required - keras training history objectmetrics=['iou','val_iou'],# optional - metrics names to plotlosses=['loss','val_loss'])# optional - loss names to plotOutput:[back to usage examples]Plot images and segmentation masksfromkeras_unet.utilsimportplot_imgsplot_imgs(org_imgs=x_val,# required - original imagesmask_imgs=y_val,# required - ground truth maskspred_imgs=y_pred,# optional - predicted masksnm_img_to_plot=9)# optional - number of images to plotOutput:[back to usage examples]Get smaller patches/crops from bigger imagefromPILimportImageimportnumpyasnpfromkeras_unet.utilsimportget_patchesx=np.array(Image.open("../docs/sat_image_1.jpg"))print("x shape: ",str(x.shape))x_crops=get_patches(img_arr=x,# required - array of images to be croppedsize=100,# default is 256stride=100)# default is 256print("x_crops shape: ",str(x_crops.shape))Output:x shape:  (1000, 1000, 3)x_crops shape:  (100, 100, 100, 3)[back to usage examples]Plot small patches into single big imagefromkeras_unet.utilsimportplot_patchesprint("x_crops shape: ",str(x_crops.shape))plot_patches(img_arr=x_crops,# required - array of cropped out imagesorg_img_size=(1000,1000),# required - original size of the imagestride=100)# use only if stride is different from patch sizeOutput:x_crops shape:  (100, 100, 100, 3)[back to usage examples]Reconstruct a bigger image from smaller patches/cropsimportmatplotlib.pyplotaspltfromkeras_unet.utilsimportreconstruct_from_patchesprint("x_crops shape: ",str(x_crops.shape))x_reconstructed=reconstruct_from_patches(img_arr=x_crops,# required - array of cropped out imagesorg_img_size=(1000,1000),# required - original size of the imagestride=100)# use only if stride is different from patch sizeprint("x_reconstructed shape: ",str(x_reconstructed.shape))plt.figure(figsize=(10,10))plt.imshow(x_reconstructed[0])plt.show()Output:x_crops shape:  (100, 100, 100, 3)x_reconstructed shape:  (1, 1000, 1000, 3)[back to usage examples]