{"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":"2021-11-19T04:04:22.298114Z","iopub.execute_input":"2021-11-19T04:04:22.298941Z","iopub.status.idle":"2021-11-19T04:04:23.908910Z","shell.execute_reply.started":"2021-11-19T04:04:22.298892Z","shell.execute_reply":"2021-11-19T04:04:23.907029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (\"\\n...IMPORTS STARTING...\\n\")\nprint (\"\\n\\tVERSION INFORMATION\")\n#Machine Learning and Data Science imports\nimport tensorflow as tf; print (f\"\\t\\t-TENSORFLOW VERSION:{tf.__version__}\");\nimport tensorflow_addons as tfa; print(f\"\\t\\t-TENSORFLOW ADDONS VERSION:{tfa.__version__}\");\nimport pandas as pd;pd.options.mode.chained_assignment = None;\nimport numpy as np; print (f\"\\t\\t-NUMPY VERSION:{np.__version__}\");\nimport sklearn; print (f\"\\t\\t-SKLEARN VERSION:{sklearn.__version__}\");\nfrom sklearn.preprocessing import RobustScaler,PolynomialFeatures\nfrom sklearn.model_selection import GroupKFold;\n\n#Build In Imports\nfrom kaggle_datasets import KaggleDatasets\nfrom collections import Counter\nfrom datetime import datetime\nfrom glob import glob\nimport warnings\nimport requests\nimport imageio\nimport IPython\nimport sklearn\nimport urllib\nimport zipfile\nimport pickle\nimport random\nimport shutil\nimport string\nimport math\nimport time\nimport gzip\nimport ast\nimport sys\nimport io\nimport os\nimport gc\nimport re\n\n# Visualization Imports\nfrom matplotlib.colors import ListedColormap\nimport matplotlib.patches as patches\nimport plotly.graph_objects as go\nimport matplotlib.pyplot as plt\nfrom tqdm.notebook import tqdm; tqdm.pandas();\nimport plotly.express as px\nimport seaborn as sns\nfrom PIL import Image,ImageEnhance\nimport  matplotlib; print(f\"\\t\\t-MATPLOTLIB VERSION:{matplotlib.__version__}\");\nimport plotly\nimport PIL\nimport cv2\n\ndef seed_it_all(seed=7):\n    \"\"\"Attempt to be Reproducible\"\"\"\n    os.environ['PYTHONHASHSEED']=str(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n    tf.random.set_seed(seed)\n    \nprint (\"\\n\\n... IMPORTS COMPLETE...\\n\")\nprint (\"\\n... SEEDING FOR DETERMINISTIC BEHAVIOUR...\\n\")\nseed_it_all()\n","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:23.910690Z","iopub.execute_input":"2021-11-19T04:04:23.910971Z","iopub.status.idle":"2021-11-19T04:04:23.930577Z","shell.execute_reply.started":"2021-11-19T04:04:23.910927Z","shell.execute_reply":"2021-11-19T04:04:23.929609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#The name you gave to the TPU to use\nTPU_WORKER = 'my-tpu-name'\n\n#or you can also specify the grpc path directly\n#TPU_WORKER = 'grpc://xxx.xxx.xxx.xxx:8470'\n\n#The zone you chose when you created the TPU to use on GCP.\nZONE = 'us-east1-b'\n\n#The name of the GCP project where you created the TPU to use on GCP.\nPROJECT = 'my-tpu-project'\n\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver(tpu=TPU_WORKER,zone=ZONE, project=PROJECT)","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:23.931923Z","iopub.execute_input":"2021-11-19T04:04:23.932680Z","iopub.status.idle":"2021-11-19T04:04:23.942289Z","shell.execute_reply.started":"2021-11-19T04:04:23.932637Z","shell.execute_reply":"2021-11-19T04:04:23.941561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (f\"\\n... ACCELERATOR SETUP STARTING...\\n\")\n\n#Detect hardware, return appropriate distribution strategy\ntry:\n    #TPU detection. No parameters necessary if TPU_NAME environment variable is set. On Kaggle this is always the case.\n    TPU = tf.distribute.cluster_resolver.TPUClusterResolver()\nexcept ValueError:\n    TPU = None\n    \nif TPU:\n    print (f\"\\n... RUNNING ON TPU - {TPU.master()}...\")\n    tf.config.experimental_connect_to_cluster(TPU)\n    tf.tpu.experimental.initialize_tpu_system(TPU)\n    strategy = tf.distribute.experimental.TPUStrategy(TPU)\nelse:\n        print(f\"\\n... RUNNING ON CPU/GPU...\")\n               \n        #Yield the default distribution strategy in Tensorflow\n        #---> Works on CPU and single GPU.\n        strategy = tf.distribute.get_strategy()\n               \n#What is Replica?\n# --->A single Cloud TPU device consists of FOUR chips, each of which has TWO TPU cores.\n# --->Therefore, for efficient utilization of Cloud TPU,a program should make use of each of the EIGHT(4×2) cores.\n# --->Each replica is essentially a copy of the training graph that is run on each core and\n# --->trains a mini-batch containing 1/8th of the overall batch size\nN_REPLICAS = strategy.num_replicas_in_sync\n            \nprint (f\"...#OF REPLICAS:{N_REPLICAS}...\\n\")\n\nprint (f\"\\n... ACCELERATOR SETUP COMPLETED...\\n\")\n     \n    ","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:23.944454Z","iopub.execute_input":"2021-11-19T04:04:23.944656Z","iopub.status.idle":"2021-11-19T04:04:23.960826Z","shell.execute_reply.started":"2021-11-19T04:04:23.944631Z","shell.execute_reply":"2021-11-19T04:04:23.960100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (\"\\n... DATA ACCESS SETUP STARTED...\\n\")\n\nif TPU:\n    #Google Cloud Dataset path to training and validation images\n    DATA_DIR = KaggleDatasets().get_gcs_path('sartorius-cell-instance-segmentation')\n    save_locally = tf.saved_model.SaveOptions(experimental_io_device='/job: localhost')\nelse:\n    #Local path to training and validation images\n    DATA_DIR = \"/kaggle/input/sartorius-cell-instance-segmentation\"\n    save_locally = None\n\nprint (f\"\\n... DATA DIRECTORY PATH IS:\\n\\t---> {DATA_DIR}\")\n\nprint (f\"\\n... IMMEDIATE CONTENTS OF DATA DIRECTORY IS:\")\nfor file in tf.io.gfile.glob(os.path.join(DATA_DIR,\"*\")): print (f\"\\--->{file}\")\n    \nprint (\"\\n\\n... DATA ACCESS SETUP COMPLETED...\\n\")","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:23.962172Z","iopub.execute_input":"2021-11-19T04:04:23.962376Z","iopub.status.idle":"2021-11-19T04:04:23.978929Z","shell.execute_reply.started":"2021-11-19T04:04:23.962350Z","shell.execute_reply":"2021-11-19T04:04:23.978052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (f\"\\n...XLA OPTIMISATIONS STARTING...\\n\")\n\nprint (f\"\\n...CONFIGURE JIT (JUST IN TIME) COMPILATION...\\n\")\n#enable XLA optimizations (10% speedup when using @tf.function calls)\ntf.config.optimizer.set_jit(True)\n\nprint (f\"\\n...XLA OPTIMIZATIONS completed...\\n\")","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:23.980173Z","iopub.execute_input":"2021-11-19T04:04:23.980493Z","iopub.status.idle":"2021-11-19T04:04:23.990212Z","shell.execute_reply.started":"2021-11-19T04:04:23.980440Z","shell.execute_reply":"2021-11-19T04:04:23.989400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"\\n... BASIC DATA SETUP STARTING ...\\n\\n\")\n\nprint(\"\\n... SET PATH INFORMATION ..\\n\")\nSEG_DIR = \"/kaggle/input/sartorius-segmentation-train-mask-dataset-npz\"\nLC_DIR = os.path.join(DATA_DIR, \"LIVECell_dataset_2021\")\nLC_ANN_DIR = os.path.join(LC_DIR, \"annotations\")\nLC_IMG_DIR = os.path.join(LC_DIR, \"images\")\nTRAIN_DIR = os.path.join(DATA_DIR, \"train\")\nTEST_DIR = os.path.join(DATA_DIR, \"test\")\nSEMI_DIR = os.path.join(DATA_DIR, \"train_semi_supervised\")\n\nprint(\"\\n... TRAIN DATAFRAME ...\\n\")\n\n# FIX THE TRAIN DATAFRAME (GROUP THE RLEs TOGETHER)\nTRAIN_CSV = os.path.join(DATA_DIR, \"train.csv\")\ntrain_df = pd.read_csv(TRAIN_CSV)\ndisplay(train_df)\n\nprint(\"\\n... SS DATAFRAME ..\\n\")\nSS_CSV = os.path.join(DATA_DIR, \"sample_submission.csv\")\nss_df = pd.read_csv(SS_CSV)\nss_df[\"img_path\"] = ss_df[\"id\"].apply(lambda x: os.path.join(TEST_DIR, x+\".png\")) # Capture Image Path As Well\ndisplay(ss_df)\n\nCELL_TYPES = list(train_df.cell_type.unique())\nFIRST_SHSY5Y_IDX = 0\nFIRST_ASTRO_IDX  = 1\nFIRST_CORT_IDX   = 2\n\n# This is required for plotting so that the smaller distributions get plotted on top\nARB_SORT_MAP = {\"astro\":0, \"shsy5y\":1, \"cort\":2}\n\nprint(\"\\n... CELL TYPES ..\")\nfor x in CELL_TYPES: print(f\"\\t--> {x}\")\n    \nprint(\"\\n\\n... BASIC DATA SETUP FINISHING ...\\n\")","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:23.991349Z","iopub.execute_input":"2021-11-19T04:04:23.991642Z","iopub.status.idle":"2021-11-19T04:04:24.413463Z","shell.execute_reply.started":"2021-11-19T04:04:23.991594Z","shell.execute_reply":"2021-11-19T04:04:24.412651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\n# modified from: https://www.kaggle.com/inversion/run-length-decoding-quick-start\ndef rle_decode(mask_rle, shape, color=1):\n    \"\"\" TBD\n    \n    Args:\n        mask_rle (str): run-length as string formated (start length)\n        shape (tuple of ints): (height,width) of array to return \n    \n    Returns: \n        Mask (np.array)\n            - 1 indicating mask\n            - 0 indicating background\n\n    \"\"\"\n    # Split the string by space, then convert it into a integer array\n    s = np.array(mask_rle.split(), dtype=int)\n\n    # Every even value is the start, every odd value is the \"run\" length\n    starts = s[0::2] - 1\n    lengths = s[1::2]\n    ends = starts + lengths\n\n    # The image image is actually flattened since RLE is a 1D \"run\"\n    if len(shape)==3:\n        h, w, d = shape\n        img = np.zeros((h * w, d), dtype=np.float32)\n    else:\n        h, w = shape\n        img = np.zeros((h * w,), dtype=np.float32)\n\n    # The color here is actually just any integer you want!\n    for lo, hi in zip(starts, ends):\n        img[lo : hi] = color\n        \n    # Don't forget to change the image back to the original shape\n    return img.reshape(shape)\n\n# https://www.kaggle.com/namgalielei/which-reshape-is-used-in-rle\ndef rle_decode_top_to_bot_first(mask_rle, shape):\n    \"\"\" TBD\n    \n    Args:\n        mask_rle (str): run-length as string formated (start length)\n        shape (tuple of ints): (height,width) of array to return \n    \n    Returns:\n        Mask (np.array)\n            - 1 indicating mask\n            - 0 indicating background\n\n    \"\"\"\n    s = mask_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    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape((shape[1], shape[0]), order='F').T  # Reshape from top -> bottom first\n\n# ref.: https://www.kaggle.com/stainsby/fast-tested-rle\ndef rle_encode(img):\n    \"\"\" TBD\n    \n    Args:\n        img (np.array): \n            - 1 indicating mask\n            - 0 indicating background\n    \n    Returns: \n        run length as string formated\n    \"\"\"\n    \n    pixels = img.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n\n\ndef flatten_l_o_l(nested_list):\n    \"\"\" Flatten a list of lists \"\"\"\n    return [item for sublist in nested_list for item in sublist]\n\n\ndef load_json_to_dict(json_path):\n    \"\"\" tbd \"\"\"\n    with open(json_path) as json_file:\n        data = json.load(json_file)\n    return data\n\n# https://github.com/PyImageSearch/imutils/blob/master/imutils/convenience.py\ndef grab_contours(cnts):\n    \"\"\" TBD \"\"\"\n    \n    # if the length the contours tuple returned by cv2.findContours\n    # is '2' then we are using either OpenCV v2.4, v4-beta, or\n    # v4-official\n    if len(cnts) == 2:\n        cnts = cnts[0]\n\n    # if the length of the contours tuple is '3' then we are using\n    # either OpenCV v3, v4-pre, or v4-alpha\n    elif len(cnts) == 3:\n        cnts = cnts[1]\n\n    # otherwise OpenCV has changed their cv2.findContours return\n    # signature yet again and I have no idea WTH is going on\n    else:\n        raise Exception((\"Contours tuple must have length 2 or 3, \"\n            \"otherwise OpenCV changed their cv2.findContours return \"\n            \"signature yet again. Refer to OpenCV's documentation \"\n            \"in that case\"))\n\n    # return the actual contours array\n    return cnts\n\ndef get_contour_bbox(msk):\n    \"\"\" Function to return the bounding box (tl, br) for a given mask \"\"\"\n    \n    # Get contour(s) --> There should be only one\n    cnts = cv2.findContours(msk.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n\n    contour = grab_contours(cnts)\n    \n    if len(contour)==0:\n        return None\n    else:\n        contour = contour[0]\n    \n    # Get extreme coordinates\n    tl = (tuple(contour[contour[:, :, 0].argmin()][0])[0], \n          tuple(contour[contour[:, :, 1].argmin()][0])[1])\n    br = (tuple(contour[contour[:, :, 0].argmax()][0])[0], \n          tuple(contour[contour[:, :, 1].argmax()][0])[1])\n    return tl, br\n\ndef tf_load_png(img_path):\n    return tf.image.decode_png(tf.io.read_file(img_path), channels=3)\n\ndef get_img_and_mask(img_path, annotation, width, height, mask_only=False, rle_fn=rle_decode):\n    \"\"\" Capture the relevant image array as well as the image mask \"\"\"\n    img_mask = np.zeros((height, width), dtype=np.uint8)\n    for i, annot in enumerate(annotation): \n        img_mask = np.where(rle_fn(annot, (height, width))!=0, i, img_mask)\n    \n    # Early Exit\n    if mask_only:\n        return img_mask\n    \n    # Else Return images\n    img = tf_load_png(img_path)[..., 0]\n    return img, img_mask\n\ndef plot_img_and_mask(img, mask, bboxes=None, invert_img=True, boost_contrast=True):\n    \"\"\" Function to take an image and the corresponding mask and plot\n    \n    Args:\n        img (np.arr): 1 channel np arr representing the image of cellular structures\n        mask (np.arr): 1 channel np arr representing the instance masks (incrementing by one)\n        bboxes (list of tuples, optional): (tl, br) coordinates of enclosing bboxes\n        invert_img (bool, optional): Whether or not to invert the base image\n        boost_contrast (bool, optional): Whether or not to boost contrast of the base image\n        \n    Returns:\n        None; Plots the two arrays and overlays them to create a merged image\n    \"\"\"\n    plt.figure(figsize=(20,10))\n    \n    plt.subplot(1,3,1)\n    _img = np.tile(np.expand_dims(img, axis=-1), 3)\n    \n    # Flip black-->white ... white-->black\n    if invert_img:\n        _img = _img.max()-_img\n    \n    if boost_contrast:\n        _img = np.asarray(ImageEnhance.Contrast(Image.fromarray(_img)).enhance(16))\n    \n    if bboxes:\n        for i, bbox in enumerate(bboxes):\n            mask = cv2.rectangle(mask, bbox[0], bbox[1], (i+1, 0, 0), thickness=2)\n    \n    plt.imshow(_img)\n    plt.axis(False)\n    plt.title(\"Cell Image\", fontweight=\"bold\")\n    \n    plt.subplot(1,3,2)\n    _mask = np.zeros_like(_img)\n    _mask[..., 0] = mask\n    plt.imshow(mask, cmap=\"inferno\")\n    plt.axis(False)\n    plt.title(\"Instance Segmentation Mask\", fontweight=\"bold\")\n    \n    merged = cv2.addWeighted(_img, 0.75, np.clip(_mask, 0, 1)*255, 0.25, 0.0,)\n    plt.subplot(1,3,3)\n    plt.imshow(merged)\n    plt.axis(False)\n    plt.title(\"Cell Image w/ Instance Segmentation Mask Overlay\", fontweight=\"bold\")\n    \n    plt.tight_layout()\n    plt.show()\n    \ndef pd_get_bboxes(row):\n    \"\"\" Get all bboxes for a given row/cell-image \"\"\"\n    mask = get_img_and_mask(row.img_path, row.annotation, row.width, row.height, mask_only=True)\n    return [get_contour_bbox(np.where(mask==i, 1, 0).astype(np.uint8)) for i in range(1, mask.max()+1)]\n\ndef get_bbox_stats(bbox_list, style=\"area\"): \n    \"\"\" TBD \n    \n    Args:\n        bbox_list(): TBD\n        style (str, optional): TBD\n    Returns:\n        TBD\n        \"\"\"\n    bbox_stats = []\n    for box in bbox_list:\n        try:\n            if style==\"area\":\n                bbox_stats.append(float((box[1][0]-box[0][0])*(box[1][1]-box[0][1])))\n            elif style==\"width\":\n                bbox_stats.append(float(box[1][0]-box[0][0]))\n            else:\n                bbox_stats.append(float(box[1][1]-box[0][1]))\n        except:\n            bbox_stats.append(0.0)\n    return bbox_stats","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:24.415088Z","iopub.execute_input":"2021-11-19T04:04:24.415545Z","iopub.status.idle":"2021-11-19T04:04:24.456126Z","shell.execute_reply.started":"2021-11-19T04:04:24.415492Z","shell.execute_reply":"2021-11-19T04:04:24.455186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Aggregate under training \ntrain_df[\"img_path\"] = train_df[\"id\"].apply(lambda x: os.path.join(TRAIN_DIR, x+\".png\")) # Capture Image Path As Well\ntmp_df = train_df.drop_duplicates(subset=[\"id\", \"img_path\"]).reset_index(drop=True)\ntmp_df[\"annotation\"] = train_df.groupby(\"id\")[\"annotation\"].agg(list).reset_index(drop=True)\ntrain_df = tmp_df.copy()\ntrain_df[\"seg_path\"] = train_df.id.apply(lambda x: os.path.join(SEG_DIR, f\"{x}.npz\"))\ndisplay(train_df)\n    ","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:24.457363Z","iopub.execute_input":"2021-11-19T04:04:24.460037Z","iopub.status.idle":"2021-11-19T04:04:24.753327Z","shell.execute_reply.started":"2021-11-19T04:04:24.460002Z","shell.execute_reply":"2021-11-19T04:04:24.752489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(2, 70, 8):\n    print(f\"\\n\\n\\n\\n... RELEVANT DATAFRAME ROW - INDEX={i} ...\\n\")\n    display(train_df.iloc[i:i+1])\n    img, msk = get_img_and_mask(**train_df[[\"img_path\", \"annotation\", \"width\", \"height\"]].iloc[i].to_dict())\n    plot_img_and_mask(img, msk)\n","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:24.756629Z","iopub.execute_input":"2021-11-19T04:04:24.758183Z","iopub.status.idle":"2021-11-19T04:04:31.649732Z","shell.execute_reply.started":"2021-11-19T04:04:24.758131Z","shell.execute_reply":"2021-11-19T04:04:31.647770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x1 = rle_decode_top_to_bot_first(train_df.iloc[0].annotation[0], (train_df.iloc[0].height, train_df.iloc[0].width))\nx2 = rle_decode(train_df.iloc[0].annotation[0], (train_df.iloc[0].height, train_df.iloc[0].width))\n\nplt.figure(figsize=(15,6))\nplt.subplot(1,2,1)\nplt.imshow(x1, cmap=\"inferno\")\nplt.axis(False)\nplt.title(\"NamGalielei RLE Decode Function\", fontweight=\"bold\")\nplt.subplot(1,2,2)\nplt.imshow(x2, cmap=\"inferno\")\nplt.axis(False)\nplt.title(\"Original RLE Decode Function\", fontweight=\"bold\")\nplt.tight_layout()\nplt.show()\nprint(f\"\\n... THERE ARE {(x1!=x2).sum()} PIXELS IN DISAGREEMENT WHEN USING THE TWO FUNCTIONS ON A SINGLE CELL...\\n\")\n\nimg1, msk1 = get_img_and_mask(**train_df[[\"img_path\", \"annotation\", \"width\", \"height\"]].iloc[0].to_dict())\nimg2, msk2 = get_img_and_mask(**train_df[[\"img_path\", \"annotation\", \"width\", \"height\"]].iloc[0].to_dict(), rle_fn=rle_decode_top_to_bot_first)\n\nplot_img_and_mask(img1, msk1)\nplot_img_and_mask(img2, msk2)\n\nprint(f\"\\n... THERE ARE {(msk2!=msk1).sum()} PIXELS IN DISAGREEMENT WHEN USING THE TWO FUNCTIONS ON ALL CELL MASK ...\\n\")\n    ","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:31.651017Z","iopub.execute_input":"2021-11-19T04:04:31.651246Z","iopub.status.idle":"2021-11-19T04:04:33.679588Z","shell.execute_reply.started":"2021-11-19T04:04:31.651215Z","shell.execute_reply":"2021-11-19T04:04:33.678782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"\\n\\n... WIDTH VALUE COUNTS ...\")\nfor k,v in train_df.width.value_counts().items():\n    print(f\"\\t--> There are {v} images with WIDTH={k}\")\n\nprint(\"\\n\\n... HEIGHT VALUE COUNTS ...\")\nfor k,v in train_df.height.value_counts().items():\n    print(f\"\\t--> There are {v} images with HEIGHT={k}\")\n\nprint(\"\\n\\n... AREA COUNTS ...\")\nfor k,v in (train_df.width*train_df.height).value_counts().items():\n    print(f\"\\t--> There are {v} images with AREA={k}\")\n\nprint(\"\\n\\n... NOTE: ALL THE IMAGES ARE THE SAME SIZE ...\\n\")\n\nprint(\"\\n\\n... PLATE TIME VALUE COUNTS ...\")\nfor k,v in train_df.plate_time.value_counts().items():\n    print(f\"\\t--> There are {v} images with PLATE_TIME={k}\")\nfig = px.histogram(train_df, x=\"plate_time\", color=\"cell_type\", title=\"<b>Plate Time Histogram</b>\")\nfig.show()\n\nprint(\"\\n\\n... SAMPLE DATE VALUE COUNTS ...\")\nfor k,v in train_df.sample_date.value_counts().items():\n    print(f\"\\t--> There are {v} images with SAMPLE_DATE={k}\")\nfig = px.histogram(train_df, train_df.sample_date.apply(lambda x: x.replace(\"-\", \"_\")), color=\"cell_type\", title=\"<b>Sample Date Value Histogram</b>\")\nfig.show()\n\nprint(\"\\n\\n... ELAPSED TIME DELTA VALUE COUNTS ...\")\nfor k,v in train_df.elapsed_timedelta.value_counts().items():\n    print(f\"\\t--> There are {v} images with SAMPLE_DATE={k}\")\nfig = px.histogram(train_df, \"elapsed_timedelta\", color=\"cell_type\", title=\"<b>Elapsed Time Delta Value Histogram</b>\")\nfig.show()\n    \nprint(\"\\n\\n... SAMPLE ID VALUE COUNTS (>1) ...\")\nprint(f\"\\t--> There are {len(train_df[train_df.sample_id.isin([x for x,v in train_df.sample_id.value_counts().items() if v>1])])} SAMPLE_IDs with more than one image\\n\")\nfor k,v in train_df[train_df.sample_id.isin([x for x,v in train_df.sample_id.value_counts().items() if v>1])].reset_index()[\"sample_id\"].value_counts().items():\n    print(f\"\\t--> There are {v} images with SAMPLE_ID={k}\")\nfig = px.histogram(train_df[train_df.sample_id.isin([x for x,v in train_df.sample_id.value_counts().items() if v>1])].reset_index(), \"sample_id\", color=\"cell_type\", title=\"<b>Sample ID Value Histogram</b>\")\nfig.show()\n\n    \nprint(\"\\n\\n... CELL TYPE VALUE COUNTS ...\")\nfor k,v in train_df.cell_type.value_counts().items():\n    print(f\"\\t--> There are {v} images with CELL_TYPE={k}\")\n    \nfig = px.histogram(train_df, x=\"cell_type\", title=\"<b>Cell Type Histogram</b>\")\nfig.show()\n\nfor ct in CELL_TYPES:\n    print(f\"\\n\\n... SHOWING THREE EXAMPLES OF CELL TYPE {ct.upper()} ...\\n\")\n    for i in range(3):\n        img, msk = get_img_and_mask(**train_df[train_df.cell_type==ct][[\"img_path\", \"annotation\", \"width\", \"height\"]].sample(3).reset_index(drop=True).iloc[i].to_dict())\n        plot_img_and_mask(img, msk)","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:33.681001Z","iopub.execute_input":"2021-11-19T04:04:33.681246Z","iopub.status.idle":"2021-11-19T04:04:40.371451Z","shell.execute_reply.started":"2021-11-19T04:04:33.681217Z","shell.execute_reply":"2021-11-19T04:04:40.370676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DEFER = True\n\nif not DEFER:\n    LC_CELL_TYPES = os.listdir(os.path.join(LC_ANN_DIR, \"LIVECell_single_cells\"))\n\n    print(\"\\n... LOADING TRAIN COCO JSON ...\\n\")\n    LC_COCO_TRAIN = os.path.join(LC_ANN_DIR, \"LIVECell\", \"livecell_coco_train.json\")\n\n    print(\"\\n... LOADING VALIDATION COCO JSON ...\\n\")\n    LC_COCO_VAL = os.path.join(LC_ANN_DIR, \"LIVECell\", \"livecell_coco_val.json\")\n\n    print(\"\\n... LOADING TEST COCO JSON ...\\n\")\n    LC_COCO_TEST = os.path.join(LC_ANN_DIR, \"LIVECell\", \"livecell_coco_test.json\")\n\n    LC_SC_TRAIN = {\n        lc_ct:os.path.join(LC_ANN_DIR, \"LIVECell_single_cells\", lc_ct, f\"livecell_{lc_ct}_train.json\") \\\n        for lc_ct in LC_CELL_TYPES\n    }\n    LC_SC_VAL = {\n        lc_ct:os.path.join(LC_ANN_DIR, \"LIVECell_single_cells\", lc_ct, f\"livecell_{lc_ct}_val.json\") \\\n        for lc_ct in LC_CELL_TYPES\n    }\n    LC_SC_TEST = {\n        lc_ct:os.path.join(LC_ANN_DIR, \"LIVECell_single_cells\", lc_ct, f\"livecell_{lc_ct}_test.json\") \\\n        for lc_ct in LC_CELL_TYPES\n    }\n\n    print(LC_SC_TRAIN)\n    print(LC_SC_VAL)\n    print(LC_SC_TEST)","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:40.372674Z","iopub.execute_input":"2021-11-19T04:04:40.372925Z","iopub.status.idle":"2021-11-19T04:04:40.381396Z","shell.execute_reply.started":"2021-11-19T04:04:40.372897Z","shell.execute_reply":"2021-11-19T04:04:40.380800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"semi_df = pd.DataFrame()\n\nsemi_df[\"cell_type\"] = [x.split(\"[\", 1)[0] for x in tf.io.gfile.listdir(SEMI_DIR)]\nsemi_df[\"compound\"] = [x.split(\"]\", 1)[0].split(\"[\", 1)[-1] for x in tf.io.gfile.listdir(SEMI_DIR)]\nsemi_df[\"img_path\"] = tf.io.gfile.glob(os.path.join(SEMI_DIR, \"**\"))\n\nfig = px.histogram(semi_df, \"cell_type\", color=\"compound\")\nfig.show()\n\nfig = px.histogram(semi_df, \"compound\", color=\"cell_type\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:40.382662Z","iopub.execute_input":"2021-11-19T04:04:40.383152Z","iopub.status.idle":"2021-11-19T04:04:40.668169Z","shell.execute_reply.started":"2021-11-19T04:04:40.383120Z","shell.execute_reply":"2021-11-19T04:04:40.667285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,26))\nfor i, img_path in zip(range(15), semi_df.img_path.to_list()):\n    plt.subplot(5,3,i+1)\n    plt.imshow((255-np.asarray(ImageEnhance.Contrast(Image.fromarray(tf_load_png(img_path).numpy())).enhance(16))), cmap=\"inferno\")\n    plt.axis(False)\n    plt.title(img_path.rsplit(\"/\", 1)[-1].rsplit(\".\", 1)[0], fontweight=\"bold\")\n    \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:40.669374Z","iopub.execute_input":"2021-11-19T04:04:40.670052Z","iopub.status.idle":"2021-11-19T04:04:44.197229Z","shell.execute_reply.started":"2021-11-19T04:04:40.670021Z","shell.execute_reply":"2021-11-19T04:04:44.195699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DEMO_IDX = 11\nimg, msk = get_img_and_mask(**train_df[[\"img_path\", \"annotation\", \"width\", \"height\"]].iloc[DEMO_IDX].to_dict())\nplot_img_and_mask(img, msk)\n\nplt.figure(figsize=(20, min(80, msk.max()//2)))\nfor i in range(1, msk.max()+1):\n    plt.subplot(10,10,i)\n    tl, br = get_contour_bbox(np.where(msk==i, 1, 0).astype(np.uint8))\n    plt.imshow(np.asarray(ImageEnhance.Contrast(Image.fromarray(255-img.numpy())).enhance(16))[tl[1]:br[1], tl[0]:br[0]], cmap=\"magma\")\n    plt.axis(False)\n    plt.title(f\"{i}\", fontweight=\"bold\")\n    if i==100:\n        break\n    \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:44.199587Z","iopub.execute_input":"2021-11-19T04:04:44.201053Z","iopub.status.idle":"2021-11-19T04:04:51.633836Z","shell.execute_reply.started":"2021-11-19T04:04:44.200970Z","shell.execute_reply":"2021-11-19T04:04:51.633210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_gt(image, _gt_classes, _gt_boxes, _gt_mask):\n    img_class = int(_gt_classes.numpy()[0])\n    img_boxes = _gt_boxes.numpy().astype(np.int32)[np.where(_gt_classes!=-1)[0]]\n    _image = _image.numpy()\n    _gt_dummy_mask = np.zeroes_like(_image)\n    _gt_dummy_mask[..., img_class] = cv2.resize(np.expand_dims(_gt_mask, axis=-1), INPUT_SHAPE[:-1])\n    _gt_mask = _gt_dummy_mask\n    \n    \n    \n    plt.figure(figsize=(20,7))\n    \n    plt.subplot(1,3,1)\n    plt.imshow(_image, cmap=\"inferno\")\n    plt.axis(False)\n    plt.title(\"Original Image After Preprocessing\", fontweight=\"bold\")\n    \n    mask_merged = cv2.addWeighted(_image, 0.55, _gt_mask, 1.25, 0.0)\n    plt.subplot(1,3,2)\n    plt.imshow(mask_merged)\n    plt.axis(False)\n    plt.title(f\"Original Image Mask (CLASS={img_class})\", fontweight=\"bold\")\n    \n    plt.subplot(1,3,3)\n    box_image = np.zeroes_like(_image)\n    for box in img_boxes:\n        ymin, xmin, ymax, xmax = box\n        box_image = cv2.rectangle(img=box_image, thickness=1, pt1=(xmin, ymin), pt2=(xmax, ymax),\n                                  color=[0 if i!=img_class else 255 for i in range(3)])\n        \n    box_merged = cv2.addWeighted(_image, 0.55, box_image, 1.25 if img_class==2 else 0.45, 0.0,)\n    plt.imshow(box_merged)\n    plt.axis(False)\n    plt.title(f\"Original Image Bounding Boxes (CLASS={img_class})\", fontweight=\"bold\")\n    \n    plt.tight_layout()\n    plt.show()\n    \ndef plot_pred(_image, _pred_boxes, _pred_scores, _pred_classes, _pred_mask, conf_thresh=0.25, iou_thresh=0.0001):\n    \"\"\"\"\"\"\n    \n    if iou_thresh is not None:\n        _indices, _pred_scores = tf.image.non_max_suppression_with_scores(\n            _pred_boxes, _pred_scores, 800, iou_threshold=iou_thresh,\n            score_threshold=conf_thresh/5, soft_nms_sigma=0.0\n        )\n        _pred_boxes = tf.gather(_pred_boxes, _indices)\n        \n        \n    above_thresh_idx = np.where(_pred_scores.numpy()>conf_thresh)[0]\n    if len(above_thresh_idx)==0:\n        print(\"\\n... NO PREDS OVER CONF THRESH... SAMPLING UP-TO FIFTY SAMPLES...\\n\")\n        above_thresh_idx = np.arrange(min(50, len(_pred_scores)))\n        \n    _image = _image.numpy()\n    _pred_class = int(np.round(_pred_classes.numpy()[above_thresh_idx].mean()))\n    \n    _pred_scores = _pred_scores.numpy()[above_thresh_idx]\n    _pred_boxes = _pred_boxes.numpy().astype(np.int32)[above_thresh_idx]\n    _pred_mask = np.where(_pred_mask[..., 1]>_pred_mask[..., 0], 1.0, 0.0)\n    _dummy_mask = np.zeroes_like(_image)\n    _dummy_mask[..., _pred_class] = cv2.resize(np.expand_dims(_pred_mask, axis=-1), INPUT_SHAPE[:-1])\n    _pred_mask = _dummy_mask\n    \n    \n    plt.figure(figsize=(20,7))\n    \n    plt.subplot(1,3,1)\n    plt.imshow(_image, cmap=\"inferno\")\n    plt.axis(False)\n    plt.title(\"Original Image After Preprocessing\", fontweight=\"bold\")\n    \n    plt.tight_layout()\n    plt.show()\n    \ndef plot_diff(_image, _gt_classes, _gt_boxes, _gt_mask, _pred_boxes, _pred_scores, _pred_classes, _pred_mask, conf_thresh=0.25, iou_thresh=0.0001):\n    \"\"\"\"\"\"\n    \n    if iou_thresh is not None:\n        _indices, _pred_scores = tf.image.non_max_suppression_with_scores(\n            _pred_boxes, _pred_scores, 800, iou_threshold=iou_thresh,\n            score_threshold=conf_thresh/5, soft_nms_sigma=0.0\n        )\n        _pred_boxes = tf.gather(_pred_boxes, _indices)\n        \n    _image = _image.numpy()\n    \n    above_thresh_idx = np.where(_pred_scores.numpy()>conf_thresh)[0]\n    gt_idxs = np.where(_gt_classes!=-1)[0]\n    \n    if len(above_thresh_idx)==0:\n        print(\"\\n... NO PREDS OVER CONF THRESH... SAMPLING UP-TO FIFTY SAMPLES...\\n\")\n        above_thresh_idx = np.arange(min(50, len(_pred_scores)))\n        \n    _img_class = int(_gt_classes.numpy()[0])\n    _pred_class = int(np.round(_pred_classes.numpy()[above_thresh_idx].mean()))\n    \n    img_boxes = _gt_boxes.numpy().astype(np.int32)[gt_idxs]\n    _pred_boxes = _pred_boxes.numpy().astype(np.int32)[above_thresh_idx]\n    \n    _pred_scores = _pred_scores.numpy()[above_thresh_idx]\n    \n    _combo_mask = np.zeroes_like(_image)\n    _combo_mask[..., 0] = cv2.resize(np.expand_dims(_gt_mask, axis=-1), INPUT_SHAPE[:-1])\n    _pred_mask = np.where(_pred_mask[..., -1]>_pred_mask[..., 0], 1.0, 0.0)\n    _combo_mask[..., 1] = cv2.resize(np.expand_dims(_pred_mask, axis=-1), INPUT_SHAPE[:-1])\n    \n    plt.figure(figsize=(20,7))\n    \n    plt.subplot(1,3,1)\n    plt.imshow(_image, cmap=\"inferno\")\n    plt.axis(False)\n    plt.title(\"Original Image After Preprocessing\", fontweight=\"bold\")\n    \n    mask_merged = cv2.addWeighted(_image, 0.55, _combo_mask, 1.25, 0.0,)\n    plt.subplot(1,3,2)\n    plt.imshow(mask_merged)\n    plt.axis(False)\n    plt.title(f\"Combo Image Mask\\n(RED=GT, GREEN=PRED, YELLOW=CONSENSUS)\", fontweight=\"bold\")\n    \n    plt.subplot(1,3,3)\n    box_image = np.zeroes_like(_image)\n    for box in img_boxes:\n        ymin, xmin, ymax, xmax = box\n        box_image = cv2.rectangle(img=box_image, thickness=1, pt1=(xmin, ymin), pt2=(xmax, ymax),\n                                  color=(0,255,0))\n        \n    box_merged = cv2.addWeighted(_image, 0.55, box_image, 1.25, 0.0)\n    plt.imshow(box_merged)\n    plt.axis(False)\n    plt.title(f\"Predicted Image Bounding Boxes\\n(RED=GT, GREEN=PRED)\", fontweight=\"bold\")\n    \n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:51.635210Z","iopub.execute_input":"2021-11-19T04:04:51.635664Z","iopub.status.idle":"2021-11-19T04:04:51.668558Z","shell.execute_reply.started":"2021-11-19T04:04:51.635631Z","shell.execute_reply":"2021-11-19T04:04:51.667908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss_df[\"predicted\"] = rle_encode(np.clip(msk, 0, 1))\nss_df = ss_df[[\"id\", \"predicted\"]]\nss_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-19T04:04:51.669567Z","iopub.execute_input":"2021-11-19T04:04:51.670409Z","iopub.status.idle":"2021-11-19T04:04:51.697173Z","shell.execute_reply.started":"2021-11-19T04:04:51.670369Z","shell.execute_reply":"2021-11-19T04:04:51.696185Z"},"trusted":true},"execution_count":null,"outputs":[]}]}