{"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":"## Train YoloR on COTS dataset (PART 1 - TRAINING) - as easy as possible to help people start with YoloR and develop this notebook\nThis notebook introduces YOLOR on Kaggle and TensorFlow - Help Protect the Great Barrier Reef competition. It shows how to train custom object detection model (COTS dataset) using YoloR. It could be good starting point for build own custom model based on YoloR detector. Full github repository you can find here - [YOLOR](https://github.com/WongKinYiu/yolor)\n\nSteps covered in this notebook:\n\n* Prepare COTS dataset for YoloR training\n* Install YoloR (YoloR, MISH CUDA, pytorch_wavelets)\n* Download Pre-Trained Weights for YoloR HUB\n* Prepare configuration files (YoloR hyperparameters and dataset)\n* Weights and Biases configuration for training logging\n* YoloR training\n* Run YoloR inference on test images\n\n<div class=\"alert alert-warning\">I found that there is no reference custom model training YoloR notebook on Kaggle. Since we have such an opportunity this is my contribution to this competition. Feel free to use it and enjoy! I really appreciate if you upvote this notebook. Thank you!</div>\n\n<div class=\"alert alert-success\" role=\"alert\">\nI introduced YoloX in TensorFlow - Help Protect the Great Barrier Reef competition as well. You can find these notebooks here:      \n    <ul>\n        <li> <a href=\"https://www.kaggle.com/remekkinas/yolox-full-training-pipeline-for-cots-dataset\">YoloX full training pipeline for COTS dataset</a></li>\n        <li> <a href=\"https://www.kaggle.com/remekkinas/yolox-inference-on-kaggle-for-cots-lb-0-507\">YoloX detections submission made on COTS dataset</a></li>\n    </ul>\n    \n</div>","metadata":{"papermill":{"duration":0.025403,"end_time":"2022-01-04T08:22:56.953721","exception":false,"start_time":"2022-01-04T08:22:56.928318","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<div align=\"center\"><img width=\"640\" src=\"https://github.com/WongKinYiu/yolor/raw/main/figure/unifued_network.png\"/></div>\n\n<div align=\"center\"><img width=\"640\" src=\"https://github.com/WongKinYiu/yolor/raw/main/figure/performance.png\"/></div>","metadata":{"papermill":{"duration":0.023512,"end_time":"2022-01-04T08:22:57.001245","exception":false,"start_time":"2022-01-04T08:22:56.977733","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## 0. IMPORT MODULES","metadata":{"papermill":{"duration":0.02421,"end_time":"2022-01-04T08:22:57.049617","exception":false,"start_time":"2022-01-04T08:22:57.025407","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import ast\nimport glob\nimport os\nimport yaml\nimport torch\n\nimport numpy as np\nimport pandas as pd\n\n\nfrom IPython.display import Image, display\nfrom IPython.core.magic import register_line_cell_magic\nfrom shutil import copyfile\nfrom tqdm import tqdm\ntqdm.pandas()","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:22:57.113383Z","iopub.status.busy":"2022-01-04T08:22:57.112541Z","iopub.status.idle":"2022-01-04T08:22:57.135060Z","shell.execute_reply":"2022-01-04T08:22:57.134494Z","shell.execute_reply.started":"2021-12-28T12:28:49.972215Z"},"papermill":{"duration":0.060691,"end_time":"2022-01-04T08:22:57.135201","exception":false,"start_time":"2022-01-04T08:22:57.074510","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ[\"CUDA_DEVICE_ORDER\"]=\"PCI_BUS_ID\"\nos.environ[\"CUDA_VISIBLE_DEVICES\"] = \"1\"\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\nprint('Device:', device)\nprint('Current cuda device:', torch.cuda.current_device())\nprint('Count of using GPUs:', torch.cuda.device_count())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HOME_DIR = './'\nCOTS_DATASET_PATH = './cots_dataset/train_images'","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:22:57.189390Z","iopub.status.busy":"2022-01-04T08:22:57.188695Z","iopub.status.idle":"2022-01-04T08:22:57.191814Z","shell.execute_reply":"2022-01-04T08:22:57.191381Z","shell.execute_reply.started":"2021-12-28T12:28:50.024398Z"},"papermill":{"duration":0.031154,"end_time":"2022-01-04T08:22:57.191952","exception":false,"start_time":"2022-01-04T08:22:57.160798","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1. PREPARE DATASET","metadata":{"papermill":{"duration":0.025111,"end_time":"2022-01-04T08:22:57.242477","exception":false,"start_time":"2022-01-04T08:22:57.217366","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# I just used spllited dataset by @julian3833 - Reef - A CV strategy: subsequences! \n# https://www.kaggle.com/julian3833/reef-a-cv-strategy-subsequences \n\ndf = pd.read_csv(\"./reef/train-validation-split/train-0.1.csv\")\ndf.head(3)","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:22:57.294602Z","iopub.status.busy":"2022-01-04T08:22:57.294082Z","iopub.status.idle":"2022-01-04T08:22:57.408146Z","shell.execute_reply":"2022-01-04T08:22:57.407639Z","shell.execute_reply.started":"2021-12-28T12:28:50.031751Z"},"papermill":{"duration":0.141715,"end_time":"2022-01-04T08:22:57.408351","exception":false,"start_time":"2022-01-04T08:22:57.266636","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_path(row):\n    return f\"{COTS_DATASET_PATH}/video_{row.video_id}/{row.video_frame}.jpg\"\n\ndef num_boxes(annotations):\n    annotations = ast.literal_eval(annotations)\n    return len(annotations)\n\ndf['path'] = df.apply(lambda row: add_path(row), axis=1)\ndf['num_bbox'] = df['annotations'].apply(lambda x: num_boxes(x))\nprint(\"New path and annotations preprocessing completed\")","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:22:57.486543Z","iopub.status.busy":"2022-01-04T08:22:57.470675Z","iopub.status.idle":"2022-01-04T08:22:58.466728Z","shell.execute_reply":"2022-01-04T08:22:58.467224Z","shell.execute_reply.started":"2021-12-28T12:28:50.159324Z"},"papermill":{"duration":1.032791,"end_time":"2022-01-04T08:22:58.467374","exception":false,"start_time":"2022-01-04T08:22:57.434583","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df[df.num_bbox > 0]\n\nprint(f'Dataset images with annotations: {len(df)}')","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:22:58.545243Z","iopub.status.busy":"2022-01-04T08:22:58.544644Z","iopub.status.idle":"2022-01-04T08:22:58.550880Z","shell.execute_reply":"2022-01-04T08:22:58.551375Z","shell.execute_reply.started":"2021-12-28T12:28:51.12614Z"},"papermill":{"duration":0.042225,"end_time":"2022-01-04T08:22:58.551505","exception":false,"start_time":"2022-01-04T08:22:58.509280","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_new_path(row):\n    if row.is_train:\n        return f\"{HOME_DIR}/yolor_dataset/images/train/{row.image_id}.jpg\"\n    else: \n        return f\"{HOME_DIR}/yolor_dataset/images/valid/{row.image_id}.jpg\"\n    \n\ndf['new_path'] = df.apply(lambda row: add_new_path(row), axis=1)\nprint(\"New image path for train/valid created\")","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:22:58.610407Z","iopub.status.busy":"2022-01-04T08:22:58.609622Z","iopub.status.idle":"2022-01-04T08:22:58.739798Z","shell.execute_reply":"2022-01-04T08:22:58.740258Z","shell.execute_reply.started":"2021-12-28T12:28:51.140175Z"},"papermill":{"duration":0.161794,"end_time":"2022-01-04T08:22:58.740395","exception":false,"start_time":"2022-01-04T08:22:58.578601","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(3)","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:22:58.806387Z","iopub.status.busy":"2022-01-04T08:22:58.797327Z","iopub.status.idle":"2022-01-04T08:22:58.809134Z","shell.execute_reply":"2022-01-04T08:22:58.809514Z","shell.execute_reply.started":"2021-12-28T12:28:51.268708Z"},"papermill":{"duration":0.041124,"end_time":"2022-01-04T08:22:58.809635","exception":false,"start_time":"2022-01-04T08:22:58.768511","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. CREATE DATASET FILE STRUCTURE","metadata":{"papermill":{"duration":0.02521,"end_time":"2022-01-04T08:22:58.861077","exception":false,"start_time":"2022-01-04T08:22:58.835867","status":"completed"},"tags":[]}},{"cell_type":"code","source":"os.makedirs(f\"{HOME_DIR}/yolor_dataset/images/train\")\nos.makedirs(f\"{HOME_DIR}/yolor_dataset/images/valid\")\nos.makedirs(f\"{HOME_DIR}/yolor_dataset/labels/train\")\nos.makedirs(f\"{HOME_DIR}/yolor_dataset/labels/valid\")\nprint(f\"Directory structure yor YoloR created\")","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:22:58.916417Z","iopub.status.busy":"2022-01-04T08:22:58.915597Z","iopub.status.idle":"2022-01-04T08:22:58.919676Z","shell.execute_reply":"2022-01-04T08:22:58.919262Z","shell.execute_reply.started":"2021-12-28T12:28:51.285428Z"},"papermill":{"duration":0.033377,"end_time":"2022-01-04T08:22:58.919777","exception":false,"start_time":"2022-01-04T08:22:58.886400","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def copy_file(row):\n  copyfile(row.path, row.new_path)\n\n_ = df.progress_apply(lambda row: copy_file(row), axis=1)","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:22:58.975979Z","iopub.status.busy":"2022-01-04T08:22:58.975162Z","iopub.status.idle":"2022-01-04T08:23:53.465229Z","shell.execute_reply":"2022-01-04T08:23:53.464748Z","shell.execute_reply.started":"2021-12-28T12:28:51.293472Z"},"papermill":{"duration":54.519684,"end_time":"2022-01-04T08:23:53.465360","exception":false,"start_time":"2022-01-04T08:22:58.945676","status":"completed"},"scrolled":true,"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. CREATE YoloR ANNOTATIONS","metadata":{"papermill":{"duration":0.229589,"end_time":"2022-01-04T08:23:53.853177","exception":false,"start_time":"2022-01-04T08:23:53.623588","status":"completed"},"tags":[]}},{"cell_type":"code","source":"IMG_WIDTH, IMG_HEIGHT = 1280, 720\n\ndef get_yolo_format_bbox(img_w, img_h, box):\n    w = box['width'] \n    h = box['height']\n    \n    if (bbox['x'] + bbox['width'] > 1280):\n        w = 1280 - bbox['x'] \n    if (bbox['y'] + bbox['height'] > 720):\n        h = 720 - bbox['y'] \n        \n    xc = box['x'] + int(np.round(w/2))\n    yc = box['y'] + int(np.round(h/2)) \n\n    return [xc/img_w, yc/img_h, w/img_w, h/img_h]\n    \n\nfor index, row in tqdm(df.iterrows()):\n    annotations = ast.literal_eval(row.annotations)\n    bboxes = []\n    for bbox in annotations:\n        bbox = get_yolo_format_bbox(IMG_WIDTH, IMG_HEIGHT, bbox)\n        bboxes.append(bbox)\n        \n    if row.is_train:\n        file_name = f\"{HOME_DIR}/yolor_dataset/labels/train/{row.image_id}.txt\"\n        os.makedirs(os.path.dirname(file_name), exist_ok=True)\n    else:\n        file_name = f\"{HOME_DIR}/yolor_dataset/labels/valid/{row.image_id}.txt\"\n        os.makedirs(os.path.dirname(file_name), exist_ok=True)\n        \n    with open(file_name, 'w') as f:\n        for i, bbox in enumerate(bboxes):\n            label = 0\n            bbox = [label]+bbox\n            bbox = [str(i) for i in bbox]\n            bbox = ' '.join(bbox)\n            f.write(bbox)\n            f.write('\\n')\n                \nprint(\"Annotations in YoloR format for all images created.\")","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:23:54.170256Z","iopub.status.busy":"2022-01-04T08:23:54.169270Z","iopub.status.idle":"2022-01-04T08:23:56.334286Z","shell.execute_reply":"2022-01-04T08:23:56.332922Z","shell.execute_reply.started":"2021-12-28T12:29:47.416737Z"},"papermill":{"duration":2.328514,"end_time":"2022-01-04T08:23:56.334408","exception":false,"start_time":"2022-01-04T08:23:54.005894","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. CREATE YoloR DATASET CONFIGURATION FILE","metadata":{"papermill":{"duration":0.176164,"end_time":"2022-01-04T08:23:56.688619","exception":false,"start_time":"2022-01-04T08:23:56.512455","status":"completed"},"tags":[]}},{"cell_type":"code","source":"data_yaml = dict(\n    train = f'{HOME_DIR}/yolor_dataset/images/train',\n    val = f'{HOME_DIR}/yolor_dataset/images/valid',\n    nc = 1,\n    names = ['sf']\n)\n\n\nwith open(f'{HOME_DIR}/YoloR-data.yaml', 'w') as outfile:\n    yaml.dump(data_yaml, outfile, default_flow_style=True)\n\nprint(f'Dataset configuration file for YoloR created')","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:23:57.066099Z","iopub.status.busy":"2022-01-04T08:23:57.065152Z","iopub.status.idle":"2022-01-04T08:23:57.070882Z","shell.execute_reply":"2022-01-04T08:23:57.070417Z","shell.execute_reply.started":"2021-12-28T12:29:49.046646Z"},"papermill":{"duration":0.180384,"end_time":"2022-01-04T08:23:57.071005","exception":false,"start_time":"2022-01-04T08:23:56.890621","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. INSTALL YoloR\n\n### 4A. CLONE YoloR GIT REPOSITORY ","metadata":{"papermill":{"duration":0.307824,"end_time":"2022-01-04T08:23:57.549250","exception":false,"start_time":"2022-01-04T08:23:57.241426","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!git clone https://github.com/WongKinYiu/yolor","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:23:57.875211Z","iopub.status.busy":"2022-01-04T08:23:57.874387Z","iopub.status.idle":"2022-01-04T08:24:00.023156Z","shell.execute_reply":"2022-01-04T08:24:00.023582Z","shell.execute_reply.started":"2021-12-28T12:29:49.058248Z"},"papermill":{"duration":2.318899,"end_time":"2022-01-04T08:24:00.023753","exception":false,"start_time":"2022-01-04T08:23:57.704854","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install torchvision --upgrade -q\n!pip install wandb --upgrade","metadata":{"_kg_hide-output":true,"execution":{"iopub.execute_input":"2022-01-04T08:24:00.345695Z","iopub.status.busy":"2022-01-04T08:24:00.344967Z","iopub.status.idle":"2022-01-04T08:25:38.829053Z","shell.execute_reply":"2022-01-04T08:25:38.828496Z","shell.execute_reply.started":"2021-12-28T12:29:51.694508Z"},"papermill":{"duration":98.646496,"end_time":"2022-01-04T08:25:38.829193","exception":false,"start_time":"2022-01-04T08:24:00.182697","status":"completed"},"scrolled":true,"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd yolor\n!pip install -qr requirements.txt","metadata":{"_kg_hide-output":true,"execution":{"iopub.execute_input":"2022-01-04T08:25:39.161924Z","iopub.status.busy":"2022-01-04T08:25:39.161119Z","iopub.status.idle":"2022-01-04T08:27:09.090226Z","shell.execute_reply":"2022-01-04T08:27:09.089719Z","shell.execute_reply.started":"2021-12-28T12:31:24.022701Z"},"papermill":{"duration":90.097681,"end_time":"2022-01-04T08:27:09.090364","exception":false,"start_time":"2022-01-04T08:25:38.992683","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 4B. INSTALL MISH CUDA","metadata":{"papermill":{"duration":0.165212,"end_time":"2022-01-04T08:27:09.421638","exception":false,"start_time":"2022-01-04T08:27:09.256426","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%cd ..\n!git clone https://github.com/JunnYu/mish-cuda\n%cd mish-cuda\n!git reset --hard 6f38976064cbcc4782f4212d7c0c5f6dd5e315a8\n!python setup.py build install\n%cd ..","metadata":{"_kg_hide-output":true,"execution":{"iopub.execute_input":"2022-01-04T08:27:09.761940Z","iopub.status.busy":"2022-01-04T08:27:09.761385Z","iopub.status.idle":"2022-01-04T08:28:10.020292Z","shell.execute_reply":"2022-01-04T08:28:10.019787Z","shell.execute_reply.started":"2021-12-28T12:32:31.494964Z"},"papermill":{"duration":60.432715,"end_time":"2022-01-04T08:28:10.020430","exception":false,"start_time":"2022-01-04T08:27:09.587715","status":"completed"},"scrolled":true,"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 4C. INSTALL PYTORCH WAVELETS ","metadata":{"papermill":{"duration":0.172,"end_time":"2022-01-04T08:28:10.364132","exception":false,"start_time":"2022-01-04T08:28:10.192132","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!git clone https://github.com/fbcotter/pytorch_wavelets\n%cd pytorch_wavelets\n!pip install .\n%cd ..","metadata":{"_kg_hide-output":true,"execution":{"iopub.execute_input":"2022-01-04T08:28:10.715721Z","iopub.status.busy":"2022-01-04T08:28:10.714711Z","iopub.status.idle":"2022-01-04T08:28:21.777646Z","shell.execute_reply":"2022-01-04T08:28:21.777187Z","shell.execute_reply.started":"2021-12-28T12:33:32.954913Z"},"papermill":{"duration":11.240669,"end_time":"2022-01-04T08:28:21.777780","exception":false,"start_time":"2022-01-04T08:28:10.537111","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 4D. DWONLOAD LATEST CHECKPOINT FROM YoloR MODEL HUB \n\nIn this notebook we take P6 model (because I want to show only how to train YoloR model on Kaggle) but you can experiment with other YoloR models: https://github.com/WongKinYiu/yolor","metadata":{"papermill":{"duration":0.177649,"end_time":"2022-01-04T08:28:22.133127","exception":false,"start_time":"2022-01-04T08:28:21.955478","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%cd yolor\n!bash scripts/get_pretrain.sh","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:28:22.492140Z","iopub.status.busy":"2022-01-04T08:28:22.491388Z","iopub.status.idle":"2022-01-04T08:28:27.253433Z","shell.execute_reply":"2022-01-04T08:28:27.252946Z","shell.execute_reply.started":"2021-12-28T12:33:43.868624Z"},"papermill":{"duration":4.943768,"end_time":"2022-01-04T08:28:27.253567","exception":false,"start_time":"2022-01-04T08:28:22.309799","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 4E. CONFIGURE WEIGHTS AND BIASES FOR EXPERIMENT LOGGING ","metadata":{"papermill":{"duration":0.180937,"end_time":"2022-01-04T08:28:27.616203","exception":false,"start_time":"2022-01-04T08:28:27.435266","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# more about Secrets -> https://www.kaggle.com/product-feedback/114053\nimport wandb\n# from kaggle_secrets import UserSecretsClient\n\n# user_secrets = UserSecretsClient()\n# wandb_api = user_secrets.get_secret(\"wandb_api\") \nwandb.login(key='your_key')\nwandb.login(anonymous='must')","metadata":{"_kg_hide-output":true,"execution":{"iopub.execute_input":"2022-01-04T08:28:28.052206Z","iopub.status.busy":"2022-01-04T08:28:28.050119Z","iopub.status.idle":"2022-01-04T08:28:30.557163Z","shell.execute_reply":"2022-01-04T08:28:30.557535Z","shell.execute_reply.started":"2021-12-28T12:33:47.932539Z"},"papermill":{"duration":2.753649,"end_time":"2022-01-04T08:28:30.557682","exception":false,"start_time":"2022-01-04T08:28:27.804033","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 4F. CONFIGURE YoloR HYPERPARAMETERS ","metadata":{"papermill":{"duration":0.182207,"end_time":"2022-01-04T08:28:30.922642","exception":false,"start_time":"2022-01-04T08:28:30.740435","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%cd yolor","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@register_line_cell_magic\ndef writetemplate(line, cell):\n    with open(line, 'w') as f:\n        f.write(cell.format(**globals()))","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:28:31.301015Z","iopub.status.busy":"2022-01-04T08:28:31.300118Z","iopub.status.idle":"2022-01-04T08:28:31.302696Z","shell.execute_reply":"2022-01-04T08:28:31.302244Z","shell.execute_reply.started":"2021-12-28T12:33:49.611542Z"},"papermill":{"duration":0.189488,"end_time":"2022-01-04T08:28:31.302821","exception":false,"start_time":"2022-01-04T08:28:31.113333","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writetemplate ./data/coco.yaml\n\nnc: 1\nnames: ['starfish',]","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:28:31.671600Z","iopub.status.busy":"2022-01-04T08:28:31.671034Z","iopub.status.idle":"2022-01-04T08:28:31.673401Z","shell.execute_reply":"2022-01-04T08:28:31.673767Z","shell.execute_reply.started":"2021-12-28T14:01:53.437888Z"},"papermill":{"duration":0.18914,"end_time":"2022-01-04T08:28:31.673931","exception":false,"start_time":"2022-01-04T08:28:31.484791","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writetemplate ./data/coco.names\n\nstarfish","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:28:32.065055Z","iopub.status.busy":"2022-01-04T08:28:32.063692Z","iopub.status.idle":"2022-01-04T08:28:32.066161Z","shell.execute_reply":"2022-01-04T08:28:32.066706Z","shell.execute_reply.started":"2021-12-28T14:03:52.536258Z"},"papermill":{"duration":0.210676,"end_time":"2022-01-04T08:28:32.066952","exception":false,"start_time":"2022-01-04T08:28:31.856276","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writetemplate ./hyp-yolor.yaml\n\nlr0: 0.01  # initial learning rate (SGD=1E-2, Adam=1E-3)\nlrf: 0.2  # 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.0  # image HSV-Hue augmentation (fraction)\nhsv_s: 0.0  # image HSV-Saturation augmentation (fraction)\nhsv_v: 0.0  # image HSV-Value augmentation (fraction)\ndegrees: 0.0  # image rotation (+/- deg)\ntranslate: 0.5  # image translation (+/- fraction)\nscale: 0.0  # image scale (+/- gain)\nshear: 0.0  # image shear (+/- deg)\nperspective: 0.0  # image perspective (+/- fraction), range 0-0.001\nflipud: 0.0  # image flip up-down (probability)\nfliplr: 0.5  # image flip left-right (probability)\nmosaic: 0.95  # image mosaic (probability)\nmixup: 0.3  # image mixup (probability)","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:28:32.450003Z","iopub.status.busy":"2022-01-04T08:28:32.449155Z","iopub.status.idle":"2022-01-04T08:28:32.451348Z","shell.execute_reply":"2022-01-04T08:28:32.452155Z","shell.execute_reply.started":"2021-12-28T12:33:49.619137Z"},"papermill":{"duration":0.193917,"end_time":"2022-01-04T08:28:32.452289","exception":false,"start_time":"2022-01-04T08:28:32.258372","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5. TRAIN YoloR","metadata":{"papermill":{"duration":0.191362,"end_time":"2022-01-04T08:28:32.827085","exception":false,"start_time":"2022-01-04T08:28:32.635723","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!python train.py \\\n --batch-size 16 \\\n --img 2560 1024 \\\n --data '../YoloR-data.yaml' \\\n --cfg './cfg/yolor_p6.cfg' \\\n --weights './yolor_p6.pt' \\\n --device 1 \\\n --name yolor_p6 \\\n --hyp './hyp-yolor.yaml' \\\n --epochs 300","metadata":{"execution":{"iopub.execute_input":"2022-01-04T08:28:33.195963Z","iopub.status.busy":"2022-01-04T08:28:33.195177Z","iopub.status.idle":"2022-01-04T10:19:46.807680Z","shell.execute_reply":"2022-01-04T10:19:46.806511Z","shell.execute_reply.started":"2021-12-28T12:34:53.815156Z"},"papermill":{"duration":6673.799972,"end_time":"2022-01-04T10:19:46.807940","exception":false,"start_time":"2022-01-04T08:28:33.007968","status":"completed"},"scrolled":true,"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We got an error - but it is connected with w&b integrations. Looking for solution.","metadata":{"papermill":{"duration":2.551684,"end_time":"2022-01-04T10:19:52.192027","exception":false,"start_time":"2022-01-04T10:19:49.640343","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## 6. INFERENCE USING YoloR ","metadata":{"papermill":{"duration":2.618838,"end_time":"2022-01-04T10:19:57.371714","exception":false,"start_time":"2022-01-04T10:19:54.752876","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%cd ..","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pwd","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INFER_PATH = f\"./cots_dataset/infer\"\n# os.makedirs(INFER_PATH)\n\ndf_infer = df.query(\"~is_train and num_bbox > 4\").sample(n = 15)\n\ndef copy_file(row):\n    new_location = INFER_PATH + '/' + row.image_id + '.jpg'\n    copyfile(row.path, new_location)\n\n_ = df_infer.progress_apply(lambda row: copy_file(row), axis=1)","metadata":{"execution":{"iopub.execute_input":"2022-01-04T10:20:02.951093Z","iopub.status.busy":"2022-01-04T10:20:02.950333Z","iopub.status.idle":"2022-01-04T10:20:03.457552Z","shell.execute_reply":"2022-01-04T10:20:03.457101Z","shell.execute_reply.started":"2021-12-28T14:10:38.315994Z"},"papermill":{"duration":3.078059,"end_time":"2022-01-04T10:20:03.457672","exception":false,"start_time":"2022-01-04T10:20:00.379613","status":"completed"},"scrolled":true,"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd yolor","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python detect.py \\\n    --source {INFER_PATH} \\\n    --cfg ./cfg/yolor_p6.cfg \\\n    --weights './runs/train/yolor_p66/weights/best_overall.pt' \\\n    --conf 0.05 \\\n    --img-size 1280 \\\n    --device 1 ","metadata":{"execution":{"iopub.execute_input":"2022-01-04T10:20:09.201797Z","iopub.status.busy":"2022-01-04T10:20:09.201012Z","iopub.status.idle":"2022-01-04T10:20:20.581385Z","shell.execute_reply":"2022-01-04T10:20:20.580589Z","shell.execute_reply.started":"2021-12-28T14:11:57.012248Z"},"papermill":{"duration":14.499391,"end_time":"2022-01-04T10:20:20.581527","exception":false,"start_time":"2022-01-04T10:20:06.082136","status":"completed"},"scrolled":true,"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img in glob.glob('./inference/output/*.jpg'): \n    display(Image(filename=img))\n    print(\"\\n\")","metadata":{},"execution_count":null,"outputs":[]}]}