{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\nimport sys\nsys.path.insert(0, \"../input/weightedboxesfusion\")\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport torch\nfrom torchvision import transforms\nfrom matplotlib import pyplot as plt\nfrom torchvision import transforms\nimport torchvision \nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\nfrom torchvision.models.detection import FasterRCNN\nfrom torch.utils.data import Dataset,DataLoader\nimport glob\nimport albumentations as A\nimport cv2\nfrom albumentations.pytorch.transforms import ToTensorV2\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\nprint(device)\nfrom ensemble_boxes import *\n\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=False,pretrained_backbone=False)\nnum_classes = 2 \nin_features = model.roi_heads.box_predictor.cls_score.in_features\nmodel.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)\nmodel.__name__ = \"fpn_resnet\"\nmodel.to(device)\n# model = load_checkpoint(MODEL_PATH,model)\nmodel.eval()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"checkpoint = torch.load('../input/gwdmodels1/cp512.pt')\nmodel.load_state_dict(checkpoint['model_state_dict'])\nepoch = checkpoint['epoch']\nloss_train = checkpoint['loss_train']\nloss_val = checkpoint['loss_val']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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\n    return \" \".join(pred_strings)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DATA_ROOT_PATH = '../input/global-wheat-detection/test/'\n\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]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset = DatasetRetriever(np.array([path.split('/')[-1][:-4] for path in glob.glob(f'{DATA_ROOT_PATH}/*.jpg')]),get_valid_transforms())\n\ndef collate_fn(batch):\n    return tuple(zip(*batch))\n\ndata_loader = DataLoader(\n    dataset,\n    batch_size=4,\n    shuffle=False,\n    num_workers=2,\n    drop_last=False,\n    collate_fn=collate_fn\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch = iter(data_loader)\nimgs,img_id = next(batch)\nimages = list(img.to(device) for img in imgs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results = []\nmodel.eval()\nmodel.to(device)\npredictions = model(images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def print_boxes(images,labels,threshold=None):\n  plt.figure(figsize=[25, 20])\n  for i, (img, label) in enumerate(zip(images, labels)):\n    plt.subplot(4, 4, i+1)\n    # boxes = label['boxes'].cpu().numpy().astype(np.int32)\n    boxes = label['boxes']\n    scores=None\n    if 'scores' in label.keys():\n      scores=label['scores']\n    sample = img.permute(1,2,0).cpu().numpy()\n    sample = sample*255.0\n    for j in range(len(boxes)):\n      box=boxes[j]\n      active=True\n\n      if scores is not None: \n        score=str(float(\"{:.3f}\".format(scores[j])))\n        if threshold is not None and float(score)<threshold:\n          active=False\n      else:\n        score=''\n      \n      if active:\n        cv2.rectangle(sample,\n                    (box[0], box[1]),\n                    (box[2], box[3]),\n                    (255, 0, 0), 1)\n        \n        cv2.putText(\n          sample,\n          score,\n          org=(int(box[0]), int(box[1] )), # bottom left\n          fontFace=cv2.FONT_HERSHEY_PLAIN,\n          fontScale=0.7,\n          color=(255,0, 0),\n          thickness=1\n        )\n\n      plt.imshow(sample.astype(np.uint8))    \n    plt.axis('off')\n  plt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print_boxes(images,predictions)# Tests~!","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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\nclass 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\nclass 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    \nclass 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    \nclass TTARotate180(BaseWheatTTA):\n    \"\"\" author: @shonenkov \"\"\"\n    \n    def augment(self, image):\n        return torch.rot90(image, 2, (1, 2))\n\n    def batch_augment(self, images):\n        return torch.rot90(images, 2, (2, 3))\n    \n    def deaugment_boxes(self, boxes):\n        boxes[:, [0,1,2,3]] = self.image_size - boxes[:, [2,3,0,1]]\n        return boxes\n    \nclass TTARotate270(BaseWheatTTA):\n    \"\"\" author: @shonenkov \"\"\"\n    \n    def augment(self, image):\n        return torch.rot90(image, 3, (1, 2))\n\n    def batch_augment(self, images):\n        return torch.rot90(images, 3, (2, 3))\n    \n    def deaugment_boxes(self, boxes):\n        res_boxes = boxes.copy()\n        res_boxes[:, [0,2]] = boxes[:, [1,3]]\n        res_boxes[:, [1,3]] = self.image_size - boxes[:, [2,0]]\n        return res_boxes\n    \nclass 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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def make_tta_predictions(images, score_threshold=0.33):\n  model.eval()\n  model.to(device)\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 = model(tta_transform.batch_augment(images.clone()))\n\n          for i in range(images.shape[0]):\n              boxes = det[i]['boxes'].detach().cpu().numpy()[:,:4]    \n              scores = det[i]['scores'].detach().cpu().numpy()[:]\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\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)).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 = ensemble_boxes_wbf.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","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"from itertools import product\n\ntta_transforms = []\nfor tta_combination in product([TTAHorizontalFlip(), None], \n                               [TTAVerticalFlip(), None],\n                               [TTARotate90(), TTARotate180(), TTARotate270(), None]):\n    tta_transforms.append(TTACompose([tta_transform for tta_transform in tta_combination if tta_transform]))\n\nvalidation_image_precisions = []\niou_thresholds = [x for x in np.arange(0.5, 0.76, 0.05)]\nmodel.eval()\nmodel.to(device)\n\nresults = []\n    \nfor images, image_ids in data_loader:\n    predictions = make_tta_predictions(images)\n    for i,img in enumerate(images):\n        # sample = images[i].permute(1,2,0).cpu().numpy()\n        boxes, scores, labels = run_wbf(predictions, image_index=i)\n        boxes_s = (boxes*(2)).astype(np.int32).clip(min=0, max=1023)\n        image_id = image_ids[i]\n#         boxes = boxes.round().astype(np.int32).clip(min=0, max=IMAGE_SIZE-1)\n        # images = list(img.to(device) for img in images)\n        # outputs = model(images)\n        # for output, target in zip(outputs, targets):\n        #     preds = output['boxes'].cpu().detach().numpy()\n        #     scores = output['scores'].cpu().detach().numpy()\n#         gt_boxes = targets[i]['boxes'].detach().numpy()\n#         preds_sorted_idx = np.argsort(scores)[::-1]\n#         preds_sorted = boxes[preds_sorted_idx]\n\n        boxes_s[:, 2] = boxes_s[:, 2] - boxes_s[:, 0]\n        boxes_s[:, 3] = boxes_s[:, 3] - boxes_s[:, 1]\n#         scores = img['scores']\n        result = {\n          'image_id': image_id,\n          'PredictionString': format_prediction_string(boxes_s,scores)\n        }\n        results.append(result)  \n        # # for idx, image in enumerate(images):\n#         image_precision = calculate_image_precision(preds_sorted,\n#                                                     gt_boxes,\n#                                                     thresholds=iou_thresholds,\n#                                                     form='pascal_voc')\n\n#         validation_image_precisions.append(image_precision)\n\n# print(\"Validation IOU: {0:.4f}\".format(np.mean(validation_image_precisions)))","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"# results = []\n# model.eval()\n# model.to(device)\n# for imgs, image_ids in data_loader:\n#   images = list(img.to(device) for img in imgs)\n#   predictions = model(images)\n#   for i, pred in enumerate(predictions):\n#       boxes = pred['boxes']\n#       boxes=boxes.cpu().detach().numpy()\n#       boxes = (boxes*(1024/224)).round().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#       scores = pred['scores']\n#       result = {\n#           'image_id': image_id,\n#           'PredictionString': format_prediction_string(boxes,scores)\n#       }\n#       results.append(result)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])\ntest_df.to_csv('submission.csv', index=False)\ntest_df.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}