{"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":"# Inference","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:11:54.684060Z","iopub.execute_input":"2022-01-11T17:11:54.684422Z","iopub.status.idle":"2022-01-11T17:11:54.689861Z","shell.execute_reply.started":"2022-01-11T17:11:54.684390Z","shell.execute_reply":"2022-01-11T17:11:54.689017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2022-01-11T17:11:54.692341Z","iopub.execute_input":"2022-01-11T17:11:54.692926Z","iopub.status.idle":"2022-01-11T17:12:40.098843Z","shell.execute_reply.started":"2022-01-11T17:11:54.692886Z","shell.execute_reply":"2022-01-11T17:12:40.097841Z"},"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\nimport pickle\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":{"execution":{"iopub.status.busy":"2022-01-11T17:12:40.102257Z","iopub.execute_input":"2022-01-11T17:12:40.102610Z","iopub.status.idle":"2022-01-11T17:12:43.145493Z","shell.execute_reply.started":"2022-01-11T17:12:40.102579Z","shell.execute_reply":"2022-01-11T17:12:43.144705Z"},"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)\n\nDATA_ROOT_PATH = '../input/gwb-test/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]\n    \n\ndataset = 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)\n    \ndef 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('../input/logtest/last-checkpoint (1).bin')\n\n    \ndef make_predictions(images, score_threshold=0.3):\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\n\n    \nimport matplotlib.pyplot as plt\n\nfor j, (images, image_ids) in enumerate(data_loader):\n    if j==2:\n        break\n\npredictions = make_predictions(images)\nprint(image_ids)\ni = 0\nsample = images[i].permute(1,2,0).cpu().numpy()\n\nboxes, scores, labels = run_wbf(predictions, image_index=i)\nboxes = boxes.astype(np.int32).clip(min=0, max=511)\n\nfig, ax = plt.subplots(1, 1, figsize=(16, 8))\n\nfor box in boxes:\n    cv2.rectangle(sample, (box[0], box[1]), (box[2], box[3]), (1, 0, 0), 2)\n    \nax.set_axis_off()\nax.imshow(sample);\n    \ndef format_prediction_string(boxes):\n    sign=0\n    for j in boxes:\n        if sign==0:\n            strings=str(j)\n        else:\n            strings+=' '+str(j)\n        sign+=1\n    return strings\n    \nresults = []\nresults2= []\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        for j in range(boxes.shape[0]):\n            result = {\n            'image_id': image_id,\n            'PredictionString': format_prediction_string(boxes[j,:])\n             }\n            boxes[j, 2] = boxes[j, 2] + boxes[j, 0]\n            boxes[j, 3] = boxes[j, 3] + boxes[j, 1]\n            result2 = {\n            'image_id': image_id,\n            'PredictionString': boxes[j,:],\n             'scores': scores[j]\n             }\n            results.append(result)\n            results2.append(result2)\n\ntest_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])\ntest_df.sort_values('image_id',inplace=True)\ntest_df.to_csv('submission.csv', index=False)\ntest_df.head()\n\n\nwbf = pd.DataFrame(results2, columns=['image_id', 'bbox','scores'])\nwbf.sort_values('image_id',inplace=True)\nf = open('store1.pckl', 'wb')\npickle.dump(wbf, f)\nf.close()\n","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:12:43.147081Z","iopub.execute_input":"2022-01-11T17:12:43.147613Z","iopub.status.idle":"2022-01-11T17:13:41.051862Z","shell.execute_reply.started":"2022-01-11T17:12:43.147562Z","shell.execute_reply":"2022-01-11T17:13:41.050857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nwbf = pd.DataFrame(results2, columns=['image_id', 'PredictionString','scores'])\nwbf.sort_values('image_id',inplace=True)\nf = open('store1.pckl', 'wb')\npickle.dump(wbf, f)\nf.close()\nprint(wbf)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.053219Z","iopub.execute_input":"2022-01-11T17:13:41.053583Z","iopub.status.idle":"2022-01-11T17:13:41.306594Z","shell.execute_reply.started":"2022-01-11T17:13:41.053542Z","shell.execute_reply":"2022-01-11T17:13:41.302067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 伪标签+TTA","metadata":{}},{"cell_type":"code","source":"# # 这里和训练的代码保持一致\n# def get_train_transforms():\n#     return A.Compose(\n#         [\n#             A.RandomSizedCrop(min_max_height=(800, 800), height=1024, width=1024, p=0.5),\n#             A.OneOf([\n#                 A.HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit= 0.2, \n#                                      val_shift_limit=0.2, p=0.9),\n#                 A.RandomBrightnessContrast(brightness_limit=0.2, \n#                                            contrast_limit=0.2, p=0.9),\n#             ],p=0.9),\n#             A.ToGray(p=0.01),\n#             A.HorizontalFlip(p=0.5),\n#             A.VerticalFlip(p=0.5),\n#             A.Resize(height=512, width=512, p=1),\n#             A.Cutout(num_holes=8, max_h_size=64, max_w_size=64, fill_value=0, p=0.5),\n#             ToTensorV2(p=1.0),\n#         ], \n#         p=1.0, \n#         bbox_params=A.BboxParams(\n#             format='pascal_voc',\n#             min_area=0, \n#             min_visibility=0,\n#             label_fields=['labels']\n#         )\n#     )\n\n# def get_valid_transforms():\n#     return A.Compose(\n#         [\n#             A.Resize(height=512, width=512, p=1.0),\n#             ToTensorV2(p=1.0),\n#         ], \n#         p=1.0, \n#         bbox_params=A.BboxParams(\n#             format='pascal_voc',\n#             min_area=0, \n#             min_visibility=0,\n#             label_fields=['labels']\n#         )\n#     )\n\n# def get_test_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":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.312306Z","iopub.execute_input":"2022-01-11T17:13:41.312773Z","iopub.status.idle":"2022-01-11T17:13:41.321866Z","shell.execute_reply.started":"2022-01-11T17:13:41.312736Z","shell.execute_reply":"2022-01-11T17:13:41.318829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import warnings\n\n# warnings.filterwarnings(\"ignore\")\n\n# class Fitter:\n    \n#     def __init__(self, model, device, config):\n#         self.config = config\n#         self.epoch = 0\n\n#         self.base_dir = f'./{config.folder}'\n#         if not os.path.exists(self.base_dir):\n#             os.makedirs(self.base_dir)\n        \n#         self.log_path = f'{self.base_dir}/log.txt'\n#         self.best_summary_loss = 10**5\n\n#         self.model = model\n#         self.device = device\n\n#         param_optimizer = list(self.model.named_parameters())\n#         no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']\n#         optimizer_grouped_parameters = [\n#             {'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.001},\n#             {'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}\n#         ] \n\n#         self.optimizer = torch.optim.AdamW(self.model.parameters(), lr=config.lr)\n#         self.scheduler = config.SchedulerClass(self.optimizer, **config.scheduler_params)\n#         self.log(f'Fitter prepared. Device is {self.device}')\n\n#     def fit(self, train_loader, validation_loader):\n#         for e in range(self.config.n_epochs):\n#             if self.config.verbose:\n#                 lr = self.optimizer.param_groups[0]['lr']\n#                 timestamp = datetime.utcnow().isoformat()\n#                 self.log(f'\\n{timestamp}\\nLR: {lr}')\n\n#             t = time.time()\n#             summary_loss = self.train_one_epoch(train_loader)\n\n#             self.log(f'[RESULT]: Train. Epoch: {self.epoch}, summary_loss: {summary_loss.avg:.5f}, time: {(time.time() - t):.5f}')\n#             self.save(f'{self.base_dir}/last-checkpoint1.bin')\n\n#             t = time.time()\n#             summary_loss = self.validation(validation_loader)\n\n#             self.log(f'[RESULT]: Val. Epoch: {self.epoch}, summary_loss: {summary_loss.avg:.5f}, time: {(time.time() - t):.5f}')\n#             if summary_loss.avg < self.best_summary_loss:\n#                 self.best_summary_loss = summary_loss.avg\n#                 self.model.eval()\n#                 self.save(f'{self.base_dir}/best-checkpoint-{str(self.epoch).zfill(3)}epoch.bin')\n#                 for path in sorted(glob(f'{self.base_dir}/best-checkpoint-*epoch.bin'))[:-3]:\n#                     os.remove(path)\n\n#             if self.config.validation_scheduler:\n#                 self.scheduler.step(metrics=summary_loss.avg)\n\n#             self.epoch += 1\n\n#     def validation(self, val_loader):\n#         self.model.eval()\n#         summary_loss = AverageMeter()\n#         t = time.time()\n#         for step, (images, targets, image_ids) in enumerate(val_loader):\n#             if self.config.verbose:\n#                 if step % self.config.verbose_step == 0:\n#                     print(\n#                         f'Val Step {step}/{len(val_loader)}, ' + \\\n#                         f'summary_loss: {summary_loss.avg:.5f}, ' + \\\n#                         f'time: {(time.time() - t):.5f}', end='\\r'\n#                     )\n#             with torch.no_grad():\n#                 images = torch.stack(images)\n#                 batch_size = images.shape[0]\n#                 images = images.to(self.device).float()\n#                 boxes = [target['boxes'].to(self.device).float() for target in targets]\n#                 labels = [target['labels'].to(self.device).float() for target in targets]\n\n#                 loss, _, _ = self.model(images, boxes, labels)\n#                 summary_loss.update(loss.detach().item(), batch_size)\n\n#         return summary_loss\n\n#     def train_one_epoch(self, train_loader):\n#         self.model.train()\n#         summary_loss = AverageMeter()\n#         t = time.time()\n#         for step, (images, targets, image_ids) in enumerate(train_loader):\n#             if self.config.verbose:\n#                 if step % self.config.verbose_step == 0:\n#                     print(\n#                         f'Train Step {step}/{len(train_loader)}, ' + \\\n#                         f'summary_loss: {summary_loss.avg:.5f}, ' + \\\n#                         f'time: {(time.time() - t):.5f}', end='\\r'\n#                     )\n            \n#             images = torch.stack(images)\n#             images = images.to(self.device).float()\n#             batch_size = images.shape[0]\n#             boxes = [target['boxes'].to(self.device).float() for target in targets]\n#             labels = [target['labels'].to(self.device).float() for target in targets]\n\n#             self.optimizer.zero_grad()\n            \n#             loss, _, _ = self.model(images, boxes, labels)\n            \n#             loss.backward()\n\n#             summary_loss.update(loss.detach().item(), batch_size)\n\n#             self.optimizer.step()\n\n#             if self.config.step_scheduler:\n#                 self.scheduler.step()\n\n#         return summary_loss\n    \n#     def save(self, path):\n#         self.model.eval()\n#         torch.save({\n#             'model_state_dict': self.model.model.state_dict(),\n#             'optimizer_state_dict': self.optimizer.state_dict(),\n#             'scheduler_state_dict': self.scheduler.state_dict(),\n#             'best_summary_loss': self.best_summary_loss,\n#             'epoch': self.epoch,\n#         }, path)\n\n#     def load(self, path):\n#         checkpoint = torch.load(path)\n#         self.model.model.load_state_dict(checkpoint['model_state_dict'])\n#         self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])\n#         self.scheduler.load_state_dict(checkpoint['scheduler_state_dict'])\n#         self.best_summary_loss = checkpoint['best_summary_loss']\n#         self.epoch = checkpoint['epoch'] + 1\n        \n#     def log(self, message):\n#         if self.config.verbose:\n#             print(message)\n#         with open(self.log_path, 'a+') as logger:\n#             logger.write(f'{message}\\n')","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.325762Z","iopub.execute_input":"2022-01-11T17:13:41.326120Z","iopub.status.idle":"2022-01-11T17:13:41.333108Z","shell.execute_reply.started":"2022-01-11T17:13:41.326075Z","shell.execute_reply":"2022-01-11T17:13:41.331956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class TrainGlobalConfig:\n#     num_workers = 2\n#     batch_size = 4 \n#     n_epochs = 5 # n_epochs = 40\n#     lr = 0.0001\n\n#     folder = 'plabel_model'\n\n#     # -------------------\n#     verbose = True\n#     verbose_step = 1\n#     # -------------------\n\n#     # --------------------\n#     step_scheduler = False  # do scheduler.step after optimizer.step\n#     validation_scheduler = True  # do scheduler.step after validation stage loss\n\n#     SchedulerClass = torch.optim.lr_scheduler.ReduceLROnPlateau\n#     scheduler_params = dict(\n#         mode='min',\n#         factor=0.5,\n#         patience=1,\n#         verbose=False, \n#         threshold=0.0001,\n#         threshold_mode='abs',\n#         cooldown=0, \n#         min_lr=1e-8,\n#         eps=1e-08\n#     )","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.334839Z","iopub.execute_input":"2022-01-11T17:13:41.335626Z","iopub.status.idle":"2022-01-11T17:13:41.343671Z","shell.execute_reply.started":"2022-01-11T17:13:41.335585Z","shell.execute_reply":"2022-01-11T17:13:41.342911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# DATA_ROOT_PATH = '../input/gwb-test/test'\n\n# class 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":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.345314Z","iopub.execute_input":"2022-01-11T17:13:41.345555Z","iopub.status.idle":"2022-01-11T17:13:41.354021Z","shell.execute_reply.started":"2022-01-11T17:13:41.345530Z","shell.execute_reply":"2022-01-11T17:13:41.353334Z"},"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_test_transforms()\n# )\n\n# def collate_fn(batch):\n#     return tuple(zip(*batch))\n\n# data_loader = DataLoader(\n#     dataset,\n#     batch_size=1,\n#     shuffle=False,\n#     num_workers=4,\n#     drop_last=False,\n#     collate_fn=collate_fn\n# )","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.355497Z","iopub.execute_input":"2022-01-11T17:13:41.355856Z","iopub.status.idle":"2022-01-11T17:13:41.362650Z","shell.execute_reply.started":"2022-01-11T17:13:41.355817Z","shell.execute_reply":"2022-01-11T17:13:41.361875Z"},"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\n\n# net = load_net('../input/logtest/last-checkpoint (1).bin')\n","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.364461Z","iopub.execute_input":"2022-01-11T17:13:41.365125Z","iopub.status.idle":"2022-01-11T17:13:41.371025Z","shell.execute_reply.started":"2022-01-11T17:13:41.365028Z","shell.execute_reply":"2022-01-11T17:13:41.370056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## TTA代码","metadata":{}},{"cell_type":"code","source":"# class BaseWheatTTA:\n#     \"\"\" author: @shonenkov \"\"\"\n#     image_size = 512\n\n#     def augment(self, image):\n#         raise NotImplementedError\n    \n#     def batch_augment(self, images):\n#         raise NotImplementedError\n    \n#     def deaugment_boxes(self, boxes):\n#         raise NotImplementedError\n\n# class TTAHorizontalFlip(BaseWheatTTA):\n#     \"\"\" author: @shonenkov \"\"\"\n\n#     def augment(self, image):\n#         return image.flip(1)\n    \n#     def batch_augment(self, images):\n#         return images.flip(2)\n    \n#     def deaugment_boxes(self, boxes):\n#         boxes[:, [1,3]] = self.image_size - boxes[:, [3,1]]\n#         return boxes\n\n# class TTAVerticalFlip(BaseWheatTTA):\n#     \"\"\" author: @shonenkov \"\"\"\n    \n#     def augment(self, image):\n#         return image.flip(2)\n    \n#     def batch_augment(self, images):\n#         return images.flip(3)\n    \n#     def deaugment_boxes(self, boxes):\n#         boxes[:, [0,2]] = self.image_size - boxes[:, [2,0]]\n#         return boxes\n    \n# class TTARotate90(BaseWheatTTA):\n#     \"\"\" author: @shonenkov \"\"\"\n    \n#     def augment(self, image):\n#         return torch.rot90(image, 1, (1, 2))\n\n#     def batch_augment(self, images):\n#         return torch.rot90(images, 1, (2, 3))\n    \n#     def deaugment_boxes(self, boxes):\n#         res_boxes = boxes.copy()\n#         res_boxes[:, [0,2]] = self.image_size - boxes[:, [1,3]]\n#         res_boxes[:, [1,3]] = boxes[:, [2,0]]\n#         return res_boxes\n\n# class TTACompose(BaseWheatTTA):\n#     \"\"\" author: @shonenkov \"\"\"\n#     def __init__(self, transforms):\n#         self.transforms = transforms\n        \n#     def augment(self, image):\n#         for transform in self.transforms:\n#             image = transform.augment(image)\n#         return image\n    \n#     def batch_augment(self, images):\n#         for transform in self.transforms:\n#             images = transform.batch_augment(images)\n#         return images\n    \n#     def prepare_boxes(self, boxes):\n#         result_boxes = boxes.copy()\n#         result_boxes[:,0] = np.min(boxes[:, [0,2]], axis=1)\n#         result_boxes[:,2] = np.max(boxes[:, [0,2]], axis=1)\n#         result_boxes[:,1] = np.min(boxes[:, [1,3]], axis=1)\n#         result_boxes[:,3] = np.max(boxes[:, [1,3]], axis=1)\n#         return result_boxes\n    \n#     def deaugment_boxes(self, boxes):\n#         for transform in self.transforms[::-1]:\n#             boxes = transform.deaugment_boxes(boxes)\n#         return self.prepare_boxes(boxes)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.372494Z","iopub.execute_input":"2022-01-11T17:13:41.373218Z","iopub.status.idle":"2022-01-11T17:13:41.381703Z","shell.execute_reply.started":"2022-01-11T17:13:41.373163Z","shell.execute_reply":"2022-01-11T17:13:41.380656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from itertools import product\n\n# tta_transforms = []\n# for tta_combination in product([TTAHorizontalFlip(), None], \n#                                [TTAVerticalFlip(), None],\n#                                [TTARotate90(), None]):\n#     tta_transforms.append(TTACompose([tta_transform for tta_transform in tta_combination if tta_transform]))","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.383447Z","iopub.execute_input":"2022-01-11T17:13:41.384058Z","iopub.status.idle":"2022-01-11T17:13:41.391692Z","shell.execute_reply.started":"2022-01-11T17:13:41.384019Z","shell.execute_reply":"2022-01-11T17:13:41.390926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # 执行 TTA\n# def make_tta_predictions(images, score_threshold=0.25):\n#     with torch.no_grad():\n#         images = torch.stack(images).float().cuda()\n#         predictions = []\n#         for tta_transform in tta_transforms:\n#             result = []\n#             det = net(tta_transform.batch_augment(images.clone()), torch.tensor([1]*images.shape[0]).float().cuda())\n\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#                 boxes = tta_transform.deaugment_boxes(boxes.copy())\n#                 result.append({\n#                     'boxes': boxes,\n#                     'scores': scores[indexes],\n#                 })\n#             predictions.append(result)\n#     return predictions\n# # 执行模型的融合\n# def 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]).astype(int).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":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.393292Z","iopub.execute_input":"2022-01-11T17:13:41.393811Z","iopub.status.idle":"2022-01-11T17:13:41.399986Z","shell.execute_reply.started":"2022-01-11T17:13:41.393768Z","shell.execute_reply":"2022-01-11T17:13:41.398885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# results_plabel = []\n\n# for images, image_ids in data_loader:\n#     predictions = make_tta_predictions(images)\n#     for i, image in enumerate(images):\n#         image_id = image_ids[i]\n#         image_ = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)\n#         h,w,_ = np.shape(image_)\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#         # make plabel\n#         for box in boxes:\n#             result_p = {\n#                 'image_id': image_id,\n#                 'width':w,\n#                 'height':h,\n#                 'source':'usask_1',\n#                 'x':box[0],\n#                 'y':box[1],\n#                 'w':box[2],\n#                 'h':box[3],\n#             }\n#             results_plabel.append(result_p)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.401571Z","iopub.execute_input":"2022-01-11T17:13:41.402216Z","iopub.status.idle":"2022-01-11T17:13:41.409855Z","shell.execute_reply.started":"2022-01-11T17:13:41.402119Z","shell.execute_reply":"2022-01-11T17:13:41.409123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# results_df = pd.DataFrame(results_plabel, columns=['image_id', 'width','height','source','x','y','w','h'])\n# results_df['image_id'] = results_df['image_id'].apply(lambda x: DATA_ROOT_PATH+'/'+ str(x)+'.jpg')\n# results_df.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.411426Z","iopub.execute_input":"2022-01-11T17:13:41.411820Z","iopub.status.idle":"2022-01-11T17:13:41.419987Z","shell.execute_reply.started":"2022-01-11T17:13:41.411780Z","shell.execute_reply":"2022-01-11T17:13:41.419255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # 这里和训练的代码保持一致\n# marking = pd.read_csv('../input/gwb-train/train.csv')\n# def tran_id(x):\n#     if(len(str(x)) < 4):\n#             x = '0'*(4-len(str(x)))+str(x)\n#     return x\n# marking['image_id'] = marking['image_id'].apply(tran_id)\n# bboxs = np.stack(marking['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))\n# for i, column in enumerate(['x', 'y', 'w', 'h']):\n#     marking[column] = bboxs[:,i]\n# marking.drop(columns=['bbox'], inplace=True)\n# print(marking.head())\n# TRAIN_ROOT_PATH = '../input/gwbtrain/train'\n# marking['image_id'] = marking['image_id'].apply(lambda x: TRAIN_ROOT_PATH+'/'+ str(x)+'.jpg')\n# print(marking.head())\n# # 把测试集的 plable 和 训练集拼接到一起\n# train_data_plabel = pd.concat([results_df,marking], axis=0)\n# print(train_data_plabel.head())","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.423449Z","iopub.execute_input":"2022-01-11T17:13:41.423799Z","iopub.status.idle":"2022-01-11T17:13:41.429216Z","shell.execute_reply.started":"2022-01-11T17:13:41.423767Z","shell.execute_reply":"2022-01-11T17:13:41.428223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.model_selection import StratifiedKFold\n# # 分层交叉验证\n# skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)\n\n# df_folds = train_data_plabel[['image_id']].copy()\n# df_folds.loc[:, 'bbox_count'] = 1\n# df_folds = df_folds.groupby('image_id').count()\n# df_folds.loc[:, 'source'] = marking[['image_id', 'source']].groupby('image_id').min()['source']\n# df_folds.loc[:, 'stratify_group'] = np.char.add(\n#     df_folds['source'].values.astype(str),\n#     df_folds['bbox_count'].apply(lambda x: f'_{x // 15}').values.astype(str)\n# )\n# df_folds.loc[:, 'fold'] = 0\n\n# for fold_number, (train_index, val_index) in enumerate(skf.split(X=df_folds.index, y=df_folds['stratify_group'])):\n#     df_folds.loc[df_folds.iloc[val_index].index, 'fold'] = fold_number","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.430715Z","iopub.execute_input":"2022-01-11T17:13:41.431315Z","iopub.status.idle":"2022-01-11T17:13:41.437413Z","shell.execute_reply.started":"2022-01-11T17:13:41.431277Z","shell.execute_reply":"2022-01-11T17:13:41.436683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class DatasetRetriever(Dataset):\n\n#     def __init__(self, marking, image_ids, transforms=None, test=False):\n#         super().__init__()\n\n#         self.image_ids = image_ids\n#         self.marking = marking\n#         self.transforms = transforms\n#         self.test = test\n\n#     def __getitem__(self, index: int):\n#         image_id = self.image_ids[index]\n        \n#         if self.test or random.random() > 0.5:\n#             image, boxes = self.load_image_and_boxes(index)\n#         else:\n#             image, boxes = self.load_cutmix_image_and_boxes(index)\n\n#         # there is only one class\n#         labels = torch.ones((boxes.shape[0],), dtype=torch.int64)\n        \n#         target = {}\n#         target['boxes'] = boxes\n#         target['labels'] = labels\n#         target['image_id'] = torch.tensor([index])\n\n#         if self.transforms:\n#             for i in range(10):\n#                 sample = self.transforms(**{\n#                     'image': image,\n#                     'bboxes': target['boxes'],\n#                     'labels': labels\n#                 })\n#                 if len(sample['bboxes']) > 0:\n#                     image = sample['image']\n#                     target['boxes'] = torch.stack(tuple(map(torch.tensor, zip(*sample['bboxes'])))).permute(1, 0)\n#                     target['boxes'][:,[0,1,2,3]] = target['boxes'][:,[1,0,3,2]]  #yxyx: be warning\n#                     break\n\n#         return image, target, image_id\n\n#     def __len__(self) -> int:\n#         return self.image_ids.shape[0]\n\n#     def load_image_and_boxes(self, index):\n#         image_id = self.image_ids[index]\n#         image = cv2.imread(image_id, cv2.IMREAD_COLOR)\n#         image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n#         image /= 255.0\n#         records = self.marking[self.marking['image_id'] == image_id]\n#         boxes = records[['x', 'y', 'w', 'h']].values\n#         boxes[:, 2] = boxes[:, 0] + boxes[:, 2]\n#         boxes[:, 3] = boxes[:, 1] + boxes[:, 3]\n#         return image, boxes\n\n#     def load_cutmix_image_and_boxes(self, index, imsize=1024):\n#         \"\"\" \n#         This implementation of cutmix author:  https://www.kaggle.com/nvnnghia \n#         Refactoring and adaptation: https://www.kaggle.com/shonenkov\n#         \"\"\"\n#         w, h = imsize, imsize\n#         s = imsize // 2\n    \n#         xc, yc = [int(random.uniform(imsize * 0.25, imsize * 0.75)) for _ in range(2)]  # center x, y\n#         indexes = [index] + [random.randint(0, self.image_ids.shape[0] - 1) for _ in range(3)]\n\n#         result_image = np.full((imsize, imsize, 3), 1, dtype=np.float32)\n#         result_boxes = []\n\n#         for i, index in enumerate(indexes):\n#             image, boxes = self.load_image_and_boxes(index)\n#             if i == 0:\n#                 x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc  # xmin, ymin, xmax, ymax (large image)\n#                 x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h  # xmin, ymin, xmax, ymax (small image)\n#             elif i == 1:  # top right\n#                 x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc\n#                 x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h\n#             elif i == 2:  # bottom left\n#                 x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)\n#                 x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, max(xc, w), min(y2a - y1a, h)\n#             elif i == 3:  # bottom right\n#                 x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)\n#                 x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)\n#             result_image[y1a:y2a, x1a:x2a] = image[y1b:y2b, x1b:x2b]\n#             padw = x1a - x1b\n#             padh = y1a - y1b\n\n#             boxes[:, 0] += padw\n#             boxes[:, 1] += padh\n#             boxes[:, 2] += padw\n#             boxes[:, 3] += padh\n\n#             result_boxes.append(boxes)\n\n#         result_boxes = np.concatenate(result_boxes, 0)\n#         np.clip(result_boxes[:, 0:], 0, 2 * s, out=result_boxes[:, 0:])\n#         result_boxes = result_boxes.astype(np.int32)\n#         result_boxes = result_boxes[np.where((result_boxes[:,2]-result_boxes[:,0])*(result_boxes[:,3]-result_boxes[:,1]) > 0)]\n#         return result_image, result_boxes","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.439048Z","iopub.execute_input":"2022-01-11T17:13:41.439526Z","iopub.status.idle":"2022-01-11T17:13:41.447065Z","shell.execute_reply.started":"2022-01-11T17:13:41.439430Z","shell.execute_reply":"2022-01-11T17:13:41.446222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fold_number = 0\n\n# train_dataset = DatasetRetriever(\n#     image_ids=df_folds[df_folds['fold'] != fold_number].index.values,\n#     marking=train_data_plabel,\n#     transforms=get_train_transforms(),\n#     test=False,\n# )\n\n# validation_dataset = DatasetRetriever(\n#     image_ids=df_folds[df_folds['fold'] == fold_number].index.values,\n#     marking=train_data_plabel,\n#     transforms=get_valid_transforms(),\n#     test=True,\n# )\n","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.448570Z","iopub.execute_input":"2022-01-11T17:13:41.449226Z","iopub.status.idle":"2022-01-11T17:13:41.456401Z","shell.execute_reply.started":"2022-01-11T17:13:41.449186Z","shell.execute_reply":"2022-01-11T17:13:41.455019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def collate_fn(batch):\n#     return tuple(zip(*batch))\n\n# def run_training():\n#     device = torch.device('cuda:0')\n#     net.to(device)\n\n#     train_loader = torch.utils.data.DataLoader(\n#         train_dataset,\n#         batch_size=TrainGlobalConfig.batch_size,\n#         sampler=RandomSampler(train_dataset),\n#         pin_memory=False,\n#         drop_last=True,\n#         num_workers=TrainGlobalConfig.num_workers,\n#         collate_fn=collate_fn,\n#     )\n#     val_loader = torch.utils.data.DataLoader(\n#         validation_dataset, \n#         batch_size=TrainGlobalConfig.batch_size,\n#         num_workers=TrainGlobalConfig.num_workers,\n#         shuffle=False,\n#         sampler=SequentialSampler(validation_dataset),\n#         pin_memory=False,\n#         collate_fn=collate_fn,\n#     )\n\n#     fitter = Fitter(model=net, device=device, config=TrainGlobalConfig)\n#     fitter.fit(train_loader, val_loader)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.457908Z","iopub.execute_input":"2022-01-11T17:13:41.458349Z","iopub.status.idle":"2022-01-11T17:13:41.466165Z","shell.execute_reply.started":"2022-01-11T17:13:41.458311Z","shell.execute_reply":"2022-01-11T17:13:41.465200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from effdet import get_efficientdet_config, EfficientDet, DetBenchTrain\n# from effdet.efficientdet import HeadNet\n\n# def get_net():\n\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#     checkpoint = torch.load('../input/logtest/last-checkpoint (1).bin')\n#     net.load_state_dict(checkpoint['model_state_dict'])\n   \n#     return DetBenchTrain(net, config)\n\n# net = get_net()\n","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.467908Z","iopub.execute_input":"2022-01-11T17:13:41.468268Z","iopub.status.idle":"2022-01-11T17:13:41.474613Z","shell.execute_reply.started":"2022-01-11T17:13:41.468225Z","shell.execute_reply":"2022-01-11T17:13:41.473472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class AverageMeter(object):\n#     \"\"\"Computes and stores the average and current value\"\"\"\n#     def __init__(self):\n#         self.reset()\n\n#     def reset(self):\n#         self.val = 0\n#         self.avg = 0\n#         self.sum = 0\n#         self.count = 0\n\n#     def update(self, val, n=1):\n#         self.val = val\n#         self.sum += val * n\n#         self.count += n\n#         self.avg = self.sum / self.count","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.477370Z","iopub.execute_input":"2022-01-11T17:13:41.478078Z","iopub.status.idle":"2022-01-11T17:13:41.482528Z","shell.execute_reply.started":"2022-01-11T17:13:41.478039Z","shell.execute_reply":"2022-01-11T17:13:41.481503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import ensemble_boxes \n# import torch\n# import numpy as np\n# import pandas as pd\n# from glob import glob\n# from torch.utils.data import Dataset,DataLoader\n# import albumentations as A\n# from albumentations.pytorch.transforms import ToTensorV2\n# import cv2\n# import gc\n# from matplotlib import pyplot as plt\n# from effdet import get_efficientdet_config, EfficientDet, DetBenchEval\n# from effdet.efficientdet import HeadNet\n# import os\n# from datetime import datetime\n# import time\n# import random\n# from sklearn.model_selection import StratifiedKFold\n# from torch.utils.data.sampler import SequentialSampler, RandomSampler\n\n\n# run_training()","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.484709Z","iopub.execute_input":"2022-01-11T17:13:41.485237Z","iopub.status.idle":"2022-01-11T17:13:41.494470Z","shell.execute_reply.started":"2022-01-11T17:13:41.485198Z","shell.execute_reply":"2022-01-11T17:13:41.493590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# wbf = pd.DataFrame(results2, columns=['image_id', 'PredictionString','scores'])\n# wbf.sort_values('image_id',inplace=True)\n# f = open('store.pckl', 'wb')\n# pickle.dump(wbf, f)\n# f.close()\n","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.496048Z","iopub.execute_input":"2022-01-11T17:13:41.496569Z","iopub.status.idle":"2022-01-11T17:13:41.503064Z","shell.execute_reply.started":"2022-01-11T17:13:41.496530Z","shell.execute_reply":"2022-01-11T17:13:41.502228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pickle\n\n# 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)\n\n# DATA_ROOT_PATH = '../input/gwb-test/test'\n\n# class 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]\n    \n\n# 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\n# def collate_fn(batch):\n#     return tuple(zip(*batch))\n\n# data_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# )\n\n    \n# 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\n# net = load_net('./plabel_model/last-checkpoint1.bin')\n\n    \n# def make_predictions(images, score_threshold=0.3):\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\n# def 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\n\n    \n# import matplotlib.pyplot as plt\n\n# for j, (images, image_ids) in enumerate(data_loader):\n#     if j==2:\n#         break\n\n# predictions = make_predictions(images)\n# print(image_ids)\n# i = 0\n# sample = images[i].permute(1,2,0).cpu().numpy()\n\n# boxes, scores, labels = run_wbf(predictions, image_index=i)\n# boxes = boxes.astype(np.int32).clip(min=0, max=511)\n\n# fig, ax = plt.subplots(1, 1, figsize=(16, 8))\n\n# for box in boxes:\n#     cv2.rectangle(sample, (box[0], box[1]), (box[2], box[3]), (1, 0, 0), 2)\n    \n# ax.set_axis_off()\n# ax.imshow(sample);\n    \n# def format_prediction_string(boxes):\n#     sign=0\n#     for j in boxes:\n#         if sign==0:\n#             strings=str(j)\n#         else:\n#             strings+=' '+str(j)\n#         sign+=1\n#     return strings\n    \n# results = []\n# results2= []\n\n# for 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#         for j in range(boxes.shape[0]):\n#             result = {\n#             'image_id': image_id,\n#             'PredictionString': format_prediction_string(boxes[j,:])\n#              }\n#             boxes[j, 2] = boxes[j, 2] + boxes[j, 0]\n#             boxes[j, 3] = boxes[j, 3] + boxes[j, 1]\n#             result2 = {\n#             'image_id': image_id,\n#             'PredictionString': boxes[j,:],\n#              'scores': scores[j]\n#              }\n#             results.append(result)\n#             results2.append(result2)\n\n# test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])\n# test_df.sort_values('image_id',inplace=True)\n# test_df.to_csv('submission.csv', index=False)\n# test_df.head()\n\n# # wbf = pd.DataFrame(results2, columns=['image_id', 'PredictionString','scores'])\n# # wbf.sort_values('image_id',inplace=True)\n# # f = open('store.pckl', 'wb')\n# # pickle.dump(wbf, f)\n# # f.close()\n","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:13:41.504809Z","iopub.execute_input":"2022-01-11T17:13:41.505474Z","iopub.status.idle":"2022-01-11T17:13:41.514268Z","shell.execute_reply.started":"2022-01-11T17:13:41.505434Z","shell.execute_reply":"2022-01-11T17:13:41.513164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nimport pandas as pd\nf = open('./store1.pckl', 'rb') \ndata = pickle.load(f) \nf.close()\ndata2=pd.read_pickle('store1.pckl')\ndata2 = data2.reindex(labels=range(len(data2)))\nprint(data2)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T17:15:43.034815Z","iopub.execute_input":"2022-01-11T17:15:43.035160Z","iopub.status.idle":"2022-01-11T17:15:43.141840Z","shell.execute_reply.started":"2022-01-11T17:15:43.035110Z","shell.execute_reply":"2022-01-11T17:15:43.140867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_index = 0\nimage_size = 1024\nprint(predictions)\nboxes = [(prediction[image_index]['boxes']).tolist()  for prediction in predictions]\n# def 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":{"execution":{"iopub.status.busy":"2022-01-11T17:15:46.854647Z","iopub.execute_input":"2022-01-11T17:15:46.854979Z","iopub.status.idle":"2022-01-11T17:15:46.868244Z","shell.execute_reply.started":"2022-01-11T17:15:46.854945Z","shell.execute_reply":"2022-01-11T17:15:46.866824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}