{"cells":[{"metadata":{"_uuid":"9cedfe305e83631440d74dfcde761c053c0e4103"},"cell_type":"markdown","source":"https://www.kaggle.com/c/carvana-image-masking-challenge"},{"metadata":{"trusted":true,"_uuid":"3e6e27b9a260af93a2d918c884b742fc195b6a75"},"cell_type":"code","source":"import cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom PIL import Image\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5139b0e1a4299080770f7f3a580d797dfbfc0124"},"cell_type":"code","source":"df = pd.read_csv('../input/train_masks.csv', usecols=['img'])\ndf['masks'] = '../input/train_masks/' + df.img.str.replace('.jpg', '_mask.gif')\ndf['img'] = '../input/train/' + df.img\nprint(df.shape)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f79e0c5e26eb49a2a4b204bb100ab02765a8d555"},"cell_type":"code","source":"img_size = 256\n\ndef read_img(path):\n    x = cv2.imread(path)\n    x = cv2.resize(x, (img_size, img_size))\n    x = cv2.cvtColor(x, cv2.COLOR_BGR2RGB)\n    return x\n\ndef read_mask(path):\n    x = Image.open(path)\n    x = x.resize([img_size, img_size])\n    return np.asarray(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"383b8dac331b2f97bb208bbe107307cc8bf3b5a8"},"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.img)\n    \nx = np.stack(x)\nx.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"675c11dc67ddca686a76d23efd3daae621650994"},"cell_type":"code","source":"with Parallel(n_jobs=12, prefer='threads', verbose=1) as ex:\n    y = ex(delayed(read_mask)(e) for e in df.masks)\n    \ny = np.stack(y)[..., None]\ny.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"41461a1271dd6e84d7fc00c43de7ad6b943420dd"},"cell_type":"markdown","source":"# Train validation split"},{"metadata":{"trusted":true,"_uuid":"f936c1fd4567503680c041e2947371e3b9f28248"},"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":{"trusted":true,"_uuid":"346be47fb6b123f01de5cbe6007f840f22d86103"},"cell_type":"markdown","source":"# View"},{"metadata":{"trusted":true,"_uuid":"6e419033af949579d3fd2ba774e9a4e9d5aac5da"},"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[:,:,0])\n    for ax in axes: ax.set_axis_off()\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1661b098a785223aefc08f7c13d7f7404447d6af"},"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":"f14a01554d4dc9c29691a0c9dc0a61c0eae5ce34"},"cell_type":"markdown","source":"# Model"},{"metadata":{"trusted":true,"_uuid":"65e56bddba4b366a4d90a7c6faa16fa9dbbf47db"},"cell_type":"code","source":"import keras.backend as K\nfrom keras.losses import binary_crossentropy\n\ndef dice_coef(y_true, y_pred, smooth=1.0):\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n\ndef bce_dice_loss(y_true, y_pred):\n    return 0.5 * binary_crossentropy(y_true, y_pred) - dice_coef(y_true, y_pred)\n\n# Ej:\n# model.compile(Adam(lr=1e-3), bce_dice_loss, metrics=['accuracy', dice_coef])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58ed094624408d659e840bf2c696353f120c6850"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fa11c36fbb36ea8b95d33567d899b30bcc6dfafe"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76f29975ff367a024e08bad4a8156c3c62e30a46"},"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}