{"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 --no-deps '../input/timm-package/timm-0.1.26-py3-none-any.whl' > /dev/null\n!pip install --no-deps '../input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl' > /dev/null","metadata":{"_uuid":"488bda82-450d-49cb-9d4d-671fbc61a2ac","_cell_guid":"f07f5bb3-3045-47b4-9d91-764cac62b559","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-29T13:01:31.617619Z","iopub.execute_input":"2023-06-29T13:01:31.618473Z","iopub.status.idle":"2023-06-29T13:02:18.433837Z","shell.execute_reply.started":"2023-06-29T13:01:31.618423Z","shell.execute_reply":"2023-06-29T13:02:18.432551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.insert(0, \"../input/timm-efficientdet-pytorch\")\nsys.path.insert(0, \"../input/omegaconf\")\nsys.path.insert(0, \"../input/weightedboxesfusion\")\n\nfrom ensemble_boxes import *\nimport torch\nimport numpy as np\nimport pandas as pd\nfrom glob import glob\nfrom torch.utils.data import Dataset,DataLoader\nimport albumentations as A\nfrom albumentations.pytorch.transforms import ToTensorV2\nimport cv2\nimport gc\nfrom matplotlib import pyplot as plt\nfrom effdet import get_efficientdet_config, EfficientDet, DetBenchEval\nfrom effdet.efficientdet import HeadNet","metadata":{"_uuid":"b9fa13c8-c5af-4e22-97e9-455bfc925cee","_cell_guid":"0f5b1b1e-c5dd-4a9c-86ef-5db3b773a2a9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-29T13:02:18.437550Z","iopub.execute_input":"2023-06-29T13:02:18.437996Z","iopub.status.idle":"2023-06-29T13:02:21.663926Z","shell.execute_reply.started":"2023-06-29T13:02:18.437961Z","shell.execute_reply":"2023-06-29T13:02:21.662968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_valid_transforms():\n    return A.Compose([\n            A.Resize(height=512, width=512, p=1.0),\n            ToTensorV2(p=1.0),\n        ], p=1.0)","metadata":{"_uuid":"78598669-c905-4fe6-81fc-099377d0b9f0","_cell_guid":"6dc8c600-bec2-49d4-b0f6-8504f8c82eb8","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-29T13:02:21.667857Z","iopub.execute_input":"2023-06-29T13:02:21.668146Z","iopub.status.idle":"2023-06-29T13:02:21.675191Z","shell.execute_reply.started":"2023-06-29T13:02:21.668117Z","shell.execute_reply":"2023-06-29T13:02:21.672395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_ROOT_PATH = '../input/global-wheat-detection/test'\n\nclass DatasetRetriever(Dataset):\n\n    def __init__(self, image_ids, transforms=None):\n        super().__init__()\n        self.image_ids = image_ids\n        self.transforms = transforms\n\n    def __getitem__(self, index: int):\n        image_id = self.image_ids[index]\n        image = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n        image /= 255.0\n        if self.transforms:\n            sample = {'image': image}\n            sample = self.transforms(**sample)\n            image = sample['image']\n        return image, image_id\n\n    def __len__(self) -> int:\n        return self.image_ids.shape[0]","metadata":{"_uuid":"9cb29919-4b15-4717-b3fa-1a728f0a7b82","_cell_guid":"882c5a09-4e06-4c46-b40d-aaa85a949dfa","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-29T13:02:21.676494Z","iopub.execute_input":"2023-06-29T13:02:21.676931Z","iopub.status.idle":"2023-06-29T13:02:21.688479Z","shell.execute_reply.started":"2023-06-29T13:02:21.676895Z","shell.execute_reply":"2023-06-29T13:02:21.687519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = DatasetRetriever(\n    image_ids=np.array([path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]),\n    transforms=get_valid_transforms()\n)\n\ndef collate_fn(batch):\n    return tuple(zip(*batch))\n\ndata_loader = DataLoader(\n    dataset,\n    batch_size=2,\n    shuffle=False,\n    num_workers=4,\n    drop_last=False,\n    collate_fn=collate_fn\n)","metadata":{"_uuid":"944355e2-035a-4c11-8307-19f2a27f70af","_cell_guid":"d59d8455-2053-47c4-885b-6cd69fe8ed57","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-29T13:02:21.693138Z","iopub.execute_input":"2023-06-29T13:02:21.693555Z","iopub.status.idle":"2023-06-29T13:02:21.704090Z","shell.execute_reply.started":"2023-06-29T13:02:21.693512Z","shell.execute_reply":"2023-06-29T13:02:21.703399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_net(checkpoint_path):\n    config = get_efficientdet_config('tf_efficientdet_d7')\n    net = EfficientDet(config, pretrained_backbone=False)\n\n    config.num_classes = 1\n    config.image_size=512\n    net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))\n\n    checkpoint = torch.load(checkpoint_path)\n    net.load_state_dict(checkpoint['model_state_dict'])\n\n    del checkpoint\n    gc.collect()\n\n    net = DetBenchEval(net, config)\n    net.eval();\n    return net.cuda()\n\nnet = load_net('/kaggle/input/soc-efficientdet-infer/efficientdet/best-checkpoint-018epoch.bin')","metadata":{"_uuid":"6bce9e4d-b913-4c76-a953-327b2130fd72","_cell_guid":"cfa912a2-1121-4444-9a60-544f0208bd23","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-29T13:02:21.705622Z","iopub.execute_input":"2023-06-29T13:02:21.705997Z","iopub.status.idle":"2023-06-29T13:02:32.095019Z","shell.execute_reply.started":"2023-06-29T13:02:21.705963Z","shell.execute_reply":"2023-06-29T13:02:32.094007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_predictions(images, score_threshold=0.00):\n    images = torch.stack(images).cuda().float()\n    predictions = []\n    with torch.no_grad():\n        det = net(images, torch.tensor([1]*images.shape[0]).float().cuda())\n        for i in range(images.shape[0]):\n            boxes = det[i].detach().cpu().numpy()[:,:4]    \n            scores = det[i].detach().cpu().numpy()[:,4]\n            indexes = np.where(scores > score_threshold)[0]\n            boxes = boxes[indexes]\n            boxes[:, 2] = boxes[:, 2] + boxes[:, 0]\n            boxes[:, 3] = boxes[:, 3] + boxes[:, 1]\n            predictions.append({\n                'boxes': boxes[indexes],\n                'scores': scores[indexes],\n            })\n    return [predictions]\n\ndef run_wbf(predictions, image_index, image_size=512, iou_thr=0.44, skip_box_thr=0.43, weights=None):\n    boxes = [(prediction[image_index]['boxes']/(image_size-1)).tolist()  for prediction in predictions]\n    scores = [prediction[image_index]['scores'].tolist()  for prediction in predictions]\n    labels = [np.ones(prediction[image_index]['scores'].shape[0]).tolist() for prediction in predictions]\n    boxes, scores, labels = weighted_boxes_fusion(boxes, scores, labels, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)\n    boxes = boxes*(image_size-1)\n    return boxes, scores, labels","metadata":{"_uuid":"6bd88e0f-339d-434f-a2ad-a082fecb0f8c","_cell_guid":"96d3883d-2ba5-4d1e-a5a2-1048c9b42005","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-29T13:02:32.096570Z","iopub.execute_input":"2023-06-29T13:02:32.096989Z","iopub.status.idle":"2023-06-29T13:02:32.115447Z","shell.execute_reply.started":"2023-06-29T13:02:32.096939Z","shell.execute_reply":"2023-06-29T13:02:32.114437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nr = 3\nc = 3\nfig, axs = plt.subplots(r, c, figsize=(16, 16), dpi=300)\n\nfor i in range(r * c):\n    image, image_id = dataset[i]\n    predictions = make_predictions([image])\n    sample = image.permute(1,2,0).cpu().numpy()\n    boxes, scores, labels = run_wbf(predictions, image_index=0)\n    boxes = boxes.astype(np.int32).clip(min=0, max=511)\n    for j in range(len(scores)):\n        box = boxes[j]\n        score = scores[j]\n        cv2.rectangle(sample, (box[0], box[1]), (box[2], box[3]), (1, 0, 0), 1)\n        cv2.putText(sample, f'{score:.2f}',(int(box[0]), int(box[1]-10)),0, 0.5, (1,0,0), 1)\n    axs[i // c, i % c].set_axis_off()\n    axs[i // c, i % c].imshow(sample)\n    \nplt.savefig('/kaggle/working/example.png', dpi=300)","metadata":{"_uuid":"378f2966-ab16-438d-84cf-262e2fd17c30","_cell_guid":"4772b78e-3d0e-4345-b2e1-cb4e758d37f2","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-29T13:02:32.116969Z","iopub.execute_input":"2023-06-29T13:02:32.117671Z","iopub.status.idle":"2023-06-29T13:02:53.460826Z","shell.execute_reply.started":"2023-06-29T13:02:32.117631Z","shell.execute_reply":"2023-06-29T13:02:53.459485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def format_prediction_string(boxes, scores):\n    pred_strings = []\n    for j in zip(scores, boxes):\n        pred_strings.append(\"{0:.4f} {1} {2} {3} {4}\".format(j[0], j[1][0], j[1][1], j[1][2], j[1][3]))\n    return \" \".join(pred_strings)","metadata":{"_uuid":"fbe51716-ec9b-4251-92bc-45cd25ed147d","_cell_guid":"4527b2cc-5695-4226-ad21-3ede190b5985","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-29T13:02:53.462530Z","iopub.execute_input":"2023-06-29T13:02:53.463053Z","iopub.status.idle":"2023-06-29T13:02:53.473884Z","shell.execute_reply.started":"2023-06-29T13:02:53.462984Z","shell.execute_reply":"2023-06-29T13:02:53.472470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = []\n\nimport time\n\nstart_time = time.time()\n\nfor images, image_ids in data_loader:\n    predictions = make_predictions(images)\n    for i, image in enumerate(images):\n        boxes, scores, labels = run_wbf(predictions, image_index=i)\n        boxes = (boxes*2).astype(np.int32).clip(min=0, max=1023)\n        image_id = image_ids[i]\n        \n        boxes[:, 2] = boxes[:, 2] - boxes[:, 0]\n        boxes[:, 3] = boxes[:, 3] - boxes[:, 1]\n\n        result = {\n            'image_id': image_id,\n            'PredictionString': format_prediction_string(boxes, scores)\n        }\n        results.append(result)\n\nend_time = time.time()\nprint(f\"Runtime: {end_time - start_time} seconds\")\n","metadata":{"_uuid":"11606fc3-f211-4248-bd67-16a8f72fd0f6","_cell_guid":"42dd6248-430a-4c84-a58f-3e9fd288702a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-29T13:02:53.476397Z","iopub.execute_input":"2023-06-29T13:02:53.478143Z","iopub.status.idle":"2023-06-29T13:02:54.704898Z","shell.execute_reply.started":"2023-06-29T13:02:53.478080Z","shell.execute_reply":"2023-06-29T13:02:54.703129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])\ntest_df.to_csv('submission.csv', index=False)\ntest_df.head()","metadata":{"_uuid":"ee8f4232-6ab4-4c47-86aa-bb629fbc61e0","_cell_guid":"96d9d149-b81d-4eb1-b63c-fc3589444b92","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-29T13:02:54.707526Z","iopub.execute_input":"2023-06-29T13:02:54.707957Z","iopub.status.idle":"2023-06-29T13:02:54.921383Z","shell.execute_reply.started":"2023-06-29T13:02:54.707911Z","shell.execute_reply":"2023-06-29T13:02:54.920379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}