{"metadata": {"language_info": {"pygments_lexer": "ipython2", "codemirror_mode": {"name": "ipython", "version": 2}, "file_extension": ".py", "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "version": "2.7.12"}, "kernelspec": {"display_name": "Python 3", "name": "python3", "language": "python"}}, "nbformat": 4, "nbformat_minor": 2, "cells": [{"metadata": {"_uuid": "904c3653dc3be905524beb3c6a02eecc123deddb"}, "source": ["# Getting a meaning of the score\n", "In this notebook I will take the train masks and by using erode and dilate try to get a sense of what the different dice scores mean."], "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"_uuid": "62ead4f842b121dfbfff77bd71ab090a302ae11f", "collapsed": true, "_execution_state": "idle"}, "source": "import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport os\nfrom tqdm import tqdm\nimport cv2\nimport glob\n\n%matplotlib inline", "outputs": [], "cell_type": "code", "execution_count": 3}, {"metadata": {"_uuid": "c14b2f2b4433fb610d53e255d62accee02e60ca0"}, "source": ["Let's get the train images"], "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"_uuid": "909049b80609fea8051f7e5f6eafb642e4f081be", "_execution_state": "idle"}, "source": "img_names = glob.glob(os.path.join('..','input', 'train_masks', '*.gif'))", "outputs": [], "cell_type": "code", "execution_count": 7}, {"metadata": {"_uuid": "2603067e98fffb51fb0a13c80b44ac000b11aee0", "collapsed": false, "_execution_state": "idle"}, "source": "print(len(img_names))", "outputs": [], "execution_count": null, "cell_type": "code"}, {"metadata": {"_uuid": "3a44c325bf1743adbc022191691671189d82eee3"}, "source": ["On a first step create a visualization of what we are going to do."], "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"_uuid": "182f48725d30f71874eb7861d2a25aa867809df3", "collapsed": true, "_execution_state": "idle"}, "source": "from PIL import Image \n\ndef read_gif_image(img_name):\n    img = Image.open(img_name).convert('RGB')\n    img_arr = np.asarray(img.getdata(), dtype=np.uint8)\n    img_arr = img_arr.reshape(img.size[1], img.size[0], 3)\n    return img_arr[:, :, 0]", "outputs": [], "cell_type": "code", "execution_count": 13}, {"metadata": {"_uuid": "451bba8d66e515159563b5a7cf09c32d62dc4df8", "collapsed": true, "_execution_state": "idle"}, "source": ["def visualize_erode_dilate(img_name, row, col, width=100):\n", "    img = read_gif_image(img_name)\n", "    vis = img.copy()/255\n", "    kernel = np.ones((3,3),np.uint8)\n", "    for i in range(1, 5):\n", "        vis += cv2.erode(img, kernel, iterations=i)/255\n", "        vis += cv2.dilate(img, kernel, iterations=i)/255\n", "    plt.figure(figsize=(12, 6))\n", "    plt.imshow(img)\n", "    plt.grid()\n", "    plt.figure(figsize=(12, 6))\n", "    plt.subplot(121)\n", "    plt.imshow(vis[row:row+width, col:col+width], cmap='viridis')\n", "    plt.grid()\n", "    plt.subplot(122)\n", "    plt.imshow(img[row:row+width, col:col+width], cmap='viridis')\n", "    plt.grid()"], "outputs": [], "cell_type": "code", "execution_count": 14}, {"metadata": {"_uuid": "f8d6e909a9e29bfff8dcdc9c48137691143ed332", "_execution_state": "idle"}, "source": "visualize_erode_dilate(img_names[1], 1000, 1100, 100)", "outputs": [], "cell_type": "code", "execution_count": 20}, {"metadata": {"_uuid": "9546728bf85c8eca2e33c8fcdbec3c611e4e8625"}, "source": ["Above we can see a detail of the erodes and dilates that we are going to use for computing the dice score."], "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"_uuid": "c18169d50a9845b5670df02ea4435f73e3577a0b"}, "source": ["## Computing dice score"], "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"_uuid": "74665c08c2fa04b246281d9f8afc4dac4f03012a", "collapsed": true, "_execution_state": "idle"}, "source": ["def get_dice_sums(img, n_iterations):\n", "    \"\"\"\n", "    Erodes or dilates the img based on the number of iterations \n", "    and computes the sum of the image and of the product\n", "    \"\"\"\n", "    kernel = np.ones((3,3),np.uint8)\n", "    if n_iterations < 0:\n", "        pred = cv2.erode(img, kernel, iterations=(- n_iterations))/255\n", "    else:\n", "        pred = cv2.dilate(img, kernel, iterations=(n_iterations))/255\n", "    return np.sum(pred), np.sum(img*pred/255)"], "outputs": [], "cell_type": "code", "execution_count": 21}, {"metadata": {"_uuid": "e9334403132292afe4ca05aa7a0d119b29e496c1", "collapsed": true, "_execution_state": "idle"}, "source": ["def compute_img_dice_sums(img_name, n_iterations):\n", "    \"\"\"\n", "    Returns the sum of original image, prediction and product of them \n", "    for the number of required iterations\n", "    \"\"\"\n", "    img = read_gif_image(img_name)\n", "    img_sum = np.sum(img/255)\n", "    sum_array = np.zeros((n_iterations*2+1, 3))\n", "    sum_array[:, 0] = img_sum\n", "    for i, n in enumerate(range(-n_iterations, n_iterations+1)):\n", "        ret = get_dice_sums(img, n)\n", "        sum_array[i, 1] = ret[0]\n", "        sum_array[i, 2] = ret[1]\n", "    \n", "    return sum_array"], "outputs": [], "cell_type": "code", "execution_count": 22}, {"metadata": {"_uuid": "0fb72192b4f11193788f948992b05f7b06f5c771", "collapsed": true, "_execution_state": "idle"}, "source": ["def compute_images_dice(img_list, n_iterations):\n", "    \"\"\"\n", "    Computes dice for all the images given and the number \n", "    of erodes and dilates required\n", "    \"\"\"\n", "    sum_array = np.zeros((n_iterations*2+1, 3))\n", "    for img_name in img_list:\n", "        sum_array += compute_img_dice_sums(img_name, n_iterations)\n", "    dice = sum_array[:,2]*2/(sum_array[:,1]+sum_array[:,0])\n", "    return dice"], "outputs": [], "cell_type": "code", "execution_count": 23}, {"metadata": {"_uuid": "be3aabca82059d371997be063998f48df6c90402", "_execution_state": "idle"}, "source": "n_iterations = 3\nplt.figure(figsize=(12, 6))\nfor i in range(5):\n    dice = compute_images_dice(np.random.choice(img_names, 10), n_iterations)\n    x = np.arange(-n_iterations, n_iterations + 1)\n    plt.plot(x, dice)\nplt.xlabel('Erode/dilate iterations')\nplt.ylabel('Dice score');", "outputs": [], "cell_type": "code", "execution_count": 24}, {"metadata": {"_uuid": "d62e7ea3842720806a228438a41fd919092d34b3"}, "source": ["So we can see that if the difference between masks is of 1 pixel the score drops to 0.996  \n", "There is people already with that scores. \n", "\n", "I think that means that the data is extremely clean, otherwise it would be difficult to reach those scores."], "outputs": [], "cell_type": "markdown", "execution_count": null}, {"metadata": {"_uuid": "a473f15fb55968ccbbbb85c9ec25fe98ddb21c7e", "collapsed": true}, "source": [], "outputs": [], "cell_type": "code", "execution_count": null}]}