{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport pandas as pd\nimport numpy as np\n\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\nfrom keras.models import Sequential\nfrom keras.layers import Convolution2D\nfrom keras.layers import MaxPooling2D\nfrom keras.layers import Dense\nfrom keras.layers import Flatten\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import SGDClassifier\nimport seaborn as sns\nfrom scipy import misc\nimport matplotlib.pyplot as plt\n\nfrom tqdm import tqdm\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee09f61340c13dcf992ba83ee014ecf866366be0"},"cell_type":"code","source":"# Initialize global variables\nSAMPLE_SIZE = 10000\nBATCH_SIZE = 32\nTEST_PERC = 0.2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9740dc8ca9a33d72c95192295657c402c9d396bb"},"cell_type":"code","source":"segmentations = pd.read_csv(\"../input/train_ship_segmentations_v2.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70508832f8d038d630d5f9c0c7de4aa37906c3a8"},"cell_type":"code","source":"segmentations['path'] = '../input/train/' + segmentations['ImageId']\nsegmentations.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3c947065d14a3b382bb9b64102514b65aabac399"},"cell_type":"code","source":"segmentations.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b302b79a117fa63ff3cc1fa26a5d91d131851bff"},"cell_type":"code","source":"segmentations = segmentations.sample(n=SAMPLE_SIZE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f15143ee9d04acb0aff9e55d630f1936811e7ba"},"cell_type":"code","source":"def has_ship(encoded_pixels):\n    hs = [0 if pd.isna(n) else 1 for n in tqdm(encoded_pixels)]\n    return hs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e8ea09399e298171483aeb7b441f342597e09e6"},"cell_type":"code","source":"# This function takes a list of pixels, and returns them in run-length encoded format.\ndef rle_encoding(x):\n    '''\n    x: numpy array of shape (height, width), 1 - mask, 0 - background\n    Returns run length as list\n    '''\n    dots = np.where(x.T.flatten()==1)[0] # .T sets Fortran order down-then-right\n    run_lengths = []\n    prev = -2\n    for b in dots:\n        if (b>prev+1): run_lengths.extend((b+1, 0))\n        run_lengths[-1] += 1\n        prev = b\n    return run_lengths","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a70ebc5cfa637efd669ef68509f9d47c956bfd87"},"cell_type":"code","source":"conv = lambda l: ' '.join(map(str, l)) # list -> string","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1c0889e4538952c064372c5ad3b5edc727a15d19"},"cell_type":"code","source":"segmentations['HasShip'] = has_ship(segmentations['EncodedPixels'].values)\nsegmentations['HasShip'].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8db09a6a3d46a6cbd8da144771dcbfe11cb39762"},"cell_type":"code","source":"segmentations","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9f3ef28313795be08206e055bdd9f6e013c74fc4"},"cell_type":"code","source":"sns.countplot(segmentations['HasShip'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e6f6629a1fd5e52a999e82dfc1ee6e5e8fb0164c"},"cell_type":"code","source":"segmentations.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1cd275603cbe3ab4838d4fbb6d43a532e7a05f81"},"cell_type":"code","source":"segmentationsTest = pd.read_csv(\"../input/sample_submission_v2.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c9af98a724bd3592d4d1e16167e560656e17b47"},"cell_type":"code","source":"#segmentationsTest = segmentationsTest.sample(n=50)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0a6d4b0a69922e1119a8fa447465a02e9d4554ba"},"cell_type":"code","source":"segmentationsTest['path'] = '../input/test/' + segmentationsTest['ImageId']\nsegmentationsTest.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a71b6dad9bf2cee6a6cc48a0f4279a43c76c02a1"},"cell_type":"code","source":"from PIL import Image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c0ef231947870c5145fbb8ebd6d98c0279d1a406"},"cell_type":"code","source":"sLength = len(segmentationsTest['ImageId'])\nsegmentationsTest['HasShip'] = pd.Series('', index=segmentationsTest.index)\nsegmentationsTest['EncodedPixels'] = pd.Series('', index=segmentationsTest.index)\nsegmentationsTest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7a1e077b7c1d407ec84431ffe9d10ae9ae3a08be","scrolled":true},"cell_type":"code","source":"for index, row in segmentationsTest.iterrows():\n   img = Image.open('../input/test_v2/' + row['ImageId'], 'r') \n   x = np.array(img.getdata(), dtype=np.uint8)\n   x = x // 255\n   val = rle_encoding(x)\n   result = conv(val) \n   print('val = ',index, conv(val))\n   if not val:\n        segmentationsTest.at[index, 'EncodedPixels'] = float('nan')\n   else:\n        segmentationsTest.at[index, 'EncodedPixels'] = str(result)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"22be39b5e6fa2bf8186fc0e9762172d37175badf"},"cell_type":"code","source":"segmentationsTest['HasShip'] = has_ship(segmentationsTest['EncodedPixels'].values)\nsegmentationsTest['HasShip'].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4adba4743580a1056393281f8337f14eff5e229c"},"cell_type":"code","source":"segmentationsTest","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7484c943aaf98f98148b336a66df55de7f6f27b6"},"cell_type":"code","source":"segmentationsTest = segmentationsTest.drop(['path','HasShip'], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9da0103f221057fa53b4500a9c761357a8ffb226"},"cell_type":"code","source":"segmentationsTest.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"41f87ea26443381b99f01406107894b2f25c2ab4"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}