{"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\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":{"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# #opencv is pre-installed on colab","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pd.read_csv('../input/submission-files/submission (2).csv').to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pd.read_csv('submission.csv')","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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Importing Necessary Libraries\nimport tensorflow as tf\nfrom PIL import Image, ImageDraw\nimport matplotlib.pyplot as plt\nfrom tqdm.notebook import tqdm\nimport plotly.express as px\nfrom ast import literal_eval\nimport plotly.graph_objects as go\n\n# 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\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\nfrom detectron2.structures import BoxMode","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# # Reading Dataset\ndataset = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')\ndataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Making Found columns for bounding box\ndataset[['x', 'y', 'w', 'h']] = pd.DataFrame(dataset.bbox.str.strip('[]').str.split(',').tolist())\n\n# Change datatype of columns to float\ndataset[['x', 'y', 'w', 'h']] = dataset[['x', 'y', 'w', 'h']].astype(float)\n\n# Adding extenson to the image_id column\ndataset['image_id'] = dataset['image_id'] + '.jpg'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_bbox_area(bbox):\n    bbox = bbox.strip('[]').split(',')\n    return float(bbox[2]) * float(bbox[3])\ndataset['bbox_area'] = dataset['bbox'].apply(get_bbox_area)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Exploration","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f\"There are total {dataset['image_id'].nunique()} number of unique image\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Data Visualisations","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = px.histogram(dataset, x=\"source\")\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = px.histogram(dataset, x=\"bbox_area\")\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = px.histogram(dataset, x=\"bbox_area\", color=\"source\")\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = go.Figure(data=[go.Histogram(x=dataset['image_id'].value_counts())])\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualising Images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_images(images, num = 5):\n    \n    images_to_show = np.random.choice(images, num)\n\n    for image_id in images_to_show:\n\n        image_path = os.path.join('/kaggle/input/global-wheat-detection/train/', image_id)\n        image = Image.open(image_path)\n\n        # get all bboxes for given image in [xmin, ymin, width, height]\n        bboxes = [literal_eval(box) for box in dataset[dataset['image_id'] == image_id]['bbox']]\n\n        # visualize them\n        draw = ImageDraw.Draw(image)\n        for bbox in bboxes:    \n            draw.rectangle([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]], width=3)\n\n        plt.figure(figsize = (15,15))\n        plt.imshow(image)\n        plt.show()\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_images(dataset['image_id'].unique(), num = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset['category_id'] = list(range(0, dataset.shape[0]))\ndataset['image_category_id'] = dataset.groupby(['image_id']).ngroup()\ndataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import ast \n\ndict_dataset = []\ndef get_dataset_dics(img_dir):\n    for i in dataset.groupby('image_id'):\n        ann_lst = []\n        \n        for b in i[1]['bbox']:\n            \n            b = ast.literal_eval(b)\n            \n            ann_dict = {'bbox': [b[0], b[1], b[2], b[3]],\n           'bbox_mode': BoxMode.XYWH_ABS,\n           'category_id': 0,\n           'iscrowd': 0}\n            \n            ann_lst.append(ann_dict)\n            \n       \n\n    image_dict = {'annotations': ann_lst,\n         'file_name': '../input/global-wheat-detection/train/'+pd.DataFrame(i[1]['image_id'].values)[0][0],\n         'height': pd.DataFrame(i[1]['height'].values)[0][0],\n         'image_id': pd.DataFrame(i[1]['image_category_id'].values)[0][0],\n         'width': pd.DataFrame(i[1]['width'].values)[0][0]}\n       \n    dict_dataset.append(image_dict)\n    return dict_dataset\n\ndict_dataset = get_dataset_dics(dataset['image_id'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from detectron2.data import DatasetCatalog, MetadataCatalog\nfor d in [\"train\"]:\n   DatasetCatalog.register(\"wheat_\" + d, lambda d=d: get_dataset_dics(dataset['image_id']))\n   MetadataCatalog.get(\"wheat_\" + d).set(thing_classes=[\"wheat_\"])\n   balloon_metadata = MetadataCatalog.get(\"wheat_train\")\n    \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset_dicts = get_dataset_dics(dataset['image_id'])\nfor d in dataset_dicts:\n    img = cv2.imread(d[\"file_name\"])\n    visualizer = Visualizer(img[:, :, ::-1], metadata=balloon_metadata, scale=1.5)\n    vis = visualizer.draw_dataset_dict(d)\n    plt.figure(figsize = (15,15))\n    plt.imshow(vis.get_image()[:, :, ::-1])\n    plt.show()\n    break","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Making the model","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"#from detectron2.engine import DefaultTrainer\n#from detectron2.config import get_cfg\n\n#cfg = get_cfg()\n#cfg.merge_from_file(model_zoo.get_config_file(\"COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml\"))\n#cfg.DATASETS.TRAIN = (\"wheat_train\",)\n#cfg.DATASETS.TEST = ()\n#cfg.DATALOADER.NUM_WORKERS = 2\n#cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(\"COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml\")  # Let training initialize from model zoo\n#cfg.SOLVER.IMS_PER_BATCH = 2\n#cfg.SOLVER.BASE_LR = 0.0025  # pick a good LR\n#cfg.SOLVER.MAX_ITER = 2000    # 300 iterations seems good enough for this toy dataset; you may need to train longer for a practical dataset\n#cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 128   # faster, and good enough for this toy dataset (default: 512)\n#cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1  # only has one class (ballon)\n\n#os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)\n#trainer = DefaultTrainer(cfg) \n#trainer.resume_or_load(resume=False)\n#trainer.train()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cfg = get_cfg()\ncfg.merge_from_file(model_zoo.get_config_file(\"COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml\"))\ncfg.DATASETS.TRAIN = (\"wheat_train\",)\ncfg.DATASETS.TEST = ()\ncfg.DATALOADER.NUM_WORKERS = 2\ncfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(\"COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml\")  # Let training initialize from model zoo\n\ncfg.SOLVER.IMS_PER_BATCH = 2\ncfg.SOLVER.BASE_LR = 0.0025  # pick a good LR\ncfg.SOLVER.MAX_ITER = 2000    # 300 iterations seems good enough for this toy dataset; you may need to train longer for a practical dataset\ncfg.SOLVER.WEIGHT_DECAY = 0.0001\n\n\n\ncfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 128   # faster, and good enough for this toy dataset (default: 512)\ncfg.MODEL.ROI_HEADS.NUM_CLASSES = 1  # only has one class (ballon)\ncfg.MODEL.WEIGHTS = os.path.join(\"../input/model-pretrained/output/model_final.pth\")\ncfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7   # set the testing threshold for this model\ncfg.DATASETS.TEST = (\"wheat_val\", )\npredictor = DefaultPredictor(cfg)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"final_outputs = []\nfor i in ['796707dd7', 'cc3532ff6', '51f1be19e', '51b3e36ab', 'f5a1f0358', 'aac893a91', '348a992bb', '2fd875eaa', 'cb8d261a3', '53f253011']:\n    image = cv2.imread(f'/kaggle/input/global-wheat-detection/test/{i}.jpg')\n    outputs = predictor(image)\n    v = Visualizer(image[:, :, ::-1],\n                   metadata=balloon_metadata, \n                   scale=5.0, \n    )\n    v = v.draw_instance_predictions(outputs[\"instances\"].to(\"cpu\"))\n    img = np.array(v.get_image()[:, :, ::-1])\n    final_outputs.append(outputs)\n    plt.figure(figsize = (15,15))\n    plt.imshow(img)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions_col = []\nfor i in final_outputs:\n    final_column_string = \"\"\n    for n in range(0, len(i['instances'].pred_boxes.tensor.tolist())):\n        x1 = i['instances'].pred_boxes.tensor.tolist()[n][0]\n        x2 = i['instances'].pred_boxes.tensor.tolist()[n][1]\n        y1 = i['instances'].pred_boxes.tensor.tolist()[n][2]\n        y2 = i['instances'].pred_boxes.tensor.tolist()[n][3]\n        score = i['instances'].scores.tolist()[n]\n        prediction_string = f\"{score} {int(x1)} {int(x2)} {int(y1)} {int(y2)} \"\n        print(prediction_string)\n        final_column_string += prediction_string\n    print('\\n' + final_column_string)\n    predictions_col.append(final_column_string)\n    print('---'*30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/global-wheat-detection/sample_submission.csv')\nsubmission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission['PredictionString'] = predictions_col\nsubmission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","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}