{"cells":[{"metadata":{},"cell_type":"markdown","source":"overview","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# Classes","execution_count":null},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"import glob\nimport os\nfrom torch.utils.data import Dataset,DataLoader\nimport cv2\nimport numpy as np\nimport torchvision\nfrom torchvision.models.detection import FasterRCNN\nfrom torchvision.models.detection.rpn import AnchorGenerator\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\nimport itertools\nimport torch\nimport time\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport albumentations as A\nfrom albumentations.pytorch.transforms import ToTensorV2","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# datasets","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true,"scrolled":true},"cell_type":"code","source":"\nclass MyDataset(Dataset):\n    def __init__(self, path, transform=None):\n        self.image_paths = glob.glob(os.path.join(path, '*.jpg'))\n        self.transform = transform\n\n    def __getitem__(self, index):\n        image = cv2.imread(self.image_paths[index], cv2.IMREAD_COLOR)  # reading an image\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)  # changing color space BGR --> RGB\n        image /= 255.0\n        if self.transform:\n            data = {'image': image}\n            data = self.transform(**data)\n        return data['image'], self.image_paths[index]  # return image tensor , path to image\n\n    def __len__(self):\n        return len(self.image_paths)\n\n    \n\n# reference : XXX need to add\nclass WheatDataset(Dataset):\n    def __init__(self, dataframe, image_dir, transforms=None):\n        super().__init__()\n        self.image_ids = dataframe['image_id'].unique()\n        self.df = dataframe\n        self.image_dir = image_dir\n        self.transforms = transforms\n\n    def __getitem__(self, index: int):\n        image_id = self.image_ids[index]\n        records = self.df[self.df['image_id'] == image_id]\n\n        image = cv2.imread(f'{self.image_dir}/{image_id}.jpg', cv2.IMREAD_COLOR)  # reading an image\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)  # changing color space BGR --> RGB\n        image /= 255.0\n\n        boxes = records[['x', 'y', 'w', 'h']].to_numpy()\n        area = (boxes[:, 3]) * (boxes[:, 2])  # Calculating area of boxes W * H\n        boxes[:, 2] = boxes[:, 0] + boxes[:, 2]  # upper coordinate X + W\n        boxes[:, 3] = boxes[:, 1] + boxes[:, 3]  # lower coordinate Y + H\n        area = torch.as_tensor(area, dtype=torch.float32)\n\n        # there is only one class\n        labels = torch.ones((records.shape[0],), dtype=torch.int64)\n\n        # suppose all instances are not crowd\n        iscrowd = torch.zeros((records.shape[0],), dtype=torch.int64)\n\n        target = {'boxes': boxes, 'labels': labels, 'image_id': torch.tensor([index]), 'area': area, 'iscrowd': iscrowd}\n\n        if self.transforms:\n            sample = {\n                'image': image,\n                'bboxes': target['boxes'],\n                'labels': labels\n            }\n            sample = self.transforms(**sample)\n            image = sample['image']\n\n            target['boxes'] = torch.tensor(sample['bboxes']).float()\n            return image, target, f'{self.image_dir}/{image_id}.jpg'  # image Tensor , target with boxes , path to image\n\n    def __len__(self):\n        return self.image_ids.shape[0]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"\nclass DatasetUtils:\n    @staticmethod\n    def collate_fn(batch):\n        return tuple(zip(*batch))\n\n    @staticmethod\n    def preprocessing_csv(data_frame):\n        # seperate bbox to x,y,w,h columns\n        seperator = lambda x: np.fromstring(x[1:-1], sep=',')\n        bbox = np.stack(data_frame['bbox'].apply(seperator))\n        for i, dim in enumerate(['x', 'y', 'w', 'h']):\n            data_frame[dim] = bbox[:, i]\n        data_frame.drop(columns='bbox', inplace=True)\n\n    @staticmethod\n    def splitData(all_data_records: pd.DataFrame, test_size=0.33):\n        image_ids = all_data_records['image_id'].unique()\n        train_ids, valid_ids = train_test_split(image_ids, test_size=test_size, random_state=42)\n        valid_df = all_data_records[all_data_records['image_id'].isin(valid_ids)]\n        train_df = all_data_records[all_data_records['image_id'].isin(train_ids)]\n        return train_df, valid_df\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# networks","execution_count":null},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"\nclass Desnet121:\n    @staticmethod\n    def getNet():\n        # load a pre-trained model for classification and return\n        # only the features\n        backbone = torchvision.models.densenet121(pretrained=False).features\n        backbone.out_channels = 1024\n        anchor_generator = AnchorGenerator(sizes=((32, 64, 128, 256, 512),),\n                                           aspect_ratios=((0.5, 1.0, 2.0),))\n        roi_pooler = torchvision.ops.MultiScaleRoIAlign(featmap_names=['0'],\n                                                        output_size=7,\n                                                        sampling_ratio=2)\n        # put the pieces together inside a FasterRCNN model\n        densenet121 = FasterRCNN(backbone,\n                                 num_classes=2,\n                                 rpn_anchor_generator=anchor_generator,\n                                 box_roi_pool=roi_pooler)\n\n        densenet121.__name__ = \"densenet121\"\n\n        return densenet121\n\nclass FpnResenet50:\n    @staticmethod\n    def getNet():\n        fpn_resnet = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=False,pretrained_backbone=False)\n        num_classes = 2  # 1 class (wheat) + background\n        # get number of input features for the classifier\n        in_features = fpn_resnet.roi_heads.box_predictor.cls_score.in_features\n        # replace the pre-trained head with a new one\n        fpn_resnet.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)\n\n        fpn_resnet.__name__ = \"fpn_resnet\"\n\n        return fpn_resnet\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# eval model","execution_count":null},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"\nclass EvalUtils:\n    @staticmethod\n    def _predictEnsemble(models, device, image, detection_threshold=0.9, iou_thr=0.5):\n        boxes_list = []\n        scores_list = []\n        image = image.to(device)\n        for model in models:\n            model.to(device)\n            model.eval()\n            with torch.no_grad():\n                outputs = model(image)\n                boxes = outputs[0]['boxes'].data.detach().cpu().numpy()\n                scores = outputs[0]['scores'].data.detach().cpu().numpy()\n                boxes = boxes[scores >= detection_threshold]\n                scores = scores[scores >= detection_threshold]\n                boxes_list.append(boxes)\n                scores_list.append(scores)\n\n        boxes_list = list(itertools.chain.from_iterable(boxes_list))  # flat array\n        boxNumpy = np.array(boxes_list)\n        scores_list = list(itertools.chain.from_iterable(scores_list))  # flat array\n        scoreNumpy = np.array(scores_list)\n        boxesTensor = torch.tensor(boxNumpy)\n        scoreTensor = torch.tensor(scoreNumpy)\n\n        indexKeep = torchvision.ops.nms(boxesTensor, scoreTensor, iou_thr)\n        boxesTensor = boxesTensor[indexKeep]\n        scoreTensor = scoreTensor[indexKeep]\n\n        return boxesTensor, scoreTensor\n\n    @staticmethod\n    def evalEnsembleModels(models, device, data_loader, detection_threshold=0.9, iou_thr=0.5):\n\n        d = next(iter(data_loader))\n        trainMode = True\n        if len(d) == 2:\n            trainMode = False\n\n        results = []\n        for data in data_loader:\n            if trainMode:\n                images, targets, image_path = data\n                targets = [{k: v.to(device) for k, v in t.items()} for t in targets]\n                images = list(image.to(device) for image in images)\n                with torch.no_grad():\n                    for image, target in zip(images, targets):\n                        image = image.unsqueeze(0)\n                        boxes_pred, score_pred = EvalUtils._predictEnsemble(models, device, image,\n                                                                            detection_threshold=detection_threshold,\n                                                                            iou_thr=iou_thr)\n                        gt_boxes = target['boxes'].cpu().numpy()\n                        image = image.squeeze(0).permute(1, 2, 0).detach().cpu().numpy()\n                        results.append((image, boxes_pred, score_pred, gt_boxes))\n            else:\n                images, image_paths = data\n                images = list(image.to(device) for image in images)\n                with torch.no_grad():\n                    for image, image_path in zip(images, image_paths):\n                        image = image.unsqueeze(0)\n                        boxes_pred, score_pred = EvalUtils._predictEnsemble(models, device, image,\n                                                                            detection_threshold=detection_threshold,\n                                                                            iou_thr=iou_thr)\n                        image = image.squeeze(0).permute(1, 2, 0).detach().cpu().numpy()\n                        results.append((image, boxes_pred, score_pred, image_path))\n\n        return results\n\n    \n    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# metrices","execution_count":null},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"\nclass Metrics:\n    @staticmethod\n    def calculate_iou(gt, pr, form='pascal_voc') -> float:\n        \"\"\"Calculates the Intersection over Union.\n\n        Args:\n            gt: (np.ndarray[Union[int, float]]) coordinates of the ground-truth box\n            pr: (np.ndarray[Union[int, float]]) coordinates of the prdected box\n            form: (str) gt/pred coordinates format\n                - pascal_voc: [xmin, ymin, xmax, ymax]\n                - coco: [xmin, ymin, w, h]\n        Returns:\n            (float) Intersection over union (0.0 <= iou <= 1.0)\n        \"\"\"\n        if form == 'coco':\n            gt = gt.copy()\n            pr = pr.copy()\n\n            gt[2] = gt[0] + gt[2]\n            gt[3] = gt[1] + gt[3]\n            pr[2] = pr[0] + pr[2]\n            pr[3] = pr[1] + pr[3]\n\n        # Calculate overlap area\n        dx = min(gt[2], pr[2]) - max(gt[0], pr[0]) + 1\n\n        if dx < 0:\n            return 0.09\n        dy = min(gt[3], pr[3]) - max(gt[1], pr[1]) + 1\n\n        if dy < 0:\n            return 0.0\n\n        overlap_area = dx * dy\n\n        # Calculate union area\n        union_area = (\n                (gt[2] - gt[0] + 1) * (gt[3] - gt[1] + 1) +\n                (pr[2] - pr[0] + 1) * (pr[3] - pr[1] + 1) -\n                overlap_area\n        )\n\n        return overlap_area / union_area\n\n    @staticmethod\n    def find_best_match(gts, pred, pred_idx, threshold=0.5, form='pascal_voc', ious=None) -> int:\n        \"\"\"Returns the index of the 'best match' between the\n        ground-truth boxes and the prediction. The 'best match'\n        is the highest IoU. (0.0 IoUs are ignored).\n\n        Args:\n            gts: (List[List[Union[int, float]]]) Coordinates of the available ground-truth boxes\n            pred: (List[Union[int, float]]) Coordinates of the predicted box\n            pred_idx: (int) Index of the current predicted box\n            threshold: (float) Threshold\n            form: (str) Format of the coordinates\n            ious: (np.ndarray) len(gts) x len(preds) matrix for storing calculated ious.\n\n        Return:\n            (int) Index of the best match GT box (-1 if no match above threshold)\n        \"\"\"\n        best_match_iou = -np.inf\n        best_match_idx = -1\n        for gt_idx in range(len(gts)):\n\n            if gts[gt_idx][0] < 0:\n                # Already matched GT-box\n                continue\n\n            iou = -1 if ious is None else ious[gt_idx][pred_idx]\n\n            if iou < 0:\n                iou = Metrics.calculate_iou(gts[gt_idx], pred, form=form)\n\n                if ious is not None:\n                    ious[gt_idx][pred_idx] = iou\n\n            if iou < threshold:\n                continue\n\n            if iou > best_match_iou:\n                best_match_iou = iou\n                best_match_idx = gt_idx\n\n        return best_match_idx\n\n    @staticmethod\n    def calculate_precision(gts, preds, threshold=0.5, form='coco', ious=None) -> float:\n        \"\"\"Calculates precision for GT - prediction pairs at one threshold.\n\n        Args:\n            gts: (List[List[Union[int, float]]]) Coordinates of the available ground-truth boxes\n            preds: (List[List[Union[int, float]]]) Coordinates of the predicted boxes,\n                   sorted by confidence value (descending)\n            threshold: (float) Threshold\n            form: (str) Format of the coordinates\n            ious: (np.ndarray) len(gts) x len(preds) matrix for storing calculated ious.\n\n        Return:\n            (float) Precision\n        \"\"\"\n        n = len(preds)\n        tp = 0\n        fp = 0\n\n        for pred_idx in range(n):\n\n            best_match_gt_idx = Metrics.find_best_match(gts, preds[pred_idx], pred_idx,\n                                                        threshold=threshold, form=form, ious=ious)\n\n            if best_match_gt_idx >= 0:\n                # True positive: The predicted box matches a gt box with an IoU above the threshold.\n                tp += 1\n                # Remove the matched GT box\n                gts[best_match_gt_idx] = -1\n            else:\n                # No match\n                # False positive: indicates a predicted box had no associated gt box.\n                fp += 1\n\n        # False negative: indicates a gt box had no associated predicted box.\n        fn = (gts.sum(axis=1) > 0).sum()\n\n        return tp / (tp + fp + fn)\n\n    @staticmethod\n    def calculate_image_precision(gts, preds, thresholds=(0.5,), form='coco') -> float:\n        \"\"\"Calculates image precision.\n\n        Args:\n            gts: (List[List[Union[int, float]]]) Coordinates of the available ground-truth boxes\n            preds: (List[List[Union[int, float]]]) Coordinates of the predicted boxes,\n                   sorted by confidence value (descending)\n            thresholds: (float) Different thresholds\n            form: (str) Format of the coordinates\n\n        Return:\n            (float) Precision\n        \"\"\"\n        n_threshold = len(thresholds)\n        image_precision = 0.0\n\n        ious = np.ones((len(gts), len(preds))) * -1\n        # ious = None\n\n        for threshold in thresholds:\n            precision_at_threshold = Metrics.calculate_precision(gts.copy(), preds, threshold=threshold,\n                                                                 form=form, ious=ious)\n            image_precision += precision_at_threshold / n_threshold\n\n        return image_precision\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# submission","execution_count":null},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"\ndef 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], round(j[1][0]), round(j[1][1]), round(j[1][2]), round(j[1][3])))\n    return \" \".join(pred_strings)\n\n\n\ndef createSubmission(models, device, data_loader, detection_threshold=0.9, iou_thr=0.5):\n    SUBMISSION_PATH = '/kaggle/working'\n    submission_id = 'submission'\n    final_csv = []\n    results = EvalUtils.evalEnsembleModels(models, device, data_loader, detection_threshold=detection_threshold, iou_thr=iou_thr)\n    for (image, boxes, scores, image_path) in results:\n        boxes = boxes.detach().cpu().numpy()\n        if boxes.shape[0] > 0 :\n            boxes[:, 2] = boxes[:, 2] - boxes[:, 0]\n            boxes[:, 3] = boxes[:, 3] - boxes[:, 1]\n            image_id = image_path.split(\"/\")[-1]\n            result = [image_id,format_prediction_string(boxes, scores)]\n            final_csv.append(result)\n\n    cur_submission_path = os.path.join(SUBMISSION_PATH, '{}.csv'.format(submission_id))\n    sample_submission = pd.DataFrame(final_csv, columns=[\"image_id\",\"PredictionString\"])\n    sample_submission.to_csv(cur_submission_path, index=False)\n    submission_df = pd.read_csv(cur_submission_path)\n    return submission_df\n\n\n\n\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# train","execution_count":null},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"\nclass Averager:\n    def __init__(self):\n        self.current_total = 0.0\n        self.iterations = 0.0\n\n    def send(self, value):\n        self.current_total += value\n        self.iterations += 1\n\n    @property\n    def value(self):\n        if self.iterations == 0:\n            return 0\n        else:\n            return 1.0 * self.current_total / self.iterations\n\n    def reset(self):\n        self.current_total = 0.0\n        self.iterations = 0.0\n\n\nclass TrainUtils:\n    @staticmethod\n    def trainModels(models, train_dataloader, valid_dataloader, device, num_epochs=1, valid_pred_min=0.65):\n        for model in models:\n            print(\"Starting train model \", str(model.__name__))\n            model.to(device)\n            # construct an optimizer\n            params = [p for p in model.parameters() if p.requires_grad]\n            optimizer = torch.optim.SGD(params, lr=0.005,\n                                        momentum=0.9, weight_decay=0.0005)\n\n            train_hist = Averager()\n            t = 0\n            for epoch in range(num_epochs):\n                model.train()\n                train_hist.reset()\n                for images, targets,image_path in train_dataloader:\n                    model.train()\n                    images = list(image.to(device) for image in images)\n                    targets = [{k: v.to(device) for k, v in t.items()} for t in targets]\n                    loss_dict = model(images, targets)\n                    losses = sum(loss for loss in loss_dict.values())\n                    train_loss = losses.item()\n                    train_hist.send(train_loss)\n                    optimizer.zero_grad()\n                    losses.backward()\n                    optimizer.step()\n                    t += 1\n\n                model.eval()\n                validation_image_precisions = []\n                iou_thresholds = [x for x in np.arange(0.5, 0.76, 0.05)]\n                for images, targets,image_path in valid_dataloader:\n                    images = list(image.to(device) for image in images)\n                    targets = [{k: v.to(device) for k, v in t.items()} for t in targets]\n\n                    with torch.no_grad():\n                        outputs = model(images)\n\n                    for i, image in enumerate(images):\n                        boxes = outputs[i]['boxes'].data.cpu().numpy()\n                        scores = outputs[i]['scores'].data.cpu().numpy()\n                        gt_boxes = targets[i]['boxes'].cpu().numpy()\n                        preds_sorted_idx = np.argsort(scores)[::-1]\n                        preds_sorted = boxes[preds_sorted_idx]\n                        image_precision = Metrics.calculate_image_precision(preds_sorted,\n                                                                            gt_boxes,\n                                                                            thresholds=iou_thresholds,\n                                                                            form='coco')\n                        validation_image_precisions.append(image_precision)\n\n                valid_prec = np.mean(validation_image_precisions)\n                print(\"Validation Precision: {0:.4f}\".format(valid_prec))\n\n                # print training/validation statistics\n                print('Epoch: {} \\tTraining Loss: {:.6f}'.format(\n                    epoch,\n                    train_loss\n                ))\n\n                if valid_prec >= valid_pred_min:\n                    print('Validation precision increased({:.6f} --> {:.6f}).  Saving model ...'.format(\n                        valid_pred_min,\n                        valid_prec))\n                    torch.save(model.state_dict(), 'faster_rrcnn_' + str(model.__name__) + '.pth')\n                    valid_pred_min = valid_prec\n            torch.save(model.state_dict(), 'faster_rrcnn_' + str(model.__name__) + '_' + str(time.time()) + '.pth')\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# transofrms","execution_count":null},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"\nclass TransformsUtils:\n\n    @staticmethod\n    def get_train_transform():\n        return A.Compose(\n            [\n\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.GaussianBlur(p=0.3),\n                A.HorizontalFlip(p=0.5),\n                A.VerticalFlip(p=0.5),\n                A.RandomBrightnessContrast(p=0.5),\n               # A.Resize(height=512, width=512, p=1),\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    @staticmethod\n    def get_valid_transform():\n        return A.Compose([\n            #A.Resize(height=512, width=512, p=1),\n            ToTensorV2(p=1.0)\n        ])\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# visualization","execution_count":null},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"\nclass VisualUtils:\n\n    # helper function for data visualization\n    @staticmethod\n    def visualize(**images):\n        \"\"\"PLot images in one row.\"\"\"\n        n = len(images)\n        plt.figure(figsize=(16, 5))\n        for i, (name, image) in enumerate(images.items()):\n            plt.subplot(1, n, i + 1)\n            plt.xticks([])\n            plt.yticks([])\n            plt.title(' '.join(name.split('_')).title())\n            plt.imshow(image)\n        plt.show()\n\n    @staticmethod\n    def draw_boxes(image, boxes, scores=None):\n        # font\n        font = cv2.FONT_HERSHEY_SIMPLEX\n        # fontScale\n        fontScale = 0.7\n        # Blue color in BGR\n        color = (255, 0, 0)\n        # Line thickness of 1 px\n        thickness = 2\n        if scores is not None:\n            for box, score in zip(boxes, scores):\n                cv2.rectangle(image, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), 20, 2)\n                cv2.putText(image, str(score.item())[:5], (int(box[0]), int(box[1]) - 10), font, fontScale, color,\n                            thickness, cv2.LINE_AA)\n        else:\n            for box in boxes:\n                cv2.rectangle(image, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), 20, 2)\n\n        return image\n\n    @staticmethod\n    def visualizePrediction(image, pre_boxes, scores, get_boxes=None):\n\n        imageCopy = image.copy()\n        imageCopy2 = image.copy()\n\n        image_pred = VisualUtils.draw_boxes(imageCopy, pre_boxes, scores)\n\n        if get_boxes is not None:\n            image_get = VisualUtils.draw_boxes(imageCopy2, get_boxes)\n            VisualUtils.visualize(\n                Original=image,\n                Ground_of_true=image_get,\n                Predict=image_pred)\n        else:\n            VisualUtils.visualize(\n                Original=image,\n                Predict=image_pred)\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Starting explore our data","execution_count":null},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"# all_wheat_dataset_train = pd.read_csv('../input/global-wheat-detection/train.csv')\n# DatasetUtils.preprocessing_csv(all_wheat_dataset_train)\n# all_wheat_dataset_train.head()\n\n# train_df,valid_df = DatasetUtils.splitData(all_data_records=all_wheat_dataset_train)\n\n\n# # take only first 1000 records\n# train_df = train_df[:1000]\n# valid_df = valid_df[:1000]\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Train section","execution_count":null},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"# train_path = \"../input/global-wheat-detection/train\"\n\n# train_dataset = WheatDataset(train_df,train_path,TransformsUtils.get_train_transform())\n# valid_dataset = WheatDataset(valid_df,train_path,TransformsUtils.get_train_transform())\n\n# train_dataloader = DataLoader(train_dataset,batch_size=2,collate_fn=DatasetUtils.collate_fn,num_workers=8)\n# valid_dataloader = DataLoader(valid_dataset,batch_size=2,collate_fn=DatasetUtils.collate_fn,num_workers=4)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\ndevice","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# models","execution_count":null},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"resnet50 = FpnResenet50.getNet()\ndesnet121 = Desnet121.getNet()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"#TrainUtils.trainModels([desnet121], train_dataloader, valid_dataloader, device, num_epochs=50, valid_pred_min=0.50)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Loading models","execution_count":null},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"#ls -l ../working/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"resnet50_path = '../input/faster-rrcnn-fpn-resnet-50-epoches/faster_rrcnn_fpn_resnet.pth'\ndesnet121_path = '../input/faster-rrcnn-densenet121/faster_rrcnn_densenet121.pth'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"resnet50.load_state_dict(torch.load(resnet50_path))\ndesnet121.load_state_dict(torch.load(desnet121_path))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false},"cell_type":"code","source":"# results = EvalUtils.evalEnsembleModels([resnet50,desnet121], device, valid_dataloader, detection_threshold=0.9, iou_thr=0.3)\n\n# for (image, boxes_pred, score_pred, gt_boxes) in results:\n#     VisualUtils.visualizePrediction(image, boxes_pred, score_pred, get_boxes=gt_boxes)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"\nquery_folder = '../input/global-wheat-detection/test'\ndataset_query = MyDataset(query_folder, transform=TransformsUtils.get_valid_transform())\ntest_loader = DataLoader(dataset_query,batch_size=1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"\nresults =  EvalUtils.evalEnsembleModels([resnet50,desnet121], device, test_loader, detection_threshold=0.5, iou_thr=0.3)\n\n#%%\n\nfor (image, boxes_pred, score_pred, image_path) in results:\n    VisualUtils.visualizePrediction(image, boxes_pred, score_pred, get_boxes=None)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# submission","execution_count":null},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"submission = createSubmission([resnet50,desnet121], device, test_loader, detection_threshold=0.5, iou_thr=0.3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"submission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}