{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom matplotlib import pyplot as plt \nimport cv2 \nimport ast\nfrom tqdm import tqdm\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:45:57.070998Z","iopub.execute_input":"2023-09-02T21:45:57.071382Z","iopub.status.idle":"2023-09-02T21:45:57.078017Z","shell.execute_reply.started":"2023-09-02T21:45:57.071354Z","shell.execute_reply":"2023-09-02T21:45:57.076265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax = plt.subplots(1,4,figsize=(10,5))\nimage = cv2.imread(\"/kaggle/input/global-wheat-detection/test/2fd875eaa.jpg\")\nimage = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)\nax[0].imshow(image)\nimage = cv2.imread(\"/kaggle/input/global-wheat-detection/test/53f253011.jpg\")\nimage = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)\nax[1].imshow(image)\nimage = cv2.imread(\"/kaggle/input/global-wheat-detection/test/cc3532ff6.jpg\")\nimage = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)\nax[2].imshow(image)\nimage = cv2.imread(\"/kaggle/input/global-wheat-detection/test/f5a1f0358.jpg\")\nimage = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)\nax[3].imshow(image)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:45:57.346367Z","iopub.execute_input":"2023-09-02T21:45:57.346723Z","iopub.status.idle":"2023-09-02T21:45:58.617079Z","shell.execute_reply.started":"2023-09-02T21:45:57.346693Z","shell.execute_reply":"2023-09-02T21:45:58.615291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_1 = pd.read_csv('/kaggle/input/global-wheat-detection/sample_submission.csv')\ndf = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:45:58.619667Z","iopub.execute_input":"2023-09-02T21:45:58.62009Z","iopub.status.idle":"2023-09-02T21:45:58.764765Z","shell.execute_reply.started":"2023-09-02T21:45:58.620043Z","shell.execute_reply":"2023-09-02T21:45:58.763222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_1","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:45:58.76613Z","iopub.execute_input":"2023-09-02T21:45:58.766455Z","iopub.status.idle":"2023-09-02T21:45:58.77862Z","shell.execute_reply.started":"2023-09-02T21:45:58.766428Z","shell.execute_reply":"2023-09-02T21:45:58.777105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_1.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:45:58.781599Z","iopub.execute_input":"2023-09-02T21:45:58.7819Z","iopub.status.idle":"2023-09-02T21:45:58.793777Z","shell.execute_reply.started":"2023-09-02T21:45:58.781874Z","shell.execute_reply":"2023-09-02T21:45:58.792578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:45:58.795007Z","iopub.execute_input":"2023-09-02T21:45:58.795349Z","iopub.status.idle":"2023-09-02T21:45:58.817683Z","shell.execute_reply.started":"2023-09-02T21:45:58.79532Z","shell.execute_reply":"2023-09-02T21:45:58.816406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:45:58.964114Z","iopub.execute_input":"2023-09-02T21:45:58.964481Z","iopub.status.idle":"2023-09-02T21:45:58.971197Z","shell.execute_reply.started":"2023-09-02T21:45:58.964456Z","shell.execute_reply":"2023-09-02T21:45:58.970293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"bbox\"]= df[\"bbox\"].apply(ast.literal_eval)\ndf[\"x_center\"]= [ i[0]+(i[2]/2) for i in df[\"bbox\"]]\ndf[\"y_center\"]= [ i[1]+(i[3]/2) for i in df[\"bbox\"]]\ndf[\"bbox_width\"]=[i[2] for i in df[\"bbox\"]]\ndf[\"bbox_height\"]=[i[3] for i in df[\"bbox\"]]\ndf[\"label\"]=0","metadata":{"execution":{"iopub.status.busy":"2023-09-02T20:53:34.920828Z","iopub.execute_input":"2023-09-02T20:53:34.921315Z","iopub.status.idle":"2023-09-02T20:53:37.532321Z","shell.execute_reply.started":"2023-09-02T20:53:34.92128Z","shell.execute_reply":"2023-09-02T20:53:37.531077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"bbox_width_normalized\"]=df[\"bbox_width\"]/ df[\"width\"]\ndf[\"x_center_normalized\"]=df[\"x_center\"]/ df[\"width\"]\ndf[\"bbox_height_normalized\"]=df[\"bbox_height\"]/ df[\"height\"]\ndf[\"y_center_normalized\"]=df[\"y_center\"]/ df[\"height\"]","metadata":{"execution":{"iopub.status.busy":"2023-09-02T20:53:37.535259Z","iopub.execute_input":"2023-09-02T20:53:37.536107Z","iopub.status.idle":"2023-09-02T20:53:37.548601Z","shell.execute_reply.started":"2023-09-02T20:53:37.536046Z","shell.execute_reply":"2023-09-02T20:53:37.546937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T20:53:37.551101Z","iopub.execute_input":"2023-09-02T20:53:37.551557Z","iopub.status.idle":"2023-09-02T20:53:37.576632Z","shell.execute_reply.started":"2023-09-02T20:53:37.551522Z","shell.execute_reply":"2023-09-02T20:53:37.575523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-09-02T20:53:37.577889Z","iopub.execute_input":"2023-09-02T20:53:37.57854Z","iopub.status.idle":"2023-09-02T20:54:09.88406Z","shell.execute_reply.started":"2023-09-02T20:53:37.578509Z","shell.execute_reply":"2023-09-02T20:54:09.883081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n%cd ../kaggle/working\nimport torch\nprint(f\"Setup complete. Using torch {torch.__version__} ({torch.cuda.get_device_properties(0).name if torch.cuda.is_available() else 'CPU'})\")","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:39.175329Z","iopub.execute_input":"2023-09-02T21:47:39.175573Z","iopub.status.idle":"2023-09-02T21:47:39.188954Z","shell.execute_reply.started":"2023-09-02T21:47:39.175551Z","shell.execute_reply":"2023-09-02T21:47:39.18821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can use data.random.split directly for each class \n","metadata":{}},{"cell_type":"code","source":"%cp -r /kaggle/input/global-wheat-detection /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:53.193219Z","iopub.execute_input":"2023-09-02T21:47:53.193659Z","iopub.status.idle":"2023-09-02T21:47:57.228918Z","shell.execute_reply.started":"2023-09-02T21:47:53.193618Z","shell.execute_reply":"2023-09-02T21:47:57.226667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_dir = '/kaggle/input/global-wheat-detection/train'\ntest_images_dir = '/kaggle/input/global-wheat-detection/test'","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:57.584849Z","iopub.status.idle":"2023-09-02T21:47:57.585213Z","shell.execute_reply.started":"2023-09-02T21:47:57.585054Z","shell.execute_reply":"2023-09-02T21:47:57.585079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_filenames = os.listdir(train_images_dir)\ntest_images_filenames = os.listdir(test_images_dir)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:57.590742Z","iopub.status.idle":"2023-09-02T21:47:57.591109Z","shell.execute_reply.started":"2023-09-02T21:47:57.590926Z","shell.execute_reply":"2023-09-02T21:47:57.590942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = []\ntest_data = []","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:57.592273Z","iopub.status.idle":"2023-09-02T21:47:57.592574Z","shell.execute_reply.started":"2023-09-02T21:47:57.592432Z","shell.execute_reply":"2023-09-02T21:47:57.592447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for filename in train_image_filenames:\n    image_path = os.path.join(train_images_dir, filename)\n    image = cv2.imread(image_path)\n    train_data.append(image)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:57.595024Z","iopub.status.idle":"2023-09-02T21:47:57.595864Z","shell.execute_reply.started":"2023-09-02T21:47:57.595496Z","shell.execute_reply":"2023-09-02T21:47:57.595531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for filename in test_images_filenames:\n    image_path = os.path.join(test_images_dir, filename)\n    image = cv2.imread(image_path)\n    test_data.append(image)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:57.597861Z","iopub.status.idle":"2023-09-02T21:47:57.59838Z","shell.execute_reply.started":"2023-09-02T21:47:57.598174Z","shell.execute_reply":"2023-09-02T21:47:57.598196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrained_size = 416\npretrained_means = [0.485, 0.456, 0.406]\npretrained_stds= [0.229, 0.224, 0.225]\n\ntrain_transforms = transforms.Compose([\n                           transforms.Resize(pretrained_size),\n                           transforms.RandomRotation(5),\n                           transforms.RandomHorizontalFlip(0.5),\n                           transforms.RandomCrop(pretrained_size, padding = 10),\n                           transforms.ToTensor(),\n                           transforms.Normalize(mean = pretrained_means, \n                                                std = pretrained_stds)\n                       ])\n\ntest_transforms = transforms.Compose([\n                           transforms.Resize(pretrained_size),\n                           transforms.CenterCrop(pretrained_size),\n                           transforms.ToTensor(),\n                           transforms.Normalize(mean = pretrained_means, \n                                                std = pretrained_stds)\n                       ])","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:57.600128Z","iopub.status.idle":"2023-09-02T21:47:57.600544Z","shell.execute_reply.started":"2023-09-02T21:47:57.600367Z","shell.execute_reply":"2023-09-02T21:47:57.600386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"VALID_RATIO = 0.9\nn_train_examples = int(len(train_image_filenames) * VALID_RATIO)\nn_valid_examples = len(train_image_filenames) - n_train_examples\n\ntrain_image_filenames, valid_image_filenames = data.random_split(train_image_filenames, [n_train_examples, n_valid_examples])","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:57.602009Z","iopub.status.idle":"2023-09-02T21:47:57.602405Z","shell.execute_reply.started":"2023-09-02T21:47:57.602242Z","shell.execute_reply":"2023-09-02T21:47:57.602258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nimport yaml\n\ndata_yaml = dict(\n    train = '/kaggle/working/global-wheat-detection/train',\n    val = '/kaggle/working/valid',\n    nc = 1,\n    names = ['Global Wheat']\n)\n\n# Note that I am creating the file in the yolov5/data/ directory.\nwith open('data.yaml', 'w') as outfile:\n    yaml.dump(data_yaml, outfile, default_flow_style=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:57.603547Z","iopub.status.idle":"2023-09-02T21:47:57.603918Z","shell.execute_reply.started":"2023-09-02T21:47:57.603768Z","shell.execute_reply":"2023-09-02T21:47:57.603785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd yolov7","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:57.60532Z","iopub.status.idle":"2023-09-02T21:47:57.605666Z","shell.execute_reply.started":"2023-09-02T21:47:57.605518Z","shell.execute_reply":"2023-09-02T21:47:57.605534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wandb disabled\n!python train.py --img 416 --batch 16 --epochs 50 --data /kaggle/working/data.yaml  --weights 'yolov7.pt'","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:57.606979Z","iopub.status.idle":"2023-09-02T21:47:57.607379Z","shell.execute_reply.started":"2023-09-02T21:47:57.607209Z","shell.execute_reply":"2023-09-02T21:47:57.607228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(\"runs/train/exp/results.png\")\nplt.figure(figsize=(15, 15))\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:57.608621Z","iopub.status.idle":"2023-09-02T21:47:57.608964Z","shell.execute_reply.started":"2023-09-02T21:47:57.608809Z","shell.execute_reply":"2023-09-02T21:47:57.608826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(\"runs/train/exp/test_batch2_pred.jpg\")\nplt.figure(figsize=(15, 15))\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:57.610214Z","iopub.status.idle":"2023-09-02T21:47:57.610587Z","shell.execute_reply.started":"2023-09-02T21:47:57.610425Z","shell.execute_reply":"2023-09-02T21:47:57.610442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_data)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:47:57.61202Z","iopub.status.idle":"2023-09-02T21:47:57.612364Z","shell.execute_reply.started":"2023-09-02T21:47:57.612215Z","shell.execute_reply":"2023-09-02T21:47:57.612231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python detect.py --source /kaggle/input/global-wheat-detection/test/2fd875eaa.jpg --weights runs/train/exp/weights/best.pt","metadata":{"execution":{"iopub.status.busy":"2023-09-02T21:48:26.73627Z","iopub.execute_input":"2023-09-02T21:48:26.736728Z","iopub.status.idle":"2023-09-02T21:48:31.6811Z","shell.execute_reply.started":"2023-09-02T21:48:26.7367Z","shell.execute_reply":"2023-09-02T21:48:31.680118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}