{"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 os\nimport sys\nimport random\nimport warnings\n\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n\nfrom tqdm import tqdm\nfrom itertools import chain\nfrom skimage.io import imread, imshow, imread_collection, concatenate_images\nfrom skimage.transform import resize\nfrom skimage.morphology import label\n\nfrom keras.models import Model, load_model\nfrom keras.layers import Input\nfrom keras.layers.core import Dropout, Lambda\nfrom keras.layers.convolutional import Conv2D, Conv2DTranspose\nfrom keras.layers.pooling import MaxPooling2D\nfrom keras.layers.merge import concatenate\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom keras import backend as K\n\nimport tensorflow as tf\n\n%matplotlib inline\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\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},"cell_type":"code","source":"IMG_WIDTH = 128\nIMG_HEIGHT = 128\nIMG_CHANNELS = 3\nTRAIN_PATH = '../input/stage1_train/'\nTEST_PATH = '../input/stage1_test/'\n\nwarnings.filterwarnings('ignore', category=UserWarning, module='skimage')\nseed = 42\nrandom.seed = seed\nnp.random.seed = seed","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_ids = next(os.walk(TRAIN_PATH))[1]\ntest_ids = next(os.walk(TEST_PATH))[1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = np.zeros((len(train_ids), IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS), dtype = np.uint8)\nY_train = np.zeros((len(train_ids), IMG_WIDTH, IMG_HEIGHT, 1), dtype = np.bool)\nprint(\"Loading training images and masks...\")\n\nfor n, im_id in tqdm(enumerate(train_ids), total = len(train_ids)):\n    path = TRAIN_PATH + im_id\n    img = imread(path+'/images/'+im_id+'.png')[:,:,:IMG_CHANNELS]\n    img = resize(img, (IMG_WIDTH, IMG_HEIGHT), mode = 'constant', preserve_range = True)\n    X_train[n] = img\n    mask = np.zeros((IMG_WIDTH, IMG_HEIGHT, 1))\n    for mask_file in next(os.walk(path+'/masks/'))[2]:\n        mask_ = imread(path + '/masks/' + mask_file)\n        mask_ = np.expand_dims(resize(mask_, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True), axis=-1)\n        mask = np.maximum(mask, mask_)\n    Y_train[n] = mask\n\nX_test = np.zeros((len(test_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nsizes_test = []\nprint('Loading test images ... ')\nfor n, id_ in tqdm(enumerate(test_ids), total=len(test_ids)):\n    path = TEST_PATH + id_\n    img = imread(path + '/images/' + id_ + '.png')[:,:,:IMG_CHANNELS]\n    sizes_test.append([img.shape[0], img.shape[1]])\n    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n    X_test[n] = img\n\nprint('Done!')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ix = random.randint(0, len(train_ids))\nplt.imshow(X_train[ix])\nplt.show()\nplt.imshow(np.squeeze(Y_train[ix]), cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def mean_iou(y_true, y_pred):\n    prec = []\n    for t in np.arange(0.5, 1.0, 0.05):\n        y_pred_ = tf.to_int32(y_pred > t)\n        score, up_opt = tf.metrics.mean_iou(y_true, y_pred_, 2)\n        K.get_session().run(tf.local_variables_initializer())\n        with tf.control_dependencies([up_opt]):\n            score = tf.identity(score)\n        prec.append(score)\n    return K.mean(K.stack(prec), axis=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"inputs = Input((IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS))\ns = Lambda(lambda x: x/255)(inputs)\n\nconv_1 = Conv2D(16, (3,3), activation=\"relu\", padding = 'same')(s)\nconv_1 = Dropout(0.1)(conv_1)\n\nconv_1 = Conv2D(16, (3,3), activation=\"relu\", padding = 'same')(conv_1)\npool_1 = MaxPooling2D((2, 2))(conv_1)\n\nconv_2 = Conv2D(32, (3,3), activation=\"relu\", padding = 'same')(pool_1)\nconv_2= Dropout(0.1)(conv_2)\n\nconv_2 = Conv2D(32, (3,3), activation=\"relu\", padding = 'same')(conv_2)\npool_2 = MaxPooling2D((2, 2))(conv_2)\n\nconv_3 = Conv2D(64, (3,3), activation=\"relu\", padding = 'same')(pool_2)\nconv_3 = Dropout(0.1)(conv_3)\n\nconv_3 = Conv2D(64, (3,3), activation=\"relu\", padding = 'same')(conv_3)\npool_3 = MaxPooling2D((2, 2))(conv_3)\n\nconv_4 = Conv2D(128, (3,3), activation=\"relu\", padding = 'same')(pool_3)\nconv_4 = Dropout(0.1)(conv_4)\n\nconv_4 = Conv2D(128, (3,3), activation=\"relu\", padding = 'same')(conv_4)\npool_4 = MaxPooling2D((2, 2))(conv_4)\n\nconv_5 = Conv2D(256, (3,3), activation=\"relu\", padding = 'same')(pool_4)\nconv_5 = Dropout(0.1)(conv_5)\n\nconv_5 = Conv2D(256, (3,3), activation=\"relu\", padding = 'same')(conv_5)\npool_5 = MaxPooling2D((2, 2))(conv_5)\n\nct_6 = Conv2DTranspose(128, (2,2), strides = (2,2), padding ='same')(conv_5)\nct_6 = concatenate([ct_6, conv_4])\nconv_6 = Conv2D(128, (3,3), activation=\"relu\", padding = 'same')(ct_6)\nconv_6 = Dropout(0.1)(conv_6)\nconv_6 = Conv2D(128, (3,3), activation=\"relu\", padding = 'same')(conv_6)\n\nct_7 = Conv2DTranspose(64, (2,2), strides = (2,2), padding ='same')(conv_6)\nct_7 = concatenate([ct_7, conv_3])\nconv_7 = Conv2D(64, (3,3), activation=\"relu\", padding = 'same')(ct_7)\nconv_7 = Dropout(0.1)(conv_7)\nconv_7 = Conv2D(64, (3,3), activation=\"relu\", padding = 'same')(conv_7)\n\nct_8 = Conv2DTranspose(32, (2,2), strides = (2,2), padding ='same')(conv_7)\nct_8 = concatenate([ct_8, conv_2])\nconv_8 = Conv2D(32, (3,3), activation=\"relu\", padding = 'same')(ct_8)\nconv_8 = Dropout(0.1)(conv_8)\nconv_8 = Conv2D(32, (3,3), activation=\"relu\", padding = 'same')(conv_8)\n\nct_9 = Conv2DTranspose(16, (2,2), strides = (2,2), padding ='same')(conv_8)\nct_9 = concatenate([ct_9, conv_1])\nconv_9 = Conv2D(16, (3,3), activation=\"relu\", padding = 'same')(ct_9)\nconv_9 = Dropout(0.1)(conv_9)\nconv_9 = Conv2D(16, (3,3), activation=\"relu\", padding = 'same')(conv_9)\n\noutputs = Conv2D(1,(1,1), activation = 'sigmoid')(conv_9)\n\nmodel = Model(inputs=[inputs], outputs=[outputs])\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=[mean_iou])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"checkpoint = ModelCheckpoint('model-dsbowl2018-1.h5', verbose=1, save_best_only=True)\nresults = model.fit(X_train, Y_train, validation_split=0.1, batch_size=16, epochs=50, \n                    callbacks=[checkpoint])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = load_model('model-dsbowl2018-1.h5', custom_objects={'mean_iou': mean_iou})\npred_val = model.predict(X_train[int(X_train.shape[0]*0.9):], verbose=1)\npred_test = model.predict(X_test, verbose = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_val_t = (pred_val > 0.5).astype(np.uint8)\npred_test_t = (pred_test > 0.5).astype(np.uint8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ix = random.randint(0, len(preds_val))\nplt.imshow(X_train[ix])\nplt.show()\nplt.imshow(np.squeeze(Y_train[ix]))\nplt.show()\nplt.imshow(np.squeeze(pred_val_t[ix]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}