{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport glob\nimport os, sys\nimport cv2\nfrom IPython.display import display\nplt.rcParams['figure.figsize'] = [7,5]\n\n# Metadata \ndata_df = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')\ndata_df.head()\n\n# Data path\ndata_path = '/kaggle/input/global-wheat-detection/train/'\n# Read a sample image\nI = cv2.cvtColor(cv2.imread(data_path + data_df.image_id[0] + '.jpg'), cv2.COLOR_BGR2RGB)\n# Show the image\nplt.imshow(I);\nplt.axis('off')\nplt.title('Wheat image')\nplt.show()\nbboxes = data_df[data_df.image_id==data_df.image_id[0]].bbox.tolist()\nJ = I.copy()\nfor i in range(len(bboxes)):\n    x = int(str(bboxes[i][1:-1]).split(',')[0][:-2])\n    y = int(str(bboxes[i][1:-1]).split(',')[1][1:-2])\n    xw = x + int(str(bboxes[i][1:-1]).split(',')[2][1:-2])\n    yh = y + int(str(bboxes[i][1:-1]).split(',')[3][1:-2])\n    cv2.rectangle(J,(x,y),(xw,yh),(180,190,0),4)\nplt.imshow(J);\nplt.axis('off')\nplt.title('Wheat image bounding boxes')\nplt.show()\nimport torchvision\nbase_model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\n\n# replace the classifier with a new one, that has num_classes which is user-defined\nnum_classes = 2  # 1 class (wheat) + background\n\n# get number of input features for the classifier\nin_features = base_model.roi_heads.box_predictor.cls_score.in_features\n\n# replace the pre-trained head with a new one\nbase_model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-02T12:11:33.053579Z","iopub.execute_input":"2021-07-02T12:11:33.054043Z","iopub.status.idle":"2021-07-02T12:11:45.942056Z","shell.execute_reply.started":"2021-07-02T12:11:33.053992Z","shell.execute_reply":"2021-07-02T12:11:45.940928Z"},"trusted":true},"execution_count":null,"outputs":[]}]}