{"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":"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":{"execution":{"iopub.execute_input":"2023-06-06T16:27:28.329141Z","iopub.status.busy":"2023-06-06T16:27:28.328204Z","iopub.status.idle":"2023-06-06T16:27:30.068198Z","shell.execute_reply":"2023-06-06T16:27:30.067099Z"},"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_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nprint(torch.__version__)\n\nimport ultralytics\nfrom ultralytics import YOLO\nultralytics.checks()","metadata":{"execution":{"iopub.execute_input":"2023-06-06T16:27:30.097946Z","iopub.status.busy":"2023-06-06T16:27:30.097320Z","iopub.status.idle":"2023-06-06T16:27:43.406694Z","shell.execute_reply":"2023-06-06T16:27:43.405769Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T16:27:43.438428Z","iopub.status.busy":"2023-06-06T16:27:43.436408Z","iopub.status.idle":"2023-06-06T16:27:44.616353Z","shell.execute_reply":"2023-06-06T16:27:44.615294Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T16:27:44.674800Z","iopub.status.busy":"2023-06-06T16:27:44.673980Z","iopub.status.idle":"2023-06-06T16:27:44.679498Z","shell.execute_reply":"2023-06-06T16:27:44.678690Z"},"papermill":{"duration":0.021094,"end_time":"2023-06-06T16:27:44.681330","exception":false,"start_time":"2023-06-06T16:27:44.660236","status":"completed"},"tags":[]},"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.694180","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":{"execution":{"iopub.execute_input":"2023-06-06T16:27:44.733780Z","iopub.status.busy":"2023-06-06T16:27:44.732415Z","iopub.status.idle":"2023-06-06T16:27:47.274037Z","shell.execute_reply":"2023-06-06T16:27:47.273090Z"},"papermill":{"duration":2.557067,"end_time":"2023-06-06T16:27:47.276080","exception":false,"start_time":"2023-06-06T16:27:44.719013","status":"completed"},"tags":[]},"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.execute_input":"2023-06-06T16:27:47.304064Z","iopub.status.busy":"2023-06-06T16:27:47.303398Z","iopub.status.idle":"2023-06-06T16:27:47.309140Z","shell.execute_reply":"2023-06-06T16:27:47.308240Z"},"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_count":null,"outputs":[]},{"cell_type":"code","source":"display_images(df)","metadata":{"execution":{"iopub.execute_input":"2023-06-06T16:27:47.337820Z","iopub.status.busy":"2023-06-06T16:27:47.337561Z","iopub.status.idle":"2023-06-06T16:27:52.648494Z","shell.execute_reply":"2023-06-06T16:27:52.647276Z"},"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_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.891360","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":{"execution":{"iopub.execute_input":"2023-06-06T16:27:53.343092Z","iopub.status.busy":"2023-06-06T16:27:53.342578Z","iopub.status.idle":"2023-06-06T16:27:53.364363Z","shell.execute_reply":"2023-06-06T16:27:53.363501Z"},"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_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_list)","metadata":{"execution":{"iopub.execute_input":"2023-06-06T16:27:53.666454Z","iopub.status.busy":"2023-06-06T16:27:53.665959Z","iopub.status.idle":"2023-06-06T16:27:53.672535Z","shell.execute_reply":"2023-06-06T16:27:53.671614Z"},"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_count":null,"outputs":[]},{"cell_type":"code","source":"len(valid_list)","metadata":{"execution":{"iopub.execute_input":"2023-06-06T16:27:53.973767Z","iopub.status.busy":"2023-06-06T16:27:53.973269Z","iopub.status.idle":"2023-06-06T16:27:53.980230Z","shell.execute_reply":"2023-06-06T16:27:53.979290Z"},"papermill":{"duration":0.159987,"end_time":"2023-06-06T16:27:53.982187","exception":false,"start_time":"2023-06-06T16:27:53.822200","status":"completed"},"tags":[]},"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":{"execution":{"iopub.execute_input":"2023-06-06T16:27:54.286422Z","iopub.status.busy":"2023-06-06T16:27:54.285547Z","iopub.status.idle":"2023-06-06T16:27:54.450941Z","shell.execute_reply":"2023-06-06T16:27:54.449640Z"},"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_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_df), len(valid_df)","metadata":{"execution":{"iopub.execute_input":"2023-06-06T16:27:54.894389Z","iopub.status.busy":"2023-06-06T16:27:54.893896Z","iopub.status.idle":"2023-06-06T16:27:54.904729Z","shell.execute_reply":"2023-06-06T16:27:54.903749Z"},"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_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.execute_input":"2023-06-06T16:27:55.328027Z","iopub.status.busy":"2023-06-06T16:27:55.324812Z","iopub.status.idle":"2023-06-06T16:27:55.345994Z","shell.execute_reply":"2023-06-06T16:27:55.344973Z"},"papermill":{"duration":0.23076,"end_time":"2023-06-06T16:27:55.348650","exception":false,"start_time":"2023-06-06T16:27:55.117890","status":"completed"},"tags":[]},"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":{"execution":{"iopub.execute_input":"2023-06-06T16:27:55.770624Z","iopub.status.busy":"2023-06-06T16:27:55.770050Z","iopub.status.idle":"2023-06-06T16:27:55.776078Z","shell.execute_reply":"2023-06-06T16:27:55.775221Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T16:27:56.144661Z","iopub.status.busy":"2023-06-06T16:27:56.144133Z","iopub.status.idle":"2023-06-06T16:27:57.105502Z","shell.execute_reply":"2023-06-06T16:27:57.104339Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T16:27:57.406837Z","iopub.status.busy":"2023-06-06T16:27:57.406025Z","iopub.status.idle":"2023-06-06T16:31:18.714726Z","shell.execute_reply":"2023-06-06T16:31:18.713680Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T16:31:19.315240Z","iopub.status.busy":"2023-06-06T16:31:19.314858Z","iopub.status.idle":"2023-06-06T16:31:19.396565Z","shell.execute_reply":"2023-06-06T16:31:19.395616Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T16:31:20.606679Z","iopub.status.busy":"2023-06-06T16:31:20.606306Z","iopub.status.idle":"2023-06-06T16:31:20.614532Z","shell.execute_reply":"2023-06-06T16:31:20.612450Z"},"papermill":{"duration":0.321759,"end_time":"2023-06-06T16:31:20.617289","exception":false,"start_time":"2023-06-06T16:31:20.295530","status":"completed"},"tags":[]},"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.execute_input":"2023-06-06T16:31:21.206610Z","iopub.status.busy":"2023-06-06T16:31:21.205665Z","iopub.status.idle":"2023-06-06T16:31:21.214043Z","shell.execute_reply":"2023-06-06T16:31:21.213163Z"},"papermill":{"duration":0.304879,"end_time":"2023-06-06T16:31:21.216009","exception":false,"start_time":"2023-06-06T16:31:20.911130","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cat dataset.yaml","metadata":{"execution":{"iopub.execute_input":"2023-06-06T16:31:21.838669Z","iopub.status.busy":"2023-06-06T16:31:21.837703Z","iopub.status.idle":"2023-06-06T16:31:22.806161Z","shell.execute_reply":"2023-06-06T16:31:22.804945Z"},"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_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🍮 Prepare Bounding Box Coordinated for YOLOv5","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":{"execution":{"iopub.execute_input":"2023-06-06T16:31:23.981468Z","iopub.status.busy":"2023-06-06T16:31:23.980399Z","iopub.status.idle":"2023-06-06T16:31:23.987739Z","shell.execute_reply":"2023-06-06T16:31:23.986903Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T16:31:24.563023Z","iopub.status.busy":"2023-06-06T16:31:24.562683Z","iopub.status.idle":"2023-06-06T16:31:24.569949Z","shell.execute_reply":"2023-06-06T16:31:24.569089Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T16:31:25.154214Z","iopub.status.busy":"2023-06-06T16:31:25.153687Z","iopub.status.idle":"2023-06-06T16:32:00.450539Z","shell.execute_reply":"2023-06-06T16:32:00.449511Z"},"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_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = 'dataset/images/train'\nTRAIN_LABEL_PATH = 'dataset/labels/train'","metadata":{"execution":{"iopub.execute_input":"2023-06-06T16:32:01.090422Z","iopub.status.busy":"2023-06-06T16:32:01.089491Z","iopub.status.idle":"2023-06-06T16:32:01.095536Z","shell.execute_reply":"2023-06-06T16:32:01.094664Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T16:32:01.728811Z","iopub.status.busy":"2023-06-06T16:32:01.728297Z","iopub.status.idle":"2023-06-06T16:32:01.737643Z","shell.execute_reply":"2023-06-06T16:32:01.736751Z"},"papermill":{"duration":0.330249,"end_time":"2023-06-06T16:32:01.739719","exception":false,"start_time":"2023-06-06T16:32:01.409470","status":"completed"},"tags":[]},"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":{"execution":{"iopub.execute_input":"2023-06-06T16:32:02.370146Z","iopub.status.busy":"2023-06-06T16:32:02.369575Z","iopub.status.idle":"2023-06-06T16:32:02.381717Z","shell.execute_reply":"2023-06-06T16:32:02.380667Z"},"papermill":{"duration":0.331863,"end_time":"2023-06-06T16:32:02.383823","exception":false,"start_time":"2023-06-06T16:32:02.051960","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_images_with_box(12)","metadata":{"execution":{"iopub.execute_input":"2023-06-06T16:32:03.027519Z","iopub.status.busy":"2023-06-06T16:32:03.025802Z","iopub.status.idle":"2023-06-06T16:32:08.183476Z","shell.execute_reply":"2023-06-06T16:32:08.182158Z"},"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_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🚅 Train with YOLOv5","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":{"execution":{"iopub.execute_input":"2023-06-06T16:32:09.996832Z","iopub.status.busy":"2023-06-06T16:32:09.996313Z","iopub.status.idle":"2023-06-06T16:32:10.006382Z","shell.execute_reply":"2023-06-06T16:32:10.005182Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T16:32:10.868640Z","iopub.status.busy":"2023-06-06T16:32:10.868096Z","iopub.status.idle":"2023-06-06T16:32:11.561528Z","shell.execute_reply":"2023-06-06T16:32:11.560559Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T16:32:12.419369Z","iopub.status.busy":"2023-06-06T16:32:12.418781Z","iopub.status.idle":"2023-06-06T18:23:35.240010Z","shell.execute_reply":"2023-06-06T18:23:35.238775Z"},"papermill":{"duration":6683.255917,"end_time":"2023-06-06T18:23:35.242850","exception":false,"start_time":"2023-06-06T16:32:11.986933","status":"completed"},"tags":[]},"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":{"execution":{"iopub.execute_input":"2023-06-06T18:23:40.575525Z","iopub.status.busy":"2023-06-06T18:23:40.575099Z","iopub.status.idle":"2023-06-06T18:23:42.307321Z","shell.execute_reply":"2023-06-06T18:23:42.306205Z"},"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_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.execute_input":"2023-06-06T18:23:45.330603Z","iopub.status.busy":"2023-06-06T18:23:45.330231Z","iopub.status.idle":"2023-06-06T18:23:45.335876Z","shell.execute_reply":"2023-06-06T18:23:45.334932Z"},"papermill":{"duration":1.312901,"end_time":"2023-06-06T18:23:45.337841","exception":false,"start_time":"2023-06-06T18:23:44.024940","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_results('/kaggle/working/runs/detect/train/results.png')","metadata":{"execution":{"iopub.execute_input":"2023-06-06T18:23:47.982263Z","iopub.status.busy":"2023-06-06T18:23:47.981888Z","iopub.status.idle":"2023-06-06T18:23:49.196584Z","shell.execute_reply":"2023-06-06T18:23:49.195765Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T18:23:54.668245Z","iopub.status.busy":"2023-06-06T18:23:54.667871Z","iopub.status.idle":"2023-06-06T18:23:54.681314Z","shell.execute_reply":"2023-06-06T18:23:54.680182Z"},"papermill":{"duration":1.386992,"end_time":"2023-06-06T18:23:54.683480","exception":false,"start_time":"2023-06-06T18:23:53.296488","status":"completed"},"tags":[]},"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":{"execution":{"iopub.execute_input":"2023-06-06T18:24:00.171954Z","iopub.status.busy":"2023-06-06T18:24:00.171416Z","iopub.status.idle":"2023-06-06T18:24:00.176566Z","shell.execute_reply":"2023-06-06T18:24:00.175574Z"},"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_count":null,"outputs":[]},{"cell_type":"code","source":"MODEL_PATH = 'runs/detect/train/weights/best.pt'","metadata":{"execution":{"iopub.execute_input":"2023-06-06T18:24:02.853562Z","iopub.status.busy":"2023-06-06T18:24:02.853019Z","iopub.status.idle":"2023-06-06T18:24:02.858546Z","shell.execute_reply":"2023-06-06T18:24:02.857612Z"},"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_count":null,"outputs":[]},{"cell_type":"code","source":"model = YOLO(MODEL_PATH)","metadata":{"execution":{"iopub.execute_input":"2023-06-06T18:24:05.571798Z","iopub.status.busy":"2023-06-06T18:24:05.570957Z","iopub.status.idle":"2023-06-06T18:24:05.618411Z","shell.execute_reply":"2023-06-06T18:24:05.617484Z"},"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_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.execute_input":"2023-06-06T18:24:08.304028Z","iopub.status.busy":"2023-06-06T18:24:08.303450Z","iopub.status.idle":"2023-06-06T18:24:08.974797Z","shell.execute_reply":"2023-06-06T18:24:08.973881Z"},"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_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ../../../\n%cd kaggle/working/","metadata":{"execution":{"iopub.execute_input":"2023-06-06T18:24:11.632302Z","iopub.status.busy":"2023-06-06T18:24:11.631905Z","iopub.status.idle":"2023-06-06T18:24:11.642995Z","shell.execute_reply":"2023-06-06T18:24:11.642060Z"},"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_count":null,"outputs":[]},{"cell_type":"code","source":"PRED_PATH = 'runs/detect/predict/labels'\n!ls {PRED_PATH}","metadata":{"execution":{"iopub.execute_input":"2023-06-06T18:24:14.821513Z","iopub.status.busy":"2023-06-06T18:24:14.821075Z","iopub.status.idle":"2023-06-06T18:24:16.010667Z","shell.execute_reply":"2023-06-06T18:24:16.009341Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T18:24:18.702661Z","iopub.status.busy":"2023-06-06T18:24:18.702239Z","iopub.status.idle":"2023-06-06T18:24:18.709426Z","shell.execute_reply":"2023-06-06T18:24:18.708464Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T18:24:21.413172Z","iopub.status.busy":"2023-06-06T18:24:21.412759Z","iopub.status.idle":"2023-06-06T18:24:21.424900Z","shell.execute_reply":"2023-06-06T18:24:21.423885Z"},"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_count":null,"outputs":[]},{"cell_type":"code","source":"display_preds_with_box(10)","metadata":{"execution":{"iopub.execute_input":"2023-06-06T18:24:24.244957Z","iopub.status.busy":"2023-06-06T18:24:24.244583Z","iopub.status.idle":"2023-06-06T18:24:28.406317Z","shell.execute_reply":"2023-06-06T18:24:28.404874Z"},"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_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.923750","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":{"execution":{"iopub.execute_input":"2023-06-06T18:24:34.264905Z","iopub.status.busy":"2023-06-06T18:24:34.263942Z","iopub.status.idle":"2023-06-06T18:24:34.283778Z","shell.execute_reply":"2023-06-06T18:24:34.282804Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T18:24:37.096743Z","iopub.status.busy":"2023-06-06T18:24:37.096388Z","iopub.status.idle":"2023-06-06T18:24:37.112731Z","shell.execute_reply":"2023-06-06T18:24:37.111862Z"},"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_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":{"execution":{"iopub.execute_input":"2023-06-06T18:24:40.022508Z","iopub.status.busy":"2023-06-06T18:24:40.022127Z","iopub.status.idle":"2023-06-06T18:24:40.052130Z","shell.execute_reply":"2023-06-06T18:24:40.051025Z"},"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_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf dataset/\n!rm -rf runs/\n!rm -rf wandb/\n!rm dataset.yaml","metadata":{"execution":{"iopub.execute_input":"2023-06-06T18:24:42.999481Z","iopub.status.busy":"2023-06-06T18:24:42.999064Z","iopub.status.idle":"2023-06-06T18:24:48.038441Z","shell.execute_reply":"2023-06-06T18:24:48.037083Z"},"papermill":{"duration":6.464078,"end_time":"2023-06-06T18:24:48.041180","exception":false,"start_time":"2023-06-06T18:24:41.577102","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}