{"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"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":19989,"databundleVersionId":1160143,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Enviroment setup","metadata":{}},{"cell_type":"code","source":"!pip install ultralytics","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-03T23:17:04.761858Z","iopub.execute_input":"2024-01-03T23:17:04.762536Z","iopub.status.idle":"2024-01-03T23:17:16.736012Z","shell.execute_reply.started":"2024-01-03T23:17:04.762500Z","shell.execute_reply":"2024-01-03T23:17:16.734735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nprint(torch.__version__)\n\nimport ultralytics\nfrom ultralytics import YOLO\nultralytics.checks()","metadata":{"execution":{"iopub.status.busy":"2024-01-03T23:17:16.738139Z","iopub.execute_input":"2024-01-03T23:17:16.738475Z","iopub.status.idle":"2024-01-03T23:17:16.768657Z","shell.execute_reply.started":"2024-01-03T23:17:16.738446Z","shell.execute_reply":"2024-01-03T23:17:16.767842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/ultralytics","metadata":{"execution":{"iopub.status.busy":"2024-01-04T03:05:22.738498Z","iopub.execute_input":"2024-01-04T03:05:22.739362Z","iopub.status.idle":"2024-01-04T03:05:23.791618Z","shell.execute_reply.started":"2024-01-04T03:05:22.739326Z","shell.execute_reply":"2024-01-04T03:05:23.790423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport seaborn as sns\nimport glob\nimport shutil\nimport torchvision\n\nfrom torchvision.io import read_image\nfrom torchvision.utils import draw_bounding_boxes\nfrom shutil import copyfile\nfrom pathlib import Path\nfrom tqdm import tqdm\nfrom sklearn.model_selection import train_test_split\nfrom PIL import Image, ImageDraw, ImageFont\nfrom PIL import __version__ as pil_version\nfrom scipy.ndimage import gaussian_filter1d\nfrom ultralytics.utils.plotting import *\nfrom ultralytics.utils import LOGGER, TryExcept, plt_settings, threaded\n\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2024-01-04T03:13:24.648826Z","iopub.execute_input":"2024-01-04T03:13:24.649201Z","iopub.status.idle":"2024-01-04T03:13:24.658665Z","shell.execute_reply.started":"2024-01-04T03:13:24.649172Z","shell.execute_reply":"2024-01-04T03:13:24.657731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"hyper parameters","metadata":{}},{"cell_type":"code","source":"TRAIN_PATH = '/kaggle/input/global-wheat-detection/train/'\nTEST_PATH = '/kaggle/input/global-wheat-detection/test/'\nIMG_SIZE = 1024\nBATCH_SIZE = 16\nEPOCHS = 100","metadata":{"execution":{"iopub.status.busy":"2024-01-04T05:34:19.800489Z","iopub.execute_input":"2024-01-04T05:34:19.801254Z","iopub.status.idle":"2024-01-04T05:34:19.806031Z","shell.execute_reply.started":"2024-01-04T05:34:19.801221Z","shell.execute_reply":"2024-01-04T05:34:19.805087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preparing and visualizing","metadata":{}},{"cell_type":"code","source":"%cd /kaggle/input\n\ndf = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')\ndf = df.assign(path = lambda row: TRAIN_PATH+row.image_id+'.jpg')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-03T22:21:32.907602Z","iopub.execute_input":"2024-01-03T22:21:32.907961Z","iopub.status.idle":"2024-01-03T22:21:33.270274Z","shell.execute_reply.started":"2024-01-03T22:21:32.907933Z","shell.execute_reply":"2024-01-03T22:21:33.269384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_images(df):\n    # set up the canvas for the subplots\n    plt.figure(figsize=(30,30))\n    j = 1\n    for i in range(0,900,100):\n        # Load an image\n        image = plt.imread(df.path[i])\n        \n        #image = cv2.resize(image, (IMAGE_SIZE, IMAGE_SIZE))\n        plt.subplot(3,3,j)\n        j+=1\n        plt.imshow(image)\n        plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2024-01-03T22:21:38.910532Z","iopub.execute_input":"2024-01-03T22:21:38.911336Z","iopub.status.idle":"2024-01-03T22:21:38.917048Z","shell.execute_reply.started":"2024-01-03T22:21:38.911305Z","shell.execute_reply":"2024-01-03T22:21:38.916084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_images(df)","metadata":{"execution":{"iopub.status.busy":"2024-01-03T22:21:53.718559Z","iopub.execute_input":"2024-01-03T22:21:53.718980Z","iopub.status.idle":"2024-01-03T22:21:59.487367Z","shell.execute_reply.started":"2024-01-03T22:21:53.718953Z","shell.execute_reply":"2024-01-03T22:21:59.485660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_list, valid_list = train_test_split(df.image_id.unique(), test_size=0.2, random_state=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-03T22:22:17.229276Z","iopub.execute_input":"2024-01-03T22:22:17.229609Z","iopub.status.idle":"2024-01-03T22:22:17.245965Z","shell.execute_reply.started":"2024-01-03T22:22:17.229584Z","shell.execute_reply":"2024-01-03T22:22:17.245140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = df.loc[df.image_id.isin(train_list)]\nvalid_df = df.loc[df.image_id.isin(valid_list)]\n\ntrain_df = train_df.assign(split = 'train')\nvalid_df = valid_df.assign(split = 'valid')\n\ndf = pd.concat([train_df, valid_df]).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-03T22:22:19.680006Z","iopub.execute_input":"2024-01-03T22:22:19.680723Z","iopub.status.idle":"2024-01-03T22:22:19.798454Z","shell.execute_reply.started":"2024-01-03T22:22:19.680694Z","shell.execute_reply":"2024-01-03T22:22:19.797568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-01-03T23:31:02.606302Z","iopub.execute_input":"2024-01-03T23:31:02.606699Z","iopub.status.idle":"2024-01-03T23:31:02.624335Z","shell.execute_reply.started":"2024-01-03T23:31:02.606653Z","shell.execute_reply":"2024-01-03T23:31:02.623435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#make directories as required for yolo\n\nos.makedirs('/kaggle/working/dataset/images/train', exist_ok=True)\nos.makedirs('/kaggle/working/dataset/images/valid', exist_ok=True)\nos.makedirs('/kaggle/working/dataset/images/test', exist_ok=True)\n\nos.makedirs('/kaggle/working/dataset/labels/train', exist_ok=True)\nos.makedirs('/kaggle/working/dataset/labels/valid', exist_ok=True)\nos.makedirs('/kaggle/working/dataset/labels/test', exist_ok=True)\n\n%cd ../../","metadata":{"execution":{"iopub.status.busy":"2024-01-03T23:31:51.735719Z","iopub.execute_input":"2024-01-03T23:31:51.736090Z","iopub.status.idle":"2024-01-03T23:31:51.751043Z","shell.execute_reply.started":"2024-01-03T23:31:51.736063Z","shell.execute_reply":"2024-01-03T23:31:51.750062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_yolo_format_bbox(img_w, img_h, row):\n    bbox = row.bbox.replace('[','').replace(']','').replace(',','')\n    bboxes = []\n    yolo_bboxes = []\n    for i in bbox.split(\" \"):\n        bboxes.append(float(i))\n    yolo_bboxes = []\n    x, y, w, h = bboxes\n    xc = x + int(w/2) # xmin + width/2\n    yc = y + int(h/2) # ymin + height/2\n    yolo_bboxes=[1, xc/img_w, yc/img_h, w/img_w, h/img_h]\n    yolo_bboxes_str = str(yolo_bboxes).replace(\"[\", \"\").replace(\"]\",\"\").replace(\",\",\"\")\n    return yolo_bboxes_str","metadata":{"execution":{"iopub.status.busy":"2024-01-04T05:28:36.539641Z","iopub.execute_input":"2024-01-04T05:28:36.540085Z","iopub.status.idle":"2024-01-04T05:28:36.548077Z","shell.execute_reply.started":"2024-01-04T05:28:36.540053Z","shell.execute_reply":"2024-01-04T05:28:36.546954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"src = '/kaggle/input/global-wheat-detection/test'\ntrg = '/kaggle/working/dataset/images/test'\n\nfiles=os.listdir(src)\n\n# iterating over all the files in\n# the source directory\nfor fname in files:\n    # copying the files to the destination directory\n    shutil.copy2(os.path.join(src,fname), trg)","metadata":{"execution":{"iopub.status.busy":"2024-01-03T22:27:38.096832Z","iopub.execute_input":"2024-01-03T22:27:38.097511Z","iopub.status.idle":"2024-01-03T22:27:38.115631Z","shell.execute_reply.started":"2024-01-03T22:27:38.097463Z","shell.execute_reply":"2024-01-03T22:27:38.114878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2024-01-03T22:27:41.792418Z","iopub.execute_input":"2024-01-03T22:27:41.793303Z","iopub.status.idle":"2024-01-03T22:27:41.799147Z","shell.execute_reply.started":"2024-01-03T22:27:41.793269Z","shell.execute_reply":"2024-01-03T22:27:41.798269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile dataset.yaml\npath: ../../../../../kaggle/working/dataset\ntrain: images/train\nval: images/valid\ntest: images/test\n\n# Classes\nnames:\n    0: none\n    1: opacity","metadata":{"execution":{"iopub.status.busy":"2024-01-03T23:41:53.620133Z","iopub.execute_input":"2024-01-03T23:41:53.620487Z","iopub.status.idle":"2024-01-03T23:41:53.626219Z","shell.execute_reply.started":"2024-01-03T23:41:53.620462Z","shell.execute_reply":"2024-01-03T23:41:53.625338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(len(df))):\n    row = df.loc[i]\n    #copy all images to the desired destination\n    # create the corresponding file for each image\n    if row.split == 'train':\n        copyfile(row.path, f'/kaggle/working/dataset/images/train/{row.image_id}.jpg')\n        file_name = f'/kaggle/working/dataset/labels/train/{row.image_id}.txt'\n    else:\n        copyfile(row.path, f'/kaggle/working/dataset/images/valid/{row.image_id}.jpg')\n        file_name = f'/kaggle/working/dataset/labels/valid/{row.image_id}.txt'\n    \n    #get the bounding boxes in yolo format\n    bboxes = get_yolo_format_bbox(IMG_SIZE, IMG_SIZE, row)\n    \n    #write the bounding boxes in each corresponding file\n    with open(file_name, 'a') as f:\n        f.write(bboxes)\n        f.write('\\n')","metadata":{"execution":{"iopub.status.busy":"2024-01-03T22:27:56.987622Z","iopub.execute_input":"2024-01-03T22:27:56.988001Z","iopub.status.idle":"2024-01-03T22:28:23.642971Z","shell.execute_reply.started":"2024-01-03T22:27:56.987975Z","shell.execute_reply":"2024-01-03T22:28:23.642120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_IMAGE_PATH = 'kaggle/working/dataset/images/train'\nTRAIN_LABEL_PATH = 'kaggle/working/dataset/labels/train'","metadata":{"execution":{"iopub.status.busy":"2024-01-04T05:22:16.007430Z","iopub.execute_input":"2024-01-04T05:22:16.008200Z","iopub.status.idle":"2024-01-04T05:22:16.012760Z","shell.execute_reply.started":"2024-01-04T05:22:16.008163Z","shell.execute_reply":"2024-01-04T05:22:16.011708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_with_bbox(num, image_path, label_path, test=False):\n    bbox = []\n    plt.figure(figsize=(30,30))\n    labels_files = os.listdir(label_path)\n    \n    for index, label_file in enumerate(labels_files[:num]):\n        with open(f'{label_path}/{label_file}') as f:\n            image_file = label_file.replace('.txt', '.jpg')\n            img = read_image(f'{image_path}/{image_file}')\n            bboxes = []\n            for label in f:\n                bbox = label[:-1].split(\" \")                       # -1 to remove the newline \\n\n                bbox = bbox[1:-1] if test==True else bbox[1:]      # keep only x, y, w, h\n                bbox = [float(i) for i in bbox]\n                bbox = [i*1024 for i in bbox]\n                [x, y, w, h] = bbox\n                [xmin, ymin, xmax, ymax] = [x-w/2, y-h/2, x+w/2, y+h/2]\n                bbox = [xmin, ymin, xmax, ymax]\n                bboxes.append(bbox)\n            bboxes = torch.tensor(bboxes, dtype=torch.int)\n            img = draw_bounding_boxes(img, bboxes, width=10,colors=(255,0,0))\n            img = torchvision.transforms.ToPILImage()(img)\n            plt.subplot(num//3+num%3,3,index+1)\n            plt.imshow(img)\n            plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2024-01-04T05:19:22.449190Z","iopub.execute_input":"2024-01-04T05:19:22.449918Z","iopub.status.idle":"2024-01-04T05:19:22.460281Z","shell.execute_reply.started":"2024-01-04T05:19:22.449887Z","shell.execute_reply":"2024-01-04T05:19:22.459352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_with_bbox(3, TRAIN_IMAGE_PATH, TRAIN_LABEL_PATH)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T05:19:25.236613Z","iopub.execute_input":"2024-01-04T05:19:25.237344Z","iopub.status.idle":"2024-01-04T05:19:26.858281Z","shell.execute_reply.started":"2024-01-04T05:19:25.237311Z","shell.execute_reply":"2024-01-04T05:19:26.857054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ../../../\n%cd kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2024-01-03T22:45:00.992509Z","iopub.execute_input":"2024-01-03T22:45:00.993410Z","iopub.status.idle":"2024-01-03T22:45:01.002784Z","shell.execute_reply.started":"2024-01-03T22:45:00.993379Z","shell.execute_reply":"2024-01-03T22:45:01.001942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train with YOLOv8","metadata":{}},{"cell_type":"code","source":"model = YOLO(\"yolov8n.yaml\")","metadata":{"execution":{"iopub.status.busy":"2024-01-03T23:34:13.205479Z","iopub.execute_input":"2024-01-03T23:34:13.205891Z","iopub.status.idle":"2024-01-03T23:34:13.689590Z","shell.execute_reply.started":"2024-01-03T23:34:13.205860Z","shell.execute_reply":"2024-01-03T23:34:13.688758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install wandb","metadata":{"execution":{"iopub.status.busy":"2024-01-03T23:42:54.645585Z","iopub.execute_input":"2024-01-03T23:42:54.645989Z","iopub.status.idle":"2024-01-03T23:43:08.814692Z","shell.execute_reply.started":"2024-01-03T23:42:54.645959Z","shell.execute_reply":"2024-01-03T23:43:08.813625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wandb off\nmodel.train(data='dataset.yaml', epochs=EPOCHS, imgsz=IMG_SIZE, batch=BATCH_SIZE, save_period=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-03T23:43:08.816480Z","iopub.execute_input":"2024-01-03T23:43:08.816810Z","iopub.status.idle":"2024-01-04T02:56:39.530884Z","shell.execute_reply.started":"2024-01-03T23:43:08.816782Z","shell.execute_reply":"2024-01-04T02:56:39.529766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cd /kaggle\n!ls ","metadata":{"execution":{"iopub.status.busy":"2024-01-04T04:00:24.420773Z","iopub.execute_input":"2024-01-04T04:00:24.421587Z","iopub.status.idle":"2024-01-04T04:00:26.586022Z","shell.execute_reply.started":"2024-01-04T04:00:24.421552Z","shell.execute_reply":"2024-01-04T04:00:26.584872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation","metadata":{}},{"cell_type":"code","source":"%matplotlib inline\n\nplot_results('/runs/detect/train2/results.csv')","metadata":{"execution":{"iopub.status.busy":"2024-01-04T04:00:58.663778Z","iopub.execute_input":"2024-01-04T04:00:58.664194Z","iopub.status.idle":"2024-01-04T04:01:01.028338Z","shell.execute_reply.started":"2024-01-04T04:00:58.664163Z","shell.execute_reply":"2024-01-04T04:01:01.027472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_results(file):\n    plt.figure(figsize=(30,30))\n    img = np.asarray(Image.open(file))\n    plt.imshow(img)\n    plt.axis('off');","metadata":{"execution":{"iopub.status.busy":"2024-01-04T04:01:33.933045Z","iopub.execute_input":"2024-01-04T04:01:33.933928Z","iopub.status.idle":"2024-01-04T04:01:33.938842Z","shell.execute_reply.started":"2024-01-04T04:01:33.933896Z","shell.execute_reply":"2024-01-04T04:01:33.937842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_results('/runs/detect/train2/results.png')","metadata":{"execution":{"iopub.status.busy":"2024-01-04T04:02:04.614278Z","iopub.execute_input":"2024-01-04T04:02:04.615190Z","iopub.status.idle":"2024-01-04T04:02:05.911052Z","shell.execute_reply.started":"2024-01-04T04:02:04.615155Z","shell.execute_reply":"2024-01-04T04:02:05.910053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"copyfile('/runs/detect/train2/results.png','/kaggle/working/results.png')\ncopyfile('/runs/detect/train2/results.csv','/kaggle/working/results.csv')\ncopyfile('/runs/detect/train2/weights/best.pt','/kaggle/working/best.pt')","metadata":{"execution":{"iopub.status.busy":"2024-01-04T04:02:36.405440Z","iopub.execute_input":"2024-01-04T04:02:36.406172Z","iopub.status.idle":"2024-01-04T04:02:36.419270Z","shell.execute_reply.started":"2024-01-04T04:02:36.406136Z","shell.execute_reply":"2024-01-04T04:02:36.418156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"TEST_PATH = 'kaggle/working/dataset/images/test'\nMODEL_PATH = 'runs/detect/train2/weights/best.pt'","metadata":{"execution":{"iopub.status.busy":"2024-01-04T04:04:37.507002Z","iopub.execute_input":"2024-01-04T04:04:37.508002Z","iopub.status.idle":"2024-01-04T04:04:37.512008Z","shell.execute_reply.started":"2024-01-04T04:04:37.507968Z","shell.execute_reply":"2024-01-04T04:04:37.511170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = YOLO(MODEL_PATH)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T04:04:42.671471Z","iopub.execute_input":"2024-01-04T04:04:42.671884Z","iopub.status.idle":"2024-01-04T04:04:42.721546Z","shell.execute_reply.started":"2024-01-04T04:04:42.671852Z","shell.execute_reply":"2024-01-04T04:04:42.720631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict(source=TEST_PATH, imgsz=IMG_SIZE, conf=0.5, iou=0.6, save_txt=True, save_conf=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T04:04:47.780215Z","iopub.execute_input":"2024-01-04T04:04:47.780612Z","iopub.status.idle":"2024-01-04T04:04:48.459623Z","shell.execute_reply.started":"2024-01-04T04:04:47.780581Z","shell.execute_reply":"2024-01-04T04:04:48.458666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PRED_PATH = 'runs/detect/predict/labels'\n!ls {PRED_PATH}","metadata":{"execution":{"iopub.status.busy":"2024-01-04T04:05:37.721938Z","iopub.execute_input":"2024-01-04T04:05:37.722730Z","iopub.status.idle":"2024-01-04T04:05:38.840048Z","shell.execute_reply.started":"2024-01-04T04:05:37.722683Z","shell.execute_reply":"2024-01-04T04:05:38.838932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_with_bbox(3, TEST_PATH, PRED_PATH, test=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T05:51:56.842128Z","iopub.execute_input":"2024-01-04T05:51:56.842920Z","iopub.status.idle":"2024-01-04T05:51:58.670607Z","shell.execute_reply.started":"2024-01-04T05:51:56.842887Z","shell.execute_reply":"2024-01-04T05:51:58.669173Z"},"trusted":true},"execution_count":null,"outputs":[]}]}