{"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 Identification using ScaledYOLOv4","metadata":{}},{"cell_type":"code","source":"# CONTROL PANEL\n\nMODEL_TYPE = 'P5' # can be one of 'P5', 'P6', or 'P7'\n\nNUM_EPOCHS = 6\n\n# Training resolution\nRESOLUTION = 1280\n\n# Depends on GPU RAM which depends on training resolution and model type\n# - suggest to run in interactive mode to determine the largest batch size that does not result in CUDA out of memory error\nBATCH_SIZE = 4","metadata":{"execution":{"iopub.status.busy":"2022-02-20T10:44:31.851528Z","iopub.execute_input":"2022-02-20T10:44:31.852158Z","iopub.status.idle":"2022-02-20T10:44:31.877737Z","shell.execute_reply.started":"2022-02-20T10:44:31.852063Z","shell.execute_reply":"2022-02-20T10:44:31.877016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Module imports\n\nimport matplotlib.pyplot as plt\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2022-02-20T10:44:33.113515Z","iopub.execute_input":"2022-02-20T10:44:33.113758Z","iopub.status.idle":"2022-02-20T10:44:33.118185Z","shell.execute_reply.started":"2022-02-20T10:44:33.113732Z","shell.execute_reply":"2022-02-20T10:44:33.117463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import Data prepped in YOLO format\n* Data was prepared using this notebook: https://www.kaggle.com/alexchwong/cots-data-prep-fold4","metadata":{}},{"cell_type":"code","source":"DATA_DIR  = '/kaggle/input/cots-data-prep-fold4'","metadata":{"execution":{"iopub.status.busy":"2022-02-20T10:44:34.424552Z","iopub.execute_input":"2022-02-20T10:44:34.425049Z","iopub.status.idle":"2022-02-20T10:44:34.429167Z","shell.execute_reply.started":"2022-02-20T10:44:34.425007Z","shell.execute_reply":"2022-02-20T10:44:34.428224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -qq -d /kaggle/working/images {DATA_DIR}/images.zip \n!unzip -qq -d /kaggle/working/labels {DATA_DIR}/labels.zip\n!cp {DATA_DIR}/cots.yaml /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2022-02-20T10:44:41.146372Z","iopub.execute_input":"2022-02-20T10:44:41.146629Z","iopub.status.idle":"2022-02-20T10:45:28.110412Z","shell.execute_reply.started":"2022-02-20T10:44:41.146601Z","shell.execute_reply":"2022-02-20T10:45:28.109303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Installing ScaledYOLOv4 for Kaggle","metadata":{}},{"cell_type":"code","source":"!cp -r /kaggle/input/scaledyolov4-installation/ScaledYOLOv4 /kaggle/working/\n!cp -r /kaggle/input/scaledyolov4-installation/mish-cuda /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2022-02-20T10:45:28.112952Z","iopub.execute_input":"2022-02-20T10:45:28.11352Z","iopub.status.idle":"2022-02-20T10:45:36.805995Z","shell.execute_reply.started":"2022-02-20T10:45:28.113477Z","shell.execute_reply":"2022-02-20T10:45:36.805086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Mish-CUDA (must enable GPU)","metadata":{}},{"cell_type":"code","source":"# mish-cuda is required in order to use the pre-trained models\n\n%cd /kaggle/working/mish-cuda\n!python setup.py build install","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-20T10:51:00.342896Z","iopub.execute_input":"2022-02-20T10:51:00.343177Z","iopub.status.idle":"2022-02-20T10:52:01.896886Z","shell.execute_reply.started":"2022-02-20T10:51:00.343147Z","shell.execute_reply":"2022-02-20T10:52:01.896042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Fixes version `GLIBCXX_3.4.26' not found on Kaggle","metadata":{}},{"cell_type":"code","source":"# Yes, this takes about 5 minutes\n\n!add-apt-repository ppa:ubuntu-toolchain-r/test -y\n!apt-get update\n!apt-get upgrade libstdc++6 -y","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-20T10:45:36.807693Z","iopub.execute_input":"2022-02-20T10:45:36.807954Z","iopub.status.idle":"2022-02-20T10:50:07.753771Z","shell.execute_reply.started":"2022-02-20T10:45:36.807914Z","shell.execute_reply":"2022-02-20T10:50:07.752778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Hyperparameters\n* Essentially the same as YOLOv5","metadata":{}},{"cell_type":"code","source":"hyps = '''\n# these settings are the same as hyp.finetune.yaml\n\nlr0: 0.01  # initial learning rate (SGD=1E-2, Adam=1E-3)\nmomentum: 0.937  # SGD momentum/Adam beta1\nweight_decay: 0.0005  # optimizer weight decay 5e-4\ngiou: 0.05  # GIoU 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\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.5  # image translation (+/- fraction)\nscale: 0.8  # 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)\nmixup: 0.2  # image mixup (probability)\n'''\n\nwith open('/kaggle/working/ScaledYOLOv4/data/hyp.custom.yaml', 'w') as f:\n    f.write(hyps)\n\n!cat /kaggle/working/ScaledYOLOv4/data/hyp.custom.yaml","metadata":{"execution":{"iopub.status.busy":"2022-02-20T10:50:07.75637Z","iopub.execute_input":"2022-02-20T10:50:07.756646Z","iopub.status.idle":"2022-02-20T10:50:08.409411Z","shell.execute_reply.started":"2022-02-20T10:50:07.756609Z","shell.execute_reply":"2022-02-20T10:50:08.408588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Checking YAML file for dataset","metadata":{}},{"cell_type":"code","source":"!cat /kaggle/working/cots.yaml","metadata":{"execution":{"iopub.status.busy":"2022-02-20T10:50:08.411116Z","iopub.execute_input":"2022-02-20T10:50:08.411419Z","iopub.status.idle":"2022-02-20T10:50:09.062583Z","shell.execute_reply.started":"2022-02-20T10:50:08.41138Z","shell.execute_reply":"2022-02-20T10:50:09.061747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Defining Model for one Object\n\n* Edit the number of classes to 1","metadata":{}},{"cell_type":"code","source":"if MODEL_TYPE == 'P5':\n    MODEL_YAML = 'yolov4-p5.yaml'\n    MODEL_PATH = '/kaggle/input/scaledyolov4-installation/ScaledYOLOv4_pretrained/yolov4_p5.pt'\nelif MODEL_TYPE == 'P6':\n    MODEL_YAML = 'yolov4-p6.yaml'\n    MODEL_PATH = '/kaggle/input/scaledyolov4-installation/ScaledYOLOv4_pretrained/yolov4_p6.pt'\nelif MODEL_TYPE == 'P7':\n    MODEL_YAML = 'yolov4-p7.yaml'\n    MODEL_PATH = '/kaggle/input/scaledyolov4-installation/ScaledYOLOv4_pretrained/yolov4_p7.pt'","metadata":{"execution":{"iopub.status.busy":"2022-02-20T10:50:09.06599Z","iopub.execute_input":"2022-02-20T10:50:09.066208Z","iopub.status.idle":"2022-02-20T10:50:09.070591Z","shell.execute_reply.started":"2022-02-20T10:50:09.066181Z","shell.execute_reply":"2022-02-20T10:50:09.069917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# edit number of classes to 1\n\nshutil.copy(f\"/kaggle/working/ScaledYOLOv4/models/{MODEL_YAML}\", f\"/kaggle/working/ScaledYOLOv4/{MODEL_YAML}\")   \n!sed -i 's/nc: 80/nc: 1/g' /kaggle/working/ScaledYOLOv4/{MODEL_YAML}\n!cat /kaggle/working/ScaledYOLOv4/{MODEL_YAML}","metadata":{"execution":{"iopub.status.busy":"2022-02-20T10:50:09.072028Z","iopub.execute_input":"2022-02-20T10:50:09.072493Z","iopub.status.idle":"2022-02-20T10:50:10.48256Z","shell.execute_reply.started":"2022-02-20T10:50:09.072457Z","shell.execute_reply":"2022-02-20T10:50:10.481625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training ScaledYOLOv4","metadata":{}},{"cell_type":"markdown","source":"### Run training script","metadata":{}},{"cell_type":"code","source":"%cd /kaggle/working/ScaledYOLOv4\n!wandb disabled\n!python train.py \\\n    --epochs {NUM_EPOCHS} \\\n    --save-epochs 3 \\\n    --hyp /kaggle/working/ScaledYOLOv4/data/hyp.custom.yaml \\\n    --batch-size {BATCH_SIZE} \\\n    --img {RESOLUTION} {RESOLUTION} \\\n    --data /kaggle/working/cots.yaml \\\n    --cfg /kaggle/working/ScaledYOLOv4/{MODEL_YAML} \\\n    --weights {MODEL_PATH} \\\n    --name cots\n\n# save-epochs: How many last epochs to save (i.e. setting this to 3 will save epochs n, n-1, and n-2)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T10:52:01.898675Z","iopub.execute_input":"2022-02-20T10:52:01.898938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Checking Model Performance","metadata":{}},{"cell_type":"code","source":"TRAIN_RUN_DIR  = '/kaggle/working/ScaledYOLOv4/runs/exp0_cots'\n!ls {TRAIN_RUN_DIR}","metadata":{"execution":{"iopub.status.busy":"2021-12-11T12:55:36.481273Z","iopub.execute_input":"2021-12-11T12:55:36.481909Z","iopub.status.idle":"2021-12-11T12:55:37.178922Z","shell.execute_reply.started":"2021-12-11T12:55:36.481861Z","shell.execute_reply":"2021-12-11T12:55:37.178075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!printf 'epoch\\tGPU\\tgIOU\\tobj\\tcls\\ttotal\\tnTarg\\tres\\tP\\tR\\tmAP50\\tmAP50:95\\tv_gIOU\\tv_obj\\tv_cls\\n'\n!cat {TRAIN_RUN_DIR}/results.txt","metadata":{"execution":{"iopub.status.busy":"2021-12-11T12:55:45.137873Z","iopub.execute_input":"2021-12-11T12:55:45.138149Z","iopub.status.idle":"2021-12-11T12:55:45.827933Z","shell.execute_reply.started":"2021-12-11T12:55:45.138112Z","shell.execute_reply":"2021-12-11T12:55:45.827118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread(f'{TRAIN_RUN_DIR}/results.png'));","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cleanup","metadata":{}},{"cell_type":"code","source":"!mv {TRAIN_RUN_DIR} /kaggle/working/","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working\n\n!rm -r /kaggle/working/ScaledYOLOv4\n!rm -r /kaggle/working/mish-cuda\n\n!rm -r /kaggle/working/images\n!rm -r /kaggle/working/labels","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Credits:\nhttps://www.kaggle.com/awsaf49/great-barrier-reef-yolov5-infer","metadata":{}}]}