{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from fastai.vision.all import *\nfrom skimage import measure\nfrom skimage.transform import rescale, resize\nfrom skimage.util import crop, montage\nfrom skimage.morphology import label, square, dilation, watershed, binary_opening\nfrom skimage.io import imsave\n\n\nfrom tqdm import tqdm\n\nfrom PIL import Image","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-09-01T09:58:19.014634Z","iopub.execute_input":"2021-09-01T09:58:19.015015Z","iopub.status.idle":"2021-09-01T09:58:21.899695Z","shell.execute_reply.started":"2021-09-01T09:58:19.014979Z","shell.execute_reply":"2021-09-01T09:58:21.898952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df= pd.read_csv('../input/cropped-imges-and-masks/crops_with_ships.csv')\ndf.head()\n","metadata":{"execution":{"iopub.status.busy":"2021-09-01T09:58:21.901252Z","iopub.execute_input":"2021-09-01T09:58:21.901507Z","iopub.status.idle":"2021-09-01T09:58:22.260905Z","shell.execute_reply.started":"2021-09-01T09:58:21.901481Z","shell.execute_reply":"2021-09-01T09:58:22.260056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Tratamiento del CSV\nComo podemos observar, en el CSV va a aparecer una entrada por barco, por lo tanto, cuando aparece más de un barco en la misma imagen, ésta aparecerá tantas veces en el csv como barcos contenga. Vamos a agrupar todos los barcos de la imagen en una sola entrada, agrupando por ImageId y uniendo encoded pixels con un espacio de separación. Además de ello, para mayor facilidad en el entrenamiento posterior añadiremos un nuevo campo que llamaremos \"has_ship\" que vale 1 en caso de contener barcos y 0 en caso de no contenerlos. https://blog.softhints.com/python-detect-prevent-typeerror/","metadata":{}},{"cell_type":"code","source":"# ref: https://www.kaggle.com/kmader/baseline-u-net-model-part-1\ndef multi_rle_encode(img):\n    labels = label(img)\n    return [rle_encode(labels==k) for k in np.unique(labels[labels>0])]\n\n# ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\ndef rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated: [start0] [length0] [start1] [length1]... in 1d array\n    '''\n    # reshape to 1d array\n    pixels = img.T.flatten() # Needed to align to RLE direction\n    # pads the head & the tail with 0 & converts to ndarray\n    pixels = np.concatenate([[0], pixels, [0]])\n    # gets all start(0->1) & end(1->0) positions \n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    # transforms end positions to lengths\n    runs[1::2] -= runs[::2]\n    # converts to the string formated: '[s0] [l0] [s1] [l1]...'\n    return ' '.join(str(x) for x in runs)\n\ndef rle_decode(mask_rle, label=1, shape=(768,768)):\n    '''\n    mask_rle: run-length as string formated: [start0] [length0] [start1] [length1]... in 1d array\n    shape: (height,width) of array to return \n    Returns numpy array according to the shape, 1 - mask, 0 - background\n    '''\n    s = mask_rle.split()\n    # gets starts & lengths 1d arrays \n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0::2], s[1::2])]\n    starts -= 1\n    # gets ends 1d array\n    ends = starts + lengths\n    # creates blank mask image 1d array\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    # sets mark pixles\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = label\n    # reshape as a 2d mask image\n    return img.reshape(shape).T  # Needed to align to RLE direction\n\ndef masks_as_image(in_mask_list, shape=(768,768)):\n    '''Take the individual ship masks and create a single mask array for all ships\n    in_mask_list: pd Series: [idx0] [RLE string0]...\n    Returns numpy array as (shape.h, sahpe.w, 1)\n    '''\n    all_masks = np.zeros(shape, dtype = np.int16)\n    # if isinstance(in_mask_list, list):\n    for label, mask in enumerate(in_mask_list):\n        if isinstance(mask, str):\n            all_masks += rle_decode(mask,label+1,shape)\n    return all_masks\n\ndef image_open(img_path):\n    return np.array(Image.open(img_path))\n\ndef apply_mask(image,mask):\n    imax,jmax=mask.shape\n    image_masked=np.copy(image)\n    for i in range(imax):\n        for j in range(jmax):\n            if mask[i,j]==1:\n                image_masked[i,j,[0,0]]=255\n    return image_masked\n","metadata":{"execution":{"iopub.status.busy":"2021-09-01T09:58:22.262604Z","iopub.execute_input":"2021-09-01T09:58:22.262911Z","iopub.status.idle":"2021-09-01T09:58:22.282336Z","shell.execute_reply.started":"2021-09-01T09:58:22.262882Z","shell.execute_reply":"2021-09-01T09:58:22.281253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask0=np.zeros([768,768])\nrle_encode(mask0)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T09:58:22.283958Z","iopub.execute_input":"2021-09-01T09:58:22.284254Z","iopub.status.idle":"2021-09-01T09:58:22.306451Z","shell.execute_reply.started":"2021-09-01T09:58:22.284226Z","shell.execute_reply":"2021-09-01T09:58:22.305676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Procesado de imagen\nEsta parte recoge todas las funciones que vamos a usar para procesar las imágenes antes y después del entrenamiento.","metadata":{}},{"cell_type":"code","source":"#recorta la imagen y devuelve un array con 9 trozos\ndef img_crop(A):\n    a = crop(A, ((0, 512), (0, 512), (0,0)), copy=True)\n    b = crop(A, ((0, 512), (256, 256), (0,0)), copy=True)\n    c = crop(A, ((0, 512), (512, 0), (0,0)), copy=True)\n    d = crop(A, ((256, 256), (0, 512), (0,0)), copy=True)\n    e = crop(A, ((256, 256), (256, 256), (0,0)), copy=True)\n    f = crop(A, ((256, 256), (512, 0), (0,0)), copy=True)\n    g = crop(A, ((512, 0), (0, 512), (0,0)), copy=True)\n    h = crop(A, ((512, 0), (256, 256), (0,0)), copy=True)\n    i = crop(A, ((512, 0), (512, 0), (0,0)), copy=True)\n    return[a,b,c,d,e,f,g,h,i]\ndef mask_crop(A):\n    a = crop(A, ((0, 512), (0, 512)), copy=False)\n    b = crop(A, ((0, 512), (256, 256)), copy=False)\n    c = crop(A, ((0, 512), (512, 0)), copy=False)\n    d = crop(A, ((256, 256), (0, 512)), copy=False)\n    e = crop(A, ((256, 256), (256, 256)), copy=False)\n    f = crop(A, ((256, 256), (512, 0)), copy=False)\n    g = crop(A, ((512, 0), (0, 512)), copy=False)\n    h = crop(A, ((512, 0), (256, 256)), copy=False)\n    i = crop(A, ((512, 0), (512, 0)), copy=False)\n    return[a,b,c,d,e,f,g,h,i]","metadata":{"execution":{"iopub.status.busy":"2021-09-01T09:58:22.309289Z","iopub.execute_input":"2021-09-01T09:58:22.309551Z","iopub.status.idle":"2021-09-01T09:58:22.329390Z","shell.execute_reply.started":"2021-09-01T09:58:22.309524Z","shell.execute_reply":"2021-09-01T09:58:22.328662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from https://github.com/selimsef/dsb2018_topcoders/blob/master/victor/create_masks.py\ndef trata_mask(mask):\n    labels = label(mask)\n    tmp = dilation(labels > 0, square(9))    \n    tmp2 = watershed(tmp, labels, mask=tmp, watershed_line=True) > 0\n    tmp = tmp ^ tmp2\n    tmp = dilation(tmp, square(7))\n    msk = (255 * tmp).astype('uint8')\n    \n    props = measure.regionprops(labels)\n    msk0 = 255 * (labels > 0)\n    msk0 = msk0.astype('uint8')\n    \n    msk1 = np.zeros_like(labels, dtype='bool')\n    \n    max_area = np.max([p.area for p in props])\n    \n    for y0 in range(labels.shape[0]):\n        for x0 in range(labels.shape[1]):\n            if not tmp[y0, x0]:\n                continue\n            if labels[y0, x0] == 0:\n                if max_area > 4000:\n                    sz = 6\n                else:\n                    sz = 3\n            else:\n                sz = 3\n                if props[labels[y0, x0] - 1].area < 300:\n                    sz = 1\n                elif props[labels[y0, x0] - 1].area < 2000:\n                    sz = 2\n            uniq = np.unique(labels[max(0, y0-sz):min(labels.shape[0], y0+sz+1), max(0, x0-sz):min(labels.shape[1], x0+sz+1)])\n            if len(uniq[uniq > 0]) > 1:\n                msk1[y0, x0] = True\n                msk0[y0, x0] = 0\n    \n    msk1 = 255 * msk1\n    msk1 = msk1.astype('uint8')\n    \n    msk2 = np.zeros_like(labels, dtype='uint8')\n    msk = np.stack((msk0, msk1, msk2))\n    msk = np.rollaxis(msk, 0, 3)\n    \n    return msk","metadata":{"execution":{"iopub.status.busy":"2021-09-01T09:58:22.332741Z","iopub.execute_input":"2021-09-01T09:58:22.333072Z","iopub.status.idle":"2021-09-01T09:58:22.350968Z","shell.execute_reply.started":"2021-09-01T09:58:22.333044Z","shell.execute_reply":"2021-09-01T09:58:22.350200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Clasificador\nPrimero utilizarémos este modelo para **eliminar todas las imágnes que no contengan barcos posibles** para, posteriormente, utilizar un segundo modelo **entrenado exclusivemente para la segmentación en imagénes que contengan barcos**. \n","metadata":{}},{"cell_type":"code","source":"def get_x(r): return os.path.join('../input/cropped-imges-and-masks/croppedmasks/crops',r['img_name'])\ndef get_y(r): return r['has_ships']\ndblock= DataBlock(blocks=(ImageBlock,CategoryBlock),get_x=get_x, get_y=get_y)\ndls=dblock.dataloaders(df, bs=64)\nlearn=cnn_learner(dls,resnet34, metrics=accuracy)\n\n","metadata":{"execution":{"iopub.status.busy":"2021-09-01T09:58:22.354321Z","iopub.execute_input":"2021-09-01T09:58:22.354591Z","iopub.status.idle":"2021-09-01T09:59:42.864098Z","shell.execute_reply.started":"2021-09-01T09:58:22.354565Z","shell.execute_reply":"2021-09-01T09:59:42.863079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.isdir('models'):\n    !mkdir models\n!cp ../input/resnet34-classifier-over-256-crops/models/Resnet34_256_crops.pth models/Resnet34.pth\n#deberías hacer esto para cargar los pesos aprendidos\nlearn.load('Resnet34')\nlearn_class_inf=learn","metadata":{"execution":{"iopub.status.busy":"2021-09-01T09:59:42.867864Z","iopub.execute_input":"2021-09-01T09:59:42.868195Z","iopub.status.idle":"2021-09-01T09:59:49.182077Z","shell.execute_reply.started":"2021-09-01T09:59:42.868146Z","shell.execute_reply":"2021-09-01T09:59:49.181107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Segmentacion\n","metadata":{"trusted":true}},{"cell_type":"code","source":"class Dice(Metric):\n    \"Dice coefficient metric for binary target in segmentation\"\n    def __init__(self, axis=1): self.axis = axis\n    def reset(self): self.inter,self.union = 0,0\n    def accumulate(self, learn):\n        pred,targ = flatten_check(learn.pred.argmax(dim=self.axis), learn.y)\n        pred, targ = TensorBase(pred), TensorBase(targ)\n        self.inter += (pred*targ).float().sum().item()\n        self.union += (pred+targ).float().sum().item()\n\n    @property\n    def value(self): return 2. * self.inter/self.union if self.union > 0 else None\n\ndef IoU(input, target):\n    \"\"\"Intersection over Union (IoU) metric.\"\"\"\n    input = input.argmax(dim=1).float()\n    target = target.squeeze(1).float()\n    \n    smooth = 1.\n    intersection = (input * target).sum()\n    union = (input + target).sum() - intersection\n    return (intersection + smooth) / (union + smooth)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T09:59:49.183523Z","iopub.execute_input":"2021-09-01T09:59:49.183793Z","iopub.status.idle":"2021-09-01T09:59:49.195328Z","shell.execute_reply.started":"2021-09-01T09:59:49.183765Z","shell.execute_reply":"2021-09-01T09:59:49.194700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(df[df['has_ships'] == False].index, inplace=True)\ndf.head()\nfnames=[]\nfor index, row in tqdm(df.iterrows()):\n    fnames.append(Path(os.path.join('../input/cropped-imges-and-masks/croppedmasks/crops',row['img_name'])))\n    \n","metadata":{"execution":{"iopub.status.busy":"2021-09-01T09:59:49.196392Z","iopub.execute_input":"2021-09-01T09:59:49.196835Z","iopub.status.idle":"2021-09-01T09:59:57.690573Z","shell.execute_reply.started":"2021-09-01T09:59:49.196805Z","shell.execute_reply":"2021-09-01T09:59:57.688795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_y(r): \n    fname=r.stem\n    mascara= image_open(os.path.join('../input/cropped-imges-and-masks/croppedmasks/masks','{0}.tif'.format(fname)))\n    barcos=mascara[:,:,0]/255\n    bordes=2*(mascara[:,:,1]/255)\n    return barcos+bordes\n   \ndls = SegmentationDataLoaders.from_label_func(\"\", bs=32, fnames = fnames, label_func = get_y)\nlearn= unet_learner(dls,resnet34,n_out=3, metrics=[Dice()])","metadata":{"execution":{"iopub.status.busy":"2021-09-01T09:59:57.692087Z","iopub.execute_input":"2021-09-01T09:59:57.692459Z","iopub.status.idle":"2021-09-01T10:00:01.417402Z","shell.execute_reply.started":"2021-09-01T09:59:57.692417Z","shell.execute_reply":"2021-09-01T10:00:01.416500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.isdir('models'):\n    !mkdir models\n!cp ../input/unet-seg-cropped-3ch-wo-leak/Unet_seg_cropped_256_bs32_3channels_wo_leakage_150epochs.pth models/Unet_seg.pth\n#deberías hacer esto para cargar los pesos aprendidos\nlearn.load('Unet_seg')\nlearn_seg_inf=learn","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:00:01.418727Z","iopub.execute_input":"2021-09-01T10:00:01.419347Z","iopub.status.idle":"2021-09-01T10:00:09.171477Z","shell.execute_reply.started":"2021-09-01T10:00:01.419300Z","shell.execute_reply":"2021-09-01T10:00:09.170257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Fase de test\n### Primer modelo clasificador determina donde hay y donde no hay barcos\n\n","metadata":{}},{"cell_type":"code","source":"print('Fase de Test')","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:00:09.173030Z","iopub.execute_input":"2021-09-01T10:00:09.173322Z","iopub.status.idle":"2021-09-01T10:00:09.178094Z","shell.execute_reply.started":"2021-09-01T10:00:09.173292Z","shell.execute_reply":"2021-09-01T10:00:09.177324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/airbus-ship-detection/sample_submission_v2.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:00:09.179229Z","iopub.execute_input":"2021-09-01T10:00:09.179502Z","iopub.status.idle":"2021-09-01T10:00:09.221258Z","shell.execute_reply.started":"2021-09-01T10:00:09.179476Z","shell.execute_reply":"2021-09-01T10:00:09.219914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#0d4694897.jpg 0ddc42ee1.jpg 00e5fb033.jpg 1e40229e8 1b5fd69bc 1e415f44b 4d70abd58\npath=Path('../input/airbus-ship-detection')\nname='000367c13.jpg'\n\nimagen=image_open(path/'test_v2'/name)\ntype(imagen)\ncrops=img_crop(imagen)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:00:09.222642Z","iopub.execute_input":"2021-09-01T10:00:09.223081Z","iopub.status.idle":"2021-09-01T10:00:09.250265Z","shell.execute_reply.started":"2021-09-01T10:00:09.223037Z","shell.execute_reply":"2021-09-01T10:00:09.249441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nwith learn_class_inf.no_bar():    \n    plt.figure(figsize=(20,20))\n    plt.subplot(331), plt.imshow(crops[0]), plt.axis('off') ,plt.title(learn_class_inf.predict(crops[0])[0])\n    plt.subplot(332), plt.imshow(crops[1]), plt.axis('off') ,plt.title(learn_class_inf.predict(crops[1])[0])\n    plt.subplot(333), plt.imshow(crops[2]), plt.axis('off') ,plt.title(learn_class_inf.predict(crops[2])[0])\n    plt.subplot(334), plt.imshow(crops[3]), plt.axis('off') ,plt.title(learn_class_inf.predict(crops[3])[0])\n    plt.subplot(335), plt.imshow(crops[4]), plt.axis('off') ,plt.title(learn_class_inf.predict(crops[4])[0])\n    plt.subplot(336), plt.imshow(crops[5]), plt.axis('off') ,plt.title(learn_class_inf.predict(crops[5])[0])\n    plt.subplot(337), plt.imshow(crops[6]), plt.axis('off') ,plt.title(learn_class_inf.predict(crops[6])[0])\n    plt.subplot(338), plt.imshow(crops[7]), plt.axis('off') ,plt.title(learn_class_inf.predict(crops[7])[0])\n    plt.subplot(339), plt.imshow(crops[8]), plt.axis('off') ,plt.title(learn_class_inf.predict(crops[8])[0])","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:00:09.251492Z","iopub.execute_input":"2021-09-01T10:00:09.251982Z","iopub.status.idle":"2021-09-01T10:00:12.084912Z","shell.execute_reply.started":"2021-09-01T10:00:09.251931Z","shell.execute_reply":"2021-09-01T10:00:12.084073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#with learn_class_inf.no_bar():\n   # index=0;\n   # for name in tqdm(df['ImageId']):\n   #     df.loc[index,'has_ships']=learn_class_inf.predict(image_open(path/'test_v2'/name))[0]\n    #    index=1+index","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-09-01T10:00:12.086405Z","iopub.execute_input":"2021-09-01T10:00:12.087030Z","iopub.status.idle":"2021-09-01T10:00:12.090272Z","shell.execute_reply.started":"2021-09-01T10:00:12.086985Z","shell.execute_reply":"2021-09-01T10:00:12.089435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Modelo segmentador segmenta los barcos ","metadata":{}},{"cell_type":"code","source":"masksin=np.array(learn_seg_inf.predict(imagen)[0])\n\nmaskt=1*(masksin==1)\nlabeled=label(maskt, connectivity=1, background=0)\nplt.figure(figsize=(20,15))\nplt.subplot(131), plt.imshow(imagen), plt.axis('off') ,plt.title(name)\nplt.subplot(132), plt.imshow(masksin), plt.axis('off') ,plt.title('Mascara')\nplt.subplot(133), plt.imshow(labeled), plt.axis('off') ,plt.title('Labels')\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:00:12.091534Z","iopub.execute_input":"2021-09-01T10:00:12.092116Z","iopub.status.idle":"2021-09-01T10:00:20.303289Z","shell.execute_reply.started":"2021-09-01T10:00:12.092075Z","shell.execute_reply":"2021-09-01T10:00:20.302210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=learn_seg_inf.predict(imagen)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:00:20.304410Z","iopub.execute_input":"2021-09-01T10:00:20.304714Z","iopub.status.idle":"2021-09-01T10:00:27.810393Z","shell.execute_reply.started":"2021-09-01T10:00:20.304686Z","shell.execute_reply":"2021-09-01T10:00:27.809532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,15))\nplt.imshow(np.array(test[2].permute(1,2,0)[:,:,1]))\nnp.max(np.array(test[2].permute(1,2,0)[:,:,1]))","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:00:27.811518Z","iopub.execute_input":"2021-09-01T10:00:27.811959Z","iopub.status.idle":"2021-09-01T10:00:28.136766Z","shell.execute_reply.started":"2021-09-01T10:00:27.811927Z","shell.execute_reply":"2021-09-01T10:00:28.135696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks=[]\nfor recorte in crops:\n    mask=np.zeros([256,256])\n    if learn_class_inf.predict(recorte)[0] == 'True':\n        mask=np.array(learn_seg_inf.predict(recorte)[0])\n        maskt=1*(mask==1)\n    masks.append(mask)\nmascara=montage(masks)\n                               ","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:00:28.138165Z","iopub.execute_input":"2021-09-01T10:00:28.138457Z","iopub.status.idle":"2021-09-01T10:00:30.627535Z","shell.execute_reply.started":"2021-09-01T10:00:28.138428Z","shell.execute_reply":"2021-09-01T10:00:30.626571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,15))\nplt.subplot(131), plt.imshow(apply_mask(imagen,mascara)), plt.axis('off') ,plt.title(name)\nplt.subplot(132), plt.imshow(apply_mask(imagen,masksin)), plt.axis('off') ,plt.title('Mascara')\nplt.subplot(133), plt.imshow(mascara-masksin), plt.axis('off') ,plt.title('Labels')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:00:30.628970Z","iopub.execute_input":"2021-09-01T10:00:30.629400Z","iopub.status.idle":"2021-09-01T10:00:32.040932Z","shell.execute_reply.started":"2021-09-01T10:00:30.629359Z","shell.execute_reply":"2021-09-01T10:00:32.039746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_pred_rows=[]\nwith learn_class_inf.no_bar():    \n    with learn_seg_inf.no_bar():\n        index=0\n        for name in tqdm(df['ImageId']):\n            #print(name)\n            imagen=image_open(path/'test_v2'/name)\n            crops=img_crop(imagen)\n            masks=[]\n            count=0\n            for recorte in crops:\n                if learn_class_inf.predict(recorte)[0] == 'True':\n                    mask=np.array(learn_seg_inf.predict(recorte)[0])\n                    maskt=1*(mask==1)\n                    masks.append(maskt)\n                else:\n                    mask=np.zeros([256,256])\n                    masks.append(mask)\n                    count += 1\n            if count == 9: \n                out_pred_rows += [{'ImageId': name, 'EncodedPixels': np.nan}]\n            else:\n                mascara=montage(masks)\n                maskt_open= binary_opening(mascara, np.ones((5,5)))\n                labeled=label(maskt_open, connectivity=1, background=0)\n                sep_rles=multi_rle_encode(labeled)\n                if len(sep_rles)>0:\n                    for rle in sep_rles:\n                        out_pred_rows += [{'ImageId': name, 'EncodedPixels': rle}]\n                else: \n                    out_pred_rows += [{'ImageId': name, 'EncodedPixels': np.nan}]\n          ","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2021-09-01T10:00:32.042563Z","iopub.execute_input":"2021-09-01T10:00:32.043011Z","iopub.status.idle":"2021-09-01T10:01:11.888999Z","shell.execute_reply.started":"2021-09-01T10:00:32.042967Z","shell.execute_reply":"2021-09-01T10:01:11.885455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mascara=image_open('../input/cropped-imges-and-masks/croppedmasks/masks/06c7d177f.tif')\nplt.imshow(mascara)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:01:11.890019Z","iopub.status.idle":"2021-09-01T10:01:11.890456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"juntos=[]\nfor name in tqdm(os.listdir('../input/cropped-imges-and-masks/croppedmasks/masks')):\n    mascara=image_open('../input/cropped-imges-and-masks/croppedmasks/masks/'+name)\n    if np.sum(mascara[:,:,1]) != 0:\n        juntos.append(name)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:01:11.891577Z","iopub.status.idle":"2021-09-01T10:01:11.892029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"juntos\n","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:01:11.892878Z","iopub.status.idle":"2021-09-01T10:01:11.893307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#prueba que debes borrar\nfrom skimage.morphology import label, square, dilation, watershed, binary_erosion, binary_opening\n#03830c435\nname='06713ec82'\nfor name in juntos:\n    print(name)\n    name=name.split('.')[0]\n    imagen=image_open('../input/cropped-imges-and-masks/croppedmasks/crops/'+name+'.jpg')\n    original=image_open('../input/cropped-imges-and-masks/croppedmasks/masks/'+name+'.tif')\n    mask=np.array(learn_seg_inf.predict(imagen)[0])\n    maskt=1*(mask==1)\n    #maskt_erode=binary_erosion(maskt, np.ones((3,3)))\n    maskt_open= binary_opening(maskt, np.ones((4,4)))\n    labeled1=label(maskt, connectivity=1, background=0)\n    labeled2=label(maskt_open, connectivity=1, background=0)\n    plt.figure(figsize=(20,10))\n    plt.subplot(141), plt.imshow(imagen), plt.axis('off'), plt.title('Imagen')\n    plt.subplot(142), plt.imshow(original), plt.axis('off'), plt.title('Ground truth')\n    plt.subplot(143), plt.imshow(labeled1), plt.axis('off'), plt.title('Predicción etiquetada sin apertura') \n    plt.subplot(144), plt.imshow(labeled2), plt.axis('off'), plt.title('Predicción etiquetada con apertura')\n    plt.show()\n    time.sleep(1)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:01:11.894156Z","iopub.status.idle":"2021-09-01T10:01:11.894574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nprint(\"something\")\ntime.sleep(5.5)    # Pause 5.5 seconds\nprint(\"something\")","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:01:11.895652Z","iopub.status.idle":"2021-09-01T10:01:11.896120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame(out_pred_rows)[['ImageId', 'EncodedPixels']]\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:01:11.897112Z","iopub.status.idle":"2021-09-01T10:01:11.897535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:01:11.898537Z","iopub.status.idle":"2021-09-01T10:01:11.898989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rle_0 = submission_df.query('ImageId==\"000367c13.jpg\"')['EncodedPixels']\nimg_0 = masks_as_image(rle_0)\nname='000367c13.jpg'\nimagen=image_open(path/'test_v2'/name)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:01:11.899977Z","iopub.status.idle":"2021-09-01T10:01:11.900395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.segmentation import mark_boundaries","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:01:11.901272Z","iopub.status.idle":"2021-09-01T10:01:11.901692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,15))\nplt.subplot(121), plt.imshow(img_0), plt.axis('off') ,plt.title(name)\nplt.subplot(122), plt.imshow(mark_boundaries(imagen,img_0,mode='thick')), plt.axis('off') ,plt.title('Mascara')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-01T10:01:11.902869Z","iopub.status.idle":"2021-09-01T10:01:11.903290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}