{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":2017694,"sourceType":"datasetVersion","datasetId":1207662},{"sourceId":3848,"sourceType":"modelInstanceVersion","modelInstanceId":2749,"modelId":324},{"sourceId":115944,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":97399,"modelId":121582}],"dockerImageVersionId":30461,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nfrom matplotlib import pyplot as plt\nimport torch\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-12-26T13:07:58.238019Z","iopub.execute_input":"2024-12-26T13:07:58.238322Z","iopub.status.idle":"2024-12-26T13:08:00.998193Z","shell.execute_reply.started":"2024-12-26T13:07:58.238287Z","shell.execute_reply":"2024-12-26T13:08:00.997134Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pip install git+https://github.com/facebookresearch/segment-anything.git","metadata":{"execution":{"iopub.status.busy":"2024-12-26T13:08:22.714089Z","iopub.execute_input":"2024-12-26T13:08:22.714763Z","iopub.status.idle":"2024-12-26T13:08:34.984537Z","shell.execute_reply.started":"2024-12-26T13:08:22.714728Z","shell.execute_reply":"2024-12-26T13:08:34.983280Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def show_anns(anns, axes=None):\n    if len(anns) == 0:\n        return\n    if axes:\n        ax = axes\n    else:\n        ax = plt.gca()\n        ax.set_autoscale_on(False)\n    sorted_anns = sorted(anns, key=(lambda x: x['area']), reverse=True)\n    polygons = []\n    color = []\n    for ann in sorted_anns:\n        m = ann['segmentation']\n        img = np.ones((m.shape[0], m.shape[1], 3))\n        color_mask = np.random.random((1, 3)).tolist()[0]\n        for i in range(3):\n            img[:,:,i] = color_mask[i]\n        ax.imshow(np.dstack((img, m*0.5)))\n\ndef show_mask(mask, ax, random_color=False):\n    if random_color:\n        color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)\n    else:\n        color = np.array([30/255, 144/255, 255/255, 0.6])\n    h, w = mask.shape[-2:]\n    mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)\n    ax.imshow(mask_image)\n\n    \ndef show_points(coords, labels, ax, marker_size=375):\n    pos_points = coords[labels==1]\n    neg_points = coords[labels==0]\n    ax.scatter(pos_points[:, 0], pos_points[:, 1], color='green', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)\n    ax.scatter(neg_points[:, 0], neg_points[:, 1], color='red', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)   \n\n    \ndef show_box(box, ax):\n    x0, y0 = box[0], box[1]\n    w, h = box[2] - box[0], box[3] - box[1]\n    ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor='green', facecolor=(0,0,0,0), lw=2))    ","metadata":{"execution":{"iopub.status.busy":"2024-12-26T13:09:26.910346Z","iopub.execute_input":"2024-12-26T13:09:26.911170Z","iopub.status.idle":"2024-12-26T13:09:26.923310Z","shell.execute_reply.started":"2024-12-26T13:09:26.911129Z","shell.execute_reply":"2024-12-26T13:09:26.922413Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from segment_anything import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor\n\nsam_checkpoint = \"/kaggle/input/segment-anything/pytorch/vit-b/1/model.pth\"\nmodel_type = \"vit_b\"\n\ndevice = \"cuda\"\n\nsam = sam_model_registry[model_type](checkpoint=sam_checkpoint)\nsam.to(device=device)\n\nmask_generator = SamAutomaticMaskGenerator(sam, points_per_batch=16)","metadata":{"execution":{"iopub.status.busy":"2024-12-26T13:09:32.528255Z","iopub.execute_input":"2024-12-26T13:09:32.528580Z","iopub.status.idle":"2024-12-26T13:09:39.030357Z","shell.execute_reply.started":"2024-12-26T13:09:32.528552Z","shell.execute_reply":"2024-12-26T13:09:39.029207Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Slide segmentation with SAM","metadata":{}},{"cell_type":"code","source":"image_path = '/kaggle/input/cervical-cancer-largest-dataset-sipakmed/im_Dyskeratotic/im_Dyskeratotic/001.bmp'\nimage_array = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\nmasks = mask_generator.generate(image_array)\n\n_, axes = plt.subplots(1,3, figsize=(16,16))\naxes[0].imshow(image_array)\nshow_anns(masks, axes[1])\naxes[2].imshow(image_array)\nshow_anns(masks, axes[2])","metadata":{"execution":{"iopub.status.busy":"2024-12-26T13:12:47.771079Z","iopub.execute_input":"2024-12-26T13:12:47.771996Z","iopub.status.idle":"2024-12-26T13:15:23.089980Z","shell.execute_reply.started":"2024-12-26T13:12:47.771957Z","shell.execute_reply":"2024-12-26T13:15:23.088923Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\ndef filter_masks(masks):\n    # Sort masks by area in descending order (largest masks first)\n    masks = sorted(masks, key=lambda x: np.sum(x['segmentation']), reverse=True)\n    \n    # Create a list to store filtered masks\n    filtered_masks = []\n    \n    # Loop through masks and filter inscribed ones\n    for i, mask1 in enumerate(masks):\n        is_inscribed = False\n        for j, mask2 in enumerate(filtered_masks):\n            # Check if mask1 is inscribed in mask2\n            intersection = np.logical_and(mask1['segmentation'], mask2['segmentation'])\n            if np.array_equal(intersection, mask1['segmentation']):  # Mask1 is fully inside Mask2\n                is_inscribed = True\n                break\n        if not is_inscribed:\n            filtered_masks.append(mask1)\n    \n    return filtered_masks\n\ndef get_bounding_boxes(masks):\n\n    bounding_boxes = []\n    for mask in masks:\n        segmentation = mask['segmentation']  # Binary mask\n        y_coords, x_coords = np.where(segmentation)  # Find non-zero points in the mask\n        x_min, x_max = np.min(x_coords), np.max(x_coords)\n        y_min, y_max = np.min(y_coords), np.max(y_coords)\n        bounding_boxes.append([x_min, y_min, x_max, y_max])\n    return bounding_boxes\n\n# Example Usage\n# Assuming `masks` is the output from the SAM model's `mask_generator.generate(image_array)`\nfiltered_masks = filter_masks(masks)  # Filter inscribed masks\nbounding_boxes = get_bounding_boxes(filtered_masks)  # Get bounding box coordinates\n\n'''# Print the bounding boxes\nfor i, bbox in enumerate(bounding_boxes):\n    print(f\"Mask {i+1} Bounding Box: {bbox}\")'''\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-26T13:17:57.922209Z","iopub.execute_input":"2024-12-26T13:17:57.923172Z","iopub.status.idle":"2024-12-26T13:18:09.243062Z","shell.execute_reply.started":"2024-12-26T13:17:57.923126Z","shell.execute_reply":"2024-12-26T13:18:09.242000Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\n\ndef draw_bounding_boxes(image, bounding_boxes):\n    image_with_boxes = image.copy()\n    \n    for bbox in bounding_boxes:\n        x_min, y_min, x_max, y_max = bbox\n        # Draw a rectangle (bounding box) on the image\n        cv2.rectangle(image_with_boxes, (x_min, y_min), (x_max, y_max), color=(255, 0, 0), thickness=2)\n    \n    return image_with_boxes\n\nimage_path = '/kaggle/input/cervical-cancer-largest-dataset-sipakmed/im_Dyskeratotic/im_Dyskeratotic/001.bmp'\nimage_array = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\n\nimage_with_boxes = draw_bounding_boxes(image_array, bounding_boxes)\n\nplt.figure(figsize=(8, 8))\nplt.imshow(image_with_boxes)\nplt.axis(\"off\")\nplt.title(\"Image with Bounding Boxes\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-26T13:18:22.250997Z","iopub.execute_input":"2024-12-26T13:18:22.251354Z","iopub.status.idle":"2024-12-26T13:18:22.902199Z","shell.execute_reply.started":"2024-12-26T13:18:22.251320Z","shell.execute_reply":"2024-12-26T13:18:22.901200Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import keras\nfrom keras.layers import Dense,Conv2D, Flatten, MaxPool2D, Dropout\nfrom keras.models import Sequential\nfrom keras.preprocessing import image\nfrom keras.callbacks import ModelCheckpoint\nfrom keras.models import load_model\n\nmodel = load_model(\"/kaggle/input/densenet169/tensorflow2/default/2/densenet169-cervical-cancer.hdf5\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-26T13:21:38.832191Z","iopub.execute_input":"2024-12-26T13:21:38.832548Z","iopub.status.idle":"2024-12-26T13:21:55.755286Z","shell.execute_reply.started":"2024-12-26T13:21:38.832517Z","shell.execute_reply":"2024-12-26T13:21:55.754150Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom PIL import Image\n\n# Define the color dictionary\nclass_colors = {\n    0: (255, 0, 0),  # Red\n    1: (0, 255, 0),  # Green\n    2: (0, 0, 255),  # Blue\n    3: (255, 255, 0),  # Yellow\n    4: (255, 0, 255),  # Magenta\n}\n\ndef draw_bounding_boxes_with_colors(image, bounding_boxes, predictions, class_colors):\n    image_with_boxes = image.copy()\n\n    for bbox, predicted_class in zip(bounding_boxes, predictions):\n        x_min, y_min, x_max, y_max = bbox\n        color = class_colors[predicted_class]\n\n        # Tint the sliding window region with the class color\n        overlay = image_with_boxes[y_min:y_max, x_min:x_max].copy()\n        tinted_region = cv2.addWeighted(\n            overlay, 0.5, np.full_like(overlay, color, dtype=np.uint8), 0.5, 0\n        )\n        image_with_boxes[y_min:y_max, x_min:x_max] = tinted_region\n\n        # Draw a rectangle (bounding box) around the region\n        cv2.rectangle(image_with_boxes, (x_min, y_min), (x_max, y_max), color=color, thickness=2)\n\n    return image_with_boxes\n\ndef preprocess_and_predict(image_array, bounding_boxes, model):\n    predictions = []\n    \n    for bbox in bounding_boxes:\n        x_min, y_min, x_max, y_max = bbox\n\n        # Extract the portion of the image inside the bounding box\n        cropped_img = image_array[y_min:y_max, x_min:x_max]\n\n        # Resize the cropped image to 64x64 (adjust to model requirements)\n        resized_img = cv2.resize(cropped_img, (64, 64))\n        \n        # Normalize the image (assuming the model requires pixel values between 0 and 1)\n        normalized_img = resized_img / 255.0\n        \n        # Add batch dimension (1, 64, 64, 3)\n        input_tensor = np.expand_dims(normalized_img, axis=0)\n\n        # Get prediction from the model\n        prediction = model.predict(input_tensor)\n        predicted_label = np.argmax(prediction, axis=1)[0]  # Assuming classification model\n        predictions.append(predicted_label)\n    \n    return predictions\n\npredictions = preprocess_and_predict(image_array, bounding_boxes, model)\n\n# Draw the bounding boxes with colors and tint the regions\nimage_with_boxes = draw_bounding_boxes_with_colors(image_array, bounding_boxes, predictions, class_colors)\n\n# Display the image with bounding boxes and colors\nplt.figure(figsize=(10, 10))\nplt.imshow(image_with_boxes)\nplt.axis(\"off\")\nplt.title(\"Image with Bounding Boxes and Color Tinting\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-26T13:27:17.457442Z","iopub.execute_input":"2024-12-26T13:27:17.457840Z","iopub.status.idle":"2024-12-26T13:27:24.041993Z","shell.execute_reply.started":"2024-12-26T13:27:17.457805Z","shell.execute_reply":"2024-12-26T13:27:24.040994Z"}},"outputs":[],"execution_count":null}]}