{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import sys\nsys.path.insert(0, \"../input/fpo-hubmap-github-dl/Kaggle-HuBMap-Unet\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Compute a Test Set single image\n\nWe'll compute the inference of a single image by :\n* Patching the image with 2048x2048px patches\n* Rescaling patches from 2048x2048px to 512x512px\n* Applying a threshold of 0.6 to the output of the softmax function second layer\n* Applying an Errosion / Dilatation to remove outliers pixels."},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from core.models.UNet_Inference import UNet_Inference\nfrom core.ImUtils import mask2rle,read_mask_file\nfrom skimage.io import imread,imsave\nIMAGE_PATH=\"../input/hubmap-kidney-segmentation/test/b2dc8411c.tiff\"\n\nMODEL_PATH=\"../input/fpo-hubmap-github-dl/Kaggle-HuBMap-Unet/checkpoints/CP_2048_0.25_epoch63_loss0.013.pth\"\nTILESIZE=2048\nDOWNSAMPLING=0.25\nCOVERING=0.5\nBatchSize=20\nthreshold=0.6\nkernelsize=7\ndevice='cuda:0'\nmodel=UNet_Inference(nclasses=2)\nmodel.LoadCheckpoint(MODEL_PATH)\n\nprediction=model.inferImage(IMAGE_PATH,\n                            threshold=threshold,\n                            kernelsize=kernelsize,\n                            TileSize=TILESIZE,\n                            TileCovering=COVERING,\n                            DownScaling=DOWNSAMPLING,\n                            BatchSize=BatchSize,\n                            inferdevice=device,mode='half')\n\nimsave('prediction.tiff',prediction)\ndel prediction","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We'll then loop over image in order to find the 100 first positive images...\nTo do so, we'll loop over indexes until we  have 100 positive element with more than 100x100px NC to display.\n\nIn this part, we'll use directly the TileManager class in order to explode the image in tiles.\nThis class has just to be innitialized to be use to transform / inverse transform an image to/from patch."},{"metadata":{"trusted":true},"cell_type":"code","source":"from core.TileManager import TileManager\nfrom core.ImUtils import read_tiff_file\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nmax_images=100 # imgs max\nSize_Target=100 # px\n\nprediction=imread('prediction.tiff')>0\nimg=read_tiff_file(IMAGE_PATH)\nprediction=np.expand_dims(prediction,axis=-1).astype(np.bool)\nTM=TileManager(TILESIZE,COVERING,DOWNSAMPLING)\nTM.fit(img.shape)\n\nnb_positive=0\nfor i in range(len(TM)):\n    if nb_positive>max_images:\n        break;\n    else:\n        target=TM.transform(prediction,i)\n        if (target>0).sum()>Size_Target*Size_Target:\n            reference=TM.transform(img,i)\n            fig,ax=plt.subplots(1,2,figsize=(10,5))\n            ax[0].imshow(reference)\n            ax[0].set_title('Reference')\n            ax[0].axis('off')\n            ax[1].imshow(target.squeeze())\n            ax[1].set_title('Detected')\n            ax[1].axis('off')\n            plt.show()\n            nb_positive+=1\n            ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}