{"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":"# **Great-Barrier-Reef Using YOLOV8 [Cutom Dataset]**","metadata":{}},{"cell_type":"markdown","source":"**Install Libraries**","metadata":{}},{"cell_type":"code","source":"!pip install -qU wandb\n!pip install -qU bbox-utility # check https://github.com/awsaf49/bbox for source code","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:23:34.118655Z","iopub.execute_input":"2023-08-27T13:23:34.119038Z","iopub.status.idle":"2023-08-27T13:24:01.219644Z","shell.execute_reply.started":"2023-08-27T13:23:34.119010Z","shell.execute_reply":"2023-08-27T13:24:01.218332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Import Libraries**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nfrom tqdm.notebook import tqdm\ntqdm.pandas()\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport glob\n\nimport shutil\nimport sys\nsys.path.append('../input/tensorflow-great-barrier-reef')\n\nfrom joblib import Parallel, delayed\n\nfrom IPython.display import display","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-27T13:24:01.223267Z","iopub.execute_input":"2023-08-27T13:24:01.223640Z","iopub.status.idle":"2023-08-27T13:24:01.498085Z","shell.execute_reply.started":"2023-08-27T13:24:01.223612Z","shell.execute_reply":"2023-08-27T13:24:01.497166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Meta Data**","metadata":{}},{"cell_type":"code","source":"FOLD      = 1 # which fold to train\nDIM       = 3000 \nMODEL     = 'yolov8n'\nBATCH     = 4\nEPOCHS    = 15\nOPTMIZER  = 'Adam'\n\nPROJECT   = 'great-barrier-reef' # w&b in yolov5\nNAME      = f'{MODEL}-dim{DIM}-fold{FOLD}' # w&b for yolov5\n\nREMOVE_NOBBOX = True # remove images with no bbox\nROOT_DIR  = '/kaggle/input/tensorflow-great-barrier-reef/'\nIMAGE_DIR = '/kaggle/images' # directory to save images\nLABEL_DIR = '/kaggle/labels' # directory to save labels","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:01.499390Z","iopub.execute_input":"2023-08-27T13:24:01.499753Z","iopub.status.idle":"2023-08-27T13:24:01.507468Z","shell.execute_reply.started":"2023-08-27T13:24:01.499726Z","shell.execute_reply":"2023-08-27T13:24:01.506524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Create Directories**","metadata":{}},{"cell_type":"code","source":"!mkdir -p {IMAGE_DIR}\n!mkdir -p {LABEL_DIR}","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:01.510908Z","iopub.execute_input":"2023-08-27T13:24:01.511176Z","iopub.status.idle":"2023-08-27T13:24:03.370337Z","shell.execute_reply.started":"2023-08-27T13:24:01.511152Z","shell.execute_reply":"2023-08-27T13:24:03.369032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Get Paths**","metadata":{}},{"cell_type":"code","source":"# Train Data\ndf = pd.read_csv(f'{ROOT_DIR}/train.csv')\ndf['old_image_path'] = f'{ROOT_DIR}/train_images/video_'+df.video_id.astype(str)+'/'+df.video_frame.astype(str)+'.jpg'\ndf['image_path']  = f'{IMAGE_DIR}/'+df.image_id+'.jpg'\ndf['label_path']  = f'{LABEL_DIR}/'+df.image_id+'.txt'\ndf['annotations'] = df['annotations'].progress_apply(eval)\ndisplay(df.head(2))","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:03.372992Z","iopub.execute_input":"2023-08-27T13:24:03.373795Z","iopub.status.idle":"2023-08-27T13:24:04.005961Z","shell.execute_reply.started":"2023-08-27T13:24:03.373758Z","shell.execute_reply":"2023-08-27T13:24:04.004900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Number of BBoxes**","metadata":{}},{"cell_type":"code","source":"df['num_bbox'] = df['annotations'].progress_apply(lambda x: len(x))\ndata = (df.num_bbox>0).value_counts(normalize=True)*100\nprint(f\"No BBox: {data[0]:0.2f}% | With BBox: {data[1]:0.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:04.007744Z","iopub.execute_input":"2023-08-27T13:24:04.008144Z","iopub.status.idle":"2023-08-27T13:24:04.107044Z","shell.execute_reply.started":"2023-08-27T13:24:04.008111Z","shell.execute_reply":"2023-08-27T13:24:04.105995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Clean Data**","metadata":{}},{"cell_type":"code","source":"if REMOVE_NOBBOX:\n    df = df.query(\"num_bbox>0\")","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:04.108651Z","iopub.execute_input":"2023-08-27T13:24:04.109041Z","iopub.status.idle":"2023-08-27T13:24:04.127600Z","shell.execute_reply.started":"2023-08-27T13:24:04.109007Z","shell.execute_reply":"2023-08-27T13:24:04.126713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Write Images**","metadata":{}},{"cell_type":"code","source":"def make_copy(row):\n    shutil.copyfile(row.old_image_path, row.image_path)\n    return","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:04.129093Z","iopub.execute_input":"2023-08-27T13:24:04.129470Z","iopub.status.idle":"2023-08-27T13:24:04.134374Z","shell.execute_reply.started":"2023-08-27T13:24:04.129439Z","shell.execute_reply":"2023-08-27T13:24:04.133291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_paths = df.old_image_path.tolist()\n_ = Parallel(n_jobs=-1, backend='threading')(delayed(make_copy)(row) for _, row in tqdm(df.iterrows(), total=len(df)))","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:04.136213Z","iopub.execute_input":"2023-08-27T13:24:04.136583Z","iopub.status.idle":"2023-08-27T13:24:38.509140Z","shell.execute_reply.started":"2023-08-27T13:24:04.136550Z","shell.execute_reply":"2023-08-27T13:24:38.508140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Helper**","metadata":{}},{"cell_type":"code","source":"# check https://github.com/awsaf49/bbox for source code of following utility functions\nfrom bbox.utils import coco2yolo, coco2voc, voc2yolo\nfrom bbox.utils import draw_bboxes, load_image\nfrom bbox.utils import clip_bbox, str2annot, annot2str\n\ndef get_bbox(annots):\n    bboxes = [list(annot.values()) for annot in annots]\n    return bboxes\n\ndef get_imgsize(row):\n    row['width'], row['height'] = imagesize.get(row['image_path'])\n    return row\n\nnp.random.seed(32)\ncolors = [(np.random.randint(255), np.random.randint(255), np.random.randint(255))\\\n          for idx in range(1)]","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:38.513866Z","iopub.execute_input":"2023-08-27T13:24:38.514405Z","iopub.status.idle":"2023-08-27T13:24:39.147056Z","shell.execute_reply.started":"2023-08-27T13:24:38.514376Z","shell.execute_reply":"2023-08-27T13:24:39.146116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Create BBox**","metadata":{}},{"cell_type":"code","source":"df['bboxes'] = df.annotations.progress_apply(get_bbox)\ndf.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:39.148521Z","iopub.execute_input":"2023-08-27T13:24:39.148859Z","iopub.status.idle":"2023-08-27T13:24:39.210456Z","shell.execute_reply.started":"2023-08-27T13:24:39.148822Z","shell.execute_reply":"2023-08-27T13:24:39.209274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Get Image-Size**","metadata":{}},{"cell_type":"code","source":"df['width']  = 1280\ndf['height'] = 720\ndisplay(df.head(2))","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:39.211969Z","iopub.execute_input":"2023-08-27T13:24:39.212406Z","iopub.status.idle":"2023-08-27T13:24:39.234142Z","shell.execute_reply.started":"2023-08-27T13:24:39.212370Z","shell.execute_reply":"2023-08-27T13:24:39.233531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Create Labels**","metadata":{}},{"cell_type":"code","source":"cnt = 0\nall_bboxes = []\nbboxes_info = []\nfor row_idx in tqdm(range(df.shape[0])):\n    row = df.iloc[row_idx]\n    image_height = row.height\n    image_width  = row.width\n    bboxes_coco  = np.array(row.bboxes).astype(np.float32).copy()\n    num_bbox     = len(bboxes_coco)\n    names        = ['cots']*num_bbox\n    labels       = np.array([0]*num_bbox)[..., None].astype(str)\n    ## Create Annotation(YOLO)\n    with open(row.label_path, 'w') as f:\n        if num_bbox<1:\n            annot = ''\n            f.write(annot)\n            cnt+=1\n            continue\n        bboxes_voc  = coco2voc(bboxes_coco, image_height, image_width)\n        bboxes_voc  = clip_bbox(bboxes_voc, image_height, image_width)\n        bboxes_yolo = voc2yolo(bboxes_voc, image_height, image_width).astype(str)\n        all_bboxes.extend(bboxes_yolo.astype(float))\n        bboxes_info.extend([[row.image_id, row.video_id, row.sequence]]*len(bboxes_yolo))\n        annots = np.concatenate([labels, bboxes_yolo], axis=1)\n        string = annot2str(annots)\n        f.write(string)\nprint('Missing:',cnt)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:39.235192Z","iopub.execute_input":"2023-08-27T13:24:39.235759Z","iopub.status.idle":"2023-08-27T13:24:43.994059Z","shell.execute_reply.started":"2023-08-27T13:24:39.235725Z","shell.execute_reply":"2023-08-27T13:24:43.992934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Create Folds**","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import GroupKFold\nkf = GroupKFold(n_splits = 3)\ndf = df.reset_index(drop=True)\ndf['fold'] = -1\nfor fold, (train_idx, val_idx) in enumerate(kf.split(df, groups=df.video_id.tolist())):\n    df.loc[val_idx, 'fold'] = fold\ndisplay(df.fold.value_counts())","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:43.995768Z","iopub.execute_input":"2023-08-27T13:24:43.996321Z","iopub.status.idle":"2023-08-27T13:24:44.377512Z","shell.execute_reply.started":"2023-08-27T13:24:43.996285Z","shell.execute_reply":"2023-08-27T13:24:44.376486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**BBox Distribution**","metadata":{}},{"cell_type":"code","source":"bbox_df = pd.DataFrame(np.concatenate([bboxes_info, all_bboxes], axis=1),\n             columns=['image_id','video_id','sequence',\n                     'xmid','ymid','w','h'])\nbbox_df[['xmid','ymid','w','h']] = bbox_df[['xmid','ymid','w','h']].astype(float)\nbbox_df['area'] = bbox_df.w * bbox_df.h * 1280 * 720\nbbox_df = bbox_df.merge(df[['image_id','fold']], on='image_id', how='left')\nbbox_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:44.378988Z","iopub.execute_input":"2023-08-27T13:24:44.379359Z","iopub.status.idle":"2023-08-27T13:24:45.488344Z","shell.execute_reply.started":"2023-08-27T13:24:44.379326Z","shell.execute_reply":"2023-08-27T13:24:45.487417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.stats import gaussian_kde\n\nall_bboxes = np.array(all_bboxes)\n\nx_val = all_bboxes[...,0]\ny_val = all_bboxes[...,1]\n\n# Calculate the point density\nxy = np.vstack([x_val,y_val])\nz = gaussian_kde(xy)(xy)\n\nfig, ax = plt.subplots(figsize = (10, 10))\n# ax.axis('off')\nax.scatter(x_val, y_val, c=z, s=100, cmap='viridis')\n# ax.set_xlabel('x_mid')\n# ax.set_ylabel('y_mid')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:45.489521Z","iopub.execute_input":"2023-08-27T13:24:45.490153Z","iopub.status.idle":"2023-08-27T13:24:47.921273Z","shell.execute_reply.started":"2023-08-27T13:24:45.490118Z","shell.execute_reply":"2023-08-27T13:24:47.920425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val = all_bboxes[...,2]\ny_val = all_bboxes[...,3]\n\n# Calculate the point density\nxy = np.vstack([x_val,y_val])\nz = gaussian_kde(xy)(xy)\n\nfig, ax = plt.subplots(figsize = (10, 10))\n# ax.axis('off')\nax.scatter(x_val, y_val, c=z, s=100, cmap='viridis')\n# ax.set_xlabel('bbox_width')\n# ax.set_ylabel('bbox_height')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:47.922230Z","iopub.execute_input":"2023-08-27T13:24:47.922567Z","iopub.status.idle":"2023-08-27T13:24:50.187269Z","shell.execute_reply.started":"2023-08-27T13:24:47.922531Z","shell.execute_reply":"2023-08-27T13:24:50.186382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib as mpl\nimport seaborn as sns\n\nf, ax = plt.subplots(figsize=(12, 6))\nsns.despine(f)\n\nsns.histplot(\n    bbox_df,\n    x=\"area\", hue=\"fold\",\n    multiple=\"stack\",\n    palette=\"viridis\",\n    edgecolor=\".3\",\n    linewidth=.5,\n    log_scale=True,\n)\nax.xaxis.set_major_formatter(mpl.ticker.ScalarFormatter())\nax.set_xticks([500, 1000, 2000, 5000, 10000]);","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:50.188644Z","iopub.execute_input":"2023-08-27T13:24:50.189568Z","iopub.status.idle":"2023-08-27T13:24:51.326426Z","shell.execute_reply.started":"2023-08-27T13:24:50.189533Z","shell.execute_reply":"2023-08-27T13:24:51.325476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = df[(df.num_bbox>0)].sample(100) # takes samples with bbox\ny = 3; x = 2\nplt.figure(figsize=(12.8*x, 7.2*y))\nfor idx in range(x*y):\n    row = df2.iloc[idx]\n    img           = load_image(row.image_path)\n    image_height  = row.height\n    image_width   = row.width\n    with open(row.label_path) as f:\n        annot = str2annot(f.read())\n    bboxes_yolo = annot[...,1:]\n    labels      = annot[..., 0].astype(int).tolist()\n    names         = ['cots']*len(bboxes_yolo)\n    plt.subplot(y, x, idx+1)\n    plt.imshow(draw_bboxes(img = img,\n                           bboxes = bboxes_yolo, \n                           classes = names,\n                           class_ids = labels,\n                           class_name = True, \n                           colors = colors, \n                           bbox_format = 'yolo',\n                           line_thickness = 2))\n    plt.axis('OFF')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:51.327834Z","iopub.execute_input":"2023-08-27T13:24:51.328656Z","iopub.status.idle":"2023-08-27T13:24:56.126715Z","shell.execute_reply.started":"2023-08-27T13:24:51.328619Z","shell.execute_reply":"2023-08-27T13:24:56.125369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files = []\nval_files   = []\ntrain_df = df.query(\"fold!=@FOLD\")\nvalid_df = df.query(\"fold==@FOLD\")\ntrain_files += list(train_df.image_path.unique())\nval_files += list(valid_df.image_path.unique())\nlen(train_files), len(val_files)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:56.128848Z","iopub.execute_input":"2023-08-27T13:24:56.129617Z","iopub.status.idle":"2023-08-27T13:24:56.157682Z","shell.execute_reply.started":"2023-08-27T13:24:56.129572Z","shell.execute_reply":"2023-08-27T13:24:56.156545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Configuration**","metadata":{}},{"cell_type":"code","source":"import yaml\n\ncwd = '/kaggle/working/'\n\nwith open(os.path.join( cwd , 'train.txt'), 'w') as f:\n    for path in train_df.image_path.tolist():\n        f.write(path+'\\n')\n            \nwith open(os.path.join(cwd , 'val.txt'), 'w') as f:\n    for path in valid_df.image_path.tolist():\n        f.write(path+'\\n')\n\ndata = dict(\n    path  = '/kaggle/working',\n    train =  os.path.join( cwd , 'train.txt') ,\n    val   =  os.path.join( cwd , 'val.txt' ),\n    nc    = 1,\n    names = ['cots'],\n    )\n\nwith open(os.path.join( cwd , 'gbr.yaml'), 'w') as outfile:\n    yaml.dump(data, outfile, default_flow_style=False)\n\nf = open(os.path.join( cwd , 'gbr.yaml'), 'r')\nprint('\\nyaml:')\nprint(f.read())","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:56.159592Z","iopub.execute_input":"2023-08-27T13:24:56.160332Z","iopub.status.idle":"2023-08-27T13:24:56.174544Z","shell.execute_reply.started":"2023-08-27T13:24:56.160294Z","shell.execute_reply":"2023-08-27T13:24:56.173676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile /kaggle/working/hyp.yaml\nlr0: 0.01  # initial learning rate (SGD=1E-2, Adam=1E-3)\nlrf: 0.1  # final OneCycleLR learning rate (lr0 * lrf)\nmomentum: 0.937  # SGD momentum/Adam beta1\nweight_decay: 0.0005  # optimizer weight decay 5e-4\nwarmup_epochs: 3.0  # warmup epochs (fractions ok)\nwarmup_momentum: 0.8  # warmup initial momentum\nwarmup_bias_lr: 0.1  # warmup initial bias lr\nbox: 0.05  # box loss gain\ncls: 0.5  # cls loss gain\ncls_pw: 1.0  # cls BCELoss positive_weight\nobj: 1.0  # obj loss gain (scale with pixels)\nobj_pw: 1.0  # obj BCELoss positive_weight\niou_t: 0.20  # IoU training threshold\nanchor_t: 4.0  # anchor-multiple threshold\n# anchors: 3  # anchors per output layer (0 to ignore)\nfl_gamma: 0.0  # focal loss gamma (efficientDet default gamma=1.5)\nhsv_h: 0.015  # image HSV-Hue augmentation (fraction)\nhsv_s: 0.7  # image HSV-Saturation augmentation (fraction)\nhsv_v: 0.4  # image HSV-Value augmentation (fraction)\ndegrees: 0.0  # image rotation (+/- deg)\ntranslate: 0.10  # image translation (+/- fraction)\nscale: 0.5  # image scale (+/- gain)\nshear: 0.0  # image shear (+/- deg)\nperspective: 0.0  # image perspective (+/- fraction), range 0-0.001\nflipud: 0.5  # image flip up-down (probability)\nfliplr: 0.5  # image flip left-right (probability)\nmosaic: 0.5  # image mosaic (probability)\nmixup: 0.5 # image mixup (probability)\ncopy_paste: 0.0  # segment copy-paste (probability)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:56.176088Z","iopub.execute_input":"2023-08-27T13:24:56.176758Z","iopub.status.idle":"2023-08-27T13:24:56.185266Z","shell.execute_reply.started":"2023-08-27T13:24:56.176728Z","shell.execute_reply":"2023-08-27T13:24:56.184469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb\n\ntry:\n    from kaggle_secrets import UserSecretsClient\n    user_secrets = UserSecretsClient()\n    api_key = user_secrets.get_secret(\"c41ae45b70dea64d9ebeaf2747a1f28103e6b341\")\n    wandb.login(key=api_key)\n    anonymous = None\nexcept:\n    wandb.login(anonymous='must')\n    print('To use your W&B account,\\nGo to Add-ons -> Secrets and provide your W&B access token. Use the Label name as WANDB. \\nGet your W&B access token from here: https://wandb.ai/authorize')","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:24:56.186774Z","iopub.execute_input":"2023-08-27T13:24:56.187376Z","iopub.status.idle":"2023-08-27T13:25:00.334206Z","shell.execute_reply.started":"2023-08-27T13:24:56.187346Z","shell.execute_reply":"2023-08-27T13:25:00.333181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/ultralytics.git yolov8","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:25:00.335594Z","iopub.execute_input":"2023-08-27T13:25:00.336457Z","iopub.status.idle":"2023-08-27T13:25:04.018712Z","shell.execute_reply.started":"2023-08-27T13:25:00.336420Z","shell.execute_reply":"2023-08-27T13:25:04.017561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd yolov8","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:25:04.023537Z","iopub.execute_input":"2023-08-27T13:25:04.025761Z","iopub.status.idle":"2023-08-27T13:25:04.037288Z","shell.execute_reply.started":"2023-08-27T13:25:04.025721Z","shell.execute_reply":"2023-08-27T13:25:04.036173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pip install -qr requirements.txt","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:25:04.040538Z","iopub.execute_input":"2023-08-27T13:25:04.041967Z","iopub.status.idle":"2023-08-27T13:25:15.766257Z","shell.execute_reply.started":"2023-08-27T13:25:04.041908Z","shell.execute_reply":"2023-08-27T13:25:15.764911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import ultralytics\nfrom ultralytics import YOLO\nultralytics.checks()","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:25:32.716407Z","iopub.execute_input":"2023-08-27T13:25:32.717130Z","iopub.status.idle":"2023-08-27T13:25:33.259964Z","shell.execute_reply.started":"2023-08-27T13:25:32.717094Z","shell.execute_reply":"2023-08-27T13:25:33.258964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install ultralytics","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:25:44.490791Z","iopub.execute_input":"2023-08-27T13:25:44.491179Z","iopub.status.idle":"2023-08-27T13:25:56.635310Z","shell.execute_reply.started":"2023-08-27T13:25:44.491147Z","shell.execute_reply":"2023-08-27T13:25:56.633697Z"},"_kg_hide-output":true,"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ** **Lets Train the YOLOV8 model using YOLO CLI** **","metadata":{}},{"cell_type":"markdown","source":"**If you want to train, validate or run inference on models and don't need to make any modifications to the code, using YOLO command line interface is the easiest way to get started. Read more about CLI in Ultralytics YOLO Docs.**","metadata":{}},{"cell_type":"markdown","source":"**yolo mode=predict runs YOLOv8 inference on a variety of sources, downloading models automatically from the latest YOLOv8 release, and saving results to runs/predict**","metadata":{}},{"cell_type":"code","source":"%cd /kaggle/working/yolov8\n!yolo task=detect mode=predict model=yolov8n.pt conf=0.25 source='https://media.roboflow.com/notebooks/examples/dog.jpeg' save=True","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:41:09.907713Z","iopub.execute_input":"2023-08-27T13:41:09.908103Z","iopub.status.idle":"2023-08-27T13:41:25.037152Z","shell.execute_reply.started":"2023-08-27T13:41:09.908071Z","shell.execute_reply":"2023-08-27T13:41:25.035919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* **As i am using custom dataset and my data has only i class called 'cots' added only that in gbr.yaml file**\n\nHence it looks like this<br><br>\nnames:<br>\n- cots<br>\nnc: 1<br>\npath: /kaggle/working<br>\ntrain: /kaggle/working/train.txt<br>\nval: /kaggle/working/val.txt<br>","metadata":{}},{"cell_type":"markdown","source":"**Lets Train the custom data set**","metadata":{}},{"cell_type":"code","source":"import os\nimport sys\n\nos.environ[\"PYTHONPATH\"]=\"/kaggle/working\"\n\n# main code to run YOLO training\nfrom ultralytics import YOLO\n\n# Load a model\n#model = YOLO(\"yolov8n.yaml\")  # build a new model from scratch\nmodel = YOLO(\"yolov8n.pt\")     # load a pretrained model (recommended for training)\n\n# Use the model\nresults = model.train(data=\"/kaggle/working/gbr.yaml\", epochs=10, pretrained=True, iou=0.5, visualize=True, patience=0)  # train the model\nresults = model.val()  # evaluate model performance on the validation set","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:57:10.220004Z","iopub.execute_input":"2023-08-27T13:57:10.220389Z","iopub.status.idle":"2023-08-27T14:10:45.710203Z","shell.execute_reply.started":"2023-08-27T13:57:10.220357Z","shell.execute_reply":"2023-08-27T14:10:45.707398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* **After Traning best model is saved at \"/kaggle/working/yolov8/runs/detect/train5/weights/best.pt\"**\n* **Please note i have tried this model with only 5 epoc to save time.**\n* **We may need to train with more than 50 epoc for good model**","metadata":{}},{"cell_type":"markdown","source":"**We can see all the validation plots at below location after traning**","metadata":{}},{"cell_type":"code","source":"!ls /kaggle/working/yolov8/runs/detect/val","metadata":{"execution":{"iopub.status.busy":"2023-08-27T14:14:28.538117Z","iopub.execute_input":"2023-08-27T14:14:28.538541Z","iopub.status.idle":"2023-08-27T14:14:29.583645Z","shell.execute_reply.started":"2023-08-27T14:14:28.538486Z","shell.execute_reply":"2023-08-27T14:14:29.582297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Lets Print all this and check the results**","metadata":{}},{"cell_type":"code","source":"\nplt.figure(figsize = (10,10))\nplt.axis('off')\nplt.imshow(plt.imread(f'/kaggle/working/yolov8/runs/detect/val/F1_curve.png'));","metadata":{"execution":{"iopub.status.busy":"2023-08-27T14:17:55.402451Z","iopub.execute_input":"2023-08-27T14:17:55.403505Z","iopub.status.idle":"2023-08-27T14:17:56.489461Z","shell.execute_reply.started":"2023-08-27T14:17:55.403451Z","shell.execute_reply":"2023-08-27T14:17:56.488293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# List of file names\nfile_names = [\n    \"F1_curve.png\", \"confusion_matrix.png\", \"val_batch1_labels.jpg\",\n    \"PR_curve.png\", \"confusion_matrix_normalized.png\", \"val_batch1_pred.jpg\",\n    \"P_curve.png\", \"val_batch0_labels.jpg\", \"val_batch2_labels.jpg\",\n    \"R_curve.png\", \"val_batch0_pred.jpg\", \"val_batch2_pred.jpg\"\n]\n\n# Set up the plot grid\nnum_rows = 6  # Number of rows\nnum_cols = 2  # Number of columns\n\nfig, axes = plt.subplots(num_rows, num_cols, figsize=(25, 25))\nfig.subplots_adjust(hspace=0.4)\n\n# Iterate through filenames and display images\nfor idx, file_name in enumerate(file_names):\n    row = idx // num_cols\n    col = idx % num_cols\n    image_path = f\"/kaggle/working/yolov8/runs/detect/val/{file_name}\"\n    image = plt.imread(image_path)\n    axes[row, col].imshow(image)\n    axes[row, col].set_title(file_name)\n    axes[row, col].axis('off')\n\n# Display the plot\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T14:23:56.222010Z","iopub.execute_input":"2023-08-27T14:23:56.222377Z","iopub.status.idle":"2023-08-27T14:24:04.372433Z","shell.execute_reply.started":"2023-08-27T14:23:56.222347Z","shell.execute_reply":"2023-08-27T14:24:04.371155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Original Vs Prediction**","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(3, 2, figsize = (2*9,3*5), constrained_layout = True)\nfor row in range(3):\n    ax[row][0].imshow(plt.imread(f'/kaggle/working/yolov8/runs/detect/val/val_batch1_labels.jpg'))\n    ax[row][0].set_xticks([])\n    ax[row][0].set_yticks([])\n    ax[row][0].set_title(f'/kaggle/working/yolov8/runs/detect/val/val_batch2_labels.jpg', fontsize = 12)\n    \n    ax[row][1].imshow(plt.imread(f'/kaggle/working/yolov8/runs/detect/val/val_batch1_pred.jpg'))\n    ax[row][1].set_xticks([])\n    ax[row][1].set_yticks([])\n    ax[row][1].set_title(f'/kaggle/working/yolov8/runs/detect/val/val_batch2_pred.jpg', fontsize = 12)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-27T14:32:23.385828Z","iopub.execute_input":"2023-08-27T14:32:23.386273Z","iopub.status.idle":"2023-08-27T14:32:26.399999Z","shell.execute_reply.started":"2023-08-27T14:32:23.386239Z","shell.execute_reply":"2023-08-27T14:32:26.392595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Conclusion","metadata":{}},{"cell_type":"markdown","source":"1. **From the above plots we can see predicted and original detection almost matches.**\n1. **we can confirm that if the model is trained for more epoc we can achive better results**\n1. **So with this we have trined our custom data set using YOLOV8 with custom classes.(gbr.yaml)**","metadata":{}}]}