%cd /kaggle/working
!git clone https://github.com/ultralytics/yolov5 # clone
!cp -r /kaggle/input/yolov5-lib-ds /kaggle/working/yolov5
%cd yolov5
%pip install -qr requirements.txt  # install

from yolov5 import utils
display = utils.notebook_init()  # check
# Install W&B 
!pip install -q --upgrade wandb

# Login 
import wandb

from kaggle_secrets import UserSecretsClient
user_secrets = UserSecretsClient() 

personal_key_for_api = user_secrets.get_secret("WNB")
! wandb login $personal_key_for_api
import os
import yaml

cwd = '/kaggle/working/'

data = hyperparams = {'lr0': 0.01,
 'lrf': 0.1, 
 'momentum': 0.937,  
 'weight_decay': 0.0005,
 'warmup_epochs': 5.0, #3.0
 'warmup_momentum': 0.8, #0.8
 'warmup_bias_lr': 0.1, #0.1
 'box': 0.05,
 'cls': 0.5, #0.5
 'cls_pw': 1.0,
 'obj': 1.0,
 'obj_pw': 1.0,
 'iou_t': 0.2,
 'anchor_t': 4.0,
 'fl_gamma': 0.0,
 'hsv_h': 0.015,
 'hsv_s': 0.7,#0.7
 'hsv_v': 0.3,#0.3
 'degrees': 0.0,
 'translate': 0.1,
 'scale': 0.7, #0.5
 'shear': 0.0,
 'perspective': 0.0,
 'flipud': 0.0,
 'fliplr': 0.5,
 'mosaic': 0.5, #1.0 # 0.0 was better
 'mixup': 0.5, #0.0
 'copy_paste': 0.0}

with open(os.path.join( cwd, 'hyper.yaml' ), 'w') as outfile:
    yaml.dump(data, outfile, default_flow_style=False)

cwd = '/kaggle/working/'
data = dict(
    path  = cwd,
    train = '../input/cotsyolostratifiedkfold/COTS-YOLOv5-StratifiedKFold/images/train',
    val   = '../input/cotsyolostratifiedkfold/COTS-YOLOv5-StratifiedKFold/images/val',
    nc    = 1,
    names = ['cots'],
    )
with open(os.path.join( cwd , 'custom.yaml'), 'w') as outfile:
    yaml.dump(data, outfile, default_flow_style=False)

f = open(os.path.join( cwd , 'custom.yaml'), 'r')
print('\nyaml:')
print(f.read())
!python train.py --img 960\
--hyp /kaggle/working/hyper.yaml\
--batch 8\
--epochs 30\
--data /kaggle/working/custom.yaml\
--weights yolov5l.pt 
"""
Reference : https://www.kaggle.com/awsaf49/great-barrier-reef-yolov5-train
"""