{"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":"import pandas as pd\nimport numpy as np\nfrom tqdm.notebook import tqdm  # progress bar\nimport matplotlib.pyplot as plt\nimport json\nimport cv2\nimport copy\nfrom typing import Optional\nimport seaborn as sns\n\n\n!pip install -q pycocotools\nfrom pycocotools.coco import COCO\nfrom PIL import Image\nimport random\nfrom pathlib import Path\n%matplotlib inline\nsns.set_theme(style='darkgrid', palette='deep', font='sans-serif', font_scale=1)\n\nfrom IPython.display import FileLink\n# torch\nimport torch\nimport gc\nimport warnings\n# Ignore \"future\" warnings and Data-Frame-Slicing warnings.\nwarnings.filterwarnings('ignore')\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-05T09:53:17.713889Z","iopub.execute_input":"2023-08-05T09:53:17.714275Z","iopub.status.idle":"2023-08-05T09:54:03.226835Z","shell.execute_reply.started":"2023-08-05T09:53:17.714228Z","shell.execute_reply":"2023-08-05T09:54:03.225763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\n\nTRAIN_IMG_DIR = Path(\"/kaggle/input/dlsprint2/badlad/images/train\")\n\nTRAIN_COCO_PATH = Path(\"/kaggle/input/dlsprint2/badlad/labels/coco_format/train/badlad-train-coco.json\")\n\nTEST_IMG_DIR = Path(\"/kaggle/input/dlsprint2/badlad/images/test\")\n\nTEST_METADATA_PATH = Path(\"/kaggle/input/dlsprint2/badlad/badlad-test-metadata.json\")\n\n# Training output directory\nOUTPUT_DIR = Path(\"./output\")\nOUTPUT_MODEL = OUTPUT_DIR/\"model_final.pth\"\n\n# Path to your pretrained model weights\nPRETRAINED_PATH = Path(\"\")","metadata":{"execution":{"iopub.status.busy":"2023-08-05T09:54:03.228856Z","iopub.execute_input":"2023-08-05T09:54:03.229510Z","iopub.status.idle":"2023-08-05T09:54:03.239345Z","shell.execute_reply.started":"2023-08-05T09:54:03.229480Z","shell.execute_reply":"2023-08-05T09:54:03.237900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coco = COCO(\"../input/dlsprint2/badlad/labels/coco_format/train/badlad-train-coco.json\")","metadata":{"execution":{"iopub.status.busy":"2023-08-05T09:54:03.240910Z","iopub.execute_input":"2023-08-05T09:54:03.241932Z","iopub.status.idle":"2023-08-05T09:54:10.334712Z","shell.execute_reply.started":"2023-08-05T09:54:03.241897Z","shell.execute_reply":"2023-08-05T09:54:10.333475Z"},"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-08-05T09:54:10.338160Z","iopub.execute_input":"2023-08-05T09:54:10.338893Z","iopub.status.idle":"2023-08-05T09:54:11.292918Z","shell.execute_reply.started":"2023-08-05T09:54:10.338866Z","shell.execute_reply":"2023-08-05T09:54:11.291778Z"},"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-08-05T09:54:11.294514Z","iopub.execute_input":"2023-08-05T09:54:11.295141Z","iopub.status.idle":"2023-08-05T09:54:11.302221Z","shell.execute_reply.started":"2023-08-05T09:54:11.295107Z","shell.execute_reply":"2023-08-05T09:54:11.301046Z"},"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-08-05T09:54:11.303709Z","iopub.execute_input":"2023-08-05T09:54:11.304830Z","iopub.status.idle":"2023-08-05T09:54:11.484582Z","shell.execute_reply.started":"2023-08-05T09:54:11.304792Z","shell.execute_reply":"2023-08-05T09:54:11.483452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_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-08-05T09:54:11.486350Z","iopub.execute_input":"2023-08-05T09:54:11.486789Z","iopub.status.idle":"2023-08-05T09:54:11.543562Z","shell.execute_reply.started":"2023-08-05T09:54:11.486750Z","shell.execute_reply":"2023-08-05T09:54:11.542317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index, row in test_metadata.iterrows():\n    print(row['image_id'],   row['file_name'])\n    image_file=row['file_name']\n    image = Image.open(TEST_IMG_DIR/image_file)\n    code = 231 + row['image_id'];\n    plt.subplot(code)\n    plt.axis('off')\n    plt.imshow(np.asarray(image))\n    if row[\"image_id\"]==2:\n        break","metadata":{"execution":{"iopub.status.busy":"2023-08-05T09:54:11.545463Z","iopub.execute_input":"2023-08-05T09:54:11.546194Z","iopub.status.idle":"2023-08-05T09:54:12.423363Z","shell.execute_reply.started":"2023-08-05T09:54:11.546160Z","shell.execute_reply":"2023-08-05T09:54:12.422319Z"},"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-08-05T09:54:12.424497Z","iopub.execute_input":"2023-08-05T09:54:12.425573Z","iopub.status.idle":"2023-08-05T09:54:25.651393Z","shell.execute_reply.started":"2023-08-05T09:54:12.425533Z","shell.execute_reply":"2023-08-05T09:54:25.650052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO\nmodel = YOLO('/kaggle/input/best20/best.pt')","metadata":{"execution":{"iopub.status.busy":"2023-08-05T10:04:32.229073Z","iopub.execute_input":"2023-08-05T10:04:32.229512Z","iopub.status.idle":"2023-08-05T10:04:32.401231Z","shell.execute_reply.started":"2023-08-05T10:04:32.229483Z","shell.execute_reply":"2023-08-05T10:04:32.399956Z"},"trusted":true},"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":{"iopub.status.busy":"2023-08-05T10:04:35.075055Z","iopub.execute_input":"2023-08-05T10:04:35.075418Z","iopub.status.idle":"2023-08-05T10:04:35.081965Z","shell.execute_reply.started":"2023-08-05T10:04:35.075391Z","shell.execute_reply":"2023-08-05T10:04:35.080794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode(mask):\n    pixels = mask.T.flatten()\n    use_padding = False\n    if pixels[0] or pixels[-1]:\n        use_padding = True\n        pixel_padded = np.zeros([len(pixels) + 2], dtype=pixels.dtype)\n        pixel_padded[1:-1] = pixels\n        pixels = pixel_padded\n    rle = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    if use_padding:\n        rle = rle - 1\n    rle[1::2] = rle[1::2] - rle[:-1:2]\n    return ' '.join(str(x) for x in rle)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-05T10:04:36.447653Z","iopub.execute_input":"2023-08-05T10:04:36.448059Z","iopub.status.idle":"2023-08-05T10:04:36.457204Z","shell.execute_reply.started":"2023-08-05T10:04:36.448029Z","shell.execute_reply":"2023-08-05T10:04:36.455922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"catagory=[0,1,2,3]","metadata":{"execution":{"iopub.status.busy":"2023-08-05T10:04:38.264615Z","iopub.execute_input":"2023-08-05T10:04:38.265298Z","iopub.status.idle":"2023-08-05T10:04:38.269919Z","shell.execute_reply.started":"2023-08-05T10:04:38.265265Z","shell.execute_reply":"2023-08-05T10:04:38.268630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_file = open(\"submission.csv\", \"w\")\nsubmission_file.write(\"Id,Predicted\\n\")","metadata":{"execution":{"iopub.status.busy":"2023-08-05T10:08:22.152784Z","iopub.execute_input":"2023-08-05T10:08:22.153154Z","iopub.status.idle":"2023-08-05T10:08:22.161638Z","shell.execute_reply.started":"2023-08-05T10:08:22.153125Z","shell.execute_reply":"2023-08-05T10:08:22.160436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index, row in test_metadata.iterrows():\n    #print(row['image_id'],   row['file_name'])\n    image_file=row['file_name']\n    image = Image.open(TEST_IMG_DIR/image_file)\n    #code = 231 + row['image_id'];\n    #plt.subplot(code)\n    #plt.axis('off')\n    #plt.imshow(np.asarray(image))\n    #if row[\"image_id\"]==3:\n        #break\n    results=model.predict(image,conf=0.3)\n    for result in results:\n        boxes = result.boxes  # Boxes object for bbox outputs\n    b=boxes.cls.short().to(\"cpu\").numpy()\n    for cat in catagory:\n        a=np.where(b==cat)\n        image_shape=boxes.orig_shape\n        combined_mask = np.zeros(image_shape, dtype=np.uint8)\n        for a1 in a[0]:\n        \n            box1 = boxes[a1]  #returns one box\n            box1.xyxy\n            z1=torch.squeeze(box1.xyxy) #squeeze one dimention \n            c1=z1.short().to(\"cpu\").numpy() #convert tensor array to numpy array\n\n            mask1=bbox_to_mask(boxes.orig_shape,c1) #from bbox to mask\n            combined_mask=np.logical_or(combined_mask,mask1) #summimg all the same catagorical mask\n\n            #plot the combine masks\n\n            #plt.subplot(1,len(catagory),(cat+1))\n            #plt.imshow(combined_mask)\n        \n        rle=rle_encode(combined_mask)\n\n        results: list[str] = []\n        results = [ f\"{row['image_id']}_{cat},{rle}\\n\"]\n        submission_file.writelines(results)\n        results = []","metadata":{"execution":{"iopub.status.busy":"2023-08-05T10:08:24.076934Z","iopub.execute_input":"2023-08-05T10:08:24.079699Z","iopub.status.idle":"2023-08-05T10:08:24.563865Z","shell.execute_reply.started":"2023-08-05T10:08:24.079662Z","shell.execute_reply":"2023-08-05T10:08:24.562782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_file.close()\nfrom pathlib import Path\nif Path(\"submission.csv\").exists:\n    display(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-08-05T10:08:25.482405Z","iopub.execute_input":"2023-08-05T10:08:25.483565Z","iopub.status.idle":"2023-08-05T10:08:25.492565Z","shell.execute_reply.started":"2023-08-05T10:08:25.483524Z","shell.execute_reply":"2023-08-05T10:08:25.491277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if Path(\"submission.csv\").exists:\n    display(FileLink(\"submission.csv\"))","metadata":{"execution":{"iopub.status.busy":"2023-08-05T10:08:27.300881Z","iopub.execute_input":"2023-08-05T10:08:27.301246Z","iopub.status.idle":"2023-08-05T10:08:27.308563Z","shell.execute_reply.started":"2023-08-05T10:08:27.301218Z","shell.execute_reply":"2023-08-05T10:08:27.307462Z"},"trusted":true},"execution_count":null,"outputs":[]}]}