{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"!pip install  '/kaggle/input/torch-15/torch-1.5.0cu101-cp37-cp37m-linux_x86_64.whl'\n!pip install  '/kaggle/input/torch-15/torchvision-0.6.0cu101-cp37-cp37m-linux_x86_64.whl'\n!pip install  '/kaggle/input/torch-15/yacs-0.1.7-py3-none-any.whl'\n!pip install  '/kaggle/input/torch-15/fvcore-0.1.1.post200513-py3-none-any.whl'\n!pip install  '/kaggle/input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl'\n!pip install  '/kaggle/input/detectron2/detectron2-0.1.3cu101-cp37-cp37m-linux_x86_64.whl'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import detectron2\nfrom detectron2.utils.logger import setup_logger\nsetup_logger()\n\n# import some common libraries\nimport numpy as np\nimport pandas as pd\nfrom multiprocessing import Pool, Process\nfrom functools import partial\nfrom collections import deque, defaultdict\nimport cv2\nimport glob\n# import some common detectron2 utilities\nfrom detectron2 import model_zoo\nfrom detectron2.engine import DefaultPredictor\nfrom detectron2.config import get_cfg\nfrom detectron2.utils.visualizer import Visualizer\nfrom detectron2.data import MetadataCatalog, DatasetCatalog\nfrom tqdm.notebook import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"MAIN_PATH = '/kaggle/input/global-wheat-detection'\nTRAIN_IMAGE_PATH = os.path.join(MAIN_PATH, 'train/')\nTEST_IMAGE_PATH = os.path.join(MAIN_PATH, 'test/')\nTRAIN_PATH = os.path.join(MAIN_PATH, 'train.csv')\nSUB_PATH = os.path.join(MAIN_PATH, 'sample_submission.csv')\nPADDING = 5\n\n\nMODEL_PATH = 'COCO-Detection/retinanet_R_101_FPN_3x.yaml'\nWEIGHT_PATH = '/kaggle/input/detectron2-faster-rcnn-101/model_final_971ab9.pkl'\n\nsub_df = pd.read_csv(SUB_PATH)\nsub_df.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def wheat_dataset(df, folder, is_train, img_unique):\n    img_id, img_name = img_unique\n    if is_train:\n        img_group = df[df['image_id']==img_name].reset_index(drop=True)\n        record = defaultdict()\n        img_path = os.path.join(folder, img_name+'.jpg')\n        \n        record['file_name'] = img_path\n        record['image_id'] = img_id\n        record['height'] = int(img_group.loc[0, 'height'])\n        record['width'] = int(img_group.loc[0, 'width'])\n        \n        annots = deque()\n        for _, ant in img_group.iterrows():\n            source = ant.source\n            annot = defaultdict()\n            box = ant.bbox[1:-1]\n            box = list(map(float, box.split(', ')))\n            x, y, w, h = list(map(int, box))\n            \n            if random.random() >= 0.75:\n                random_x = random.randint(0, PADDING)       \n                if (x+random_x <= int(img_group.loc[0, 'width'])) and (w >= random_x):\n                    x += random_x\n                    w -= random_x                \n            elif random.random() >= 0.75:\n                random_y = random.randint(0, PADDING)\n                if (y+random_y <= int(img_group.loc[0, 'height'])) and (h >= random_y):\n                    y += random_y\n                    h -= random_y\n            else:\n                if random.random() >= 0.75:\n                    random_w = random.randint(0, PADDING)\n                    if w >= random_w:\n                        w -= random_w\n                elif random.random() >= 0.75:\n                    random_h = random.randint(0, PADDING)\n                    if h >= random_h:\n                        h -= random_h\n                            \n            annot['bbox'] = (x, y, x+w, y+h)\n            annot['bbox_mode'] = BoxMode.XYXY_ABS\n            annot['category_id'] = 0\n            \n            annots.append(dict(annot))\n            \n        record['annotations'] = list(annots)\n    \n    else:\n        img_group = df[df['image_id']==img_name].reset_index(drop=True)\n        record = defaultdict()\n        img_path = os.path.join(folder, img_name+'.jpg')\n        img = cv2.imread(img_path)\n        h, w = img.shape[:2]\n        \n        record['file_name'] = img_path\n        record['image_id'] = img_id\n        record['height'] = int(h)\n        record['width'] = int(w)\n    \n    return dict(record)\n\ndef wheat_parallel(df, folder):\n      \n    pool = Pool()\n    img_uniques = list(zip(range(df['image_id'].nunique()), df['image_id'].unique()))\n    func = partial(wheat_dataset, df, folder, False)\n    detaset_dict = pool.map(func, img_uniques)\n    pool.close()\n    pool.join()\n    \n    return detaset_dict\n\nDatasetCatalog.register('wheat_test', lambda d: wheat_parallel(sub_df,TEST_IMAGE_PATH))\nMetadataCatalog.get('wheat_test')\n    \n# micro_metadata = MetadataCatalog.get('wheat_train')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndef cfg_test():\n    cfg = get_cfg()\n    cfg.merge_from_file(model_zoo.get_config_file(MODEL_PATH))\n    cfg.MODEL.WEIGHTS = '../input/gwdmodels/Retina_Det_model_final.pth'\n    cfg.DATASETS.TEST = ('wheat_test',)\n    cfg.MODEL.RETINANET.NUM_CLASSES = 1\n    cfg.MODEL.RETINANET.SCORE_THRESH_TEST = 0.45\n    \n    return cfg\n\ncfg = cfg_test()\npredict = DefaultPredictor(cfg)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def submit():\n    for idx, row in tqdm(sub_df.iterrows(), total=len(sub_df)):\n        img_path = os.path.join(TEST_IMAGE_PATH, row.image_id+'.jpg')\n        img = cv2.imread(img_path)\n        outputs = predict(img)['instances']\n        boxes = [i.cpu().detach().numpy() for i in outputs.pred_boxes]\n        scores = outputs.scores.cpu().detach().numpy()\n        list_str = []\n        for box, score in zip(boxes, scores):\n            box[3] -= box[1]\n            box[2] -= box[0]\n            box = list(map(int, box))\n            score = round(score, 4)\n            list_str.append(score) \n            list_str.extend(box)\n        sub_df.loc[idx, 'PredictionString'] = ' '.join(map(str, list_str))\n    \n    return sub_df\n\nsub_df = submit()    \nsub_df.to_csv('submission.csv', index=False)\nsub_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\ndef visual_predict(dataset):\n    for sample in dataset:\n        img = cv2.imread(sample['file_name'])\n        output = predict(img)\n        \n        v = Visualizer(img[:, :, ::-1], metadata=micro_metadata, scale=0.5)\n        v = v.draw_instance_predictions(output['instances'].to('cpu'))\n        plt.figure(figsize = (14, 10))\n        plt.imshow(cv2.cvtColor(v.get_image()[:, :, ::-1], cv2.COLOR_BGR2RGB))\n        plt.show()\n\ntest_dataset = wheat_parallel(sub_df, TEST_IMAGE_PATH)\n# visual_predict(test_dataset)","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}