{"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\nimport sys\nsys.path.append('../input/tensorflow-great-barrier-reef')\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":"2022-02-11T08:43:00.827991Z","iopub.execute_input":"2022-02-11T08:43:00.828354Z","iopub.status.idle":"2022-02-11T08:43:01.33799Z","shell.execute_reply.started":"2022-02-11T08:43:00.828284Z","shell.execute_reply":"2022-02-11T08:43:01.337106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install wandb\nimport wandb","metadata":{"execution":{"iopub.status.busy":"2022-02-11T08:43:01.34274Z","iopub.execute_input":"2022-02-11T08:43:01.34496Z","iopub.status.idle":"2022-02-11T08:43:11.070098Z","shell.execute_reply.started":"2022-02-11T08:43:01.344915Z","shell.execute_reply":"2022-02-11T08:43:11.069044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\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":"2022-02-11T08:43:11.072952Z","iopub.execute_input":"2022-02-11T08:43:11.073453Z","iopub.status.idle":"2022-02-11T08:43:11.078729Z","shell.execute_reply.started":"2022-02-11T08:43:11.073408Z","shell.execute_reply":"2022-02-11T08:43:11.077836Z"},"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":"2022-02-11T08:43:11.081491Z","iopub.execute_input":"2022-02-11T08:43:11.081979Z","iopub.status.idle":"2022-02-11T08:43:12.513474Z","shell.execute_reply.started":"2022-02-11T08:43:11.081941Z","shell.execute_reply":"2022-02-11T08:43:12.512516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Get Paths","metadata":{}},{"cell_type":"code","source":"def get_path(row):\n    row['old_image_path'] = f'{ROOT_DIR}/train_images/video_{row.video_id}/{row.video_frame}.jpg'\n    row['image_path'] = f'{IMAGE_DIR}/video_{row.video_id}_{row.video_frame}.jpg'\n    row['label_path'] = f'{LABEL_DIR}/video_{row.video_id}_{row.video_frame}.txt'\n    return row","metadata":{"execution":{"iopub.status.busy":"2022-02-11T08:43:12.515277Z","iopub.execute_input":"2022-02-11T08:43:12.515566Z","iopub.status.idle":"2022-02-11T08:43:12.523409Z","shell.execute_reply.started":"2022-02-11T08:43:12.515531Z","shell.execute_reply":"2022-02-11T08:43:12.521859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df = pd.read_csv(f'../input/split-fold-cots/train-5folds.csv')\n# df['old_image_path'] = f'{ROOT_DIR}/train_images/video_'+df.video_id.astype(str)+'/'+df.video_frame.astype(str)+'.jpg'\n# df['image_path']  = f'{IMAGE_DIR}/'+df.image_id+'.jpg'\n# df['label_path']  = f'{LABEL_DIR}/'+df.image_id+'.txt'\n# df['annotations'] = df['annotations'].progress_apply(eval)\n# display(df.head(2))","metadata":{"execution":{"iopub.status.busy":"2022-02-11T08:43:12.525959Z","iopub.execute_input":"2022-02-11T08:43:12.5262Z","iopub.status.idle":"2022-02-11T08:43:12.532096Z","shell.execute_reply.started":"2022-02-11T08:43:12.526166Z","shell.execute_reply":"2022-02-11T08:43:12.531306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(f'../input/split-fold-cots/cross-validation/train-5folds.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":"2022-02-11T08:43:12.533548Z","iopub.execute_input":"2022-02-11T08:43:12.534049Z","iopub.status.idle":"2022-02-11T08:43:13.013973Z","shell.execute_reply.started":"2022-02-11T08:43:12.53401Z","shell.execute_reply":"2022-02-11T08:43:13.013184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2022-02-11T08:43:13.015276Z","iopub.execute_input":"2022-02-11T08:43:13.015599Z","iopub.status.idle":"2022-02-11T08:43:13.115284Z","shell.execute_reply.started":"2022-02-11T08:43:13.01556Z","shell.execute_reply":"2022-02-11T08:43:13.114474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if REMOVE_NOBBOX:\n    df = df.query(\"num_bbox>0\")","metadata":{"execution":{"iopub.status.busy":"2022-02-11T08:43:13.116591Z","iopub.execute_input":"2022-02-11T08:43:13.116925Z","iopub.status.idle":"2022-02-11T08:43:13.139759Z","shell.execute_reply.started":"2022-02-11T08:43:13.116886Z","shell.execute_reply":"2022-02-11T08:43:13.138994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2022-02-11T08:43:13.142942Z","iopub.execute_input":"2022-02-11T08:43:13.143136Z","iopub.status.idle":"2022-02-11T08:43:13.146829Z","shell.execute_reply.started":"2022-02-11T08:43:13.14311Z","shell.execute_reply":"2022-02-11T08:43:13.145967Z"},"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":"2022-02-11T08:43:13.148221Z","iopub.execute_input":"2022-02-11T08:43:13.148474Z","iopub.status.idle":"2022-02-11T08:43:42.659567Z","shell.execute_reply.started":"2022-02-11T08:43:13.148438Z","shell.execute_reply":"2022-02-11T08:43:42.658866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-11T08:43:42.660919Z","iopub.execute_input":"2022-02-11T08:43:42.66136Z","iopub.status.idle":"2022-02-11T08:43:42.821504Z","shell.execute_reply.started":"2022-02-11T08:43:42.66132Z","shell.execute_reply":"2022-02-11T08:43:42.820783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create BBox","metadata":{}},{"cell_type":"code","source":"df['width']  = 1280\ndf['height'] = 720\ndisplay(df.head(2))","metadata":{"execution":{"iopub.status.busy":"2022-02-11T08:43:42.823423Z","iopub.execute_input":"2022-02-11T08:43:42.823821Z","iopub.status.idle":"2022-02-11T08:43:42.842082Z","shell.execute_reply.started":"2022-02-11T08:43:42.823771Z","shell.execute_reply":"2022-02-11T08:43:42.84137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df['fold'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-11T08:43:42.843305Z","iopub.execute_input":"2022-02-11T08:43:42.844053Z","iopub.status.idle":"2022-02-11T08:43:42.847862Z","shell.execute_reply.started":"2022-02-11T08:43:42.844012Z","shell.execute_reply":"2022-02-11T08:43:42.846982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['bboxes'] = df.annotations.progress_apply(get_bbox)\ndf.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-02-11T08:43:42.848951Z","iopub.execute_input":"2022-02-11T08:43:42.8495Z","iopub.status.idle":"2022-02-11T08:43:42.922034Z","shell.execute_reply.started":"2022-02-11T08:43:42.84946Z","shell.execute_reply":"2022-02-11T08:43:42.921373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnt = 0\nall_bboxes = []\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       = [0]*num_bbox\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_yolo  = coco2yolo(image_height, image_width, bboxes_coco)\n        bboxes_yolo  = np.clip(bboxes_yolo, 0, 1)\n        all_bboxes.extend(bboxes_yolo)\n        for bbox_idx in range(len(bboxes_yolo)):\n            annot = [str(labels[bbox_idx])]+ list(bboxes_yolo[bbox_idx].astype(str))+(['\\n'] if num_bbox!=(bbox_idx+1) else [''])\n            annot = ' '.join(annot)\n            annot = annot.strip(' ')\n            f.write(annot)\nprint('Missing:',cnt)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-11T08:43:42.923525Z","iopub.execute_input":"2022-02-11T08:43:42.923974Z","iopub.status.idle":"2022-02-11T08:43:45.789474Z","shell.execute_reply.started":"2022-02-11T08:43:42.923938Z","shell.execute_reply":"2022-02-11T08:43:45.788632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🍚 Dataset","metadata":{}},{"cell_type":"code","source":"fold = 0\ntrain_files = []\nval_files   = []\ntrain_df = df[df['fold'] != fold]\nvalid_df = df[df['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":"2022-02-11T08:43:45.790868Z","iopub.execute_input":"2022-02-11T08:43:45.791233Z","iopub.status.idle":"2022-02-11T08:43:45.806761Z","shell.execute_reply.started":"2022-02-11T08:43:45.791189Z","shell.execute_reply":"2022-02-11T08:43:45.805585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 , '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":"2022-02-11T08:43:45.808149Z","iopub.execute_input":"2022-02-11T08:43:45.808603Z","iopub.status.idle":"2022-02-11T08:43:46.244377Z","shell.execute_reply.started":"2022-02-11T08:43:45.808566Z","shell.execute_reply":"2022-02-11T08:43:46.243475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working\n!rm -r /kaggle/working/yolov5\n# !git clone https://github.com/ultralytics/yolov5 # clone\n# !git clone https://github.com/jqkAA/yolov5\n!git clone https://github.com/Hung297/yolov5\n# !cp -r /kaggle/input/yolov5-lib-ds /kaggle/working/yolov5\n%cd yolov5\n%pip install -qr requirements.txt  # install\n\nfrom yolov5 import utils\ndisplay = utils.notebook_init()  # check","metadata":{"execution":{"iopub.status.busy":"2022-02-11T08:43:46.245756Z","iopub.execute_input":"2022-02-11T08:43:46.246021Z","iopub.status.idle":"2022-02-11T08:43:58.309932Z","shell.execute_reply.started":"2022-02-11T08:43:46.245985Z","shell.execute_reply":"2022-02-11T08:43:58.308843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Weights & Biases  (optional)\nimport wandb\nwandb.login(anonymous='must')","metadata":{"execution":{"iopub.status.busy":"2022-02-11T08:43:58.313158Z","iopub.execute_input":"2022-02-11T08:43:58.314024Z","iopub.status.idle":"2022-02-11T08:43:59.292991Z","shell.execute_reply.started":"2022-02-11T08:43:58.313935Z","shell.execute_reply":"2022-02-11T08:43:59.292091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🚅 Training","metadata":{}},{"cell_type":"code","source":"import yaml\nos.makedirs('/kaggle/hyps', exist_ok=True)\nconfig = dict(\n    lr0= 0.01,  # initial learning rate (SGD=1E-2, Adam=1E-3)\n    lrf= 0.1,  # final OneCycleLR learning rate (lr0 * lrf)\n    momentum= 0.937,  # SGD momentum/Adam beta1\n    weight_decay= 0.0005,  # optimizer weight decay 5e-4\n    warmup_epochs= 3.0,  # warmup epochs (fractions ok)\n    warmup_momentum= 0.8,  # warmup initial momentum\n    warmup_bias_lr= 0.1,  # warmup initial bias lr\n    box= 0.05,  # box loss gain\n    cls= 0.5,  # cls loss gain\n    cls_pw= 1.0,  # cls BCELoss positive_weight\n    obj= 1.0,  # obj loss gain (scale with pixels)\n    obj_pw= 1.0,  # obj BCELoss positive_weight\n    iou_t= 0.2,  # IoU training threshold\n    anchor_t= 4.0,  # anchor-multiple threshold\n    # anchors: 3  # anchors per output layer (0 to ignore)\n    fl_gamma= 0.0,  # focal loss gamma (efficientDet default gamma0.0)\n    hsv_h= 0.015,  # image HSV-Hue augmentation (fraction)\n    hsv_s= 0.7,  # image HSV-Saturation augmentation (fraction)\n    hsv_v= 0.4,  # image HSV-Value augmentation (fraction)\n    degrees= 0.0,  # image rotation (+/- deg)\n    translate= 0.1,  # image translation (+/- fraction)\n    scale= 0.5,  # image scale (+/- gain)\n    shear= 0.0,  # image shear (+/- deg)\n    perspective= 0.0,  # image perspective (+/- fraction), range 0-0.001\n    flipud= 0.5,  # image flip up-down (probability)\n    fliplr= 0.5,  # image flip left-right (probability)\n    mosaic= 1.0,  # image mosaic (probability)\n    mixup= 0.3,  # image mixup (probability)\n    copy_paste=0.0\n)\nwith open(os.path.join('/kaggle/hyps/' , 'configs.yaml'), 'w+') as f:\n     yaml.dump(config, f, default_flow_style=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-11T08:43:59.294634Z","iopub.execute_input":"2022-02-11T08:43:59.295027Z","iopub.status.idle":"2022-02-11T08:43:59.31021Z","shell.execute_reply.started":"2022-02-11T08:43:59.294985Z","shell.execute_reply":"2022-02-11T08:43:59.308676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python train.py --img 3520\\\n--hyp /kaggle/hyps/configs.yaml\\\n--batch 3\\\n--epochs 12\\\n--data /kaggle/working/bgr.yaml\\\n--weights yolov5s.pt \\\n--workers 4\\\n--patience 3","metadata":{"execution":{"iopub.status.busy":"2022-02-11T08:46:06.499418Z","iopub.execute_input":"2022-02-11T08:46:06.499896Z","iopub.status.idle":"2022-02-11T19:01:40.489305Z","shell.execute_reply.started":"2022-02-11T08:46:06.499863Z","shell.execute_reply":"2022-02-11T19:01:40.488408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimg = cv2.imread('/kaggle/working/yolov5/runs/train/exp/train_batch2.jpg')[:,:,::-1]\nplt.figure(figsize=(20, 20))\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-02-11T08:46:04.032172Z","iopub.execute_input":"2022-02-11T08:46:04.03241Z","iopub.status.idle":"2022-02-11T08:46:05.353605Z","shell.execute_reply.started":"2022-02-11T08:46:04.032378Z","shell.execute_reply":"2022-02-11T08:46:05.351347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls /kaggle/working/yolov5/runs/train/exp/weights","metadata":{"execution":{"iopub.status.busy":"2022-02-11T08:46:05.354632Z","iopub.execute_input":"2022-02-11T08:46:05.354906Z","iopub.status.idle":"2022-02-11T08:46:06.035036Z","shell.execute_reply.started":"2022-02-11T08:46:05.354871Z","shell.execute_reply":"2022-02-11T08:46:06.033968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tarfile\nimport os.path\n\ndef make_tarfile(output_filename, source_dir):\n    with tarfile.open(output_filename, \"w:gz\") as tar:\n        tar.add(source_dir, arcname=os.path.basename(source_dir))\nmake_tarfile('/kaggle/working/yolov5s_3520_hiuptm.tar.gz', '/kaggle/working/yolov5/runs/train/exp2/weights/best.pt')","metadata":{"execution":{"iopub.status.busy":"2022-02-11T19:01:56.139383Z","iopub.execute_input":"2022-02-11T19:01:56.13968Z","iopub.status.idle":"2022-02-11T19:01:59.258367Z","shell.execute_reply.started":"2022-02-11T19:01:56.139625Z","shell.execute_reply":"2022-02-11T19:01:59.257396Z"},"trusted":true},"execution_count":null,"outputs":[]}]}