{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# install dependencies: (use cu101 because colab has CUDA 10.1)\n!pip install -U torch==1.5 torchvision==0.6 -f https://download.pytorch.org/whl/cu101/torch_stable.html \n!pip install cython pyyaml==5.1\n!pip install -U 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'\nimport torch, torchvision\nprint(torch.__version__, torch.cuda.is_available())\n!gcc --version\n\n# ---- OUTPUT ----\n# ERROR: allennlp 0.9.0 has requirement spacy<2.2,>=2.1.0, but you'll have spacy 2.2.3 which is incompatible\n# ERROR: kubernetes 10.1.0 has requirement pyyaml~=3.12, but you'll have pyyaml 5.1 which is incompatible.\n# ERROR: Cannot uninstall 'PyYAML'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(torch.__version__, torch.cuda.is_available())\n!gcc --version","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# install detectron2:\n!pip install detectron2==0.1.2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/index.html","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# You may need to restart your runtime prior to this, to let your installation take effect\n# Some basic setup:\n# Setup detectron2 logger\nimport detectron2\nfrom detectron2.utils.logger import setup_logger\nsetup_logger()\n\n# import some common libraries\nimport numpy as np\nimport cv2\nimport random\n# from google.colab.patches import cv2_imshow # THIS IS NOT AVAILABLE IN KAGGLE\n\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\n\nimport matplotlib.pyplot as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!wget http://images.cocodataset.org/val2017/000000439715.jpg -O input.jpg\nim = cv2.imread(\"./input.jpg\")\nplt.imshow(im) # SAVE IMAGE USING MATPLOTLIB BECAUSE CV2 CRASHES KERNEL\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cfg = get_cfg()\n# add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library\ncfg.merge_from_file(model_zoo.get_config_file(\"COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml\"))\ncfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # set threshold for this model\n# Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well\ncfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(\"COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml\")\npredictor = DefaultPredictor(cfg)\noutputs = predictor(im)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# look at the outputs. See https://detectron2.readthedocs.io/tutorials/models.html#model-output-format for specification\noutputs[\"instances\"].pred_classes\noutputs[\"instances\"].pred_boxes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# We can use `Visualizer` to draw the predictions on the image.\nv = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)\nv = v.draw_instance_predictions(outputs[\"instances\"].to(\"cpu\"))\nplt.imshow(v.get_image()[:, :, ::-1])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# if your dataset is in COCO format, this cell can be replaced by the following three lines:\n# from detectron2.data.datasets import register_coco_instances\n# register_coco_instances(\"my_dataset_train\", {}, \"json_annotation_train.json\", \"path/to/image/dir\")\n# register_coco_instances(\"my_dataset_val\", {}, \"json_annotation_val.json\", \"path/to/image/dir\")\n\nimport os\nimport numpy as np\nimport json\nimport glob\nfrom detectron2.structures import BoxMode\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\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\nimport pandas as pd\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\n\nimg_root = '/kaggle/input/global-wheat-detection/train/'\nann_root = '/kaggle/input/global-wheat-detection/train.csv'\n\ndf = pd.read_csv(ann_root) # Read in training CSV.\n\n# from https://www.kaggle.com/ateplyuk/gwd-starter-efficientdet-train/notebook\ndf['bbox'] = df['bbox'].apply(lambda x: x[1:-1].split(\",\"))\ndf['x'] = df['bbox'].apply(lambda x: x[0]).astype('float32')\ndf['y'] = df['bbox'].apply(lambda x: x[1]).astype('float32')\ndf['w'] = df['bbox'].apply(lambda x: x[2]).astype('float32')\ndf['h'] = df['bbox'].apply(lambda x: x[3]).astype('float32')\n\ndf = df[['image_id','x', 'y', 'w', 'h']]\nprint(df.head())\n\ndef get_jpg(img_root):\n    jpg_fps = glob.glob(img_root + '*.jpg')\n    return list(set(jpg_fps))\n\ndef get_dataset(img_dir, anns): \n    image_fps = get_jpg(img_dir)\n    image_annotations = {fp: [] for fp in image_fps}\n    \n    for index, row in anns.iterrows(): \n        fp = os.path.join(img_root, row['image_id'] + '.jpg')\n        image_annotations[fp].append(row)\n    \n    return image_fps, image_annotations \n\n# grab all image file path with concern annotation\ntrain_image_fps, train_image_annotations = get_dataset(img_root, df)\n\n# def get_wheat_head_dicts():    \n\n\n#     dataset_dicts = []\n#     for idx, row in df.iterrows():\n#         print(idx, row['image_id'], row['bbox'])\n#         bboxes_string = row['bbox'][1:-1]\n#         bboxes_string = bboxes_string.replace(' ', '').split('][')\n#         print(bboxes_string[0])\n#         bboxes = [list('[' + string + ']') for string in bboxes_string]\n#         print(bboxes)\n#         record = {}\n        \n#         filename = os.path.join(img_dir, v[\"filename\"])\n#         height, width = cv2.imread(filename).shape[:2]\n        \n#         record[\"file_name\"] = filename\n#         record[\"image_id\"] = idx\n#         record[\"height\"] = 1024 # ALWAYS ?\n#         record[\"width\"] = 1024 # ALWAYS ?\n\n#         annos = v[\"regions\"]\n#         objs = []\n#         for _, anno in annos.items():\n#             assert not anno[\"region_attributes\"]\n#             anno = anno[\"shape_attributes\"]\n#             px = anno[\"all_points_x\"]\n#             py = anno[\"all_points_y\"]\n#             poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)]\n#             poly = [p for x in poly for p in x]\n\n#             obj = {\n#                 \"bbox\": [np.min(px), np.min(py), np.max(px), np.max(py)],\n#                 \"bbox_mode\": BoxMode.XYWH_ABS,# USE XYWH MODE\n#                 \"segmentation\": [poly],\n#                 \"category_id\": 0,\n#                 \"iscrowd\": 0\n#             }\n#             objs.append(obj)\n#         record[\"annotations\"] = objs\n#         dataset_dicts.append(record)\n#     return dataset_dicts\n\n# from detectron2.data import DatasetCatalog, MetadataCatalog\n# for d in [\"train\", \"val\"]:\n#     DatasetCatalog.register(\"balloon_\" + d, lambda d=d: get_balloon_dicts(\"balloon/\" + d))\n#     MetadataCatalog.get(\"balloon_\" + d).set(thing_classes=[\"balloon\"])\n# balloon_metadata = MetadataCatalog.get(\"balloon_train\")\n# dataset_dicts = get_wheat_head_dicts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train_image_annotations)","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}