{"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":"!pip install -q scipy==1.11","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-15T20:10:52.157202Z","iopub.execute_input":"2023-07-15T20:10:52.157633Z","iopub.status.idle":"2023-07-15T20:11:21.072686Z","shell.execute_reply.started":"2023-07-15T20:10:52.157597Z","shell.execute_reply":"2023-07-15T20:11:21.071587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Installing necessary dependencies\n!pip install -q ultralytics pycocotools","metadata":{"execution":{"iopub.status.busy":"2023-07-15T20:11:21.075077Z","iopub.execute_input":"2023-07-15T20:11:21.075424Z","iopub.status.idle":"2023-07-15T20:12:02.378484Z","shell.execute_reply.started":"2023-07-15T20:11:21.075393Z","shell.execute_reply":"2023-07-15T20:12:02.376973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom pycocotools.coco import COCO\nfrom tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-07-15T20:12:02.380195Z","iopub.execute_input":"2023-07-15T20:12:02.380601Z","iopub.status.idle":"2023-07-15T20:12:02.495345Z","shell.execute_reply.started":"2023-07-15T20:12:02.380565Z","shell.execute_reply":"2023-07-15T20:12:02.494519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ** Datasets **","metadata":{}},{"cell_type":"code","source":"coco = COCO(\"../input/dlsprint2/badlad/labels/coco_format/train/badlad-train-coco.json\")","metadata":{"execution":{"iopub.status.busy":"2023-07-15T20:12:02.497423Z","iopub.execute_input":"2023-07-15T20:12:02.498490Z","iopub.status.idle":"2023-07-15T20:12:10.529895Z","shell.execute_reply.started":"2023-07-15T20:12:02.498457Z","shell.execute_reply":"2023-07-15T20:12:10.528592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann_ids = coco.getAnnIds()\nanns = coco.loadAnns(ann_ids)\n\ncat_ids = [ann[\"category_id\"] for ann in anns]\nimg_ids = [ann[\"image_id\"] for ann in anns]\n\nann_ids = pd.Series(ann_ids)\ncat_ids = pd.Series(cat_ids)\nimg_ids = pd.Series(img_ids)\nimg_ids\ncat_ids","metadata":{"execution":{"iopub.status.busy":"2023-07-15T20:41:54.653449Z","iopub.execute_input":"2023-07-15T20:41:54.653919Z","iopub.status.idle":"2023-07-15T20:41:55.644153Z","shell.execute_reply.started":"2023-07-15T20:41:54.653882Z","shell.execute_reply":"2023-07-15T20:41:55.643134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def organize_coco_data(data_dict: dict) -> tuple[list[str], list[dict], list[dict]]:\n    thing_classes: list[str] = []\n\n    # Map Category Names to IDs\n    for cat in data_dict['categories']:\n        thing_classes.append(cat['name'])\n\n    # Images\n    images_metadata: list[dict] = data_dict['images']\n    return thing_classes, images_metadata","metadata":{"execution":{"iopub.status.busy":"2023-07-15T20:59:02.472654Z","iopub.execute_input":"2023-07-15T20:59:02.473045Z","iopub.status.idle":"2023-07-15T20:59:02.480186Z","shell.execute_reply.started":"2023-07-15T20:59:02.473013Z","shell.execute_reply":"2023-07-15T20:59:02.478937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Data Load ###\n\nimport json\nfrom pathlib import Path\n\nTEST_METADATA_PATH = Path(\"/kaggle/input/dlsprint2/badlad/badlad-test-metadata.json\")\nwith TEST_METADATA_PATH.open() as f:\n    test_dict = json.load(f)\nthing_classes_test, images_metadata_test = organize_coco_data(test_dict)\ntest_metadata = pd.DataFrame(images_metadata_test)\ntest_metadata = test_metadata[['id', 'file_name', 'width', 'height']]\ntest_metadata = test_metadata.rename(columns={\"id\": \"image_id\"})\nprint(\"test_metadata size=\", len(test_metadata))\ntest_metadata.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T20:59:16.724682Z","iopub.execute_input":"2023-07-15T20:59:16.725110Z","iopub.status.idle":"2023-07-15T20:59:16.849191Z","shell.execute_reply.started":"2023-07-15T20:59:16.725063Z","shell.execute_reply":"2023-07-15T20:59:16.848422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Creating Validation Data**","metadata":{}},{"cell_type":"code","source":"import os\nfrom sklearn.model_selection import StratifiedGroupKFold\n\nFOLDS = 5\nSEED = 3000\n\nsgkf = StratifiedGroupKFold(n_splits=FOLDS, shuffle=True, random_state=3000)\n\ncounts = cat_ids.value_counts()\n\nprint(f\"Number of images: {len(img_ids.unique())}\")\n\nfor cls, count in zip(counts.index, counts):\n    print(f\"Number of instances of class {cls}: {count}\")","metadata":{"execution":{"iopub.status.busy":"2023-07-15T07:48:19.919461Z","iopub.execute_input":"2023-07-15T07:48:19.920245Z","iopub.status.idle":"2023-07-15T07:48:20.567328Z","shell.execute_reply.started":"2023-07-15T07:48:19.920210Z","shell.execute_reply":"2023-07-15T07:48:20.566193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folds = []\nnumber_of_images = []\nparagraph = []\ntext_box = []\nimage = []\ntable = []\n\nfor fold, (_, val_idx) in enumerate(sgkf.split(ann_ids, cat_ids, img_ids)):\n    folds.append(fold)\n    val_ann_ids = ann_ids[val_idx]\n    val_cat_ids = cat_ids[val_idx]\n    val_img_ids = set(img_ids[val_idx])\n    \n    os.makedirs(f\"val_{fold}/images\")\n    os.makedirs(f\"val_{fold}/labels\")\n    \n    for img in tqdm(coco.loadImgs(val_img_ids)):\n        img_src = \"/kaggle/input/dlsprint2/badlad/images/train/\" + img[\"file_name\"]\n        img_dst = f\"val_{fold}/images/\" + img[\"file_name\"]\n        os.symlink(img_src, img_dst)\n        label_src = \"/kaggle/input/dlsprint2/badlad/labels/yolov8_format/train/\" + img[\"file_name\"][:-4] + \".txt\"\n        label_dst = f\"val_{fold}/labels/\" + img[\"file_name\"][:-4] + \".txt\"\n        os.symlink(label_src, label_dst)\n    \n    number_of_images.append(len(val_img_ids))\n  \n    paragraph.append(sum(val_cat_ids == 0))\n    text_box.append(sum(val_cat_ids == 1))\n    image.append(sum(val_cat_ids == 2))\n    table.append(sum(val_cat_ids == 3))\n    \ndf = pd.DataFrame({\n    \"Fold\": folds,\n    \"Number of Images\": number_of_images,\n    \"Paragraph\": paragraph,\n    \"text_box\": text_box,\n    \"image\": image,\n    \"table\": table,\n})\n\ndf.set_index(\"Fold\")","metadata":{"execution":{"iopub.status.busy":"2023-07-15T07:48:20.568884Z","iopub.execute_input":"2023-07-15T07:48:20.569244Z","iopub.status.idle":"2023-07-15T07:48:32.001418Z","shell.execute_reply.started":"2023-07-15T07:48:20.569210Z","shell.execute_reply":"2023-07-15T07:48:32.000260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb\n\nwandb.init(mode=\"disabled\")","metadata":{"execution":{"iopub.status.busy":"2023-07-15T07:48:32.003176Z","iopub.execute_input":"2023-07-15T07:48:32.003584Z","iopub.status.idle":"2023-07-15T07:48:33.135096Z","shell.execute_reply.started":"2023-07-15T07:48:32.003545Z","shell.execute_reply":"2023-07-15T07:48:33.134185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile badlad.yaml\npath: /kaggle/working/\ntrain:\n    - val_0/\n    - val_1/\n    - val_2/\n    - val_3/\nval: val_4/\n\nnames:\n    0: paragraph\n    1: text_box\n    2: image\n    3: table","metadata":{"execution":{"iopub.status.busy":"2023-07-15T07:48:33.136620Z","iopub.execute_input":"2023-07-15T07:48:33.137571Z","iopub.status.idle":"2023-07-15T07:48:33.144576Z","shell.execute_reply.started":"2023-07-15T07:48:33.137534Z","shell.execute_reply":"2023-07-15T07:48:33.143666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO\n\nmodel = YOLO(\"yolov8m.yaml\")","metadata":{"execution":{"iopub.status.busy":"2023-07-15T07:48:33.147997Z","iopub.execute_input":"2023-07-15T07:48:33.148663Z","iopub.status.idle":"2023-07-15T07:48:39.824992Z","shell.execute_reply.started":"2023-07-15T07:48:33.148617Z","shell.execute_reply":"2023-07-15T07:48:39.824100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result= model.train(data=\"/kaggle/working/badlad.yaml\", \n    epochs=7,\n    pretrained=False,\n    imgsz=512,device=[0, 1])","metadata":{"execution":{"iopub.status.busy":"2023-07-15T07:52:34.525067Z","iopub.execute_input":"2023-07-15T07:52:34.525457Z","iopub.status.idle":"2023-07-15T08:42:28.786479Z","shell.execute_reply.started":"2023-07-15T07:52:34.525427Z","shell.execute_reply":"2023-07-15T08:42:28.785110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Training Result**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\ndf=pd.read_csv('/kaggle/working/runs/detect/train/results.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-15T08:58:59.727107Z","iopub.execute_input":"2023-07-15T08:58:59.727842Z","iopub.status.idle":"2023-07-15T08:58:59.754973Z","shell.execute_reply.started":"2023-07-15T08:58:59.727806Z","shell.execute_reply":"2023-07-15T08:58:59.753833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T08:59:17.093226Z","iopub.execute_input":"2023-07-15T08:59:17.093633Z","iopub.status.idle":"2023-07-15T08:59:17.112414Z","shell.execute_reply.started":"2023-07-15T08:59:17.093601Z","shell.execute_reply":"2023-07-15T08:59:17.111156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# code for displaying multiple images in one figure\n\n#import libraries\nimport cv2\nfrom matplotlib import pyplot as plt\n\n# create figure\nfig = plt.figure(figsize=(10, 7))\n\n# setting values to rows and column variables\nrows = 2\ncolumns = 2\n\n# reading images\nImage1 = cv2.imread('/kaggle/working/runs/detect/train/P_curve.png')\nImage2 = cv2.imread('/kaggle/working/runs/detect/train/confusion_matrix_normalized.png')\nImage3 = cv2.imread('/kaggle/working/runs/detect/train/F1_curve.png')\nImage4 = cv2.imread('/kaggle/working/runs/detect/train/PR_curve.png')\n\n# Adds a subplot at the 1st position\nfig.add_subplot(rows, columns, 1)\n\n# showing image\nplt.imshow(Image1)\nplt.axis('off')\nplt.title(\"First\")\n\n# Adds a subplot at the 2nd position\nfig.add_subplot(rows, columns, 2)\n\n# showing image\nplt.imshow(Image2)\nplt.axis('off')\nplt.title(\"Second\")\n\n# Adds a subplot at the 3rd position\nfig.add_subplot(rows, columns, 3)\n\n# showing image\nplt.imshow(Image3)\nplt.axis('off')\nplt.title(\"Third\")\n\n# Adds a subplot at the 4th position\nfig.add_subplot(rows, columns, 4)\n\n# showing image\nplt.imshow(Image4)\nplt.axis('off')\nplt.title(\"Fourth\")\n","metadata":{"execution":{"iopub.status.busy":"2023-07-15T09:43:33.768785Z","iopub.execute_input":"2023-07-15T09:43:33.770151Z","iopub.status.idle":"2023-07-15T09:43:37.230147Z","shell.execute_reply.started":"2023-07-15T09:43:33.770072Z","shell.execute_reply":"2023-07-15T09:43:37.229148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Inference**","metadata":{}},{"cell_type":"code","source":"results=model.predict(model='/kaggle/working/runs/detect/train/weights/best.pt' ,\n             source='/kaggle/input/dlsprint2/badlad/images/test/0004ffad-d055-4b02-a9a3-4b2aef301594.png')","metadata":{"execution":{"iopub.status.busy":"2023-07-15T09:56:12.285663Z","iopub.execute_input":"2023-07-15T09:56:12.286055Z","iopub.status.idle":"2023-07-15T09:56:12.323108Z","shell.execute_reply.started":"2023-07-15T09:56:12.286024Z","shell.execute_reply":"2023-07-15T09:56:12.322173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results","metadata":{"execution":{"iopub.status.busy":"2023-07-15T10:04:24.596036Z","iopub.execute_input":"2023-07-15T10:04:24.596857Z","iopub.status.idle":"2023-07-15T10:04:24.605577Z","shell.execute_reply.started":"2023-07-15T10:04:24.596818Z","shell.execute_reply":"2023-07-15T10:04:24.604564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport cv2 \nimport numpy as np\nimport pathlib\nimport matplotlib.pyplot as plt\n\nimg = cv2.imread(\"/kaggle/input/dlsprint2/badlad/images/test/0004ffad-d055-4b02-a9a3-4b2aef301594.png\")\nmodel = YOLO(\"/kaggle/working/runs/detect/train/weights/best.pt\")\nresults = model(img)\nres_plotted = results[0].plot()","metadata":{"execution":{"iopub.status.busy":"2023-07-15T10:07:15.977771Z","iopub.execute_input":"2023-07-15T10:07:15.978236Z","iopub.status.idle":"2023-07-15T10:07:16.524633Z","shell.execute_reply.started":"2023-07-15T10:07:15.978201Z","shell.execute_reply":"2023-07-15T10:07:16.523673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(res_plotted)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T10:10:03.534259Z","iopub.execute_input":"2023-07-15T10:10:03.534695Z","iopub.status.idle":"2023-07-15T10:10:03.936773Z","shell.execute_reply.started":"2023-07-15T10:10:03.534663Z","shell.execute_reply":"2023-07-15T10:10:03.935861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for result in results:\n    boxes = result.boxes  # Boxes object for bbox outputs\n    masks = result.masks  # Masks object for segmentation masks outputs\n    keypoints = result.keypoints  # Keypoints object for pose outputs\n    probs = result.probs  # Class probabilities for classification outputs\n    key= result.keys\nresults","metadata":{"execution":{"iopub.status.busy":"2023-07-15T10:11:19.777535Z","iopub.execute_input":"2023-07-15T10:11:19.777948Z","iopub.status.idle":"2023-07-15T10:11:19.783846Z","shell.execute_reply.started":"2023-07-15T10:11:19.777916Z","shell.execute_reply":"2023-07-15T10:11:19.782578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(boxes)\nprint(masks)\nprint(key)\nprint(probs)","metadata":{"execution":{"iopub.status.busy":"2023-07-18T18:09:23.989540Z","iopub.execute_input":"2023-07-18T18:09:23.990408Z","iopub.status.idle":"2023-07-18T18:09:24.391521Z","shell.execute_reply.started":"2023-07-18T18:09:23.990367Z","shell.execute_reply":"2023-07-18T18:09:24.390369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nres_plotted = results[0].plot()\nplt.imshow(res_plotted)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boxes = results[0].boxes\nbox1 = boxes[2]  # returns one box\nbox1.xyxy\ncats=box1.cls.short().to(\"cpu\").numpy()\n#cats=np.sort(cats)\nz1=torch.squeeze(box1.xyxy)\nz1\nc1=z.short().to(\"cpu\").numpy()\nbox1.xyxy\nc\nbox2 = boxes[1]  # returns one box\nbox2.xyxy\nz2=torch.squeeze(box2.xyxy)\nz2\nc2=z2.short().to(\"cpu\").numpy()\nz1","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.orig_shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\ndef bbox_to_mask(image_shape, bounding_boxs):\n    \n    binary_mask = np.zeros(image_shape, dtype=np.uint8)\n\n    x_min, y_min, x_max, y_max = bounding_boxs\n\n    # Update the corresponding region in the binary mask\n    binary_mask[y_min:y_max, x_min:x_max] = 1\n\n    return binary_mask","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask1=bbox_to_mask(result.orig_shape,c1)\nmask2=bbox_to_mask(result.orig_shape,c2)\nmask=np.logical_or(mask1,mask2)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(mask)","metadata":{},"execution_count":null,"outputs":[]}]}