{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.10","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"},{"sourceId":5201183,"sourceType":"datasetVersion","datasetId":3024733},{"sourceId":5799257,"sourceType":"datasetVersion","datasetId":3330777}],"dockerImageVersionId":30498,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# ☀️ Imports and Setup","metadata":{"papermill":{"duration":0.013312,"end_time":"2023-06-06T16:27:28.302478","exception":false,"start_time":"2023-06-06T16:27:28.289166","status":"completed"},"tags":[]}},{"cell_type":"code","source":"#for offline run\n%cd ../input/yolov8\n!pip install -e\n#!pip install -r requirements.txt","metadata":{"papermill":{"duration":1.755793,"end_time":"2023-06-06T16:27:30.070667","exception":false,"start_time":"2023-06-06T16:27:28.314874","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:26:36.883651Z","iopub.execute_input":"2023-11-20T04:26:36.884440Z","iopub.status.idle":"2023-11-20T04:26:38.643779Z","shell.execute_reply.started":"2023-11-20T04:26:36.884403Z","shell.execute_reply":"2023-11-20T04:26:38.642797Z"},"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":{"papermill":{"duration":13.3255,"end_time":"2023-06-06T16:27:43.409221","exception":false,"start_time":"2023-06-06T16:27:30.083721","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:26:38.646225Z","iopub.execute_input":"2023-11-20T04:26:38.646516Z","iopub.status.idle":"2023-11-20T04:26:51.323566Z","shell.execute_reply.started":"2023-11-20T04:26:38.646490Z","shell.execute_reply":"2023-11-20T04:26:51.322719Z"},"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 ultralytics.yolo.utils import LOGGER, TryExcept, plt_settings, threaded\nfrom ultralytics.yolo.utils.plotting import *\nfrom scipy.ndimage import gaussian_filter1d\n\n%matplotlib inline","metadata":{"papermill":{"duration":1.196604,"end_time":"2023-06-06T16:27:44.619017","exception":false,"start_time":"2023-06-06T16:27:43.422413","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:26:51.324645Z","iopub.execute_input":"2023-11-20T04:26:51.325113Z","iopub.status.idle":"2023-11-20T04:26:52.088422Z","shell.execute_reply.started":"2023-11-20T04:26:51.325088Z","shell.execute_reply":"2023-11-20T04:26:52.087588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🦆 Hyperparameters","metadata":{"papermill":{"duration":0.013167,"end_time":"2023-06-06T16:27:44.647075","exception":false,"start_time":"2023-06-06T16:27:44.633908","status":"completed"},"tags":[]}},{"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":{"papermill":{"duration":0.021094,"end_time":"2023-06-06T16:27:44.68133","exception":false,"start_time":"2023-06-06T16:27:44.660236","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:26:52.089572Z","iopub.execute_input":"2023-11-20T04:26:52.089814Z","iopub.status.idle":"2023-11-20T04:26:52.094234Z","shell.execute_reply.started":"2023-11-20T04:26:52.089792Z","shell.execute_reply":"2023-11-20T04:26:52.093393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🔨 Prepare Dataset","metadata":{"papermill":{"duration":0.012256,"end_time":"2023-06-06T16:27:44.706436","exception":false,"start_time":"2023-06-06T16:27:44.69418","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%cd ../\n\ndf = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')\ndf['path'] = df.apply(lambda row: TRAIN_PATH+row.image_id+'.jpg', axis=1)\ndf.head()","metadata":{"papermill":{"duration":2.557067,"end_time":"2023-06-06T16:27:47.27608","exception":false,"start_time":"2023-06-06T16:27:44.719013","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:26:52.097217Z","iopub.execute_input":"2023-11-20T04:26:52.097524Z","iopub.status.idle":"2023-11-20T04:26:54.637362Z","shell.execute_reply.started":"2023-11-20T04:26:52.097501Z","shell.execute_reply":"2023-11-20T04:26:54.636390Z"},"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":{"papermill":{"duration":0.022043,"end_time":"2023-06-06T16:27:47.311317","exception":false,"start_time":"2023-06-06T16:27:47.289274","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:26:54.638544Z","iopub.execute_input":"2023-11-20T04:26:54.638836Z","iopub.status.idle":"2023-11-20T04:26:54.644960Z","shell.execute_reply.started":"2023-11-20T04:26:54.638810Z","shell.execute_reply":"2023-11-20T04:26:54.643961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_images(df)","metadata":{"papermill":{"duration":5.415792,"end_time":"2023-06-06T16:27:52.739827","exception":false,"start_time":"2023-06-06T16:27:47.324035","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:26:54.646189Z","iopub.execute_input":"2023-11-20T04:26:54.646514Z","iopub.status.idle":"2023-11-20T04:27:00.226716Z","shell.execute_reply.started":"2023-11-20T04:26:54.646484Z","shell.execute_reply":"2023-11-20T04:27:00.225183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🍘 Train-validation split","metadata":{"papermill":{"duration":0.154225,"end_time":"2023-06-06T16:27:53.045585","exception":false,"start_time":"2023-06-06T16:27:52.89136","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_list, valid_list = train_test_split(df.image_id.unique(), test_size=0.2, random_state=1)","metadata":{"papermill":{"duration":0.172927,"end_time":"2023-06-06T16:27:53.366393","exception":false,"start_time":"2023-06-06T16:27:53.193466","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:27:00.228728Z","iopub.execute_input":"2023-11-20T04:27:00.229550Z","iopub.status.idle":"2023-11-20T04:27:00.256889Z","shell.execute_reply.started":"2023-11-20T04:27:00.229503Z","shell.execute_reply":"2023-11-20T04:27:00.255923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_list)","metadata":{"papermill":{"duration":0.159499,"end_time":"2023-06-06T16:27:53.674638","exception":false,"start_time":"2023-06-06T16:27:53.515139","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:27:00.258092Z","iopub.execute_input":"2023-11-20T04:27:00.258380Z","iopub.status.idle":"2023-11-20T04:27:00.264567Z","shell.execute_reply.started":"2023-11-20T04:27:00.258354Z","shell.execute_reply":"2023-11-20T04:27:00.263617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(valid_list)","metadata":{"papermill":{"duration":0.159987,"end_time":"2023-06-06T16:27:53.982187","exception":false,"start_time":"2023-06-06T16:27:53.8222","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:27:00.265589Z","iopub.execute_input":"2023-11-20T04:27:00.265839Z","iopub.status.idle":"2023-11-20T04:27:00.274133Z","shell.execute_reply.started":"2023-11-20T04:27:00.265817Z","shell.execute_reply":"2023-11-20T04:27:00.273248Z"},"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.loc[:, 'split'] = 'train'\nvalid_df.loc[:, 'split'] = 'valid'\n\ndf = pd.concat([train_df, valid_df]).reset_index(drop=True)","metadata":{"papermill":{"duration":0.324912,"end_time":"2023-06-06T16:27:54.454468","exception":false,"start_time":"2023-06-06T16:27:54.129556","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:27:00.275239Z","iopub.execute_input":"2023-11-20T04:27:00.275502Z","iopub.status.idle":"2023-11-20T04:27:00.406171Z","shell.execute_reply.started":"2023-11-20T04:27:00.275468Z","shell.execute_reply":"2023-11-20T04:27:00.405193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_df), len(valid_df)","metadata":{"papermill":{"duration":0.233116,"end_time":"2023-06-06T16:27:54.907778","exception":false,"start_time":"2023-06-06T16:27:54.674662","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:27:00.407397Z","iopub.execute_input":"2023-11-20T04:27:00.407716Z","iopub.status.idle":"2023-11-20T04:27:00.413921Z","shell.execute_reply.started":"2023-11-20T04:27:00.407689Z","shell.execute_reply":"2023-11-20T04:27:00.413019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"papermill":{"duration":0.23076,"end_time":"2023-06-06T16:27:55.34865","exception":false,"start_time":"2023-06-06T16:27:55.11789","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:27:00.415061Z","iopub.execute_input":"2023-11-20T04:27:00.415400Z","iopub.status.idle":"2023-11-20T04:27:00.437736Z","shell.execute_reply.started":"2023-11-20T04:27:00.415371Z","shell.execute_reply":"2023-11-20T04:27:00.436799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Size of dataset: {len(df)}, training images: {len(train_df)}. validation images: {len(valid_df)}')","metadata":{"papermill":{"duration":0.230022,"end_time":"2023-06-06T16:27:55.782063","exception":false,"start_time":"2023-06-06T16:27:55.552041","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:27:00.442752Z","iopub.execute_input":"2023-11-20T04:27:00.443061Z","iopub.status.idle":"2023-11-20T04:27:00.448264Z","shell.execute_reply.started":"2023-11-20T04:27:00.443037Z","shell.execute_reply":"2023-11-20T04:27:00.447425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ../../\n%cd kaggle/working/\n\nos.makedirs('dataset/images/train', exist_ok=True)\nos.makedirs('dataset/images/valid', exist_ok=True)\nos.makedirs('dataset/images/test', exist_ok=True)\n\nos.makedirs('dataset/labels/train', exist_ok=True)\nos.makedirs('dataset/labels/valid', exist_ok=True)\nos.makedirs('dataset/labels/test', exist_ok=True)\n\n%cd ../../\n!tree kaggle/working/dataset","metadata":{"papermill":{"duration":1.134747,"end_time":"2023-06-06T16:27:57.107895","exception":false,"start_time":"2023-06-06T16:27:55.973148","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:27:00.449774Z","iopub.execute_input":"2023-11-20T04:27:00.450760Z","iopub.status.idle":"2023-11-20T04:27:01.428138Z","shell.execute_reply.started":"2023-11-20T04:27:00.450717Z","shell.execute_reply":"2023-11-20T04:27:01.427179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Move the images to relevant split folder.\nfor i in tqdm(range(len(df))):\n    row = df.loc[i]\n    if row.split == 'train':\n        copyfile(row.path, f'kaggle/working/dataset/images/train/{row.image_id}.jpg')\n    else:\n        copyfile(row.path, f'kaggle/working/dataset/images/valid/{row.image_id}.jpg')","metadata":{"papermill":{"duration":201.459969,"end_time":"2023-06-06T16:31:18.716835","exception":false,"start_time":"2023-06-06T16:27:57.256866","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:27:01.429558Z","iopub.execute_input":"2023-11-20T04:27:01.429834Z","iopub.status.idle":"2023-11-20T04:29:56.535039Z","shell.execute_reply.started":"2023-11-20T04:27:01.429809Z","shell.execute_reply":"2023-11-20T04:29:56.534038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Move the test images to relevant split folder.\n# paths\nsrc = '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\n    # destination directory\n    shutil.copy2(os.path.join(src,fname), trg)","metadata":{"papermill":{"duration":0.385675,"end_time":"2023-06-06T16:31:19.398842","exception":false,"start_time":"2023-06-06T16:31:19.013167","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:29:56.536473Z","iopub.execute_input":"2023-11-20T04:29:56.536856Z","iopub.status.idle":"2023-11-20T04:29:56.619177Z","shell.execute_reply.started":"2023-11-20T04:29:56.536820Z","shell.execute_reply":"2023-11-20T04:29:56.618324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🍜 Create `.YAML` file","metadata":{"papermill":{"duration":0.298336,"end_time":"2023-06-06T16:31:19.994321","exception":false,"start_time":"2023-06-06T16:31:19.695985","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%cd kaggle/working/","metadata":{"papermill":{"duration":0.321759,"end_time":"2023-06-06T16:31:20.617289","exception":false,"start_time":"2023-06-06T16:31:20.29553","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:29:56.620380Z","iopub.execute_input":"2023-11-20T04:29:56.620726Z","iopub.status.idle":"2023-11-20T04:29:56.627416Z","shell.execute_reply.started":"2023-11-20T04:29:56.620692Z","shell.execute_reply":"2023-11-20T04:29:56.626538Z"},"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":{"papermill":{"duration":0.304879,"end_time":"2023-06-06T16:31:21.216009","exception":false,"start_time":"2023-06-06T16:31:20.91113","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:29:56.628797Z","iopub.execute_input":"2023-11-20T04:29:56.629074Z","iopub.status.idle":"2023-11-20T04:29:56.637953Z","shell.execute_reply.started":"2023-11-20T04:29:56.629051Z","shell.execute_reply":"2023-11-20T04:29:56.637103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cat dataset.yaml","metadata":{"papermill":{"duration":1.306598,"end_time":"2023-06-06T16:31:22.808821","exception":false,"start_time":"2023-06-06T16:31:21.502223","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:29:56.639242Z","iopub.execute_input":"2023-11-20T04:29:56.639610Z","iopub.status.idle":"2023-11-20T04:29:57.645941Z","shell.execute_reply.started":"2023-11-20T04:29:56.639547Z","shell.execute_reply":"2023-11-20T04:29:57.644832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🍮 Prepare Bounding Box Coordinated for YOLOv8","metadata":{"papermill":{"duration":0.293184,"end_time":"2023-06-06T16:31:23.393889","exception":false,"start_time":"2023-06-06T16:31:23.100705","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def get_bbox(row):\n    bboxes = []\n    bbox = []\n    list1 = row.bbox.replace(\"[\", \"\").replace(\"]\",\"\").replace(\",\",\"\")\n    for i in list1.split(\" \"):\n        bboxes.append(float(i))\n    return bboxes","metadata":{"papermill":{"duration":0.296854,"end_time":"2023-06-06T16:31:23.989766","exception":false,"start_time":"2023-06-06T16:31:23.692912","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:29:57.647406Z","iopub.execute_input":"2023-11-20T04:29:57.647716Z","iopub.status.idle":"2023-11-20T04:29:57.653714Z","shell.execute_reply.started":"2023-11-20T04:29:57.647687Z","shell.execute_reply":"2023-11-20T04:29:57.652767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert the bounding boxes in YOLO format.\ndef get_yolo_format_bbox(img_w, img_h, bboxes):\n    yolo_boxes = []\n    x = bboxes[0]\n    y = bboxes[1]\n    w = bboxes[2] \n    h = bboxes[3] \n    xc = x + int(w/2) # xmin + width/2\n    yc = y + int(h/2) # ymin + height/2\n    yolo_boxes=[1, xc/img_w, yc/img_h, w/img_w, h/img_h] # x_center y_center width height\n    yolo_boxes = str(yolo_boxes).replace(\"[\", \"\").replace(\"]\",\"\").replace(\",\",\"\")\n    return yolo_boxes","metadata":{"papermill":{"duration":0.298753,"end_time":"2023-06-06T16:31:24.572161","exception":false,"start_time":"2023-06-06T16:31:24.273408","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:29:57.655021Z","iopub.execute_input":"2023-11-20T04:29:57.655341Z","iopub.status.idle":"2023-11-20T04:29:58.391939Z","shell.execute_reply.started":"2023-11-20T04:29:57.655308Z","shell.execute_reply":"2023-11-20T04:29:58.390988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare the txt files for bounding box\nfor i in tqdm(range(len(df))):\n    row = df.loc[i]\n    # Get image id\n    img_id = row.image_id\n    # Get split\n    split = row.split\n    \n    if row.split=='train':\n        file_name = f'dataset/labels/train/{row.image_id}.txt'\n    else:\n        file_name = f'dataset/labels/valid/{row.image_id}.txt'\n        \n    bboxes = get_bbox(row)\n        \n    # Format for YOLOv5\n    yolo_bboxes = get_yolo_format_bbox(IMG_SIZE, IMG_SIZE, bboxes)\n        \n    with open(file_name, 'a') as f:\n        f.write(yolo_bboxes)\n        f.write('\\n')\n    #print(file_name)\n    #print(yolo_bboxes)","metadata":{"papermill":{"duration":35.588921,"end_time":"2023-06-06T16:32:00.452897","exception":false,"start_time":"2023-06-06T16:31:24.863976","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:29:58.393343Z","iopub.execute_input":"2023-11-20T04:29:58.393723Z","iopub.status.idle":"2023-11-20T04:30:31.641754Z","shell.execute_reply.started":"2023-11-20T04:29:58.393690Z","shell.execute_reply":"2023-11-20T04:30:31.640807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = 'dataset/images/train'\nTRAIN_LABEL_PATH = 'dataset/labels/train'","metadata":{"papermill":{"duration":0.321333,"end_time":"2023-06-06T16:32:01.097671","exception":false,"start_time":"2023-06-06T16:32:00.776338","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:30:31.642954Z","iopub.execute_input":"2023-11-20T04:30:31.643270Z","iopub.status.idle":"2023-11-20T04:30:31.647604Z","shell.execute_reply.started":"2023-11-20T04:30:31.643237Z","shell.execute_reply":"2023-11-20T04:30:31.646606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files = os.listdir(TRAIN_LABEL_PATH)\nprint('Number of test images predicted as opaque: ', len(train_files))","metadata":{"papermill":{"duration":0.330249,"end_time":"2023-06-06T16:32:01.739719","exception":false,"start_time":"2023-06-06T16:32:01.40947","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:30:31.648736Z","iopub.execute_input":"2023-11-20T04:30:31.649310Z","iopub.status.idle":"2023-11-20T04:30:31.660500Z","shell.execute_reply.started":"2023-11-20T04:30:31.649270Z","shell.execute_reply":"2023-11-20T04:30:31.659711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_images_with_box(num):\n    pred = []\n    bbox = []\n    image_id = []\n    k = 1\n    plt.figure(figsize=(30,30))\n    for i in train_files[:num]:\n        with open(f'{TRAIN_LABEL_PATH}/{i}') as f:\n            img = read_image(f'{TRAIN_PATH}/{i[:-4]}.jpg')\n            bbox = []\n            for j in f:\n                pred = i[:-4] + \" \" + j[:-1]\n                pred = pred.split(\" \")\n                pred.pop(0)\n                pred.pop(0)\n                pred = [eval(i) for i in pred]\n                pred = [i*1024 for i in pred]\n                [x, y, w, h] = pred\n                [xmin, ymin, xmax, ymax] = [x-w/2, y-h/2, x+w/2, y+h/2,]\n                pred = [xmin, ymin, xmax, ymax]\n                bbox.append(pred)\n            bbox = torch.tensor(bbox, dtype=torch.int)\n            img = draw_bounding_boxes(img, bbox, width=10,colors=(255,0,0))\n            img = torchvision.transforms.ToPILImage()(img)\n            plt.subplot(num//3+num%3,3,k)\n            k+=1\n            plt.imshow(img)\n            plt.axis('off')","metadata":{"papermill":{"duration":0.331863,"end_time":"2023-06-06T16:32:02.383823","exception":false,"start_time":"2023-06-06T16:32:02.05196","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:30:31.662052Z","iopub.execute_input":"2023-11-20T04:30:31.662473Z","iopub.status.idle":"2023-11-20T04:30:31.673514Z","shell.execute_reply.started":"2023-11-20T04:30:31.662440Z","shell.execute_reply":"2023-11-20T04:30:31.672806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_images_with_box(12)","metadata":{"papermill":{"duration":5.540259,"end_time":"2023-06-06T16:32:08.245597","exception":false,"start_time":"2023-06-06T16:32:02.705338","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:30:31.674541Z","iopub.execute_input":"2023-11-20T04:30:31.674817Z","iopub.status.idle":"2023-11-20T04:30:36.526426Z","shell.execute_reply.started":"2023-11-20T04:30:31.674793Z","shell.execute_reply":"2023-11-20T04:30:36.524774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🚅 Train ","metadata":{"papermill":{"duration":0.420843,"end_time":"2023-06-06T16:32:09.139832","exception":false,"start_time":"2023-06-06T16:32:08.718989","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%cd ../../../\n%cd kaggle/working/","metadata":{"papermill":{"duration":0.446683,"end_time":"2023-06-06T16:32:10.008564","exception":false,"start_time":"2023-06-06T16:32:09.561881","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:30:36.528547Z","iopub.execute_input":"2023-11-20T04:30:36.529234Z","iopub.status.idle":"2023-11-20T04:30:36.547923Z","shell.execute_reply.started":"2023-11-20T04:30:36.529184Z","shell.execute_reply":"2023-11-20T04:30:36.546871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = YOLO(\"yolov8n.yaml\")  # build a new model from scratch\nmodel = YOLO(\"../input/roi-mammography416-yolov8/ROI Mammography 416/yolov8n.pt\")  # load a pretrained model","metadata":{"papermill":{"duration":1.125634,"end_time":"2023-06-06T16:32:11.564038","exception":false,"start_time":"2023-06-06T16:32:10.438404","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:30:36.549132Z","iopub.execute_input":"2023-11-20T04:30:36.549416Z","iopub.status.idle":"2023-11-20T04:30:37.096771Z","shell.execute_reply.started":"2023-11-20T04:30:36.549391Z","shell.execute_reply":"2023-11-20T04:30:37.095938Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"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, cls=7)","metadata":{"papermill":{"duration":6683.255917,"end_time":"2023-06-06T18:23:35.24285","exception":false,"start_time":"2023-06-06T16:32:11.986933","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T04:30:37.097985Z","iopub.execute_input":"2023-11-20T04:30:37.098278Z","iopub.status.idle":"2023-11-20T07:53:16.539075Z","shell.execute_reply.started":"2023-11-20T04:30:37.098252Z","shell.execute_reply":"2023-11-20T07:53:16.538010Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📈 Evaluation","metadata":{"papermill":{"duration":1.351517,"end_time":"2023-06-06T18:23:37.881982","exception":false,"start_time":"2023-06-06T18:23:36.530465","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%matplotlib inline\nplot_results('/kaggle/working/runs/detect/train/results.csv')","metadata":{"papermill":{"duration":3.077222,"end_time":"2023-06-06T18:23:42.314723","exception":false,"start_time":"2023-06-06T18:23:39.237501","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:16.541429Z","iopub.execute_input":"2023-11-20T07:53:16.542049Z","iopub.status.idle":"2023-11-20T07:53:18.708724Z","shell.execute_reply.started":"2023-11-20T07:53:16.541973Z","shell.execute_reply":"2023-11-20T07:53:18.707420Z"},"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":{"papermill":{"duration":1.312901,"end_time":"2023-06-06T18:23:45.337841","exception":false,"start_time":"2023-06-06T18:23:44.02494","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:18.710053Z","iopub.execute_input":"2023-11-20T07:53:18.710400Z","iopub.status.idle":"2023-11-20T07:53:18.718279Z","shell.execute_reply.started":"2023-11-20T07:53:18.710357Z","shell.execute_reply":"2023-11-20T07:53:18.717053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_results('/kaggle/working/runs/detect/train/results.png')","metadata":{"papermill":{"duration":2.51576,"end_time":"2023-06-06T18:23:49.202066","exception":false,"start_time":"2023-06-06T18:23:46.686306","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:18.719827Z","iopub.execute_input":"2023-11-20T07:53:18.720275Z","iopub.status.idle":"2023-11-20T07:53:20.033900Z","shell.execute_reply.started":"2023-11-20T07:53:18.720231Z","shell.execute_reply":"2023-11-20T07:53:20.032797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Copy somefile for later use:","metadata":{"papermill":{"duration":1.306487,"end_time":"2023-06-06T18:23:51.967809","exception":false,"start_time":"2023-06-06T18:23:50.661322","status":"completed"},"tags":[]}},{"cell_type":"code","source":"copyfile('/kaggle/working/runs/detect/train/results.png','/kaggle/working/results.png')\ncopyfile('/kaggle/working/runs/detect/train/results.csv','/kaggle/working/results.csv')\ncopyfile('/kaggle/working/runs/detect/train/weights/best.pt','/kaggle/working/best.pt')","metadata":{"papermill":{"duration":1.386992,"end_time":"2023-06-06T18:23:54.68348","exception":false,"start_time":"2023-06-06T18:23:53.296488","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:20.035396Z","iopub.execute_input":"2023-11-20T07:53:20.035720Z","iopub.status.idle":"2023-11-20T07:53:20.053336Z","shell.execute_reply.started":"2023-11-20T07:53:20.035691Z","shell.execute_reply":"2023-11-20T07:53:20.052502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📝 Prediction","metadata":{"papermill":{"duration":1.490152,"end_time":"2023-06-06T18:23:57.467106","exception":false,"start_time":"2023-06-06T18:23:55.976954","status":"completed"},"tags":[]}},{"cell_type":"code","source":"TEST_PATH = 'dataset/images/test'","metadata":{"papermill":{"duration":1.410286,"end_time":"2023-06-06T18:24:00.178491","exception":false,"start_time":"2023-06-06T18:23:58.768205","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:20.054829Z","iopub.execute_input":"2023-11-20T07:53:20.055204Z","iopub.status.idle":"2023-11-20T07:53:20.062172Z","shell.execute_reply.started":"2023-11-20T07:53:20.055171Z","shell.execute_reply":"2023-11-20T07:53:20.061036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODEL_PATH = 'runs/detect/train/weights/best.pt'","metadata":{"papermill":{"duration":1.312833,"end_time":"2023-06-06T18:24:02.860666","exception":false,"start_time":"2023-06-06T18:24:01.547833","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:20.063929Z","iopub.execute_input":"2023-11-20T07:53:20.064232Z","iopub.status.idle":"2023-11-20T07:53:20.074822Z","shell.execute_reply.started":"2023-11-20T07:53:20.064208Z","shell.execute_reply":"2023-11-20T07:53:20.073373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = YOLO(MODEL_PATH)","metadata":{"papermill":{"duration":1.357279,"end_time":"2023-06-06T18:24:05.620595","exception":false,"start_time":"2023-06-06T18:24:04.263316","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:20.076939Z","iopub.execute_input":"2023-11-20T07:53:20.077609Z","iopub.status.idle":"2023-11-20T07:53:20.222070Z","shell.execute_reply.started":"2023-11-20T07:53:20.077571Z","shell.execute_reply":"2023-11-20T07:53:20.220042Z"},"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":{"papermill":{"duration":1.98527,"end_time":"2023-06-06T18:24:08.976716","exception":false,"start_time":"2023-06-06T18:24:06.991446","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:20.223485Z","iopub.execute_input":"2023-11-20T07:53:20.223806Z","iopub.status.idle":"2023-11-20T07:53:20.904932Z","shell.execute_reply.started":"2023-11-20T07:53:20.223779Z","shell.execute_reply":"2023-11-20T07:53:20.903921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ../../../\n%cd kaggle/working/","metadata":{"papermill":{"duration":1.372891,"end_time":"2023-06-06T18:24:11.645148","exception":false,"start_time":"2023-06-06T18:24:10.272257","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:20.906333Z","iopub.execute_input":"2023-11-20T07:53:20.906683Z","iopub.status.idle":"2023-11-20T07:53:20.918591Z","shell.execute_reply.started":"2023-11-20T07:53:20.906650Z","shell.execute_reply":"2023-11-20T07:53:20.917549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PRED_PATH = 'runs/detect/predict/labels'\n!ls {PRED_PATH}","metadata":{"papermill":{"duration":2.937964,"end_time":"2023-06-06T18:24:16.013203","exception":false,"start_time":"2023-06-06T18:24:13.075239","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:20.919931Z","iopub.execute_input":"2023-11-20T07:53:20.920377Z","iopub.status.idle":"2023-11-20T07:53:21.996047Z","shell.execute_reply.started":"2023-11-20T07:53:20.920342Z","shell.execute_reply":"2023-11-20T07:53:21.994824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_files = os.listdir(PRED_PATH)\nprint('Number of test images predicted as opaque: ', len(prediction_files))","metadata":{"papermill":{"duration":1.315826,"end_time":"2023-06-06T18:24:18.711724","exception":false,"start_time":"2023-06-06T18:24:17.395898","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:22.005359Z","iopub.execute_input":"2023-11-20T07:53:22.005785Z","iopub.status.idle":"2023-11-20T07:53:22.014227Z","shell.execute_reply.started":"2023-11-20T07:53:22.005740Z","shell.execute_reply":"2023-11-20T07:53:22.013025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_preds_with_box(num):\n    pred = []\n    bbox = []\n    image_id = []\n    k = 1\n    plt.figure(figsize=(30,30))\n    for i in prediction_files[:num]:\n        with open(f'{PRED_PATH}/{i}') as f:\n            img = read_image(f'{TEST_PATH}/{i[:-4]}.jpg')\n            bbox = []\n            for j in f:\n                pred = i[:-4] + \" \" + j[:-1]\n                pred = pred.split(\" \")\n                pred.pop(0)\n                pred.pop(0)\n                pred.pop(-1)\n                pred = [eval(i) for i in pred]\n                pred = [i*1024 for i in pred]\n                [x, y, w, h] = pred\n                [xmin, ymin, xmax, ymax] = [x-w/2, y-h/2, x+w/2, y+h/2]\n                pred = [xmin, ymin, xmax, ymax]\n                bbox.append(pred)\n            bbox = torch.tensor(bbox, dtype=torch.int)\n            img = draw_bounding_boxes(img, bbox, width=10,colors=(255,0,0))\n            img = torchvision.transforms.ToPILImage()(img)\n            plt.subplot(num//3+num%3,3,k)\n            k+=1\n            plt.imshow(img)\n            plt.axis('off')","metadata":{"papermill":{"duration":1.33197,"end_time":"2023-06-06T18:24:21.426858","exception":false,"start_time":"2023-06-06T18:24:20.094888","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:22.015819Z","iopub.execute_input":"2023-11-20T07:53:22.016407Z","iopub.status.idle":"2023-11-20T07:53:22.029750Z","shell.execute_reply.started":"2023-11-20T07:53:22.016380Z","shell.execute_reply":"2023-11-20T07:53:22.028732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_preds_with_box(10)","metadata":{"papermill":{"duration":5.631071,"end_time":"2023-06-06T18:24:28.458077","exception":false,"start_time":"2023-06-06T18:24:22.827006","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:22.031148Z","iopub.execute_input":"2023-11-20T07:53:22.031515Z","iopub.status.idle":"2023-11-20T07:53:26.255641Z","shell.execute_reply.started":"2023-11-20T07:53:22.031484Z","shell.execute_reply":"2023-11-20T07:53:26.254099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📃 Submission file","metadata":{"papermill":{"duration":1.395849,"end_time":"2023-06-06T18:24:31.319599","exception":false,"start_time":"2023-06-06T18:24:29.92375","status":"completed"},"tags":[]}},{"cell_type":"code","source":"width = IMG_SIZE\nheight = IMG_SIZE\nresults = []\n\nresult_dir = PRED_PATH\nfor filename in glob.glob(os.path.join(result_dir,'*.txt')):\n    prefix = filename.split(\"/\")[-1].split(\".\")[0]\n    predict_str = \"\"\n    with open(filename, 'r') as fp:\n        for cnt, line in enumerate(fp):\n            entries = np.array(line.strip('\\n').split()).astype(float)\n            bbox = np.round([(entries[1]-entries[3]/2.0)*width,(entries[2]-entries[4]/2.0)*height,entries[3]*width,entries[4]*height],3).tolist()\n            conf = entries[-1]\n            predict_str = predict_str + str(conf) + \" \" + \" \".join(map(str,bbox)) + \" \" \n        results.append({'image_id':prefix, 'PredictionString':predict_str.rstrip()}) \n        \nresultFrame = pd.DataFrame(results)","metadata":{"papermill":{"duration":1.569817,"end_time":"2023-06-06T18:24:34.285996","exception":false,"start_time":"2023-06-06T18:24:32.716179","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:26.257578Z","iopub.execute_input":"2023-11-20T07:53:26.258106Z","iopub.status.idle":"2023-11-20T07:53:26.287629Z","shell.execute_reply.started":"2023-11-20T07:53:26.258056Z","shell.execute_reply":"2023-11-20T07:53:26.286718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = []\nfor filename in glob.glob('/kaggle/input/global-wheat-detection/test/*.jpg'):\n    \n    prefix = filename.split(\"/\")[-1].split(\".\")[0]\n    if resultFrame['image_id'].str.contains(prefix).any():\n        pass\n    else:\n        temp.append({'image_id':prefix, 'PredictionString':\"\"})\n        \ndf = pd.DataFrame(temp)\nsubmission_df = pd.concat([resultFrame,df])","metadata":{"papermill":{"duration":1.414466,"end_time":"2023-06-06T18:24:37.114867","exception":false,"start_time":"2023-06-06T18:24:35.700401","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:26.289133Z","iopub.execute_input":"2023-11-20T07:53:26.290250Z","iopub.status.idle":"2023-11-20T07:53:26.309369Z","shell.execute_reply.started":"2023-11-20T07:53:26.290211Z","shell.execute_reply":"2023-11-20T07:53:26.308051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ../../../\n%cd kaggle/working/\n\nsubmission_df.to_csv('submission.csv', index=False)\nsubmission_df.tail(11)","metadata":{"papermill":{"duration":1.445311,"end_time":"2023-06-06T18:24:40.054479","exception":false,"start_time":"2023-06-06T18:24:38.609168","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:53:26.311400Z","iopub.execute_input":"2023-11-20T07:53:26.312260Z","iopub.status.idle":"2023-11-20T07:53:26.338591Z","shell.execute_reply.started":"2023-11-20T07:53:26.312215Z","shell.execute_reply":"2023-11-20T07:53:26.336871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf dataset/\n!rm -rf runs/\n!rm -rf wandb/\n!rm dataset.yaml","metadata":{"papermill":{"duration":6.464078,"end_time":"2023-06-06T18:24:48.04118","exception":false,"start_time":"2023-06-06T18:24:41.577102","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-20T07:57:48.475842Z","iopub.execute_input":"2023-11-20T07:57:48.476787Z","iopub.status.idle":"2023-11-20T07:57:52.802009Z","shell.execute_reply.started":"2023-11-20T07:57:48.476751Z","shell.execute_reply":"2023-11-20T07:57:52.800780Z"},"trusted":true},"execution_count":null,"outputs":[]}]}