{"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_minor":4,"nbformat":4,"cells":[{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-13T10:26:20.532421Z","iopub.execute_input":"2023-02-13T10:26:20.532749Z","iopub.status.idle":"2023-02-13T10:26:20.562951Z","shell.execute_reply.started":"2023-02-13T10:26:20.532674Z","shell.execute_reply":"2023-02-13T10:26:20.561940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom keras.models import Model\nfrom keras.layers import Input, Add, Dense, Dropout, Activation, ZeroPadding2D, BatchNormalization, Concatenate, Flatten, Conv2D, AveragePooling2D, MaxPool2D, Reshape, Conv2DTranspose\n\nimport os\n\nfrom tqdm import tqdm\nfrom skimage.io import imread, imshow\nfrom skimage.transform import resize\nimport matplotlib.pyplot as plt\nfrom skimage.morphology import label\n","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:26:20.564750Z","iopub.execute_input":"2023-02-13T10:26:20.565385Z","iopub.status.idle":"2023-02-13T10:26:27.917097Z","shell.execute_reply.started":"2023-02-13T10:26:20.565332Z","shell.execute_reply":"2023-02-13T10:26:27.916114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q /kaggle/input/data-science-bowl-2018/stage1_test.zip -d stage1_test\n!unzip -q /kaggle/input/data-science-bowl-2018/stage1_train.zip -d stage1_train\n","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:26:40.312677Z","iopub.execute_input":"2023-02-13T10:26:40.313284Z","iopub.status.idle":"2023-02-13T10:26:45.197306Z","shell.execute_reply.started":"2023-02-13T10:26:40.313250Z","shell.execute_reply":"2023-02-13T10:26:45.195997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_training(hist):\n    '''\n    This function take training model and plot history of accuracy and losses with the best epoch in both of them.\n    '''\n\n    # Define needed variables\n    tr_acc = hist.history['accuracy']\n    tr_loss = hist.history['loss']\n    val_acc = hist.history['val_accuracy']\n    val_loss = hist.history['val_loss']\n    index_loss = np.argmin(val_loss)\n    val_lowest = val_loss[index_loss]\n    index_acc = np.argmax(val_acc)\n    acc_highest = val_acc[index_acc]\n    Epochs = [i+1 for i in range(len(tr_acc))]\n    loss_label = f'best epoch= {str(index_loss + 1)}'\n    acc_label = f'best epoch= {str(index_acc + 1)}'\n\n    # Plot training history\n    plt.figure(figsize= (20, 8))\n    plt.style.use('fivethirtyeight')\n\n    plt.subplot(1, 2, 1)\n    plt.plot(Epochs, tr_loss, 'r', label= 'Training loss')\n    plt.plot(Epochs, val_loss, 'g', label= 'Validation loss')\n    plt.scatter(index_loss + 1, val_lowest, s= 150, c= 'blue', label= loss_label)\n    plt.title('Training and Validation Loss')\n    plt.xlabel('Epochs')\n    plt.ylabel('Loss')\n    plt.legend()\n\n    plt.subplot(1, 2, 2)\n    plt.plot(Epochs, tr_acc, 'r', label= 'Training Accuracy')\n    plt.plot(Epochs, val_acc, 'g', label= 'Validation Accuracy')\n    plt.scatter(index_acc + 1 , acc_highest, s= 150, c= 'blue', label= acc_label)\n    plt.title('Training and Validation Accuracy')\n    plt.xlabel('Epochs')\n    plt.ylabel('Accuracy')\n    plt.legend()\n\n    plt.tight_layout\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:26:47.628571Z","iopub.execute_input":"2023-02-13T10:26:47.628952Z","iopub.status.idle":"2023-02-13T10:26:47.641614Z","shell.execute_reply.started":"2023-02-13T10:26:47.628919Z","shell.execute_reply":"2023-02-13T10:26:47.640641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The convolation block \ndef conv_block(input, num_filters):\n    x = Conv2D(num_filters, 3, padding=\"same\")(input)\n    x = BatchNormalization()(x)\n    x = Activation(\"relu\")(x)\n    x = Conv2D(num_filters, 3, padding=\"same\")(x)\n    x = BatchNormalization()(x)\n    x = Activation(\"relu\")(x)\n    \n    return x\n\n\n# The encoder block\ndef encoder_block(input, num_filters):\n    x = conv_block(input, num_filters)\n    p = MaxPool2D((2, 2))(x)\n    \n    return x, p\n\n\n# The decoder block\ndef decoder_block(input, skip_features, num_filters):\n    x = Conv2DTranspose(num_filters, (2, 2), strides=2, padding=\"same\")(input)\n    x = Concatenate()([x, skip_features])\n    x = conv_block(x, num_filters)\n\n    return x\n\n","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:26:52.167172Z","iopub.execute_input":"2023-02-13T10:26:52.167545Z","iopub.status.idle":"2023-02-13T10:26:52.186648Z","shell.execute_reply.started":"2023-02-13T10:26:52.167512Z","shell.execute_reply":"2023-02-13T10:26:52.185261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def unet(input_shape):\n    inputs = Input(input_shape)\n\n    s1, p1 = encoder_block(inputs, 64)\n    s2, p2 = encoder_block(p1, 128)\n    s3, p3 = encoder_block(p2, 256)\n    s4, p4 = encoder_block(p3, 512)\n\n    b1 = conv_block(p4, 1024)\n\n    d1 = decoder_block(b1, s4, 512)\n    d2 = decoder_block(d1, s3, 256)\n    d3 = decoder_block(d2, s2, 128)\n    d4 = decoder_block(d3, s1, 64)\n\n    outputs = Conv2D(1, 1, padding=\"same\", activation=\"sigmoid\")(d4)\n\n    modelUnet = Model(inputs=inputs, outputs=outputs, name=\"OriginalUNET\")\n\n    modelUnet.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n\n    return modelUnet","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:26:52.639718Z","iopub.execute_input":"2023-02-13T10:26:52.640079Z","iopub.status.idle":"2023-02-13T10:26:52.647554Z","shell.execute_reply.started":"2023-02-13T10:26:52.640043Z","shell.execute_reply":"2023-02-13T10:26:52.646596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainPath = \"stage1_train/\"\ntestPath = \"stage1_test/\"","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:26:53.210406Z","iopub.execute_input":"2023-02-13T10:26:53.211129Z","iopub.status.idle":"2023-02-13T10:26:53.218238Z","shell.execute_reply.started":"2023-02-13T10:26:53.211090Z","shell.execute_reply":"2023-02-13T10:26:53.217216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ids = next(os.walk(trainPath))[1]\ntest_ids = next(os.walk(testPath))[1]","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:26:54.042815Z","iopub.execute_input":"2023-02-13T10:26:54.043191Z","iopub.status.idle":"2023-02-13T10:26:54.051159Z","shell.execute_reply.started":"2023-02-13T10:26:54.043158Z","shell.execute_reply":"2023-02-13T10:26:54.050045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.zeros((len(train_ids), 128, 128, 3), dtype=np.uint8)\nY_train = np.zeros((len(train_ids), 128, 128, 1), dtype=np.bool_)","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:26:54.771110Z","iopub.execute_input":"2023-02-13T10:26:54.771487Z","iopub.status.idle":"2023-02-13T10:26:54.777158Z","shell.execute_reply.started":"2023-02-13T10:26:54.771458Z","shell.execute_reply":"2023-02-13T10:26:54.776138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Resizing training images and masks\")\nfor n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):\n    path = trainPath + id_\n    img = imread(path + \"/images/\" + id_ + \".png\")[:, :, :3]\n    img = resize(img, (128, 128), mode=\"constant\", preserve_range=True)\n    X_train[n] = img\n    mask = np.zeros((128, 128, 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_, (128, 128), mode=\"constant\", preserve_range=True), axis=-1)\n        mask = np.maximum(mask, mask_)\n\n    Y_train[n] = mask","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:26:55.632715Z","iopub.execute_input":"2023-02-13T10:26:55.633056Z","iopub.status.idle":"2023-02-13T10:31:06.311688Z","shell.execute_reply.started":"2023-02-13T10:26:55.633026Z","shell.execute_reply":"2023-02-13T10:31:06.310610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = np.zeros((len(test_ids), 128, 128, 3), dtype=np.uint8)\nsizes_test = []\n\nprint(\"Resizing test images\")\nfor n, id_ in tqdm(enumerate(test_ids), total=len(test_ids)):\n    path = testPath + id_\n    img = imread(path + \"/images/\" + id_ + \".png\")[:,:,:3]\n    sizes_test.append([img.shape[0], img.shape[1]])\n    img = resize(img, (128, 128), mode = \"constant\", preserve_range=True)\n    X_test[n] = img","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:31:06.314080Z","iopub.execute_input":"2023-02-13T10:31:06.314833Z","iopub.status.idle":"2023-02-13T10:31:07.664107Z","shell.execute_reply.started":"2023-02-13T10:31:06.314791Z","shell.execute_reply":"2023-02-13T10:31:07.663054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimage_x = random.randint(0, len(train_ids))\nimshow(X_train[image_x])\nplt.show()\nplt.imshow(np.squeeze(Y_train[image_x]))\n","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:31:07.665888Z","iopub.execute_input":"2023-02-13T10:31:07.666685Z","iopub.status.idle":"2023-02-13T10:31:08.136486Z","shell.execute_reply.started":"2023-02-13T10:31:07.666640Z","shell.execute_reply":"2023-02-13T10:31:08.135331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelUNet = unet((128, 128, 3))\n# checkpointer = tf.keras.callbacks.ModelCheckpoint(\"modelUnet.h5\", verbose=1, save_best_only=True)\ncallbacks=[\n#     checkpointer,\n    tf.keras.callbacks.EarlyStopping(patience=2, monitor=\"val_loss\"),\n    tf.keras.callbacks.TensorBoard(log_dir=\"logs\")]\n\n\nresults = modelUNet.fit(X_train, Y_train, validation_split=0.1, batch_size=16, epochs=25, callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:31:08.139721Z","iopub.execute_input":"2023-02-13T10:31:08.140519Z","iopub.status.idle":"2023-02-13T10:32:49.879786Z","shell.execute_reply.started":"2023-02-13T10:31:08.140474Z","shell.execute_reply":"2023-02-13T10:32:49.878839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_training(results)","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:32:49.881632Z","iopub.execute_input":"2023-02-13T10:32:49.882281Z","iopub.status.idle":"2023-02-13T10:32:50.553140Z","shell.execute_reply.started":"2023-02-13T10:32:49.882241Z","shell.execute_reply":"2023-02-13T10:32:50.552230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = random.randint(0, len(X_train))\n\n\npreds_train = modelUNet.predict(X_train[:int(X_train.shape[0]*0.9)], verbose=1)\npreds_val = modelUNet.predict(X_train[int(X_train.shape[0]*0.9):], verbose=1)\npreds_test = modelUNet.predict(X_test, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:32:50.554336Z","iopub.execute_input":"2023-02-13T10:32:50.554726Z","iopub.status.idle":"2023-02-13T10:33:00.405716Z","shell.execute_reply.started":"2023-02-13T10:32:50.554688Z","shell.execute_reply":"2023-02-13T10:33:00.404768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_train_t = (preds_train > 0.5).astype(np.uint8)\npreds_val_t = (preds_val > 0.5).astype(np.uint8)\npreds_test_t = (preds_test > 0.5).astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:33:00.407539Z","iopub.execute_input":"2023-02-13T10:33:00.407924Z","iopub.status.idle":"2023-02-13T10:33:00.420801Z","shell.execute_reply.started":"2023-02-13T10:33:00.407886Z","shell.execute_reply":"2023-02-13T10:33:00.419781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform a sanity check on some random training samples\nix = random.randint(0, len(preds_train_t))\nimshow(X_train[ix])\nplt.show()\n# plt.imshow(np.squeeze(Y_train[ix]))\nplt.imshow(np.squeeze(preds_train_t[ix]))\n","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:33:00.423950Z","iopub.execute_input":"2023-02-13T10:33:00.424924Z","iopub.status.idle":"2023-02-13T10:33:00.817532Z","shell.execute_reply.started":"2023-02-13T10:33:00.424893Z","shell.execute_reply":"2023-02-13T10:33:00.816568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform a sanity check on some random validation samples\nix = random.randint(0, len(preds_val_t))\nimshow(X_train[int(X_train.shape[0]*0.9):][ix])\nplt.show()\n# plt.imshow(np.squeeze(Y_train[int(Y_train.shape[0]*0.9):][ix]))\nplt.imshow(np.squeeze(preds_val_t[ix]))\n","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:33:00.819345Z","iopub.execute_input":"2023-02-13T10:33:00.820037Z","iopub.status.idle":"2023-02-13T10:33:01.212011Z","shell.execute_reply.started":"2023-02-13T10:33:00.819999Z","shell.execute_reply":"2023-02-13T10:33:01.211362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Run-length encoding stolen from https://www.kaggle.com/rakhlin/fast-run-length-encoding-python\ndef rle_encoding(x):\n    dots = np.where(x.T.flatten() == 1)[0]\n    run_lengths = []\n    prev = -2\n    for b in dots:\n        if (b>prev+1): run_lengths.extend((b + 1, 0))\n        run_lengths[-1] += 1\n        prev = b\n    return run_lengths\n\ndef prob_to_rles(x, cutoff=0.5):\n    lab_img = label(x > cutoff)\n    for i in range(1, lab_img.max() + 1):\n        yield rle_encoding(lab_img == i)","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:33:01.215122Z","iopub.execute_input":"2023-02-13T10:33:01.215918Z","iopub.status.idle":"2023-02-13T10:33:01.223151Z","shell.execute_reply.started":"2023-02-13T10:33:01.215880Z","shell.execute_reply":"2023-02-13T10:33:01.221846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create list of upsampled test masks\npreds_test_upsampled = []\nfor 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))","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:33:01.224845Z","iopub.execute_input":"2023-02-13T10:33:01.225216Z","iopub.status.idle":"2023-02-13T10:33:01.640489Z","shell.execute_reply.started":"2023-02-13T10:33:01.225180Z","shell.execute_reply":"2023-02-13T10:33:01.639408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_test_ids = []\nrles = []\nfor n, id_ in enumerate(test_ids):\n    rle = list(prob_to_rles(preds_test_upsampled[n]))\n    rles.extend(rle)\n    new_test_ids.extend([id_] * len(rle))","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:33:01.641835Z","iopub.execute_input":"2023-02-13T10:33:01.642255Z","iopub.status.idle":"2023-02-13T10:33:05.223647Z","shell.execute_reply.started":"2023-02-13T10:33:01.642215Z","shell.execute_reply":"2023-02-13T10:33:05.222062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create submission DataFrame\nsub = pd.DataFrame()\nsub['ImageId'] = new_test_ids\nsub['EncodedPixels'] = pd.Series(rles).apply(lambda x: ' '.join(str(y) for y in x))\nsub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:33:05.225461Z","iopub.execute_input":"2023-02-13T10:33:05.226036Z","iopub.status.idle":"2023-02-13T10:33:05.414443Z","shell.execute_reply.started":"2023-02-13T10:33:05.225997Z","shell.execute_reply":"2023-02-13T10:33:05.413415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}