{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":114094,"databundleVersionId":13610510,"sourceType":"competition"},{"sourceId":654297,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":494379,"modelId":509780}],"dockerImageVersionId":31193,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Experiment Setup\n\n- **Model Configuration**\n  - model type: `modelrcnn`\n  - number of classes: `3`\n  - classifier:\n    - cls_score in_features = `modelrcnn.roi_heads.box_predictor.cls_score.in_features`\n    - cls_score out_features = `3`\n  - bbox predictor:\n    - bbox_pred out_features = `12`\n\n- **Image Transform Settings**\n  - min size: `768`\n  - max size: `1920`\n\n- **Device**\n  - model moved to: `device`\n\n## Results\n- **Train loss:** `0.3369`\n","metadata":{"execution":{"iopub.status.busy":"2025-11-19T10:44:01.537861Z","iopub.execute_input":"2025-11-19T10:44:01.538388Z","iopub.status.idle":"2025-11-19T10:44:01.54688Z","shell.execute_reply.started":"2025-11-19T10:44:01.538363Z","shell.execute_reply":"2025-11-19T10:44:01.545904Z"}}},{"cell_type":"code","source":"# from effdet.efficientdet import HeadNet\nfrom tqdm import tqdm\nfrom torchmetrics.detection.mean_ap import MeanAveragePrecision\nimport torchvision\nimport os\nimport cv2\nimport torch\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nfrom torch.utils.data import Dataset, DataLoader\n# from effdet import EfficientDet, get_efficientdet_config, DetBenchTrain,DetBenchPredict\n# from effdet.efficientdet import HeadNet\nfrom torch.optim import AdamW\nfrom torch.optim.lr_scheduler import CosineAnnealingWarmRestarts\n\nimport torch.distributed as dist\nfrom torch import nn\n# from effdet import create_model\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-11-24T03:57:02.842012Z","iopub.execute_input":"2025-11-24T03:57:02.842323Z","iopub.status.idle":"2025-11-24T03:57:02.847607Z","shell.execute_reply.started":"2025-11-24T03:57:02.8423Z","shell.execute_reply":"2025-11-24T03:57:02.846799Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T03:57:07.237283Z","iopub.execute_input":"2025-11-24T03:57:07.237571Z","iopub.status.idle":"2025-11-24T03:57:07.359263Z","shell.execute_reply.started":"2025-11-24T03:57:07.237547Z","shell.execute_reply":"2025-11-24T03:57:07.358344Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class GroceryDataset(Dataset):\n    def __init__(self, root, transform=None):\n        self.root = root\n        self.transform = transform\n        self.imgsPath = sorted(os.listdir(os.path.join(root, \"images\")))\n        self.labelsPath = sorted(os.listdir(os.path.join(root, \"labels\")))\n\n        # b=['000000002.png', '000000003.png', '000000037.png', '000000041.png', '000000072.png', '000000088.png', '000000093.png', '000000109.png', '000000145.png', '000000150.png', '000000151.png', '000000158.png', '000000162.png', '000000184.png', '000000189.png', '000000206.png']\n        # a=['000000002.txt', '000000003.txt', '000000037.txt', '000000041.txt', '000000072.txt', '000000088.txt', '000000093.txt', '000000109.txt', '000000145.txt', '000000150.txt', '000000151.txt', '000000158.txt', '000000162.txt', '000000184.txt', '000000189.txt', '000000206.txt']\n\n        self.labelsPath=['000000000.txt', '000000001.txt', '000000004.txt', '000000005.txt', '000000006.txt', '000000007.txt', '000000008.txt', '000000009.txt', '000000010.txt', '000000011.txt', '000000012.txt', '000000013.txt', '000000014.txt', '000000015.txt', '000000016.txt', '000000017.txt', '000000018.txt', '000000019.txt', '000000020.txt', '000000021.txt', '000000022.txt', '000000023.txt', '000000024.txt', '000000025.txt', '000000026.txt', '000000027.txt', '000000028.txt', '000000029.txt', '000000030.txt', '000000031.txt', '000000032.txt', '000000033.txt', '000000034.txt', '000000035.txt', '000000036.txt', '000000038.txt', '000000039.txt', '000000040.txt', '000000042.txt', '000000043.txt', '000000044.txt', '000000045.txt', '000000046.txt', '000000047.txt', '000000048.txt', '000000049.txt', '000000050.txt', '000000051.txt', '000000052.txt', '000000053.txt', '000000054.txt', '000000055.txt', '000000056.txt', '000000057.txt', '000000058.txt', '000000059.txt', '000000060.txt', '000000061.txt', '000000062.txt', '000000063.txt', '000000064.txt', '000000065.txt', '000000066.txt', '000000067.txt', '000000068.txt', '000000069.txt', '000000070.txt', '000000071.txt', '000000073.txt', '000000074.txt', '000000075.txt', '000000076.txt', '000000077.txt', '000000078.txt', '000000079.txt', '000000080.txt', '000000081.txt', '000000082.txt', '000000083.txt', '000000084.txt', '000000085.txt', '000000086.txt', '000000087.txt', '000000089.txt', '000000090.txt', '000000091.txt', '000000092.txt', '000000094.txt', '000000095.txt', '000000096.txt', '000000097.txt', '000000098.txt', '000000099.txt', '000000100.txt', '000000101.txt', '000000102.txt', '000000103.txt', '000000104.txt', '000000105.txt', '000000106.txt', '000000107.txt', '000000108.txt', '000000110.txt', '000000111.txt', '000000112.txt', '000000113.txt', '000000114.txt', '000000115.txt', '000000116.txt', '000000117.txt', '000000118.txt', '000000119.txt', '000000120.txt', '000000121.txt', '000000122.txt', '000000123.txt', '000000124.txt', '000000125.txt', '000000126.txt', '000000127.txt', '000000128.txt', '000000129.txt', '000000130.txt', '000000131.txt', '000000132.txt', '000000133.txt', '000000134.txt', '000000135.txt', '000000136.txt', '000000137.txt', '000000138.txt', '000000139.txt', '000000140.txt', '000000141.txt', '000000142.txt', '000000143.txt', '000000144.txt', '000000146.txt', '000000147.txt', '000000148.txt', '000000149.txt', '000000152.txt', '000000153.txt', '000000154.txt', '000000155.txt', '000000156.txt', '000000157.txt', '000000159.txt', '000000160.txt', '000000161.txt', '000000163.txt', '000000164.txt', '000000165.txt', '000000166.txt', '000000167.txt', '000000168.txt', '000000169.txt', '000000170.txt', '000000171.txt', '000000172.txt', '000000173.txt', '000000174.txt', '000000175.txt', '000000176.txt', '000000177.txt', '000000178.txt', '000000179.txt', '000000180.txt', '000000181.txt', '000000182.txt', '000000183.txt', '000000185.txt', '000000186.txt', '000000187.txt', '000000188.txt', '000000190.txt', '000000191.txt', '000000192.txt', '000000193.txt', '000000194.txt', '000000195.txt', '000000196.txt', '000000197.txt', '000000198.txt', '000000199.txt', '000000200.txt', '000000201.txt', '000000202.txt', '000000203.txt', '000000204.txt', '000000205.txt', '000000207.txt', '000000208.txt', '000000209.txt', '000000210.txt', '000000211.txt', '000000212.txt', '000000213.txt', '000000214.txt', '000000215.txt', '000000216.txt', '000000217.txt', '000000218.txt', '000000219.txt', '000000220.txt', '000000221.txt', '000000222.txt', '000000223.txt', '000000224.txt', '000000225.txt']\n\n        self.imgsPath=['000000000.png', '000000001.png', '000000004.png', '000000005.png', '000000006.png', '000000007.png', '000000008.png', '000000009.png', '000000010.png', '000000011.png', '000000012.png', '000000013.png', '000000014.png', '000000015.png', '000000016.png', '000000017.png', '000000018.png', '000000019.png', '000000020.png', '000000021.png', '000000022.png', '000000023.png', '000000024.png', '000000025.png', '000000026.png', '000000027.png', '000000028.png', '000000029.png', '000000030.png', '000000031.png', '000000032.png', '000000033.png', '000000034.png', '000000035.png', '000000036.png', '000000038.png', '000000039.png', '000000040.png', '000000042.png', '000000043.png', '000000044.png', '000000045.png', '000000046.png', '000000047.png', '000000048.png', '000000049.png', '000000050.png', '000000051.png', '000000052.png', '000000053.png', '000000054.png', '000000055.png', '000000056.png', '000000057.png', '000000058.png', '000000059.png', '000000060.png', '000000061.png', '000000062.png', '000000063.png', '000000064.png', '000000065.png', '000000066.png', '000000067.png', '000000068.png', '000000069.png', '000000070.png', '000000071.png', '000000073.png', '000000074.png', '000000075.png', '000000076.png', '000000077.png', '000000078.png', '000000079.png', '000000080.png', '000000081.png', '000000082.png', '000000083.png', '000000084.png', '000000085.png', '000000086.png', '000000087.png', '000000089.png', '000000090.png', '000000091.png', '000000092.png', '000000094.png', '000000095.png', '000000096.png', '000000097.png', '000000098.png', '000000099.png', '000000100.png', '000000101.png', '000000102.png', '000000103.png', '000000104.png', '000000105.png', '000000106.png', '000000107.png', '000000108.png', '000000110.png', '000000111.png', '000000112.png', '000000113.png', '000000114.png', '000000115.png', '000000116.png', '000000117.png', '000000118.png', '000000119.png', '000000120.png', '000000121.png', '000000122.png', '000000123.png', '000000124.png', '000000125.png', '000000126.png', '000000127.png', '000000128.png', '000000129.png', '000000130.png', '000000131.png', '000000132.png', '000000133.png', '000000134.png', '000000135.png', '000000136.png', '000000137.png', '000000138.png', '000000139.png', '000000140.png', '000000141.png', '000000142.png', '000000143.png', '000000144.png', '000000146.png', '000000147.png', '000000148.png', '000000149.png', '000000152.png', '000000153.png', '000000154.png', '000000155.png', '000000156.png', '000000157.png', '000000159.png', '000000160.png', '000000161.png', '000000163.png', '000000164.png', '000000165.png', '000000166.png', '000000167.png', '000000168.png', '000000169.png', '000000170.png', '000000171.png', '000000172.png', '000000173.png', '000000174.png', '000000175.png', '000000176.png', '000000177.png', '000000178.png', '000000179.png', '000000180.png', '000000181.png', '000000182.png', '000000183.png', '000000185.png', '000000186.png', '000000187.png', '000000188.png', '000000190.png', '000000191.png', '000000192.png', '000000193.png', '000000194.png', '000000195.png', '000000196.png', '000000197.png', '000000198.png', '000000199.png', '000000200.png', '000000201.png', '000000202.png', '000000203.png', '000000204.png', '000000205.png', '000000207.png', '000000208.png', '000000209.png', '000000210.png', '000000211.png', '000000212.png', '000000213.png', '000000214.png', '000000215.png', '000000216.png', '000000217.png', '000000218.png', '000000219.png', '000000220.png', '000000221.png', '000000222.png', '000000223.png', '000000224.png', '000000225.png']\n    def __len__(self):\n        return len(self.imgsPath)\n\n    def boxConvert(self, coors, imgw, imgh):\n        cx, cy, w, h = coors\n        cx, cy, w, h = cx * imgw, cy * imgh, w * imgw, h * imgh\n        x1 = cx - w / 2\n        y1 = cy - h / 2\n        x2 = cx + w / 2\n        y2 = cy + h / 2\n        return [x1, y1, x2, y2]\n\n    def splitter(self, coor, imgw, imgh):\n        # print(coor)\n        coor = list(map(float, coor))\n        coors, targets, fcoor = [], [], []\n        c = 0\n        for i in coor:\n            if (c == 0) or (c % 5 == 0):\n                targets.append(int(i))\n            else:\n                coors.append(i)\n            c += 1\n            if c % 5 == 0:\n                fcoor.append(self.boxConvert(coors, imgw, imgh))\n                coors = []\n        return targets, fcoor\n\n    def __getitem__(self, idx):\n        imgPath = os.path.join(self.root, \"images\", self.imgsPath[idx])\n        labelPath = os.path.join(self.root, \"labels\", self.labelsPath[idx])\n\n        img = cv2.imread(imgPath)\n        if img is None:\n            raise FileNotFoundError(imgPath)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        imgh, imgw = img.shape[:2]\n\n        lal = open(labelPath).read().split()\n        labels, coors = self.splitter(lal, imgw, imgh)\n        tt = self.transform(image=img, bboxes=coors, labels=labels)\n        aaa=[]\n        for cors in tt[\"bboxes\"]:\n        #     cors[: 4] = cors [1],cors[0],cors[3],cors[2]\n              aaa.append(cors[: 4])\n        target = {\n            \"boxes\": torch.tensor(aaa, dtype=torch.float32).to(device),\n            \"labels\": torch.tensor(list(map(lambda x:x+1,tt[\"labels\"])), dtype=torch.int64).to(device)\n        }\n\n        return tt[\"image\"], target","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T03:57:07.495337Z","iopub.execute_input":"2025-11-24T03:57:07.495659Z","iopub.status.idle":"2025-11-24T03:57:07.515761Z","shell.execute_reply.started":"2025-11-24T03:57:07.495634Z","shell.execute_reply":"2025-11-24T03:57:07.514833Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class GroceryDatasetVal(Dataset):\n    def __init__(self, root, transform=None):\n        self.root = root\n        self.transform = transform\n        self.imgsPath = sorted(os.listdir(os.path.join(root, \"images\")))\n        self.labelsPath = sorted(os.listdir(os.path.join(root, \"labels\")))\n\n        # b=['000000002.png', '000000003.png', '000000037.png', '000000041.png', '000000072.png', '000000088.png', '000000093.png', '000000109.png', '000000145.png', '000000150.png', '000000151.png', '000000158.png', '000000162.png', '000000184.png', '000000189.png', '000000206.png']\n        # a=['000000002.txt', '000000003.txt', '000000037.txt', '000000041.txt', '000000072.txt', '000000088.txt', '000000093.txt', '000000109.txt', '000000145.txt', '000000150.txt', '000000151.txt', '000000158.txt', '000000162.txt', '000000184.txt', '000000189.txt', '000000206.txt']\n\n        #self.labelsPath=['000000000.txt', '000000001.txt', '000000004.txt', '000000005.txt', '000000006.txt', '000000007.txt', '000000008.txt', '000000009.txt', '000000010.txt', '000000011.txt', '000000012.txt', '000000013.txt', '000000014.txt', '000000015.txt', '000000016.txt', '000000017.txt', '000000018.txt', '000000019.txt', '000000020.txt', '000000021.txt', '000000022.txt', '000000023.txt', '000000024.txt', '000000025.txt', '000000026.txt', '000000027.txt', '000000028.txt', '000000029.txt', '000000030.txt', '000000031.txt', '000000032.txt', '000000033.txt', '000000034.txt', '000000035.txt', '000000036.txt', '000000038.txt', '000000039.txt', '000000040.txt', '000000042.txt', '000000043.txt', '000000044.txt', '000000045.txt', '000000046.txt', '000000047.txt', '000000048.txt', '000000049.txt', '000000050.txt', '000000051.txt', '000000052.txt', '000000053.txt', '000000054.txt', '000000055.txt', '000000056.txt', '000000057.txt', '000000058.txt', '000000059.txt', '000000060.txt', '000000061.txt', '000000062.txt', '000000063.txt', '000000064.txt', '000000065.txt', '000000066.txt', '000000067.txt', '000000068.txt', '000000069.txt', '000000070.txt', '000000071.txt', '000000073.txt', '000000074.txt', '000000075.txt', '000000076.txt', '000000077.txt', '000000078.txt', '000000079.txt', '000000080.txt', '000000081.txt', '000000082.txt', '000000083.txt', '000000084.txt', '000000085.txt', '000000086.txt', '000000087.txt', '000000089.txt', '000000090.txt', '000000091.txt', '000000092.txt', '000000094.txt', '000000095.txt', '000000096.txt', '000000097.txt', '000000098.txt', '000000099.txt', '000000100.txt', '000000101.txt', '000000102.txt', '000000103.txt', '000000104.txt', '000000105.txt', '000000106.txt', '000000107.txt', '000000108.txt', '000000110.txt', '000000111.txt', '000000112.txt', '000000113.txt', '000000114.txt', '000000115.txt', '000000116.txt', '000000117.txt', '000000118.txt', '000000119.txt', '000000120.txt', '000000121.txt', '000000122.txt', '000000123.txt', '000000124.txt', '000000125.txt', '000000126.txt', '000000127.txt', '000000128.txt', '000000129.txt', '000000130.txt', '000000131.txt', '000000132.txt', '000000133.txt', '000000134.txt', '000000135.txt', '000000136.txt', '000000137.txt', '000000138.txt', '000000139.txt', '000000140.txt', '000000141.txt', '000000142.txt', '000000143.txt', '000000144.txt', '000000146.txt', '000000147.txt', '000000148.txt', '000000149.txt', '000000152.txt', '000000153.txt', '000000154.txt', '000000155.txt', '000000156.txt', '000000157.txt', '000000159.txt', '000000160.txt', '000000161.txt', '000000163.txt', '000000164.txt', '000000165.txt', '000000166.txt', '000000167.txt', '000000168.txt', '000000169.txt', '000000170.txt', '000000171.txt', '000000172.txt', '000000173.txt', '000000174.txt', '000000175.txt', '000000176.txt', '000000177.txt', '000000178.txt', '000000179.txt', '000000180.txt', '000000181.txt', '000000182.txt', '000000183.txt', '000000185.txt', '000000186.txt', '000000187.txt', '000000188.txt', '000000190.txt', '000000191.txt', '000000192.txt', '000000193.txt', '000000194.txt', '000000195.txt', '000000196.txt', '000000197.txt', '000000198.txt', '000000199.txt', '000000200.txt', '000000201.txt', '000000202.txt', '000000203.txt', '000000204.txt', '000000205.txt', '000000207.txt', '000000208.txt', '000000209.txt', '000000210.txt', '000000211.txt', '000000212.txt', '000000213.txt', '000000214.txt', '000000215.txt', '000000216.txt', '000000217.txt', '000000218.txt', '000000219.txt', '000000220.txt', '000000221.txt', '000000222.txt', '000000223.txt', '000000224.txt', '000000225.txt']\n\n        #self.imgsPath=['000000000.png', '000000001.png', '000000004.png', '000000005.png', '000000006.png', '000000007.png', '000000008.png', '000000009.png', '000000010.png', '000000011.png', '000000012.png', '000000013.png', '000000014.png', '000000015.png', '000000016.png', '000000017.png', '000000018.png', '000000019.png', '000000020.png', '000000021.png', '000000022.png', '000000023.png', '000000024.png', '000000025.png', '000000026.png', '000000027.png', '000000028.png', '000000029.png', '000000030.png', '000000031.png', '000000032.png', '000000033.png', '000000034.png', '000000035.png', '000000036.png', '000000038.png', '000000039.png', '000000040.png', '000000042.png', '000000043.png', '000000044.png', '000000045.png', '000000046.png', '000000047.png', '000000048.png', '000000049.png', '000000050.png', '000000051.png', '000000052.png', '000000053.png', '000000054.png', '000000055.png', '000000056.png', '000000057.png', '000000058.png', '000000059.png', '000000060.png', '000000061.png', '000000062.png', '000000063.png', '000000064.png', '000000065.png', '000000066.png', '000000067.png', '000000068.png', '000000069.png', '000000070.png', '000000071.png', '000000073.png', '000000074.png', '000000075.png', '000000076.png', '000000077.png', '000000078.png', '000000079.png', '000000080.png', '000000081.png', '000000082.png', '000000083.png', '000000084.png', '000000085.png', '000000086.png', '000000087.png', '000000089.png', '000000090.png', '000000091.png', '000000092.png', '000000094.png', '000000095.png', '000000096.png', '000000097.png', '000000098.png', '000000099.png', '000000100.png', '000000101.png', '000000102.png', '000000103.png', '000000104.png', '000000105.png', '000000106.png', '000000107.png', '000000108.png', '000000110.png', '000000111.png', '000000112.png', '000000113.png', '000000114.png', '000000115.png', '000000116.png', '000000117.png', '000000118.png', '000000119.png', '000000120.png', '000000121.png', '000000122.png', '000000123.png', '000000124.png', '000000125.png', '000000126.png', '000000127.png', '000000128.png', '000000129.png', '000000130.png', '000000131.png', '000000132.png', '000000133.png', '000000134.png', '000000135.png', '000000136.png', '000000137.png', '000000138.png', '000000139.png', '000000140.png', '000000141.png', '000000142.png', '000000143.png', '000000144.png', '000000146.png', '000000147.png', '000000148.png', '000000149.png', '000000152.png', '000000153.png', '000000154.png', '000000155.png', '000000156.png', '000000157.png', '000000159.png', '000000160.png', '000000161.png', '000000163.png', '000000164.png', '000000165.png', '000000166.png', '000000167.png', '000000168.png', '000000169.png', '000000170.png', '000000171.png', '000000172.png', '000000173.png', '000000174.png', '000000175.png', '000000176.png', '000000177.png', '000000178.png', '000000179.png', '000000180.png', '000000181.png', '000000182.png', '000000183.png', '000000185.png', '000000186.png', '000000187.png', '000000188.png', '000000190.png', '000000191.png', '000000192.png', '000000193.png', '000000194.png', '000000195.png', '000000196.png', '000000197.png', '000000198.png', '000000199.png', '000000200.png', '000000201.png', '000000202.png', '000000203.png', '000000204.png', '000000205.png', '000000207.png', '000000208.png', '000000209.png', '000000210.png', '000000211.png', '000000212.png', '000000213.png', '000000214.png', '000000215.png', '000000216.png', '000000217.png', '000000218.png', '000000219.png', '000000220.png', '000000221.png', '000000222.png', '000000223.png', '000000224.png', '000000225.png']\n    def __len__(self):\n        return len(self.imgsPath)\n\n    def boxConvert(self, coors, imgw, imgh):\n        cx, cy, w, h = coors\n        cx, cy, w, h = cx * imgw, cy * imgh, w * imgw, h * imgh\n        x1 = cx - w / 2\n        y1 = cy - h / 2\n        x2 = cx + w / 2\n        y2 = cy + h / 2\n        return [x1, y1, x2, y2]\n\n    def splitter(self, coor, imgw, imgh):\n        # print(coor)\n        coor = list(map(float, coor))\n        coors, targets, fcoor = [], [], []\n        c = 0\n        for i in coor:\n            if (c == 0) or (c % 5 == 0):\n                targets.append(int(i))\n            else:\n                coors.append(i)\n            c += 1\n            if c % 5 == 0:\n                fcoor.append(self.boxConvert(coors, imgw, imgh))\n                coors = []\n        return targets, fcoor\n\n    def __getitem__(self, idx):\n        imgPath = os.path.join(self.root, \"images\", self.imgsPath[idx])\n        labelPath = os.path.join(self.root, \"labels\", self.labelsPath[idx])\n\n        img = cv2.imread(imgPath)\n        if img is None:\n            raise FileNotFoundError(imgPath)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        imgh, imgw = img.shape[:2]\n\n        lal = open(labelPath).read().split()\n        labels, coors = self.splitter(lal, imgw, imgh)\n        tt = self.transform(image=img, bboxes=coors, labels=labels)\n        aaa=[]\n        for cors in tt[\"bboxes\"]:\n        #     cors[: 4] = cors [1],cors[0],cors[3],cors[2]\n              aaa.append(cors[: 4])\n        target = {\n            \"boxes\": torch.tensor(aaa, dtype=torch.float32).to(device),\n            \"labels\": torch.tensor(list(map(lambda x:x+1,tt[\"labels\"])), dtype=torch.int64).to(device)\n        }\n\n        return tt[\"image\"], target","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T03:57:10.073791Z","iopub.execute_input":"2025-11-24T03:57:10.07407Z","iopub.status.idle":"2025-11-24T03:57:10.085175Z","shell.execute_reply.started":"2025-11-24T03:57:10.074052Z","shell.execute_reply":"2025-11-24T03:57:10.084438Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transform=A.Compose([A.Resize(1080,1920),A.HorizontalFlip(p=0.5),A.ToTensorV2()],bbox_params=A.BboxParams(format=\"pascal_voc\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T03:57:12.383278Z","iopub.execute_input":"2025-11-24T03:57:12.383938Z","iopub.status.idle":"2025-11-24T03:57:12.390564Z","shell.execute_reply.started":"2025-11-24T03:57:12.383912Z","shell.execute_reply":"2025-11-24T03:57:12.389815Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"datasetTrain=GroceryDataset(\"/kaggle/input/grocery-items-multi-class-object-detection/train/train\",transform=transform)\ndatasetVal=GroceryDatasetVal(\"/kaggle/input/grocery-items-multi-class-object-detection/val/val\",transform=transform)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T03:57:12.951214Z","iopub.execute_input":"2025-11-24T03:57:12.951964Z","iopub.status.idle":"2025-11-24T03:57:13.009654Z","shell.execute_reply.started":"2025-11-24T03:57:12.951938Z","shell.execute_reply":"2025-11-24T03:57:13.00909Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"datasetVal[3]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T03:57:14.764806Z","iopub.execute_input":"2025-11-24T03:57:14.765558Z","iopub.status.idle":"2025-11-24T03:57:15.665876Z","shell.execute_reply.started":"2025-11-24T03:57:14.765533Z","shell.execute_reply":"2025-11-24T03:57:15.665183Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelrcnn = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights='DEFAULT')\noptimizer = AdamW(modelrcnn.named_parameters(), lr=0.00001)\nscheduler =CosineAnnealingWarmRestarts(T_0=5,T_mult=2,optimizer=optimizer)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T05:31:05.969925Z","iopub.execute_input":"2025-11-24T05:31:05.970234Z","iopub.status.idle":"2025-11-24T05:31:06.568179Z","shell.execute_reply.started":"2025-11-24T05:31:05.970211Z","shell.execute_reply":"2025-11-24T05:31:06.567356Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def collate_fn(batch):\n    img = [i[0] for i in batch]\n    target = [i[1] for i in batch]\n    return img, target","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T04:44:45.238202Z","iopub.execute_input":"2025-11-24T04:44:45.239038Z","iopub.status.idle":"2025-11-24T04:44:45.242807Z","shell.execute_reply.started":"2025-11-24T04:44:45.239007Z","shell.execute_reply":"2025-11-24T04:44:45.242178Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataLoaderTrain=DataLoader(dataset=datasetTrain,batch_size=4,shuffle=False,collate_fn=collate_fn)\ndataLoaderVal=DataLoader(dataset=datasetVal,batch_size=4,shuffle=False,collate_fn=collate_fn)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T04:44:46.953684Z","iopub.execute_input":"2025-11-24T04:44:46.954275Z","iopub.status.idle":"2025-11-24T04:44:46.95811Z","shell.execute_reply.started":"2025-11-24T04:44:46.954249Z","shell.execute_reply":"2025-11-24T04:44:46.95736Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelrcnn","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-21T05:00:42.85965Z","iopub.execute_input":"2025-11-21T05:00:42.860241Z","iopub.status.idle":"2025-11-21T05:00:42.866512Z","shell.execute_reply.started":"2025-11-21T05:00:42.860215Z","shell.execute_reply":"2025-11-21T05:00:42.865934Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numCls=3\nmodelrcnn.roi_heads.box_predictor.cls_score.out_features=3\nmodelrcnn.roi_heads.box_predictor.bbox_pred.out_features=12\nmodelrcnn.transform.min_size=(768,)\nmodelrcnn.transform.max_size=1920\nmodelrcnn=modelrcnn.to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T05:31:09.570158Z","iopub.execute_input":"2025-11-24T05:31:09.570978Z","iopub.status.idle":"2025-11-24T05:31:09.626121Z","shell.execute_reply.started":"2025-11-24T05:31:09.570948Z","shell.execute_reply":"2025-11-24T05:31:09.625199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\n\ndef iou(box1, box2):\n    x1 = torch.max(box1[0], box2[0])\n    y1 = torch.max(box1[1], box2[1])\n    x2 = torch.min(box1[2], box2[2])\n    y2 = torch.min(box1[3], box2[3])\n\n    inter = torch.clamp(x2 - x1, min=0) * torch.clamp(y2 - y1, min=0)\n\n    area1 = (box1[2]-box1[0]) * (box1[3]-box1[1])\n    area2 = (box2[2]-box2[0]) * (box2[3]-box2[1])\n\n    union = area1 + area2 - inter + 1e-6\n    return inter / union\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for img,tar in dataLoaderTrain:\n    imgg=img[0].float().to(device)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# modelrcnn= nn.DataParallel(modelrcnn)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-21T05:03:07.73014Z","iopub.execute_input":"2025-11-21T05:03:07.730427Z","iopub.status.idle":"2025-11-21T05:03:07.733848Z","shell.execute_reply.started":"2025-11-21T05:03:07.730406Z","shell.execute_reply":"2025-11-21T05:03:07.73324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"epochs=20\n\nfor i in (range(1,epochs+1)):\n    Map=MeanAveragePrecision(iou_type=\"bbox\",class_metrics=True,extended_summary=True)\n\n    modelrcnn.train()\n\n    runloss=0.0\n    for img,tar in tqdm(dataLoaderTrain):\n        img=torch.stack(img).float().to(device)\n        #print(tar)\n        optimizer.zero_grad()\n        outsy=modelrcnn(img,tar)\n        #print(outsy)\n        loss=sum(j for j in outsy.values())\n        \n        loss.sum().backward()\n        optimizer.step()\n        runloss+=loss.item()\n    scheduler.step()\n    print(i,\": \",\"loss: \",runloss/len(dataLoaderTrain))\n    #print(outsy)\n    modelrcnn.eval()\n    with torch.no_grad():\n        for img,tarr in tqdm(dataLoaderTrain):\n                # torch.cuda.empty_cache()\n                imgg=(torch.stack(img).float().to(device))\n                #img=img[0].numpy().transpose(1,2,0)\n                #print(img)\n                tar=tarr[0]\n            \n                outsy=modelrcnn(imgg)\n                \n                \n                # for i in tar[\"boxes\"]:\n                #     cv2.rectangle(img,tuple(map(int,i[:2])),tuple(map(int,i[2:4])),(255,0,0),5)\n                # for i in range(3):\n                #     # if (outsy[0][\"scores\"][i]>0.73):\n                        \n                #     cv2.rectangle(img,tuple(map(int,outsy[0][\"boxes\"][i][:2])),tuple(map(int,outsy[0][\"boxes\"][i][2:4])),(0,0,255),5)\n                # plt.imshow(img)\n                # plt.show()\n                \n                #print(img,tar)\n                #print(tar)\n                #print(outsy)\n                Map.update(outsy,tarr)\n                \n                # torch.cuda.empty_cache()\n        mp=Map.compute()\n        print(\"map_100: \",mp[\"map\"],\"map_50: \",mp[\"map_50\"],\" map per class: \",mp[\"map_per_class\"][:3])                ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T05:31:24.168353Z","iopub.execute_input":"2025-11-24T05:31:24.168687Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"torch.save(modelrcnn.state_dict(),\"model_rcnn4_fullSize3.pth\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T05:28:41.454096Z","iopub.execute_input":"2025-11-24T05:28:41.454716Z","iopub.status.idle":"2025-11-24T05:28:41.705821Z","shell.execute_reply.started":"2025-11-24T05:28:41.454688Z","shell.execute_reply":"2025-11-24T05:28:41.705229Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelrcnn.eval()\n\nwith torch.no_grad():\n    Map=MeanAveragePrecision(iou_type=\"bbox\",class_metrics=True,extended_summary=True)\n    for img,tarr in tqdm(dataLoaderVal):\n            \n\n\n            # torch.cuda.empty_cache()\n            imgg=torch.stack(img).float().to(device)\n            img=img[0].numpy().transpose(1,2,0)\n            # print(imgg.shape)\n            tar=tarr[0]\n            \n            outsy=modelrcnn(imgg)\n            #print(outsy)\n            # print(imgg)\n\n            Map.update(outsy,tarr)\n            #print(outsy)\n            break\n            \n            # torch.cuda.empty_cache()\n    mp=Map.compute()\n    print(mp)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T04:17:25.426266Z","iopub.execute_input":"2025-11-24T04:17:25.426562Z","iopub.status.idle":"2025-11-24T04:17:26.474393Z","shell.execute_reply.started":"2025-11-24T04:17:25.426539Z","shell.execute_reply":"2025-11-24T04:17:26.473599Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"outsy","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-21T04:14:07.90012Z","iopub.execute_input":"2025-11-21T04:14:07.90069Z","iopub.status.idle":"2025-11-21T04:14:07.908439Z","shell.execute_reply.started":"2025-11-21T04:14:07.900668Z","shell.execute_reply":"2025-11-21T04:14:07.907646Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-19T09:28:10.554832Z","iopub.execute_input":"2025-11-19T09:28:10.555101Z","iopub.status.idle":"2025-11-19T09:28:10.836135Z","shell.execute_reply.started":"2025-11-19T09:28:10.555083Z","shell.execute_reply":"2025-11-19T09:28:10.8355Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelrcnn.load_state_dict(torch.load(\"/kaggle/input/rcnn1/pytorch/default/1/model_rcnn1.pth\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T03:57:37.141797Z","iopub.execute_input":"2025-11-24T03:57:37.142576Z","iopub.status.idle":"2025-11-24T03:57:39.192199Z","shell.execute_reply.started":"2025-11-24T03:57:37.142548Z","shell.execute_reply":"2025-11-24T03:57:39.191574Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dir(modelrcnn)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-19T05:33:17.052902Z","iopub.execute_input":"2025-11-19T05:33:17.05378Z","iopub.status.idle":"2025-11-19T05:33:17.061509Z","shell.execute_reply.started":"2025-11-19T05:33:17.053747Z","shell.execute_reply":"2025-11-19T05:33:17.060531Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"dataLoaderVis=DataLoader(dataset=datasetVal,batch_size=1,shuffle=False,collate_fn=collate_fn)\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-24T03:57:41.527178Z","iopub.execute_input":"2025-11-24T03:57:41.527502Z","iopub.status.idle":"2025-11-24T03:57:41.532141Z","shell.execute_reply.started":"2025-11-24T03:57:41.527469Z","shell.execute_reply":"2025-11-24T03:57:41.531385Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelrcnn.eval()\nMap=MeanAveragePrecision(iou_type=\"bbox\",class_metrics=True)\n\nwith torch.no_grad():\n    for img,tarr in tqdm(dataLoaderVis):\n            # torch.cuda.empty_cache()\n            imgg=(img[0].float().to(device))\n            img=img[0].numpy().transpose(1,2,0)\n            #print(img)\n            tar=tarr[0]\n        \n            outsy=modelrcnn([imgg])\n            \n            \n            for i in tar[\"boxes\"]:\n                cv2.rectangle(img,tuple(map(int,i[:2])),tuple(map(int,i[2:4])),(255,0,0),5)\n            for i in range(3):\n                # if (outsy[0][\"scores\"][i]>0.73):\n                    \n                cv2.rectangle(img,tuple(map(int,outsy[0][\"boxes\"][i][:2])),tuple(map(int,outsy[0][\"boxes\"][i][2:4])),(0,0,255),5)\n            plt.imshow(img)\n            plt.show()\n            \n            #print(img,tar)\n            #print(tar)\n            #print(outsy)\n            Map.update(outsy,tarr)\n            \n            # torch.cuda.empty_cache()\n    print(Map.compute())\n            \n            \n    #print(i,\": \",\"loss: \",runloss/len(dataLoaderTrain))\n                ","metadata":{"trusted":true,"scrolled":true,"execution":{"iopub.status.busy":"2025-11-21T05:25:39.867587Z","iopub.execute_input":"2025-11-21T05:25:39.868378Z","iopub.status.idle":"2025-11-21T05:26:03.845989Z","shell.execute_reply.started":"2025-11-21T05:25:39.868347Z","shell.execute_reply":"2025-11-21T05:26:03.845008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loss.backward()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-19T05:24:42.954059Z","iopub.execute_input":"2025-11-19T05:24:42.954422Z","iopub.status.idle":"2025-11-19T05:24:42.995318Z","shell.execute_reply.started":"2025-11-19T05:24:42.954399Z","shell.execute_reply":"2025-11-19T05:24:42.994407Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelrcnn\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-19T07:33:04.665352Z","iopub.execute_input":"2025-11-19T07:33:04.665964Z","iopub.status.idle":"2025-11-19T07:33:04.672193Z","shell.execute_reply.started":"2025-11-19T07:33:04.665939Z","shell.execute_reply":"2025-11-19T07:33:04.6716Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Experiment Setup\n\n- **Model Configuration**\n  - model type: `modelrcnn`\n  - number of classes: `3`\n  - classifier:\n    - cls_score in_features = `modelrcnn.roi_heads.box_predictor.cls_score.in_features`\n    - cls_score out_features = `3`\n  - bbox predictor:\n    - bbox_pred out_features = `12`\n\n- **Image Transform Settings**\n  - min size: `768`\n  - max size: `1920`\n\n- **Device**\n  - model moved to: `device`\n\n## Results\n- **Train loss:** `0.3369`\n","metadata":{}}]}