{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# https://www.kaggle.com/bguberfain/naive-keras-2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom skimage.io import imread\nfrom skimage.transform import downscale_local_mean\nfrom os.path import join\nfrom tqdm import tqdm_notebook\nimport cv2\nfrom sklearn.model_selection import train_test_split\n\ninput_folder = join('..', 'input/carvana-image-masking-challenge/')\n\ndf_mask = pd.read_csv(join(input_folder, 'train_masks.csv'), usecols=['img'])\nids_train = df_mask['img'].map(lambda s: s.split('_')[0]).unique()\n\nimgs_idx = list(range(1, 17))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"load_img = lambda im, idx: imread(join(input_folder, 'train', '{}_{:02d}.jpg'.format(im, idx)))\nload_mask = lambda im, idx: imread(join(input_folder, 'train_masks', '{}_{:02d}_mask.gif'.format(im, idx)))\nresize = lambda im: downscale_local_mean(im, (4,4) if im.ndim==2 else (4,4,1))\nmask_image = lambda im, mask: (im * np.expand_dims(mask, 2))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"num_train = 32  # len(ids_train)\n\n# Load data for position id=1\nX = np.empty((num_train, 320, 480, 12), dtype=np.float32)\ny = np.empty((num_train, 320, 480, 1), dtype=np.float32)\n\nwith tqdm_notebook(total=num_train) as bar:\n    idx = 1 # Rotation index\n    for i, img_id in enumerate(ids_train[:num_train]):\n        imgs_id = [resize(load_img(img_id, j)) for j in imgs_idx]\n        # Input is image + mean image per channel + std image per channel\n        X[i, ..., :9] = np.concatenate([imgs_id[idx-1], np.mean(imgs_id, axis=0), np.std(imgs_id, axis=0)], axis=2)\n        y[i] = resize(np.expand_dims(load_mask(img_id, idx), 2)) / 255.\n        del imgs_id # Free memory\n        bar.update()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Concat overall y info to X\n# This is important as the kernels of CNN used below has no information of its location\ny_train_mean = y_train.mean(axis=0)\ny_train_std = y_train.std(axis=0)\ny_train_min = y_train.min(axis=0)\n\ny_features = np.concatenate([y_train_mean, y_train_std, y_train_min], axis=2)\n\nX_train[:, ..., -3:] = y_features\nX_val[:, ..., -3:] = y_features","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Normalize input and output\nX_mean = X_train.mean(axis=(0,1,2), keepdims=True)\nX_std = X_train.std(axis=(0,1,2), keepdims=True)\n\nX_train -= X_mean\nX_train /= X_std\n\nX_val -= X_mean\nX_val /= X_std","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Create simple model\nfrom keras.layers import Conv2D, MaxPooling2D, Conv2DTranspose, Input, concatenate\nfrom keras.models import Model\nimport keras.backend as K\n\ninp = Input((320, 480, 12))\nconv1 = Conv2D(12, 3, activation='relu', padding='same')(inp)\nmax1 = MaxPooling2D(2)(conv1)\nconv2 = Conv2D(12, 3, activation='relu', padding='same')(max1)\nmax2 = MaxPooling2D(2)(conv2)\nconv3 = Conv2D(12, 3, activation='relu', padding='same')(max2)\n\ndeconv3 = Conv2DTranspose(12, 3, strides=4, activation='relu', padding='same')(conv3)\ndeconv2 = Conv2DTranspose(12, 3, strides=2, activation='relu', padding='same')(conv2)\n\ndeconvs = concatenate([conv1, deconv2, deconv3])\n\nout = Conv2D(1, 7, activation='sigmoid', padding='same')(deconvs)\n\nmodel = Model(inp, out)\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.optimizers import Adam\nfrom keras.losses import binary_crossentropy\n\nsmooth = 1.\n\n# From here: https://github.com/jocicmarko/ultrasound-nerve-segmentation/blob/master/train.py\ndef dice_coef(y_true, y_pred):\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\ndef dice_loss(y_true, y_pred):\n    return -dice_coef(y_true, y_pred)\n\nmodel.compile(Adam(lr=1e-3), dice_loss, metrics=['accuracy', dice_coef])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(X_train, y_train, epochs=15, validation_data=(X_val, y_val), batch_size=5, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pd.DataFrame(history.history)[['dice_coef', 'val_dice_coef']].plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"idx = 0\nx = X_val[idx]\n\nfig, ax = plt.subplots(5,3, figsize=(16, 16))\nax = ax.ravel()\n\ncmaps = ['Reds', 'Greens', 'Blues']\nfor i in range(x.shape[-1]):\n    ax[i].imshow(x[...,i], cmap='gray') #cmaps[i%3])\n    ax[i].set_title('channel {}'.format(i))\n\nax[-3].imshow((x[...,:3] * X_std[0,...,:3] + X_mean[0,...,:3]) / 255.)\nax[-3].set_title('X')\n\nax[-2].imshow(y_val[idx,...,0], cmap='gray')\nax[-2].set_title('y')\n\ny_pred = model.predict(x[None]).squeeze()\nax[-1].imshow(y_pred, cmap='gray')\nax[-1].set_title('y_pred')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(y_pred > 0.5, cmap='gray')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}