{"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":"code","source":"from itertools import groupby\nimport numpy as np\nfrom tqdm.notebook import tqdm\ntqdm.pandas()\nimport pandas as pd\nimport os\nimport pickle\nimport cv2\nfrom multiprocessing import Pool\nimport matplotlib.pyplot as plt\n# import cupy as cp\nimport ast\nimport glob\n\nimport shutil\n\nfrom joblib import Parallel, delayed\n\nfrom IPython.display import display, HTML\n\nfrom matplotlib import animation, rc\nrc('animation', html='jshtml')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-06T03:48:19.823660Z","iopub.execute_input":"2023-05-06T03:48:19.824103Z","iopub.status.idle":"2023-05-06T03:48:20.088273Z","shell.execute_reply.started":"2023-05-06T03:48:19.824064Z","shell.execute_reply":"2023-05-06T03:48:20.087467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb\nfrom kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\napi_key = user_secrets.get_secret(\"WANDB_API_KEY\")\nwandb.login(key=api_key)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T03:48:20.091387Z","iopub.execute_input":"2023-05-06T03:48:20.091668Z","iopub.status.idle":"2023-05-06T03:48:24.084021Z","shell.execute_reply.started":"2023-05-06T03:48:20.091645Z","shell.execute_reply":"2023-05-06T03:48:24.083078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Make Project\nos.makedirs(\"yolo_project\", exist_ok=True)\nos.makedirs(\"yolo_project/custom_dataset\", exist_ok=True)\nos.makedirs(\"yolo_project/custom_dataset/images\", exist_ok=True)\nos.makedirs(\"yolo_project/custom_dataset/labels\", exist_ok=True)\nos.makedirs(\"yolo_project/custom_dataset/images/train\", exist_ok=True)\nos.makedirs(\"yolo_project/custom_dataset/images/val\", exist_ok=True)\nos.makedirs(\"yolo_project/custom_dataset/labels/train\", exist_ok=True)\nos.makedirs(\"yolo_project/custom_dataset/labels/val\", exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T03:48:24.085972Z","iopub.execute_input":"2023-05-06T03:48:24.086326Z","iopub.status.idle":"2023-05-06T03:48:24.093737Z","shell.execute_reply.started":"2023-05-06T03:48:24.086293Z","shell.execute_reply":"2023-05-06T03:48:24.092619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Set Dataset","metadata":{}},{"cell_type":"code","source":"pdf = pd.concat([\n    pd.DataFrame(glob.glob(\"/kaggle/input/birdclefobjectdetectiondataset/**/*.jpg\"),columns=[\"old_path\"]),\n    pd.DataFrame(glob.glob(\"/kaggle/input/birdclefobjectdetectiondataset/**/*.txt\"),columns=[\"old_path\"])\n])\npdf[\"data_type\"] = pdf.apply(lambda x: x[\"old_path\"].split(\"/\")[4].split(\"_\")[0],axis=1)\npdf[\"fold_type\"] = pdf.apply(lambda x: x[\"old_path\"].split(\"/\")[4].split(\"_\")[1],axis=1)\npdf[\"Filename\"] = pdf.apply(lambda x: x[\"old_path\"].split(\"/\")[5],axis=1)\npdf[\"new_path\"] = pdf.apply(lambda x: f\"yolo_project/custom_dataset/{x.data_type}/{x.fold_type}/{x.Filename}\",axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T03:48:24.095245Z","iopub.execute_input":"2023-05-06T03:48:24.095646Z","iopub.status.idle":"2023-05-06T03:48:43.033706Z","shell.execute_reply.started":"2023-05-06T03:48:24.095616Z","shell.execute_reply":"2023-05-06T03:48:43.032740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = Parallel(n_jobs=4, backend='threading')(delayed(shutil.copy)(row.old_path, row.new_path) for idx, row in tqdm(pdf.iterrows(),total=len(pdf)))","metadata":{"execution":{"iopub.status.busy":"2023-05-06T03:48:43.036132Z","iopub.execute_input":"2023-05-06T03:48:43.036497Z","iopub.status.idle":"2023-05-06T03:54:36.110325Z","shell.execute_reply.started":"2023-05-06T03:48:43.036466Z","shell.execute_reply":"2023-05-06T03:54:36.109522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Hyper Parameter","metadata":{}},{"cell_type":"code","source":"%%writefile yolo_project/hyp.cratch.yaml\n\nlr0: 0.01\nlrf: 0.032\nmomentum: 0.937\nweight_decay: 0.0005\nwarmup_epochs: 3.0\nwarmup_momentum: 0.8\nwarmup_bias_lr: 0.1\nbox: 0.1\ncls: 1.0\ncls_pw: 0.5\nobj: 2.0\nobj_pw: 0.5\niou_t: 0.2\nanchor_t: 4.0\nanchors: 0\nfl_gamma: 1.5\nhsv_h: 0.015\nhsv_s: 0.7\nhsv_v: 0.4\ndegrees: 0.0\ntranslate: 0.2\nscale: 0.6\nshear: 0.0\nperspective: 0.0\nflipud: 0.2\nfliplr: 0.5\nmosaic: 1.0\nmixup: 0.1\ncopy_paste: 0.06","metadata":{"execution":{"iopub.status.busy":"2023-05-06T03:54:36.111438Z","iopub.execute_input":"2023-05-06T03:54:36.111688Z","iopub.status.idle":"2023-05-06T03:54:36.118659Z","shell.execute_reply.started":"2023-05-06T03:54:36.111666Z","shell.execute_reply":"2023-05-06T03:54:36.117600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Set Config File","metadata":{}},{"cell_type":"code","source":"import yaml\n\ncwd = '/kaggle/working/yolo_project/'\n\nwith open(os.path.join( cwd , 'train.txt'), 'w') as f:\n    for path in pdf[(pdf.data_type==\"images\")&(pdf.fold_type==\"train\")].new_path.to_list():\n        f.write(path+'\\n')\n            \nwith open(os.path.join(cwd , 'val.txt'), 'w') as f:\n    for path in pdf[(pdf.data_type==\"images\")&(pdf.fold_type==\"val\")].new_path.to_list():\n        f.write(path+'\\n')\n\ndata = dict(\n    path  =  cwd,\n    train =  os.path.join( cwd , 'train.txt') ,\n    val   =  os.path.join( cwd , 'val.txt' ),\n    nc    = 1,\n    names = ['events'],\n    )\n\nwith open(os.path.join( cwd , 'bgr.yaml'), 'w') as outfile:\n    yaml.dump(data, outfile, default_flow_style=False)\n\nf = open(os.path.join( cwd , 'bgr.yaml'), 'r')\nprint('\\nyaml:')\nprint(f.read())","metadata":{"execution":{"iopub.status.busy":"2023-05-06T03:54:36.120410Z","iopub.execute_input":"2023-05-06T03:54:36.121445Z","iopub.status.idle":"2023-05-06T03:54:36.282647Z","shell.execute_reply.started":"2023-05-06T03:54:36.121390Z","shell.execute_reply":"2023-05-06T03:54:36.281633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Helper Function","metadata":{}},{"cell_type":"code","source":"def voc2yolo(image_height, image_width, bboxes):\n    \"\"\"\n    voc  => [x1, y1, x2, y1]\n    yolo => [xmid, ymid, w, h] (normalized)\n    \"\"\"\n    \n    bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int\n    \n    bboxes[..., [0, 2]] = bboxes[..., [0, 2]]/ image_width\n    bboxes[..., [1, 3]] = bboxes[..., [1, 3]]/ image_height\n    \n    w = bboxes[..., 2] - bboxes[..., 0]\n    h = bboxes[..., 3] - bboxes[..., 1]\n    \n    bboxes[..., 0] = bboxes[..., 0] + w/2\n    bboxes[..., 1] = bboxes[..., 1] + h/2\n    bboxes[..., 2] = w\n    bboxes[..., 3] = h\n    \n    return bboxes\n\ndef yolo2voc(image_height, image_width, bboxes):\n    \"\"\"\n    yolo => [xmid, ymid, w, h] (normalized)\n    voc  => [x1, y1, x2, y1]\n    \n    \"\"\" \n    bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int\n    \n    bboxes[..., [0, 2]] = bboxes[..., [0, 2]]* image_width\n    bboxes[..., [1, 3]] = bboxes[..., [1, 3]]* image_height\n    \n    bboxes[..., [0, 1]] = bboxes[..., [0, 1]] - bboxes[..., [2, 3]]/2\n    bboxes[..., [2, 3]] = bboxes[..., [0, 1]] + bboxes[..., [2, 3]]\n    \n    return bboxes\n\ndef coco2yolo(image_height, image_width, bboxes):\n    \"\"\"\n    coco => [xmin, ymin, w, h]\n    yolo => [xmid, ymid, w, h] (normalized)\n    \"\"\"\n    \n    bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int\n    \n    # normolizinig\n    bboxes[..., [0, 2]]= bboxes[..., [0, 2]]/ image_width\n    bboxes[..., [1, 3]]= bboxes[..., [1, 3]]/ image_height\n    \n    # converstion (xmin, ymin) => (xmid, ymid)\n    bboxes[..., [0, 1]] = bboxes[..., [0, 1]] + bboxes[..., [2, 3]]/2\n    \n    return bboxes\n\ndef yolo2coco(image_height, image_width, bboxes):\n    \"\"\"\n    yolo => [xmid, ymid, w, h] (normalized)\n    coco => [xmin, ymin, w, h]\n    \n    \"\"\" \n    bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int\n    \n    # denormalizing\n    bboxes[..., [0, 2]]= bboxes[..., [0, 2]]* image_width\n    bboxes[..., [1, 3]]= bboxes[..., [1, 3]]* image_height\n    \n    # converstion (xmid, ymid) => (xmin, ymin) \n    bboxes[..., [0, 1]] = bboxes[..., [0, 1]] - bboxes[..., [2, 3]]/2\n    \n    return bboxes\n\ndef load_image(image_path):\n    return cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\n\n\ndef plot_one_box(x, img, color=None, label=None, line_thickness=None):\n    # Plots one bounding box on image img\n    tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1  # line/font thickness\n    color = color or [random.randint(0, 255) for _ in range(3)]\n    c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))\n    cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)\n    if label:\n        tf = max(tl - 1, 1)  # font thickness\n        t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]\n        c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3\n        cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA)  # filled\n        cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)\n\ndef draw_bboxes(img, bboxes, classes, class_ids, colors = None, show_classes = None, bbox_format = 'yolo', class_name = False, line_thickness = 2):  \n     \n    image = img.copy()\n    show_classes = classes if show_classes is None else show_classes\n    colors = (0, 255 ,0) if colors is None else colors\n    \n    if bbox_format == 'yolo':\n        \n        for idx in range(len(bboxes)):  \n            \n            bbox  = bboxes[idx]\n            cls   = classes[idx]\n            cls_id = class_ids[idx]\n            color = colors[cls_id] if type(colors) is list else colors\n            \n            if cls in show_classes:\n            \n                x1 = round(float(bbox[0])*image.shape[1])\n                y1 = round(float(bbox[1])*image.shape[0])\n                w  = round(float(bbox[2])*image.shape[1]/2) #w/2 \n                h  = round(float(bbox[3])*image.shape[0]/2)\n\n                voc_bbox = (x1-w, y1-h, x1+w, y1+h)\n                plot_one_box(voc_bbox, \n                             image,\n                             color = color,\n                             label = cls if class_name else str(get_label(cls)),\n                             line_thickness = line_thickness)\n            \n    elif bbox_format == 'coco':\n        \n        for idx in range(len(bboxes)):  \n            \n            bbox  = bboxes[idx]\n            cls   = classes[idx]\n            cls_id = class_ids[idx]\n            color = colors[cls_id] if type(colors) is list else colors\n            \n            if cls in show_classes:            \n                x1 = int(round(bbox[0]))\n                y1 = int(round(bbox[1]))\n                w  = int(round(bbox[2]))\n                h  = int(round(bbox[3]))\n\n                voc_bbox = (x1, y1, x1+w, y1+h)\n                plot_one_box(voc_bbox, \n                             image,\n                             color = color,\n                             label = cls if class_name else str(cls_id),\n                             line_thickness = line_thickness)\n\n    elif bbox_format == 'voc_pascal':\n        \n        for idx in range(len(bboxes)):  \n            \n            bbox  = bboxes[idx]\n            cls   = classes[idx]\n            cls_id = class_ids[idx]\n            color = colors[cls_id] if type(colors) is list else colors\n            \n            if cls in show_classes: \n                x1 = int(round(bbox[0]))\n                y1 = int(round(bbox[1]))\n                x2 = int(round(bbox[2]))\n                y2 = int(round(bbox[3]))\n                voc_bbox = (x1, y1, x2, y2)\n                plot_one_box(voc_bbox, \n                             image,\n                             color = color,\n                             label = cls if class_name else str(cls_id),\n                             line_thickness = line_thickness)\n    else:\n        raise ValueError('wrong bbox format')\n\n    return image\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\n\n# https://www.kaggle.com/diegoalejogm/great-barrier-reefs-eda-with-animations\ndef create_animation(ims):\n    fig = plt.figure(figsize=(16, 12))\n    plt.axis('off')\n    im = plt.imshow(ims[0])\n\n    def animate_func(i):\n        im.set_array(ims[i])\n        return [im]\n\n    return animation.FuncAnimation(fig, animate_func, frames = len(ims), interval = 1000//12)\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-05-06T03:54:36.285561Z","iopub.execute_input":"2023-05-06T03:54:36.285832Z","iopub.status.idle":"2023-05-06T03:54:36.336514Z","shell.execute_reply.started":"2023-05-06T03:54:36.285808Z","shell.execute_reply":"2023-05-06T03:54:36.335535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pip install method (recommended)\n%pip install ultralytics\nimport ultralytics\nultralytics.checks()","metadata":{"execution":{"iopub.status.busy":"2023-05-06T03:54:36.338082Z","iopub.execute_input":"2023-05-06T03:54:36.338382Z","iopub.status.idle":"2023-05-06T03:54:55.773865Z","shell.execute_reply.started":"2023-05-06T03:54:36.338354Z","shell.execute_reply":"2023-05-06T03:54:55.772970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO\n\n# Load a model\nmodel = 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\nmodel.train(data=\"yolo_project/bgr.yaml\", epochs=3, imgsz=[128,512], batch=64)  # train the model","metadata":{"execution":{"iopub.status.busy":"2023-05-06T03:54:55.775228Z","iopub.execute_input":"2023-05-06T03:54:55.776110Z","iopub.status.idle":"2023-05-06T04:45:46.705328Z","shell.execute_reply.started":"2023-05-06T03:54:55.776074Z","shell.execute_reply":"2023-05-06T04:45:46.696493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r yolo_project\n!rm -r wandb\n!zip -qjr runs.zip runs\n!rm -r runs","metadata":{"execution":{"iopub.status.busy":"2023-05-06T04:51:16.781729Z","iopub.execute_input":"2023-05-06T04:51:16.782208Z","iopub.status.idle":"2023-05-06T04:51:29.822059Z","shell.execute_reply.started":"2023-05-06T04:51:16.782172Z","shell.execute_reply":"2023-05-06T04:51:29.820655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-05-06T04:52:34.326109Z","iopub.execute_input":"2023-05-06T04:52:34.328068Z","iopub.status.idle":"2023-05-06T04:52:38.289162Z","shell.execute_reply.started":"2023-05-06T04:52:34.328013Z","shell.execute_reply":"2023-05-06T04:52:38.287565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-05-06T04:52:45.847380Z","iopub.execute_input":"2023-05-06T04:52:45.848334Z","iopub.status.idle":"2023-05-06T04:52:46.938809Z","shell.execute_reply.started":"2023-05-06T04:52:45.848299Z","shell.execute_reply":"2023-05-06T04:52:46.937216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}