{"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":"401384ccc7f0b424c431addf805c12f2886fa994"},"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":"5b4af0402c9c6f7fc5b6ea242f0098171c356bda"},"cell_type":"code","source":"from keras.models import Model\nfrom keras.layers import Input, Conv2D, Conv2DTranspose, BatchNormalization, add\n\nlayers = {}\nn_layers = 4\nnf = 16\n\nm_in = Input((img_size, img_size, 3))\nm_x = BatchNormalization()(m_in)\nlayers[0] = Conv2D(nf, 3, padding='same')(m_x)\n\ndef downsample(x): return Conv2D(nf, 3, strides=(2,2), padding='same', activation='relu')(x)\ndef upsample(x): return Conv2DTranspose(nf, 3, strides=(2,2), padding='same', activation='relu')(x)\n\nfor i in range(n_layers):\n    nf *= 2\n    layers[i+1] = downsample(layers[i])\nelse: m_x = layers[i+1]\n\nfor i in range(n_layers)[::-1]:\n    nf //= 2\n    m_x = add([upsample(m_x), layers[i]])\n    \nm_out = Conv2D(1, 1, padding='same', activation='sigmoid')(m_x)\n\nmodel = Model(m_in, m_out)\nmodel.compile('adam', bce_dice_loss, metrics=['accuracy', dice_coef])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d9d08106a9148a7e43931f35f3675cede33284ef"},"cell_type":"markdown","source":"# Train"},{"metadata":{"trusted":true,"_uuid":"ee639214d1be8f32faece7993a45c536339b2a31"},"cell_type":"code","source":"bs = 128\nlog = model.fit(x_train, y_train, bs, epochs=5, validation_data=[x_val, y_val])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e81a6c44a4e720938c4059762262724570834ca7"},"cell_type":"code","source":"K.eval(model.optimizer.lr.assign(1e-4))\nlog = model.fit(x_train, y_train, bs, epochs=5, validation_data=[x_val, y_val])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0cb1faf3c5c0f46306413d1d109288d6cfd7f578"},"cell_type":"code","source":"idx = np.random.choice(len(x_val))\nsample_x, sample_y = x_val[idx], y_val[idx]\nsample_pred = model.predict(sample_x[None])[0] > 0.5\nplot_img(sample_x, sample_y)\nplot_img(sample_x, sample_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06291f2f2f2f10dfbf2c4b6e8fb0762b9a078183"},"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}