{"cells":[{"metadata":{},"cell_type":"markdown","source":"import required packages"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import json\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image,ImageDraw\nimport tifffile as tiff\nimport seaborn as sns\nfrom skimage.measure import label,regionprops\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"list the items present in the train folder"},{"metadata":{"trusted":true},"cell_type":"code","source":"ls ../input/hubmap-kidney-segmentation/train","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"list the items in test folder"},{"metadata":{"trusted":true},"cell_type":"code","source":"ls ../input/hubmap-kidney-segmentation/test/","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"list the items present in hubmap-kidney-segmentation folder"},{"metadata":{"trusted":true},"cell_type":"code","source":"ls ../input/hubmap-kidney-segmentation/","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"read the HuBMAP-20-dataset_information.csv file and make data frame"},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN_PATH = \"../input/hubmap-kidney-segmentation/train/\"\nTEST_PATH = \"../input/hubmap-kidney-segmentation/test/\"\n#info_df = pd.read_csv(\"../input/hubmap-kidney-segmentation/HuBMAP-20-dataset_information.csv\")\n#print(info_df.shape)\n#(info_df.T)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"read the train csv file and make data frame"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(\"../input/hubmap-kidney-segmentation/train.csv\")\nprint(train_df.shape)\ntrain_df.T","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.iloc[4]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.iloc[4,0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.iloc[4,1]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"open the 7th index anatomical_structures_segmention_file.json file and load the json data"},{"metadata":{"trusted":true},"cell_type":"code","source":"#with open(TRAIN_PATH + train_df.iloc[4,0] + \"-anatomical-structure.json\")as f:\n    #data = json.load(f)\n#print(data)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"read the 7th index tiff image file"},{"metadata":{"trusted":true},"cell_type":"code","source":"image1 = tiff.imread(TRAIN_PATH + train_df.iloc[4,0] + \".tiff\")\nprint(image1.shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"plot the kidney image"},{"metadata":{"trusted":true},"cell_type":"code","source":"image1 = image1[0][0].transpose(1,2,0)\nprint(image1.shape)\nplt.figure(figsize=(10, 10))\nplt.imshow(image1)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## We need to decode the mask from encoding column of train.csv\n## https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\ndef mask2rle(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels= img.T.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n \ndef rle2mask(mask_rle, shape):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    print(starts, ends)\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## Plot all the Glomeruli in this particular kidney\nmask1 = rle2mask(train_df.iloc[4, 1], (image1.shape[1], image1.shape[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## The same kidney image with all the masks\nplt.figure(figsize=(10, 10))\nplt.imshow(image1)\nplt.imshow(mask1, alpha=0.5, cmap='plasma')\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lbl_0 = label(mask1)\nprops = regionprops(lbl_0)\nprint(len(props))\nbboxes = []\nfor prop in props:\n    bboxes.append([prop.bbox[0] - 30, prop.bbox[1] - 30, \n                   prop.bbox[2] + 30, prop.bbox[3] + 30])\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10,10))\nplt.imshow(image1[bboxes[0][0]:bboxes[0][2],bboxes[0][1]:bboxes[0][3],:])\nplt.imshow(mask1[bboxes[0][0]:bboxes[0][2],bboxes[0][1]:bboxes[0][3]],alpha=0.5,cmap='plasma') \nplt.show()            ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#import the necessary packages\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport tifffile as tiff\nimport cv2\nimport os\nfrom tqdm.notebook import tqdm\nimport zipfile","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sz = 256\nreduce = 4\nMASKS = '../input/hubmap-kidney-segmentation/train.csv'\nDATA = '../input/hubmap-kidney-segmentation/train/'\nOUT_TRAIN = 'train.zip'\nOUT_MASKS = 'masks.zip'\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#functions to convert encoding to mask and mask to encoding\ndef enc2mask(encs, shape):\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for m,enc in enumerate(encs):\n        if isinstance(enc,np.float) and np.isnan(enc): continue\n        s = enc.split()\n        for i in range(len(s)//2):\n            start = int(s[2*i]) - 1\n            length = int(s[2*i+1])\n            img[start:start+length] = 1 + m\n    return img.reshape(shape).T\n\ndef mask2enc(mask, n=1):\n    pixels = mask.T.flatten()\n    encs = []\n    for i in range(1,n+1):\n        p = (pixels == i).astype(np.int8)\n        if p.sum() == 0: encs.append(np.nan)\n        else:\n            p = np.concatenate([[0], p, [0]])\n            runs = np.where(p[1:] != p[:-1])[0] + 1\n            runs[1::2] -= runs[::2]\n            encs.append(' '.join(str(x) for x in runs))\n    return encs\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_masks = pd.read_csv(MASKS).set_index('id')\ndf_masks.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"s_th = 40\np_th = 200*sz//256\n\nx_tot, x2_tot = [],[]\nwith zipfile.ZipFile(OUT_TRAIN,'w') as img_out, \\\nzipfile.ZipFile(OUT_MASKS,'w') as mask_out:\n    for index,encs in tqdm(df_masks.iterrows(),total=len(df_masks)):\n        #read image and generate the mask\n        img = tiff.imread(os.path.join(DATA,index+'.tiff'))\n        if len(img.shape) == 5:\n            img = np.transpose(img.squeeze(),(1,2,0))\n            mask = enc2mask(encs,(img.shape[1],img.shape[0]))\n            #add padding to make the image dividable into tiles\n            shape = img.shape\n            pad0 = (reduce*sz - shape[0]%(reduce*sz))%(reduce*sz)\n            pad1 = (reduce*sz - shape[1]%(reduce*sz))%(reduce*sz)\n            img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],constant_values=0)\n            mask = np.pad(mask,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2]],constant_values=0)\n            #split image and mask into tiles using the reshape+transpose trick\n            img = cv2.resize(img,(img.shape[1]//reduce,img.shape[0]//reduce),interpolation=cv2.INTER_AREA)\n            img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)\n            img = img.transpose(0,2,1,3,4).reshape(-1,sz,sz,3)\n            \n            mask = cv2.resize(mask,(mask.shape[1]//reduce,mask.shape[0]//reduce),interpolation=cv2.INTER_NEAREST)\n            mask = mask.reshape(mask.shape[0]//sz,sz,mask.shape[1]//sz,sz)\n            mask = mask.transpose(0,2,1,3).reshape(-1,sz,sz)\n            \n            for i,(im,m) in enumerate(zip(img,mask)):\n                hsv = cv2.cvtColor(im,cv2.COLOR_BGR2HSV)\n                h,s,v = cv2.split(hsv)\n                if (s>s_th).sum()<=p_th or im.sum() <= p_th:\n                    continue\n                x_tot.append((im/255.0).reshape(-1,3).mean(0))\n                x2_tot.append(((im/255.0)**2).reshape(-1,3).mean(0))\n                \n                im = cv2.imencode('.png',cv2.cvtColor(im,cv2.COLOR_RGB2BGR))[1]\n                img_out.writestr(f'{index}_{i}.png',im)\n                m = cv2.imencode('.png',m)[1]\n                mask_out.writestr(f'{index}_{i}.png',m)\n#image stats\nimg_avr= np.array(x_tot).mean(0)\nimg_std = np.sqrt(np.array(x2_tot).mean(0)-img_avr**2)\nprint('mean:',img_avr, ', std:',img_std)\n                \n            \n            \n            \n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"columns, rows = 4,4\nidx0 = 30\nfig = plt.figure(figsize=(columns*4,rows*4))\nwith zipfile.ZipFile(OUT_TRAIN,'r') as img_arch, \\\nzipfile.ZipFile(OUT_MASKS,'r') as msk_arch:\n    fnames = sorted(img_arch.namelist())[8:]\n    for i in range(rows):\n        for j in range(columns):\n            idx = i+j*columns\n            img = cv2.imdecode(np.frombuffer(img_arch.read(fnames[idx0+idx]),np.uint8),cv2.IMREAD_COLOR)\n            img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)\n            \n            mask = cv2.imdecode(np.frombuffer(msk_arch.read(fnames[idx0+idx]),np.uint8),cv2.IMREAD_GRAYSCALE)\n            fig.add_subplot(rows,columns,idx+1)\n            plt.axis('off')\n            plt.imshow(Image.fromarray(img))\n            plt.imshow(Image.fromarray(mask),alpha=0.2)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline\n\nfrom fastai.vision.all import *\nfrom torch.utils.data import Dataset, DataLoader\nimport pandas as pd\nimport numpy as np\nimport os\nimport cv2\nimport gc\nimport random\nfrom albumentations import *\nfrom sklearn.model_selection import KFold\nimport matplotlib.pyplot as plt\nfrom lovasz import lovasz_hinge\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bs = 64\nnfolds = 4\nfold = 0\nSEED = 2021\nTRAIN = '../input/hubmap-256x256/train/'\nMASKS = '../input/hubmap-256x256/masks/'\nLABELS = '../input/hubmap-kidney-segmentation/train.csv'\nNUM_WORKERS = 4","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mean = np.array([0.61081577, 0.40450876, 0.63537308])\nstd = np.array([0.14212932, 0.22208199, 0.12562794])\n\ndef img2tensor(img,dtype:np.dtype=np.float32):\n    if img.ndim == 2: \n        img = np.expand_dims(img,2)\n    img = np.transpose(img,(2,0,1))\n    return torch.from_numpy(img.astype(dtype,copy=False))\n\nclass HuBMAPDataset(Dataset):\n    def __init__(self, fold=fold, train=True, tfms=None):\n        ids = pd.read_csv(LABELS).id.values\n        kf = KFold(n_splits=nfolds,random_state=SEED,shuffle=True)\n        ids = set(ids[list(kf.split(ids))[fold][0 if train else 1]])\n        self.fnames = [fname for fname in os.listdir(TRAIN) if fname.split('_')[0] in ids]\n        self.train = train\n        self.tfms = tfms\n    \n    \n    def __len__(self):\n        return len(self.fnames)\n    \n    def __getitem__(self,idx):\n        fname = self.fnames[idx]\n        img = cv2.cvtColor(cv2.imread(os.path.join(TRAIN,fname)),cv2.COLOR_BGR2RGB)\n        mask = cv2.imread(os.path.join(MASKS,fname),cv2.IMREAD_GRAYSCALE)\n        if self.tfms is not None:\n            augmented = self.tfms(image=img,mask=mask)\n            img,mask = augmented['image'],augmented['mask']\n        \n        return img2tensor((img/255.0 - mean)/std),img2tensor(mask)\n    \ndef get_aug(p=1.0):\n    return Compose([\n            HorizontalFlip(),\n            VerticalFlip(),\n            RandomRotate90(),\n            ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=15, p=0.9, border_mode=cv2.BORDER_REFLECT),\n            OneOf([\n                OpticalDistortion(p=0.3),\n                GridDistortion(p=.1),\n                IAAPiecewiseAffine(p=0.3),\n            ],p=0.3),\n            OneOf([\n                HueSaturationValue(10,15,10),\n                CLAHE(clip_limit=2),\n                RandomBrightnessContrast(),\n            ],p=0.3),],p=p)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ds = HuBMAPDataset(tfms=get_aug())\ndl = DataLoader(ds,batch_size=64,shuffle=False,num_workers=NUM_WORKERS)\nimgs,masks = next(iter(dl))\n\nplt.figure(figsize=(16,16))\nfor i,(img,mask) in enumerate(zip(imgs,masks)):\n    img = ((img.permute(1,2,0)*std+mean)*255.0).numpy().astype(np.uint8)\n    plt.subplot(8,8,i+1)\n    plt.imshow(img,vmin=0,vmax=255)\n    plt.imshow(mask.squeeze().numpy(),alpha=0.2)\n    plt.axis('off')\n    plt.subplots_adjust(wspace=None,hspace=None)\n\ndel ds,dl,imgs,masks","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ind ='e79de561c.tiff' \nimg = tiff.imread(os.path.join(DATA,ind))\nprint(img.shape)\nprint(len(img.shape))\nimg = np.transpose(img.squeeze(),(1,2,0))\nprint(img.shape)\nprint(len(img.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"(reduce*sz - 16180%(reduce*sz))%(reduce*sz)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"reduce*sz","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pad0 = 204\npad0//2,pad0-pad0//2","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}