{"nbformat_minor": 0, "nbformat": 4, "cells": [{"outputs": [], "metadata": {"_execution_state": "idle", "collapsed": false, "_uuid": "63109f1cb9d4fea521583278b4bf3a07208f8ed0", "_cell_guid": "338c5e53-0e85-46ae-95bc-fd69068af4c7"}, "cell_type": "markdown", "source": "## Fast run length encoding, tested on the provided training mask data.", "execution_count": null}, {"outputs": [], "metadata": {"_execution_state": "idle", "trusted": false, "_uuid": "45755362eee45cb77ea165876eaff677bb0fea86", "_cell_guid": "4d4cbe07-25f5-47a4-8e25-f6ccdd15d457"}, "cell_type": "code", "source": "import time\n\nimport numpy as np\nimport pandas as pd\nfrom scipy import ndimage\n\nfrom matplotlib import pyplot as plt\n\nPROJECT_PATH = '..'\nINPUT_PATH = PROJECT_PATH + '/input'\n\nTRAIN_MASKS_CSV_PATH = INPUT_PATH + '/train_masks.csv'\nTRAIN_MASKS_PATH = INPUT_PATH + '/train_masks'", "execution_count": 1}, {"outputs": [], "metadata": {"_execution_state": "idle", "collapsed": false, "trusted": false, "_uuid": "423f68737d19871f3132007463072089419c8482", "_cell_guid": "892271e1-5b2a-48d9-a3c2-0bf9141852ee"}, "cell_type": "code", "source": "def read_train_masks():\n    global train_masks\n    train_masks = pd.read_csv(TRAIN_MASKS_CSV_PATH)\n    print(train_masks.head())\n\n\nread_train_masks()", "execution_count": 2}, {"outputs": [], "metadata": {"_execution_state": "idle", "collapsed": false, "trusted": false, "_uuid": "fbf68ee9fc4e2f1ed510dcf43c769bcdf37217b0", "_cell_guid": "23b0058b-850a-4697-a907-f437b41414fd"}, "cell_type": "code", "source": "def read_mask_image(car_code, angle_code):\n    mask_img_path = TRAIN_MASKS_PATH + '/' + car_code + '_' + angle_code + '_mask.gif';\n    mask_img = ndimage.imread(mask_img_path, mode = 'L')\n    mask_img[mask_img <= 127] = 0\n    mask_img[mask_img > 127] = 1\n    return mask_img\n\n\ndef show_mask_image(car_code, angle_code):\n    mask_img = read_mask_image(car_code, angle_code)\n    plt.imshow(mask_img, cmap = 'Greys_r')\n    plt.show()\n\n\nshow_mask_image('00087a6bd4dc', '04')", "execution_count": 3}, {"outputs": [], "metadata": {"_execution_state": "idle", "collapsed": false, "trusted": false, "_uuid": "485bdefa3a17f27e8b8734d4b1ed1dce465fb3f8", "_cell_guid": "ea5666a0-0703-4321-aee1-d35043517acc"}, "cell_type": "code", "source": "def rle_encode(mask_image):\n    pixels = mask_image.flatten()\n    # We avoid issues with '1' at the start or end (at the corners of \n    # the original image) by setting those pixels to '0' explicitly.\n    # We do not expect these to be non-zero for an accurate mask, \n    # so this should not harm the score.\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] = runs[1::2] - runs[:-1:2]\n    return runs\n\n\ndef rle_to_string(runs):\n    return ' '.join(str(x) for x in runs)\n\n\ndef test_rle_encode():\n    test_mask = np.asarray([[0, 0, 0, 0], [0, 0, 1, 1], [0, 0, 1, 1], [0, 0, 0, 0]])\n    assert rle_to_string(rle_encode(test_mask)) == '7 2 11 2'\n    num_masks = len(train_masks['img'])\n    print('Verfiying RLE encoding on', num_masks, 'masks ...')\n    time_read = 0.0 # seconds\n    time_rle = 0.0 # seconds\n    time_stringify = 0.0 # seconds\n    for mask_idx in range(num_masks):\n        img_file_name = train_masks.loc[mask_idx, 'img']\n        car_code, angle_code = img_file_name.split('.')[0].split('_')\n        t0 = time.clock()\n        mask_image = read_mask_image(car_code, angle_code)\n        time_read += time.clock() - t0\n        t0 = time.clock()\n        rle_truth_str = train_masks.loc[mask_idx, 'rle_mask']\n        rle = rle_encode(mask_image)\n        time_rle += time.clock() - t0\n        t0 = time.clock()\n        rle_str = rle_to_string(rle)\n        time_stringify += time.clock() - t0\n        assert rle_str == rle_truth_str\n        if mask_idx and (mask_idx % 500) == 0:\n            print('  ..', mask_idx, 'tested ..')\n    print('Time spent reading mask images:', time_read, 's, =>', \\\n            1000*(time_read/num_masks), 'ms per mask.')\n    print('Time spent RLE encoding masks:', time_rle, 's, =>', \\\n            1000*(time_rle/num_masks), 'ms per mask.')\n    print('Time spent stringifying RLEs:', time_stringify, 's, =>', \\\n            1000*(time_stringify/num_masks), 'ms per mask.')\n\n\ntest_rle_encode()", "execution_count": 4}], "metadata": {"language_info": {"pygments_lexer": "ipython3", "nbconvert_exporter": "python", "version": "3.6.1", "file_extension": ".py", "codemirror_mode": {"version": 3, "name": "ipython"}, "name": "python", "mimetype": "text/x-python"}, "kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}}}