{"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":"# download this for future use! \n# %%capture\n!pip --quiet install segmentation-models-pytorch\n!pip --quiet install -q torch_snippets pytorch_model_summary\n!pip --quiet install segmentation-models","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:19:29.182466Z","iopub.execute_input":"2022-08-19T07:19:29.183624Z","iopub.status.idle":"2022-08-19T07:20:10.388326Z","shell.execute_reply.started":"2022-08-19T07:19:29.183461Z","shell.execute_reply":"2022-08-19T07:20:10.386752Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import everything! \n\nfrom tqdm import tqdm \nfrom pathlib import Path\nfrom torch_snippets import *\nimport matplotlib.pyplot as plt\nimport torch.nn.functional as F \nimport torchvision.datasets as datasets \nfrom torch.utils.data import TensorDataset\nimport torchvision.transforms as transforms\nfrom torch.utils.data import Dataset, DataLoader\nfrom sklearn.model_selection import train_test_split\nimport torch, torchvision, os, glob, numpy as np, pandas as pd, torch.nn as nn\n\nfrom IPython import display \ndisplay.set_matplotlib_formats('svg')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-19T07:21:21.811085Z","iopub.execute_input":"2022-08-19T07:21:21.811667Z","iopub.status.idle":"2022-08-19T07:21:29.164562Z","shell.execute_reply.started":"2022-08-19T07:21:21.811618Z","shell.execute_reply":"2022-08-19T07:21:29.162469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## DataFrame","metadata":{}},{"cell_type":"code","source":"# Getting the dataframe \ndata_path = Path('/kaggle/input/hubmap-organ-segmentation')\n\nall_df = pd.read_csv(data_path / 'train.csv')\ntrain_df, test_df = train_test_split(all_df, test_size=0.20, random_state=42, stratify=all_df['organ'])","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:21:33.727570Z","iopub.execute_input":"2022-08-19T07:21:33.728680Z","iopub.status.idle":"2022-08-19T07:21:34.049371Z","shell.execute_reply.started":"2022-08-19T07:21:33.728640Z","shell.execute_reply":"2022-08-19T07:21:34.048423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Explore the data of images and train_annotations\n\ntrain_images = sorted(glob.glob('../input/hubmap-organ-segmentation/train_images/*'))\ntrain_annotations = sorted(glob.glob('../input/hubmap-organ-segmentation/train_annotations/*'))\n\n\ntrain_df = pd.DataFrame.from_dict({'id' : [os.path.splitext(os.path.basename(i))[0] for i in train_images],\n                                   'Train Images Name' : [os.path.basename(i) for i in train_images], \n                                   'Train Annotations Name' : [os.path.basename(i) for i in train_annotations],\n                                  'Train Images Path' : train_images, \n                                   'Train Annotations Path' : train_annotations})\n\ntrain_df['id'] = train_df['id'].astype('int64')\ntrain_csv = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')\ntrain_csv = pd.merge(train_df, train_csv, on='id')","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:24:21.063307Z","iopub.execute_input":"2022-08-19T07:24:21.063747Z","iopub.status.idle":"2022-08-19T07:24:21.233503Z","shell.execute_reply.started":"2022-08-19T07:24:21.063710Z","shell.execute_reply":"2022-08-19T07:24:21.232511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['organ'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:24:23.584974Z","iopub.execute_input":"2022-08-19T07:24:23.585573Z","iopub.status.idle":"2022-08-19T07:24:23.600089Z","shell.execute_reply.started":"2022-08-19T07:24:23.585514Z","shell.execute_reply":"2022-08-19T07:24:23.598914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This function helps to visualize the masked images\ndef rle2mask(rle, size):\n    '''\n    Helps to visualize the masked images using annotations. \n    '''\n    rle = np.array(list(map(int, rle.split())))\n    label = np.zeros((size*size), dtype=np.uint8)\n    for start, end in zip(rle[::2], rle[1::2]):\n        label[start:start+end] = 1\n    return label.reshape(size, size).T","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:24:25.054775Z","iopub.execute_input":"2022-08-19T07:24:25.055862Z","iopub.status.idle":"2022-08-19T07:24:25.062867Z","shell.execute_reply.started":"2022-08-19T07:24:25.055815Z","shell.execute_reply":"2022-08-19T07:24:25.061875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Default height, & weight \nIMAGE_HEIGHT = 224 \nIMAGE_WIDTH = 224\n\n# This helps to do some pre processing for input image \ndef image_converter(img, masked): \n    if masked == 'True': \n        img = cv2.resize(img, (IMAGE_HEIGHT, IMAGE_WIDTH))\n        img = np.expand_dims(img, 2)\n        return img \n    elif masked == 'False': \n        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n        img = cv2.resize(img, (IMAGE_HEIGHT, IMAGE_WIDTH))\n        img = np.expand_dims(img, 2)\n        return img ","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:24:27.274916Z","iopub.execute_input":"2022-08-19T07:24:27.275931Z","iopub.status.idle":"2022-08-19T07:24:27.282804Z","shell.execute_reply.started":"2022-08-19T07:24:27.275883Z","shell.execute_reply":"2022-08-19T07:24:27.281642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"https://www.kaggle.com/code/b11gden/hubmap-hpa-simple-pytorch-baseline","metadata":{}},{"cell_type":"code","source":"# storing the input(X) image and output(y) annotation in variables \nX = np.zeros(( 351, IMAGE_HEIGHT, IMAGE_WIDTH, 1), dtype = np.float32)  \ny = np.zeros(( 351, IMAGE_HEIGHT, IMAGE_WIDTH, 1), dtype = np.float32)\n\n\n# Path to train images \nPATH_IMAGES_= '../input/hubmap-organ-segmentation/train_images'\n\n# This for loop helps to store the image arrays in numpy matrices \nfrom tqdm import tqdm \nfor file in tqdm(range(len(train_csv.rle))): \n    img = cv2.imread(train_csv['Train Images Path'][file])\n    img = image_converter(img, 'False')\n    X[file] = img \n    \n    mask = cv2.imread(train_csv['Train Annotations Path'][file])\n    mask =  rle2mask(train_csv.iloc[file].rle, train_csv.iloc[file].img_width)\n    mask = image_converter(mask, 'True')\n    y[file] = mask","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:24:32.387912Z","iopub.execute_input":"2022-08-19T07:24:32.388956Z","iopub.status.idle":"2022-08-19T07:27:10.642545Z","shell.execute_reply.started":"2022-08-19T07:24:32.388907Z","shell.execute_reply":"2022-08-19T07:27:10.641508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Splitting the dataset \nxtrain, xtest, ytrain, ytest = train_test_split(X, y, test_size = 0.2)\n\n# Putting in tensor dataset\ntrain_dataset = TensorDataset(torch.tensor(xtrain),torch.tensor(ytrain)) \ntest_dataset = TensorDataset(torch.tensor(xtest), torch.tensor(ytest))\n\n# Putting inside the DataLoader \ntrain_dataloader = DataLoader(train_dataset, batch_size = 8, shuffle = True)\ntest_dataloader = DataLoader(test_dataset, batch_size = 8, shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:27:18.485015Z","iopub.execute_input":"2022-08-19T07:27:18.485381Z","iopub.status.idle":"2022-08-19T07:27:18.691017Z","shell.execute_reply.started":"2022-08-19T07:27:18.485353Z","shell.execute_reply":"2022-08-19T07:27:18.690045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Segmentation models imports! \nimport tensorflow as tf\nimport segmentation_models as sm\nfrom segmentation_models.metrics import iou_score\nfrom segmentation_models import Unet, get_preprocessing \nfrom segmentation_models.losses import bce_jaccard_loss \nfrom tensorflow.keras.losses import binary_crossentropy\nsm.set_framework('tf.keras')\n\n# Setting the device \ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n# Import the model layers \nfrom keras.layers import Input, Conv2D\nfrom keras.models import Model\n\n# Building the Model! \nbase_model = Unet('resnet34', encoder_weights='imagenet')\ninp = Input(shape=(IMAGE_HEIGHT, IMAGE_WIDTH, 1)) \nl1 = Conv2D(3, (1, 1))(inp) \nout = base_model(l1)\n\n# Base Model \nmodel = Model(inp, out, name=base_model.name)\n\n# Dice function \ndef dice(ytrue, ypred): \n    numerator = 2 * tf.reduce_sum(ytrue * ypred)\n    denominator = tf.reduce_sum(ytrue + ypred)\n    return numerator / (denominator + tf.keras.backend.epsilon())\n\n# Loss function \ndef loss(y_true, y_pred): \n    return binary_crossentropy(y_pred, y_true) - tf.math.log(dice(y_true, y_pred) + tf.keras.backend.epsilon())\n\n# Compile the model \nmodel.compile('Adam', loss = loss, metrics = [dice])","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:27:35.133659Z","iopub.execute_input":"2022-08-19T07:27:35.136833Z","iopub.status.idle":"2022-08-19T07:27:54.051586Z","shell.execute_reply.started":"2022-08-19T07:27:35.136784Z","shell.execute_reply":"2022-08-19T07:27:54.050468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's run the model! \nmodel.fit(x = xtrain, y = ytrain, batch_size = 8, epochs = 500, validation_data = (xtest, ytest))","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-08-19T07:27:54.053513Z","iopub.execute_input":"2022-08-19T07:27:54.053901Z","iopub.status.idle":"2022-08-19T07:51:20.494351Z","shell.execute_reply.started":"2022-08-19T07:27:54.053863Z","shell.execute_reply":"2022-08-19T07:51:20.493118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# let's do prediction! \npreds_train = model.predict(xtrain, verbose = 1)\npreds_val = model.predict(xtest, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:51:51.439100Z","iopub.execute_input":"2022-08-19T07:51:51.440123Z","iopub.status.idle":"2022-08-19T07:51:53.724478Z","shell.execute_reply.started":"2022-08-19T07:51:51.440082Z","shell.execute_reply":"2022-08-19T07:51:53.723559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"https://stackoverflow.com/questions/68044429/how-to-visulize-image-using-json-file-in-python","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:31:53.310205Z","iopub.execute_input":"2022-08-10T12:31:53.310611Z","iopub.status.idle":"2022-08-10T12:31:53.364870Z","shell.execute_reply.started":"2022-08-10T12:31:53.310578Z","shell.execute_reply":"2022-08-10T12:31:53.363885Z"}}},{"cell_type":"code","source":"def show_everything(id_): \n    '''\n    This function helps to visualize the input, mask and predicted images! \n    '''\n    a = xtest[id_][:, :, 0]\n    b = ytest[id_][:, :, 0]\n    c = np.squeeze(preds_val[id_][:, :, 0])\n    \n    plt.style.use('dark_background')\n    f, ax = plt.subplots(1, 3, figsize = (15, 8))\n    \n    ax[0].imshow(a)\n    ax[1].imshow(b)\n    ax[2].imshow(c)\n    \n    ax[0].set_title('Original Image', color = 'pink',fontweight='bold')\n    ax[1].set_title('Original Masked Image', color = 'green', fontweight='bold')\n    ax[2].set_title('Predicted Mased Image 😍', color = 'skyblue', fontweight='bold')\n    \n    ax[0].axis('off')\n    ax[1].axis('off')\n   \n    \nshow_everything(48)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:52:15.760559Z","iopub.execute_input":"2022-08-19T07:52:15.761472Z","iopub.status.idle":"2022-08-19T07:52:16.074198Z","shell.execute_reply.started":"2022-08-19T07:52:15.761436Z","shell.execute_reply":"2022-08-19T07:52:16.073117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_final = cv2.imread('../input/hubmap-organ-segmentation/test_images/10078.tiff', 0)\ntest_final = cv2.resize(test_final, (224, 224))\n\n\ntest_final = np.expand_dims(test_final, axis = 0)\n\npredictions = model.predict(test_final)  \n# type(test_final)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T09:33:44.985868Z","iopub.execute_input":"2022-08-19T09:33:44.986955Z","iopub.status.idle":"2022-08-19T09:33:45.881950Z","shell.execute_reply.started":"2022-08-19T09:33:44.986909Z","shell.execute_reply":"2022-08-19T09:33:45.880890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(predictions.squeeze())","metadata":{"execution":{"iopub.status.busy":"2022-08-19T09:36:13.281157Z","iopub.execute_input":"2022-08-19T09:36:13.281856Z","iopub.status.idle":"2022-08-19T09:36:13.483716Z","shell.execute_reply.started":"2022-08-19T09:36:13.281818Z","shell.execute_reply":"2022-08-19T09:36:13.482616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode_less_memory(img):\n    #the image should be transposed\n    pixels = img.T.flatten()\n    \n    # This simplified method requires first and last pixel to be zero\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] -= runs[::2]\n    \n    return ' '.join(str(x) for x in runs)\n\nTHRESHOLD = 0.390\nmask_binary = (predictions > THRESHOLD).astype(np.int8)\nfinal_predictions = rle_encode_less_memory(mask_binary)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T09:47:14.956703Z","iopub.execute_input":"2022-08-19T09:47:14.957683Z","iopub.status.idle":"2022-08-19T09:47:14.966239Z","shell.execute_reply.started":"2022-08-19T09:47:14.957636Z","shell.execute_reply":"2022-08-19T09:47:14.965116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"di = {'id': [10078], 'rle': final_predictions}\ntest_df = pd.DataFrame(di)\ntest_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T09:50:19.974399Z","iopub.execute_input":"2022-08-19T09:50:19.975060Z","iopub.status.idle":"2022-08-19T09:50:19.986879Z","shell.execute_reply.started":"2022-08-19T09:50:19.975024Z","shell.execute_reply":"2022-08-19T09:50:19.985780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T09:50:27.852086Z","iopub.execute_input":"2022-08-19T09:50:27.852679Z","iopub.status.idle":"2022-08-19T09:50:27.874145Z","shell.execute_reply.started":"2022-08-19T09:50:27.852637Z","shell.execute_reply":"2022-08-19T09:50:27.873214Z"},"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":"","metadata":{"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}