{"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":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n# Load the library\n---","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\n\nfrom tqdm.notebook import tqdm\nfrom multiprocessing import cpu_count\n\nimport imageio\nimport tifffile\nimport cv2\nimport math\nimport sys\nimport glob\nimport os\nimport joblib","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:42:48.498551Z","iopub.execute_input":"2022-07-15T21:42:48.499011Z","iopub.status.idle":"2022-07-15T21:42:48.754503Z","shell.execute_reply.started":"2022-07-15T21:42:48.498958Z","shell.execute_reply":"2022-07-15T21:42:48.753131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Install older version of Tensorflow to suppress 'Cleanup Called...' bug\n!pip install -q tensorflow==2.4.4","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:40:58.741839Z","iopub.execute_input":"2022-07-15T21:40:58.743235Z","iopub.status.idle":"2022-07-15T21:42:42.575906Z","shell.execute_reply.started":"2022-07-15T21:40:58.743186Z","shell.execute_reply":"2022-07-15T21:42:42.574378Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n# Loading Data\n---","metadata":{}},{"cell_type":"code","source":"# Load training DataFrame\ntrain_df = pd.read_csv('/kaggle/input/hubmap-organ-segmentation/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:42:51.628097Z","iopub.execute_input":"2022-07-15T21:42:51.628528Z","iopub.status.idle":"2022-07-15T21:42:51.768480Z","shell.execute_reply.started":"2022-07-15T21:42:51.628497Z","shell.execute_reply":"2022-07-15T21:42:51.767021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:43:45.893649Z","iopub.execute_input":"2022-07-15T21:43:45.894046Z","iopub.status.idle":"2022-07-15T21:43:45.920100Z","shell.execute_reply.started":"2022-07-15T21:43:45.894013Z","shell.execute_reply":"2022-07-15T21:43:45.918450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:43:26.600527Z","iopub.execute_input":"2022-07-15T21:43:26.600907Z","iopub.status.idle":"2022-07-15T21:43:26.631686Z","shell.execute_reply.started":"2022-07-15T21:43:26.600876Z","shell.execute_reply":"2022-07-15T21:43:26.630453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MAX_WIDTH = train['img_width'].max()\nMAX_HEIGHT = train['img_height'].max()\nN_CHANNELS = 3\nIMG_SIZE = 3000\nPATCH_SIZE = 300\nN_PATCHES_PER_IMAGE = (IMG_SIZE // PATCH_SIZE) ** 2\nN_SAMPLES = len(train)\nN_PATCHES = N_SAMPLES * N_PATCHES_PER_IMAGE\n\nprint(f'N_SAMPLES: {N_SAMPLES}, N_PATCHES: {N_PATCHES}, MAX_WIDTH: {MAX_WIDTH}, MAX_HEIGHT: {MAX_HEIGHT}')\nprint(f'IMG_SIZE: {IMG_SIZE}, PATCH_SIZE: {PATCH_SIZE}, N_PATCHES_PER_IMAGE: {N_PATCHES_PER_IMAGE}')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:44:19.151343Z","iopub.execute_input":"2022-07-15T21:44:19.151750Z","iopub.status.idle":"2022-07-15T21:44:19.161060Z","shell.execute_reply.started":"2022-07-15T21:44:19.151719Z","shell.execute_reply":"2022-07-15T21:44:19.159597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n# Make some of EDA\n---","metadata":{}},{"cell_type":"code","source":"train_df['sex'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:45:14.536517Z","iopub.execute_input":"2022-07-15T21:45:14.537132Z","iopub.status.idle":"2022-07-15T21:45:14.549771Z","shell.execute_reply.started":"2022-07-15T21:45:14.537067Z","shell.execute_reply":"2022-07-15T21:45:14.548324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pixel Size Distribution\ntrain_df['pixel_size'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:46:03.997172Z","iopub.execute_input":"2022-07-15T21:46:03.998398Z","iopub.status.idle":"2022-07-15T21:46:04.011493Z","shell.execute_reply.started":"2022-07-15T21:46:03.998348Z","shell.execute_reply":"2022-07-15T21:46:04.009806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,5))\ntrain_df['age'].plot(kind='hist',color='black')\nplt.title('Age Distribution', size=24)\nplt.xlim(0, 100)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:47:09.205437Z","iopub.execute_input":"2022-07-15T21:47:09.205826Z","iopub.status.idle":"2022-07-15T21:47:09.448916Z","shell.execute_reply.started":"2022-07-15T21:47:09.205795Z","shell.execute_reply":"2022-07-15T21:47:09.447368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\ntrain_df['age'].value_counts().plot(kind='pie', autopct='%1.1f%%', title='age Distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:47:55.020673Z","iopub.execute_input":"2022-07-15T21:47:55.021144Z","iopub.status.idle":"2022-07-15T21:47:55.547391Z","shell.execute_reply.started":"2022-07-15T21:47:55.021079Z","shell.execute_reply":"2022-07-15T21:47:55.545999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\ntrain_df['organ'].value_counts().plot(kind='pie', autopct='%1.1f%%', title='Organ Distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:47:33.721822Z","iopub.execute_input":"2022-07-15T21:47:33.722252Z","iopub.status.idle":"2022-07-15T21:47:33.898373Z","shell.execute_reply.started":"2022-07-15T21:47:33.722218Z","shell.execute_reply":"2022-07-15T21:47:33.896753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\ntrain_df['sex'].value_counts().plot(kind='pie', autopct='%1.1f%%', title='Sex Distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:48:26.834232Z","iopub.execute_input":"2022-07-15T21:48:26.834686Z","iopub.status.idle":"2022-07-15T21:48:26.982023Z","shell.execute_reply.started":"2022-07-15T21:48:26.834654Z","shell.execute_reply":"2022-07-15T21:48:26.980539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n# We can show sample of image\n---","metadata":{}},{"cell_type":"code","source":"# Resize all images to 3000x3000\ndef resize_tensor(tensor):\n    return cv2.resize(tensor, [IMG_SIZE, IMG_SIZE], interpolation=cv2.INTER_CUBIC).astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:49:14.979603Z","iopub.execute_input":"2022-07-15T21:49:14.980123Z","iopub.status.idle":"2022-07-15T21:49:14.987535Z","shell.execute_reply.started":"2022-07-15T21:49:14.980074Z","shell.execute_reply":"2022-07-15T21:49:14.985675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n### Get the mask\n---","metadata":{}},{"cell_type":"code","source":"def get_mask(image_id):\n    row = train.loc[train['id'] == image_id].squeeze()\n    h, w = row[['img_height', 'img_width']]\n    mask = np.zeros(shape=[h * w], dtype=np.uint8)\n    s = row['rle'].split()\n    starts, lengths = [ np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2]) ]\n    starts -= 1\n    ends = starts + lengths\n    for lo, hi in zip(starts, ends):\n        mask[lo : hi] = 1\n        \n    mask = mask.reshape([h, w]).T\n        \n    mask = resize_tensor(mask)\n    \n    mask = np.expand_dims(mask, axis=2)\n        \n    return mask\n#this referance fromhttps://www.kaggle.com/paulorzp/run-length-encode-and-decode","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:50:37.400839Z","iopub.execute_input":"2022-07-15T21:50:37.401275Z","iopub.status.idle":"2022-07-15T21:50:37.414746Z","shell.execute_reply.started":"2022-07-15T21:50:37.401227Z","shell.execute_reply":"2022-07-15T21:50:37.411079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n### Get the image\n---","metadata":{}},{"cell_type":"code","source":"# Reads an image and makes a negative to make tissue colored and background black\ndef get_image(image_id, negative):\n    image = tifffile.imread(f'/kaggle/input/hubmap-organ-segmentation/train_images/{image_id}.tiff')\n    if len(image.shape) == 5:\n        image = image.squeeze().transpose(1, 2, 0)\n        \n    # Reverse pixels to make tissue colored and background black\n    if negative:\n        image = image - image.min()\n        image = image / (image.max() - image.min())\n        image = image * 255\n        image = 255 - image.astype(np.uint8)\n        \n    image = resize_tensor(image)\n        \n    return image\n\nimage = get_image(train.loc[0, 'id'], True)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:53:39.925786Z","iopub.execute_input":"2022-07-15T21:53:39.926254Z","iopub.status.idle":"2022-07-15T21:53:40.287966Z","shell.execute_reply.started":"2022-07-15T21:53:39.926224Z","shell.execute_reply":"2022-07-15T21:53:40.286538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n### Show image and masks\n---","metadata":{}},{"cell_type":"code","source":"# Shows a batch of images\ndef show_image_and_masks(rows=4, cols=4):\n    # Figure\n    fig, axes = plt.subplots(nrows=rows, ncols=cols, figsize=(cols*8, rows*6))\n    # Unique Image Ids\n    image_ids = train['id'].unique()\n    \n    for r in range(rows):\n        image_id = image_ids[r]\n        df_row = train.loc[train['id'] == image_id].head(1).squeeze()\n        # Rad Image\n        image = get_image(image_id, False)\n        image_negative = get_image(image_id, True)\n        \n        # Image Original\n        axes[r, 0].imshow(image)\n        axes[r, 0].set_title(f'Image {image_id} Raw', size=16)\n        axes[r, 0].axis(False)\n        \n        # Image Negative Original\n        axes[r, 1].imshow(image_negative)\n        axes[r, 1].set_title(f'Image {image_id} Negative min: {image_negative.min()} max: {image_negative.max()}', size=16)\n        axes[r, 1].axis(False)\n        \n        # Mask\n        mask = get_mask(image_id)\n        axes[r, 2].imshow(mask)\n        axes[r, 2].set_title('Mask', size=16)\n        axes[r, 2].axis(False)\n        \n        # Image with Mask\n        axes[r, 3].imshow(image_negative)\n        axes[r, 3].imshow((mask * np.array([255, 0, 0])), alpha=0.50)\n        axes[r, 3].set_title('Image and Mask', size=16)\n        axes[r, 3].axis(False)\n            \n    # Adjust Vertical Space Between Subplots\n    fig.subplots_adjust(wspace=0.10)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:53:42.550196Z","iopub.execute_input":"2022-07-15T21:53:42.550889Z","iopub.status.idle":"2022-07-15T21:53:42.565182Z","shell.execute_reply.started":"2022-07-15T21:53:42.550852Z","shell.execute_reply":"2022-07-15T21:53:42.563837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_image_and_masks(rows=8)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:53:44.507806Z","iopub.execute_input":"2022-07-15T21:53:44.508326Z","iopub.status.idle":"2022-07-15T21:54:32.908303Z","shell.execute_reply.started":"2022-07-15T21:53:44.508265Z","shell.execute_reply":"2022-07-15T21:54:32.906770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = np.array(image)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:13:53.990134Z","iopub.execute_input":"2022-07-15T22:13:53.990656Z","iopub.status.idle":"2022-07-15T22:13:54.010526Z","shell.execute_reply.started":"2022-07-15T22:13:53.990613Z","shell.execute_reply":"2022-07-15T22:13:54.008039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:04:41.816097Z","iopub.execute_input":"2022-07-15T22:04:41.816639Z","iopub.status.idle":"2022-07-15T22:04:41.827061Z","shell.execute_reply.started":"2022-07-15T22:04:41.816591Z","shell.execute_reply":"2022-07-15T22:04:41.825378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_images[:1000]\ny_train = train_images[:1000]","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:21:22.874965Z","iopub.execute_input":"2022-07-15T22:21:22.875510Z","iopub.status.idle":"2022-07-15T22:21:22.888729Z","shell.execute_reply.started":"2022-07-15T22:21:22.875459Z","shell.execute_reply":"2022-07-15T22:21:22.887094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:21:23.519462Z","iopub.execute_input":"2022-07-15T22:21:23.520085Z","iopub.status.idle":"2022-07-15T22:21:23.535003Z","shell.execute_reply.started":"2022-07-15T22:21:23.520023Z","shell.execute_reply":"2022-07-15T22:21:23.533580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:21:23.965281Z","iopub.execute_input":"2022-07-15T22:21:23.965727Z","iopub.status.idle":"2022-07-15T22:21:23.975597Z","shell.execute_reply.started":"2022-07-15T22:21:23.965672Z","shell.execute_reply":"2022-07-15T22:21:23.973761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport tensorflow as tf\nimport cv2\nfrom tensorflow import keras\nfrom tensorflow.keras import layers, Input\nfrom keras.layers import InputLayer, MaxPooling2D, Flatten, Dense, Conv2D, Dropout\nfrom keras.losses import BinaryCrossentropy\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions, ResNet50\nfrom tensorflow.keras.optimizers import Adam, SGD","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:21:24.267155Z","iopub.execute_input":"2022-07-15T22:21:24.267689Z","iopub.status.idle":"2022-07-15T22:21:24.279601Z","shell.execute_reply.started":"2022-07-15T22:21:24.267644Z","shell.execute_reply":"2022-07-15T22:21:24.278175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef model(input_shape):\n\n    model = Sequential()\n    \n    model.add(Input(shape=input_shape))\n    \n    model.add(Conv2D(16, kernel_size=4, strides=(2, 2), padding=\"same\", activation=\"relu\", kernel_initializer=\"he_normal\"))\n    model.add(Conv2D(16, kernel_size=4, strides=(2, 2), padding=\"same\", activation=\"relu\", kernel_initializer=\"he_normal\"))\n    model.add(MaxPooling2D(pool_size=(2, 2), data_format=\"channels_last\", padding='same'))\n            \n    model.add(Conv2D(32, kernel_size=4, strides=(2, 2), padding=\"same\", activation=\"relu\", kernel_initializer=\"he_normal\"))\n    model.add(Conv2D(32, kernel_size=4, strides=(2, 2), padding=\"same\", activation=\"relu\", kernel_initializer=\"he_normal\"))\n    model.add(MaxPooling2D(pool_size=(2, 2), data_format=\"channels_last\", padding='same'))\n    \n    model.add(Conv2D(64, kernel_size=4, strides=(2, 2), padding=\"same\", activation=\"relu\", kernel_initializer=\"he_normal\"))\n    model.add(Conv2D(64, kernel_size=4, strides=(2, 2), padding=\"same\", activation=\"relu\", kernel_initializer=\"he_normal\"))\n    model.add(MaxPooling2D(pool_size=(2, 2), data_format=\"channels_last\", padding='same'))\n    \n    model.add(Flatten())\n    model.add(Dense(256, activation=\"relu\"))\n    model.add(Dense(128, activation=\"relu\"))\n\n    model.add(Dense(1, activation=\"sigmoid\"))    # Never use sigmoid for binary classification\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:25:41.584858Z","iopub.execute_input":"2022-07-15T22:25:41.585296Z","iopub.status.idle":"2022-07-15T22:25:41.601612Z","shell.execute_reply.started":"2022-07-15T22:25:41.585262Z","shell.execute_reply":"2022-07-15T22:25:41.600274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_HEIGHT = 3000\nIMG_WIDTH = 3000","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:25:42.050040Z","iopub.execute_input":"2022-07-15T22:25:42.050385Z","iopub.status.idle":"2022-07-15T22:25:42.057720Z","shell.execute_reply.started":"2022-07-15T22:25:42.050355Z","shell.execute_reply":"2022-07-15T22:25:42.056033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = model(input_shape = (IMG_HEIGHT, IMG_WIDTH, 4))","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:25:42.627235Z","iopub.execute_input":"2022-07-15T22:25:42.627695Z","iopub.status.idle":"2022-07-15T22:25:42.734104Z","shell.execute_reply.started":"2022-07-15T22:25:42.627663Z","shell.execute_reply":"2022-07-15T22:25:42.732951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:25:43.493260Z","iopub.execute_input":"2022-07-15T22:25:43.493655Z","iopub.status.idle":"2022-07-15T22:25:43.506168Z","shell.execute_reply.started":"2022-07-15T22:25:43.493625Z","shell.execute_reply":"2022-07-15T22:25:43.504521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = SGD(learning_rate=0.01)\nloss_fn = BinaryCrossentropy(from_logits=True)\nmodel.compile(optimizer=optimizer, loss=loss_fn, metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-15T22:25:44.003061Z","iopub.execute_input":"2022-07-15T22:25:44.003818Z","iopub.status.idle":"2022-07-15T22:25:44.018263Z","shell.execute_reply.started":"2022-07-15T22:25:44.003769Z","shell.execute_reply":"2022-07-15T22:25:44.016932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training the model\nhistory = model.fit(x=X_train, y=y_train, epochs=10, batch_size=10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}