{"cells":[{"metadata":{"_uuid":"5c38504af3a84bee68c66d3cde74443c58df422f","_cell_guid":"c332549b-8d23-4bb5-8497-e7a8eb8b21d2","collapsed":true,"trusted":false},"cell_type":"code","source":"import os\nimport sys\nimport random\nimport warnings\nimport cv2\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 import feature\nfrom skimage.color import rgb2grey\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# Set some parameters\nIMG_WIDTH = 256\nIMG_HEIGHT = 256\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\n\n# Get train and test IDs\ntrain_ids = next(os.walk(TRAIN_PATH))[1]\ntest_ids = next(os.walk(TEST_PATH))[1]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0aa97d66c29f45dfac9b0f45fcf74ba0e778ba5d","_cell_guid":"c1dbc57c-b497-4ccb-b077-2053203ab7ed","collapsed":true,"trusted":false},"cell_type":"code","source":"def buildUNetModel():\n    print(\"Building UNet Model ...\")\n    sys.stdout.flush()\n    # Build U-Net model\n    inputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))\n    s = Lambda(lambda x: x / 255) (inputs)\n\n    c1 = Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (s)\n    c1 = Dropout(0.1) (c1)\n    c1 = Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c1)\n    p1 = MaxPooling2D((2, 2)) (c1)\n\n    c2 = Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (p1)\n    c2 = Dropout(0.1) (c2)\n    c2 = Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c2)\n    p2 = MaxPooling2D((2, 2)) (c2)\n\n    c3 = Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (p2)\n    c3 = Dropout(0.2) (c3)\n    c3 = Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c3)\n    p3 = MaxPooling2D((2, 2)) (c3)\n\n    c4 = Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (p3)\n    c4 = Dropout(0.2) (c4)\n    c4 = Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c4)\n    p4 = MaxPooling2D(pool_size=(2, 2)) (c4)\n\n    c5 = Conv2D(256, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (p4)\n    c5 = Dropout(0.3) (c5)\n    c5 = Conv2D(256, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c5)\n\n    u6 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same') (c5)\n    u6 = concatenate([u6, c4])\n    c6 = Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (u6)\n    c6 = Dropout(0.2) (c6)\n    c6 = Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c6)\n\n    u7 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same') (c6)\n    u7 = concatenate([u7, c3])\n    c7 = Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (u7)\n    c7 = Dropout(0.2) (c7)\n    c7 = Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c7)\n\n    u8 = Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same') (c7)\n    u8 = concatenate([u8, c2])\n    c8 = Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (u8)\n    c8 = Dropout(0.1) (c8)\n    c8 = Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c8)\n\n    u9 = Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same') (c8)\n    u9 = concatenate([u9, c1], axis=3)\n    c9 = Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (u9)\n    c9 = Dropout(0.1) (c9)\n    c9 = Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c9)\n\n    outputs = Conv2D(1, (1, 1), activation='sigmoid') (c9)\n\n    model = Model(inputs=[inputs], outputs=[outputs])\n    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n    print(\"UNet Model built\")\n    sys.stdout.flush()    \n    return model\n\ndef fitAndPredict(model, results, epochs):\n    X_train = results['X_train']\n    Y_train = results['Y_train']    \n    print(\"Fitting model ...\")\n    sys.stdout.flush()    \n\n\n    # Fit model\n    earlystopper = EarlyStopping(patience=5, verbose=1)\n    checkpointer = ModelCheckpoint('model-dsbowl2018-1.h5', verbose=1, save_best_only=True)\n    results = model.fit(X_train, Y_train, validation_split=0.1, batch_size=16, epochs=epochs, \n                        callbacks=[earlystopper, checkpointer])\n    #print(\"Model has been fit\")\n    #sys.stdout.flush()    \n\n#     print(\"Predicting on train, val and test ...\")\n#     sys.stdout.flush()    \n\n#     # Predict on train, val and test\n#     model = load_model('model-dsbowl2018-1.h5', custom_objects={'mean_iou': mean_iou})\n#     preds_train = model.predict(X_train[:int(X_train.shape[0]*0.9)], verbose=1)\n#     preds_val = model.predict(X_train[int(X_train.shape[0]*0.9):], verbose=1)\n#     preds_test = model.predict(X_test, verbose=1)\n\n#     # Threshold predictions\n#     preds_train_t = (preds_train > 0.5).astype(np.uint8)\n#     preds_val_t = (preds_val > 0.5).astype(np.uint8)\n#     preds_test_t = (preds_test > 0.5).astype(np.uint8)\n\n#     # Create list of upsampled test masks\n#     preds_test_upsampled = []\n#     for i in range(len(preds_test)):\n#         preds_test_upsampled.append(resize(np.squeeze(preds_test[i]), \n#                                            (sizes_test[i][0], sizes_test[i][1]), \n#                                            mode='constant', preserve_range=True))\n        \n#     print(\"Perform a sanity check on some random training samples\")\n#     sys.stdout.flush()         \n#     ix = random.randint(0, len(preds_train_t))\n#     imshow(X_train[ix])\n#     plt.show()\n#     imshow(np.squeeze(Y_train[ix]))\n#     plt.show()\n#     #print(preds_train_t)\n#     #print(ix)\n#     imshow(np.squeeze(preds_train_t[ix]))\n#     plt.show()\n\n#     print(\"Perform a sanity check on some random validation samples\")\n#     sys.stdout.flush()         \n#     ix = random.randint(0, len(preds_val_t))\n#     imshow(X_train[int(X_train.shape[0]*0.9):][ix])\n#     plt.show()\n#     imshow(np.squeeze(Y_train[int(Y_train.shape[0]*0.9):][ix]))\n#     plt.show()\n#     imshow(np.squeeze(preds_val_t[ix]))\n#     plt.show()\n\ndef checkTrainingData(ids, trains, masks):\n    # Check training data\n    ix = random.randint(0, len(ids) - 1)\n    print(\"Image\")\n    imshow(trains[ix])\n    plt.show()\n    print(\"Mask\")    \n    imshow(np.squeeze(masks[ix]))\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"e8240b4b-f250-40af-aaec-188b181490cb","_uuid":"9530bbf5d873364f4da33fc1583cabd13f7c0877","trusted":false},"cell_type":"code","source":"def testModel(images, modelBuildingFunction, epochs):\n    print(\"Images pre-processed, moving on to fitting and predictions ...\")\n    sys.stdout.flush()\n    fitAndPredict(\n        modelBuildingFunction(),\n        images,\n        epochs\n    )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"737e502d06d048824b4a60b7482222d3dcfac6f6","_cell_guid":"e75cf7b2-dad9-4414-962a-31abcf9d8388","collapsed":true,"trusted":false},"cell_type":"code","source":"def preprocessImagesOne(numOfSamples=5):\n    # Get and resize train images and masks\n    X_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\n    Y_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)\n    print('Getting and resizing train images and masks ... ')\n    sys.stdout.flush()\n    for n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):\n        path = TRAIN_PATH + id_\n        img = imread(path + '/images/' + id_ + '.png')[:,:,:IMG_CHANNELS]\n\n        img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n\n        X_train[n] = img\n        mask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)\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', \n                                          preserve_range=True), axis=-1)\n            mask = np.maximum(mask, mask_)\n        Y_train[n] = mask\n\n    print('Done!')\n    # Check training data\n    for i in range(numOfSamples):\n        checkTrainingData(train_ids, X_train, Y_train)\n\n    return { 'X_train': X_train, 'Y_train': Y_train }","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"49006698f105541099f6d4e352e226360c5131dc","_cell_guid":"8f0f18ad-6c66-49c8-8be5-8cb79bfbf5d6","collapsed":true,"trusted":false},"cell_type":"code","source":"def preprocessImagesTwo(numOfSamples=5):\n    # Get and resize train images and masks\n    X_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\n    Y_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)\n    print('Getting and resizing train images and masks ... ')\n    sys.stdout.flush()\n    for n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):\n        path = TRAIN_PATH + id_\n        img = imread(path + '/images/' + id_ + '.png')[:,:,:IMG_CHANNELS]\n\n        #print(img)\n        img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n        #img_edges = cv2.Canny(img, 100, 200)\n        img_edges = 255*feature.canny(rgb2grey(img))\n\n    #    img_edges = img_edges[..., np.newaxis]\n        \n        img_edges = np.stack((img_edges,)*3, -1)\n     #   print(img_edges)\n#         img_edges = resize(img_edges, (IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))        \n#         print(stacked_img)\n        #imshow(img_edges)\n        #plt.show()\n        \n        \n        \n        X_train[n] = img_edges\n        mask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)\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', \n                                          preserve_range=True), axis=-1)\n            mask = np.maximum(mask, mask_)\n        Y_train[n] = mask\n\n    print('Done!')\n    # Check training data\n    for i in range(numOfSamples):\n        checkTrainingData(train_ids, X_train, Y_train)\n\n    return { 'X_train': X_train, 'Y_train': Y_train }","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"fb16e2a5-9ce3-4e58-a51b-0b18e57d3b60","_uuid":"a0210cdd7c8ef2bd1918bd52f4c1537d902549bd","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"e6687acf-5615-4ffb-9147-38836bf69752","_uuid":"fab551af8b128ccc86797b0ddb5fe14a53d3c94c","trusted":false},"cell_type":"code","source":"def preprocessImagesThree(numOfSamples=5):\n    # Get and resize train images and masks\n    X_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\n    Y_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)\n    print('Getting and resizing train images and masks ... ')\n    sys.stdout.flush()\n    for n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):\n        path = TRAIN_PATH + id_\n        img = imread(path + '/images/' + id_ + '.png')[:,:,:IMG_CHANNELS]\n\n        #print(img)\n        img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n        img_edges = 255*feature.canny(rgb2grey(img))\n        img_edges = np.stack((img_edges,)*3, -1)\n#         old_set = [[0, 1], [4, 5]]\n#         new_set = [[2, 7], [0, 1]]\n        blended_img = (np.array(img_edges) + np.array(img)) / 2\n\n        imshow(blended_img)\n        plt.show()\n        #imshow(img)\n        #plt.show()\n        #imshow(img_edges)\n        #plt.show()\n        \n        \n        \n        X_train[n] = img_edges\n        mask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)\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', \n                                          preserve_range=True), axis=-1)\n            mask = np.maximum(mask, mask_)\n        Y_train[n] = mask\n\n    print('Done!')\n    # Check training data\n    for i in range(numOfSamples):\n        checkTrainingData(train_ids, X_train, Y_train)\n\n    return { 'X_train': X_train, 'Y_train': Y_train }\n\n#preprocessImagesThree(0)\n#print('done')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"21be85242af1289644191385012b0c22e8fbef1e","_cell_guid":"2b8f842f-3a2c-411a-b1a9-b8fc84862307","trusted":false,"collapsed":true},"cell_type":"code","source":"images = preprocessImagesOne(3)\ntestModel(images, buildUNetModel, epochs=25)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d02062666caaf32f3ad55a6162ce43b36e56b926","_cell_guid":"7990efb9-504a-4638-8d94-44222a683c6f","trusted":false,"collapsed":true},"cell_type":"code","source":"images = preprocessImagesTwo(3)\ntestModel(images, buildUNetModel, epochs=5)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2fd51f03defc109777388c3137a4df663507e7ed","_cell_guid":"e1bb9378-645a-4fa3-aac0-25121862e6dd","trusted":false,"collapsed":true},"cell_type":"code","source":"images = preprocessImagesThree(3)\ntestModel(images, buildUNetModel, epochs=5)","execution_count":null,"outputs":[]}],"metadata":{"language_info":{"name":"python","pygments_lexer":"ipython3","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"nbconvert_exporter":"python","version":"3.6.4","mimetype":"text/x-python"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}