{"cells":[{"metadata":{"_uuid":"806d21da3c4de855bdba682b825cdcdee20282fa"},"cell_type":"markdown","source":"https://www.kaggle.com/c/airbus-ship-detection"},{"metadata":{"trusted":true,"_uuid":"95acb5c2c94bd2ad3dcf07e2b1640555f267738f"},"cell_type":"code","source":"!ls -lh ../input","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9cd14a1863a3757b26c85782d2b8caa8fc51a404"},"cell_type":"code","source":"import cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"827b0f900e9de93a0d145bb30e596b5b5f821854"},"cell_type":"code","source":"n_samples = 10_000\ndf = pd.read_csv('../input/train_ship_segmentations.csv').dropna().sample(n_samples, random_state=34)\ndf.reset_index(drop=True, inplace=True)\ndf.head(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e8b57851b0ae3576250db43a845f022743de7221"},"cell_type":"code","source":"img_size = 240\n\ndef read_img(path):\n    x = cv2.imread('../input/train/' + path)\n    x = cv2.resize(x, (img_size, img_size))\n    x = cv2.cvtColor(x, cv2.COLOR_BGR2RGB)\n    return x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c0752ba41956935be771de42e631c1bef46b7b2c"},"cell_type":"code","source":"def get_mask(encoded_pixels):\n    mask = np.zeros((img_size, img_size), np.uint8)\n    if not pd.isna(encoded_pixels):\n        scale = lambda x: min(img_size-1, round(x * img_size / 768))\n        rle_code = [int(i) for i in encoded_pixels.split()]\n        pixels = [(scale(pixel_position % 768), scale(pixel_position // 768)) \n                     for start, length in list(zip(rle_code[0:-1:2], rle_code[1:-2:2])) \n                     for pixel_position in range(start, start + length)]\n        mask[tuple(zip(*pixels))] = 1\n    return mask","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ab2c282873c1badac830734718bde77a18df258d"},"cell_type":"code","source":"from joblib import Parallel, delayed\n\nwith Parallel(n_jobs=12, prefer='threads', verbose=1) as ex:\n    x = ex(delayed(read_img)(e) for e in df.ImageId)\n    \nx = np.stack(x)\nx.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7102d9d14a795ac08d32d9c2faff5137a4726911"},"cell_type":"code","source":"with Parallel(n_jobs=12, prefer='threads', verbose=1) as ex:\n    y = ex(delayed(get_mask)(e) for e in df.EncodedPixels)\n    \ny = np.stack(y)\ny.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"29fe53c208bb31f34b361c4f1a97bb45b79dc5cc"},"cell_type":"markdown","source":"# Train validation split"},{"metadata":{"trusted":true,"_uuid":"96a75c8fbe8c5a908332df20e7c595e321d158a7"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.2, random_state=42)\nx_train.shape, x_val.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9607355a993f624d45fcf91d20e31c1f675085a8"},"cell_type":"markdown","source":"# View"},{"metadata":{"trusted":true,"_uuid":"78a654d4f9067c0fe1c50e9f1f6e203343282299"},"cell_type":"code","source":"def plot_img(x, y):\n    fig, axes = plt.subplots(1, 2, figsize=(15,6))\n    axes[0].imshow(x)\n    axes[1].imshow(y)\n    for ax in axes: ax.set_axis_off()\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33ac51abeb61cbda0ab919cd458d9f721d27e8b1"},"cell_type":"code","source":"idx = np.random.choice(len(x_train))\nsample_x, sample_y = x_train[idx], y_train[idx]\nplot_img(sample_x, sample_y)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"019ed211591b0f0d278fdcd7f30ecc3a89472722"},"cell_type":"markdown","source":"# Model"},{"metadata":{"trusted":true,"_uuid":"a8141867a7ddda185d000d2eeed1e4f101dc0fc5"},"cell_type":"code","source":"from keras import backend as K\n\ndef jaccard_distance(y_true, y_pred, smooth=100):\n    intersection = K.sum(K.abs(y_true * y_pred), axis=-1)\n    sum_ = K.sum(K.abs(y_true) + K.abs(y_pred), axis=-1)\n    jac = (intersection + smooth) / (sum_ - intersection + smooth)\n    return (1 - jac) * smooth","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"66ba986e43c9e6122e8c6e37bcfeb84f219fdd2c"},"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}