{"cells":[{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"ec401073c12d2bc1d1681e1f00ab7acc877b99fa"},"cell_type":"code","source":"import os\nimport sys\nimport math\n\nfrom collections import defaultdict\nfrom multiprocessing.dummy import Pool as ThreadPool\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"f5e306089a0c355ec315a974305215ad38568f4c"},"cell_type":"code","source":"%matplotlib inline\n\nscale = 1.5\nplt.rcParams['figure.figsize'] = [6.4*scale, 4.8*scale]\nplt.rcParams['image.interpolation'] = 'nearest'\nplt.rcParams['image.cmap'] = 'gray'","execution_count":null,"outputs":[]},{"metadata":{"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"_uuid":"d14a978fb955002f400cf40d90c6b0663025a73b"},"cell_type":"markdown","source":"##### Data"},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"7de2c2eee651773e4bfc0d2eda5032418b7a4572"},"cell_type":"code","source":"os.listdir('../input')","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"f14cc428f6a1daa00f8c02d968a0a946d6c50806"},"cell_type":"code","source":"len(list(open('../input/train_ship_segmentations.csv'))) - 1","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"0c32e4bf019190fb95107f0876a5c9c620a676a1"},"cell_type":"code","source":"len(list(open('../input/sample_submission.csv'))) - 1","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"4ac2e716968b9577a0856545f19c769f342b73a2"},"cell_type":"code","source":"train_path = '../input/train'\ntest_path = '../input/test'","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"df189a51f03c4c1bdcf44334d605a8481bb4b86b"},"cell_type":"code","source":"train_files = os.listdir(f'{train_path}')\nprint(len(train_files))\n\ntest_files = os.listdir(f'{test_path}')\nprint(len(test_files))","execution_count":null,"outputs":[]},{"metadata":{"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"_uuid":"7b7912026fc3fbf431a94138685dbdf08412e1a1"},"cell_type":"markdown","source":"##### Images"},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"6694fc39596ed0905bef8e2000a0778d8c02dc7b"},"cell_type":"code","source":"im = plt.imread(f'{test_path}/fec9bf8f4.jpg')\nplt.imshow(im)","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"7960b18b1a9ed02ae5839a46af03492832ac3515"},"cell_type":"code","source":"im.shape","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"f15e3335e33eeb5943c88f5683765402c90eccf7"},"cell_type":"code","source":"idx = np.random.permutation(len(train_files))[:9]\n\nfig = plt.figure(figsize=(10, 10))\nfig.subplots_adjust(wspace=0, hspace=0)\nfor i, id in enumerate(idx):\n    fig.add_subplot(3, 3, i + 1)\n\n    im = plt.imread(f'{train_path}/{train_files[id]}')\n    plt.imshow(im)\n    plt.axis('off')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"cc7c040f1a918db3052cfcf9ad2ae7c7d1fdd52e"},"cell_type":"code","source":"def get_im_shape(fpath):\n    im = plt.imread(fpath)\n    return im.shape\n\npool = ThreadPool(4)\n\ntrain_fpaths = [os.path.join(train_path, fname) for fname in train_files]\ntrain_im_shapes = pool.map(get_im_shape, train_fpaths)\n\ntest_fpaths = [os.path.join(test_path, fname) for fname in test_files]\ntest_im_shapes = pool.map(get_im_shape, test_fpaths)","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"deea355887431dcc4050e631837cf0e68aa398f7"},"cell_type":"code","source":"counter = defaultdict(int)\nfor shape in train_im_shapes:\n    counter[len(shape)] += 1\nprint(f'All train images have a channel: {counter}')","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"c1c050d0ff147957ef021f81a4d4d25b9c0e35ed"},"cell_type":"code","source":"# There is one invalid image\ninvalid_idx = [i for i in range(len(train_im_shapes)) if len(train_im_shapes[i]) != 3][0]\ninvalid_idx","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"be589a06a19aab875c18408a3c900f540c7cc37b"},"cell_type":"code","source":"os.path.isfile(train_fpaths[invalid_idx])","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"4bc4f39d242cee69d89766ffb9b37601e057f64c"},"cell_type":"code","source":"im = plt.imread(train_fpaths[invalid_idx])\nim.shape","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"780b19c1d3d57f95a45e803dffb2cea7b321b6b2"},"cell_type":"code","source":"print(f'Don\\'t use image: {train_fpaths[invalid_idx]}')","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"3ba261c226d9006bc9ed42cdd2af67ea0054d9ab"},"cell_type":"code","source":"counter = defaultdict(int)\nfor i, shape in enumerate(train_im_shapes):\n    if i == invalid_idx: continue\n    counter[shape[2]] += 1\nprint(f'All train images have 3 channels: {counter}')\n\ncounter = defaultdict(int)\nfor i, shape in enumerate(train_im_shapes):\n    if i == invalid_idx: continue\n    counter[shape[1]] += 1\nprint(f'Train images\\' width: {counter}')\n\ncounter = defaultdict(int)\nfor i, shape in enumerate(train_im_shapes):\n    if i == invalid_idx: continue\n    counter[shape[0]] += 1\nprint(f'Train images\\' height: {counter}')","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"d4e6a4b9e7682bdfdf664884730df22ee03b21f3"},"cell_type":"code","source":"counter = defaultdict(int)\nfor shape in test_im_shapes:\n    counter[len(shape)] += 1\nprint(f'All test images have a channel: {counter}')\n\ncounter = defaultdict(int)\nfor i, shape in enumerate(test_im_shapes):\n    counter[shape[2]] += 1\nprint(f'All test images have 3 channels: {counter}')\n\ncounter = defaultdict(int)\nfor i, shape in enumerate(test_im_shapes):\n    counter[shape[1]] += 1\nprint(f'Test images\\' width: {counter}')\n\ncounter = defaultdict(int)\nfor i, shape in enumerate(test_im_shapes):\n    counter[shape[0]] += 1\nprint(f'Test images\\' height: {counter}')","execution_count":null,"outputs":[]},{"metadata":{"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"_uuid":"8b42f86a44bf1221aa46de2f515719ad8245665d"},"cell_type":"markdown","source":"##### Labels"},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"b3435f818892c74db87a8b62905e6cb826d34dc9"},"cell_type":"code","source":"masks = pd.read_csv('../input/train_ship_segmentations.csv')\nprint(masks.shape)\nmasks.head()","execution_count":null,"outputs":[]},{"metadata":{"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"_uuid":"d0be310c05a6091af5051729a1592b6450c967b8"},"cell_type":"markdown","source":"- For each image, one line per one ship\n- If no ship in an image, EncodedPixels is NaN"},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"b84bfb5bfde093bd18823f6d36f4ba13074c7c20"},"cell_type":"code","source":"# How many image ids?\nlen(masks['ImageId'].unique())","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"b1b66418d2c7e78000e6c945307830eb433f5e91"},"cell_type":"code","source":"# For images without ship, there is only one line per image id\ndf_tmp = masks[masks['EncodedPixels'].isna()]\n\nprint(len(df_tmp['ImageId'].unique()))\nprint(len(df_tmp))","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"ad6bdd01a51dc6a4243f6c533da8f7f592852f5e"},"cell_type":"code","source":"# Number of images with ships and without ships\nn_im_no_ships = len(masks[masks['EncodedPixels'].isna()]['ImageId'].unique())\nn_im_ships = len(masks[~masks['EncodedPixels'].isna()]['ImageId'].unique())\nsns.barplot(x=['Ships', 'No ships'], y=[n_im_ships, n_im_no_ships])","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"d747b7f298423b779924decb5689167c92a26bc7"},"cell_type":"code","source":"# Distribution of number of ships in images\ndf_tmp = masks[~masks['EncodedPixels'].isna()]\nsns.distplot(df_tmp['ImageId'].value_counts().values, kde=False)","execution_count":null,"outputs":[]},{"metadata":{"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"_uuid":"b09ef6c77c2697b8b4ec454b276477bcef6f3b17"},"cell_type":"markdown","source":"###### From run length encoding to masked 2-d array"},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"6537dc34ac4cf641ec21b3abcf84383408f8e617"},"cell_type":"code","source":"# ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\ndef rle_decode(mask_rle, shape=(768, 768)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) 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    im = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        im[lo:hi] = 1\n    return im.reshape(shape).T\n\ndef rle_encode(im):\n    '''\n    im: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = im.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)","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"36563c62c9bb1c9861d3caf20e603e3b41367857"},"cell_type":"code","source":"# One image can have multiple masks so there are multiple rows for the image.\n# Use pandas to find all rows with the same image and put their masks together.\n#fname = masks[~masks['EncodedPixels'].isna()].sample(1)['ImageId'].values[0]\nfname = 'a09398d99.jpg'\nim = plt.imread(f'{train_path}/{fname}')\nrles = masks.loc[masks['ImageId'] == fname, 'EncodedPixels'].tolist()\n\nall_masks = np.zeros((768, 768))\nfirst_masks = np.zeros((768, 768))\nfor i, rle in enumerate(rles):\n    if i == 0: first_masks += rle_decode(rle)\n    all_masks += rle_decode(rle)\n\nfig, axarr = plt.subplots(1, 3)\naxarr[0].axis('off')\naxarr[1].axis('off')\naxarr[2].axis('off')\naxarr[0].imshow(im)\naxarr[1].imshow(all_masks)\naxarr[2].imshow(im)\naxarr[2].imshow(all_masks, alpha=0.4)\nplt.tight_layout(h_pad=0.1, w_pad=0.1)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"51b7bfffcee960db06cd93fbff0fd1e19151d943"},"cell_type":"code","source":"rle_encode(first_masks)","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"1737e8ddb4c249aba2d994a75a67b7b40c557b38"},"cell_type":"code","source":"rles[0] == rle_encode(first_masks)","execution_count":null,"outputs":[]},{"metadata":{"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"_uuid":"4efb81e4fbc9fc49791b00a2b080951aee494df8"},"cell_type":"markdown","source":"###### From centered rotated rectangle to masked 2-d array"},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"4a2204515e65da93f443abba3de3ce2278ca937b"},"cell_type":"code","source":"fpath = f'{test_path}/fec9bf8f4.jpg'\nim = cv2.imread(fpath)\n\n# (centered x, centered y, width, height, rotation in degree, confidence score)\nlocs = [(305.589397186, 357.82121801, 167.674564232, 31.5170499716, -8.00823881288, 0.999999880791)]\nmask = np.zeros(shape=im.shape[0:2])\n\nfor loc in locs:\n\n    x, y, w, h, d = loc[0:5]\n\n    theta = np.radians(d)\n    cos_theta, sin_theta = np.cos(theta), np.sin(theta)\n\n    pts = [(w/2, h/2), (-w/2, h/2), (-w/2, -h/2), (w/2, -h/2)]\n    pts = [(p[0] * cos_theta + p[1] * sin_theta,\n           -(p[0] * sin_theta) + p[1] * cos_theta) for p in pts]\n    pts = [(p[0] + x, p[1] + y) for p in pts]\n    pts = [(int(p[0]), int(p[1])) for p in pts]\n    pts = np.array(pts)\n\n    im = cv2.fillPoly(im, pts=[pts], color=(255, 0, 0))\n    mask = cv2.fillPoly(mask, pts=[np.array(pts)], color=(255, 255, 255))\n\nplt.imshow(im[:, :, (2, 1, 0)])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"61774f547cebe9cd275b7577f3fb32cbf6c7812e"},"cell_type":"code","source":"plt.imshow(mask)","execution_count":null,"outputs":[]},{"metadata":{"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"_uuid":"ac2b4350fabc34010dd02a99d1ae19dfe8b6004d"},"cell_type":"markdown","source":"###### From marked 2-d array to centered rotated rectangle"},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"4cee95f89dce6cc991a3cc4e5e702e9b22276654"},"cell_type":"code","source":"fname = 'a09398d99.jpg'\nrles = masks.loc[masks['ImageId'] == fname, 'EncodedPixels'].tolist()\nim_mask = rle_decode(rles[1])\nplt.imshow(im_mask)","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"2992de1d21133fa41b2ce80a296ed261f7216434"},"cell_type":"code","source":"# https://stackoverflow.com/questions/49957431/findcontours-of-a-single-channel-image-in-opencv-python\n_, contours, hierarchy = cv2.findContours(im_mask.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\ncv2.minAreaRect(contours[0])","execution_count":null,"outputs":[]},{"metadata":{"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"_uuid":"18543281aaa43a2adc21cd8f59153044441b03c6"},"cell_type":"markdown","source":"##### Evaluation"},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"ddc56af6d659aeb3dd6dfc1c5dadee8662feb74a"},"cell_type":"code","source":"# https://www.kaggle.com/raresbarbantan/f2-metric/notebook\n# https://www.kaggle.com/sgalwan/airbus-ship-detection-challenge-eda-metrics/notebook\ndef read_masks(masks, im_name):\n    mask_list = masks.loc[masks['ImageId'] == im_name, 'EncodedPixels'].tolist()\n    all_masks = np.zeros((len(mask_list), 768, 768))\n    for idx, mask in enumerate(mask_list):\n        if isinstance(mask, str):\n            all_masks[idx] = rle_decode(mask)\n    return all_masks\n\ndef read_flat_mask(masks, im_name):\n    all_masks = read_masks(masks, im_name)\n    return np.sum(all_masks, axis=0)\n\ndef iou(mask1, mask2):\n    i = np.sum((mask1 >= 0.5) & (mask2 >= 0.5))\n    u = np.sum((mask1 >= 0.5) | (mask2 >= 0.5))\n    return i / (1e-8 + u)","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"18128f5968f0386c32576be9aedea38740e8442a"},"cell_type":"code","source":"im_name_with_ships = '00021ddc3.jpg'\nim_name_with_no_ships = '00003e153.jpg'\n\nim_with_ships = plt.imread(f'{train_path}/00021ddc3.jpg')\nim_with_no_ships = plt.imread(f'{train_path}/00003e153.jpg')\n\n_, axarr = plt.subplots(1, 2)\naxarr[0].axis('off')\naxarr[1].axis('off')\naxarr[0].imshow(im_with_ships)\naxarr[0].imshow(read_flat_mask(masks, im_name_with_ships), alpha=0.6)\naxarr[1].imshow(im_with_no_ships)\naxarr[1].imshow(read_flat_mask(masks, im_name_with_no_ships), alpha=0.6)","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"2da6d606b5e8619cb041304211f6ab23aec6f039"},"cell_type":"code","source":"m = read_flat_mask(masks, im_name_with_ships)\nprint(f'{iou(m, m)}, {iou(m, np.zeros((768, 768)))}, {iou(m, np.ones((768, 768)))}')\n\nm = read_flat_mask(masks, im_name_with_no_ships)\nprint(f'{iou(m, m)}, {iou(m, np.zeros((768, 768)))}, {iou(m, np.ones((768, 768)))}')","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"9d50575a67ab9a2b2c0e3411f45245770c806a8d"},"cell_type":"code","source":"def f2(true_masks, pred_masks):\n    # a correct prediction on no ships in image would have F2 of zero (according to formula),\n    # but should be rewarded as 1\n    if np.sum(true_masks) == np.sum(pred_masks) == 0:\n        return 1.0\n\n    pred_masks = [m for m in pred_masks if np.any(m >= 0.5)]\n    true_masks = [m for m in true_masks if np.any(m >= 0.5)]\n\n    f2_total = 0\n    thresholds = [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95]\n\n    for threshold in thresholds:\n        if len(true_masks) == 0:\n            tp, fn, fp = 0.0, 0.0, float(len(pred_masks))\n        else:\n            pred_hits = np.zeros(len(pred_masks), dtype=np.bool)\n            true_hits = np.zeros(len(true_masks), dtype=np.bool)\n\n            for i, pred_mask in enumerate(pred_masks):\n                for j, true_mask in enumerate(true_masks):\n                    if iou(pred_mask, true_mask) > threshold:\n                        pred_hits[i] = True\n                        true_hits[j] = True\n\n            tp = np.sum(pred_hits)\n            fp = len(pred_masks) - tp\n            fn = len(true_masks) - np.sum(true_hits)\n\n        f2 = (5*tp)/(5*tp + 4*fn + fp)\n        f2_total += f2\n\n    return f2_total / len(thresholds)","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"2b337d5e6b1003ebd8945269a494a851402f235b"},"cell_type":"code","source":"m = read_masks(masks, im_name_with_ships)\nprint(f'{f2(m, m)}, {f2(m, np.zeros((768, 768)))}, {f2(m, np.ones((768, 768)))}')\n\nm = read_masks(masks, im_name_with_no_ships)\nprint(f'{f2(m, m)}, {f2(m, np.zeros((768, 768)))}, {f2(m, np.ones((768, 768)))}')","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"e7ddc5fc01a7332193db3e31e317f1003612407a"},"cell_type":"code","source":"# Compute the average F2 on a subset of images with a single blank prediction, images with no ships would get 1 and with ships would get 0. F2 score would be close the ratio of number of images with on ships and number of total images (0.72).\nsubset_images = 2000\nrandom_files = masks['ImageId'].unique()\nnp.random.shuffle(random_files)\n\nf2_sum = 0\nfor fname in random_files[:subset_images]:\n    mask = read_masks(masks, fname)\n    score = f2(mask, [np.zeros((768, 768))])\n    f2_sum += score\n\nprint(f2_sum/subset_images)","execution_count":null,"outputs":[]},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"45bd2bd5c08d07b146fc938a839e9234955114cc"},"cell_type":"code","source":"len(masks[masks['EncodedPixels'].isna()]['ImageId'].unique()) / len(masks['ImageId'].unique())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2c4303afe7d695c8789690fe04cee4d625c3ddf8"},"cell_type":"markdown","source":"###### Sample images with ships"},{"metadata":{"trusted":true,"_uuid":"a47ad6ac947d15f7ae2807aaccb1a0d5779a8825"},"cell_type":"code","source":"# https://www.kaggle.com/ezietsman/airbus-eda/notebook\nsample = masks[~masks.EncodedPixels.isna()].sample(9)\nfig, ax = plt.subplots(3, 3, figsize=(10, 10))\nfig.subplots_adjust(wspace=0, hspace=0)\nfor i, im_id in enumerate(sample.ImageId):\n    row, col = i // 3, i % 3\n\n    im = plt.imread(f'{train_path}/{im_id}')\n    ax[row, col].imshow(im)\n    ax[row, col].axis('off')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"_uuid":"9023870bdee61f7a9c8e0863be7e4642d2fe1174"},"cell_type":"markdown","source":"##### Ignore images in test"},{"metadata":{"autoscroll":false,"ein.hycell":false,"ein.tags":"worksheet-0","slideshow":{"slide_type":"-"},"trusted":true,"_uuid":"b54704cea583e3dbb3e618102abeb15f0b180cc7"},"cell_type":"code","source":"ignore_files = ['13703f040.jpg', '14715c06d.jpg', '33e0ff2d5.jpg', '4d4e09f2a.jpg', '877691df8.jpg', '8b909bb20.jpg', 'a8d99130e.jpg', 'ad55c3143.jpg', 'c8260c541.jpg', 'd6c7f17c7.jpg', 'dc3e7c901.jpg', 'e44dffe88.jpg', 'ef87bad36.jpg', 'f083256d8.jpg']\n\nfig = plt.figure(figsize=(10, 17))\nfig.subplots_adjust(wspace=0, hspace=0)\nfor i in range(len(ignore_files)):\n    fig.add_subplot(5, 3, i + 1)\n\n    im = plt.imread(f'{test_path}/{ignore_files[i]}')\n    plt.imshow(im)\n    plt.axis('off')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"694b5c8fe3a07572a9015598438b101303ab32c2"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.6"},"name":"eda.ipynb","varInspector":{"cols":{"lenName":16,"lenType":16,"lenVar":40},"kernels_config":{"python":{"delete_cmd_postfix":"","delete_cmd_prefix":"del ","library":"var_list.py","varRefreshCmd":"print(var_dic_list())"},"r":{"delete_cmd_postfix":") ","delete_cmd_prefix":"rm(","library":"var_list.r","varRefreshCmd":"cat(var_dic_list()) "}},"types_to_exclude":["module","function","builtin_function_or_method","instance","_Feature"],"window_display":false}},"nbformat":4,"nbformat_minor":1}