{"metadata": {"kernelspec": {"language": "python", "name": "python3", "display_name": "Python 3"}, "language_info": {"pygments_lexer": "ipython3", "mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "3.6.1", "codemirror_mode": {"version": 3, "name": "ipython"}, "file_extension": ".py", "name": "python"}}, "cells": [{"cell_type": "markdown", "source": ["The size of the imported images is 1918 x 1280.  However, the dimensions of a typical convolutional layer is 128 x 128,  256 x 256, 512 x 512, or 1024 x 1024, whcih means the scale in the X-axis is not the same as that in the Y-axis.  I, therefore, modified the orginal code from **4ui_iurz1 ** a little bit to fit the actual sitiution. "], "metadata": {}}, {"metadata": {"_uuid": "1a9645109acfce8f4f02a8defd89df021e99ff3a", "_cell_guid": "51dd6fcf-f498-40f6-a999-1a3cbbbe00bd", "collapsed": true}, "cell_type": "code", "outputs": [], "source": ["import os\n", "from glob import glob\n", "from tqdm import tqdm\n", "import pandas as pd\n", "from skimage.io import imread\n", "import cv2\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "plt.style.use('ggplot') "], "execution_count": 1}, {"metadata": {"_uuid": "48c481627d8f7be4b69a0e4d74d1d24bbc96bc48", "_cell_guid": "6b868a54-0411-4b59-9b38-9dbc3e2fbe43"}, "cell_type": "code", "outputs": [], "source": ["img_paths = glob(os.path.join('../input/train', '*.jpg'))\n", "gt_dir = '../input/train_masks'\n", "\n", "y = []\n", "for i in tqdm(range(len(img_paths[:1000]))):  \n", "    img_path = img_paths[i]    \n", "    gt = imread(os.path.join(gt_dir, os.path.splitext(os.path.basename(img_path))[0]+'_mask.gif'))\n", "    y.append(gt)\n", "    \n", "y = np.array(y)"], "execution_count": 2}, {"metadata": {"_uuid": "fee9c32355f2823bf06858b182f2511ab0e70089", "_cell_guid": "53867e15-a7df-426d-b3b7-efc7e87c73c9"}, "cell_type": "code", "outputs": [], "source": ["Xscales = np.array([128/1280, 256/1280, 512/1280, 1024/1280])\n", "Yscales = Xscales/(1918/1280)\n", "mean_dices = []\n", "\n", "for xscale, yscale in zip(Xscales, Yscales):\n", "    \n", "    dices = []\n", "    for i in tqdm(range(len(y))):\n", "        \n", "        mask = y[i]\n", "        seg = cv2.resize(mask, dsize=None, fx=xscale, fy=yscale)\n", "        seg = cv2.resize(seg, (1918, 1280))\n", "        \n", "        mask = mask > 127\n", "        seg = seg > 127\n", "        \n", "        dice = 2.0 * np.sum(seg&mask) / (np.sum(seg) + np.sum(mask))\n", "        dices.append(dice)\n", "    \n", "    dices = np.array(dices)\n", "    mean_dices.append(np.mean(dices))\n", "\n", "mean_dices = np.array(mean_dices)\n", "        "], "execution_count": 3}, {"metadata": {"_uuid": "5b2d6bb0b5f92e3e2af697cb8527a28c075607bc", "_cell_guid": "d6dbb9f0-6b21-4521-8039-34ce1c3c893e", "scrolled": true}, "cell_type": "code", "outputs": [], "source": ["plt.figure(figsize=(15, 7))\n", "plt.plot(1280*Xscales, mean_dices)\n", "plt.xlabel('pixel_size')\n", "plt.ylabel('dice')\n", "for i in range(len(Xscales)):\n", "    plt.text(1280*Xscales[i], mean_dices[i], '%.5f'%mean_dices[i])\n", "plt.show()"], "execution_count": 5}, {"metadata": {"_uuid": "848c9aaffa74d038ec8ad27e8c1d88a31fc36524", "_cell_guid": "9ab04488-6b5a-41dd-a9cf-fab089e01c94", "collapsed": true}, "cell_type": "code", "outputs": [], "source": [], "execution_count": null}], "nbformat": 4, "nbformat_minor": 1}