{"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":"# Introduction\n\nSo this year's HuBMAP competitions is a **instance segmentation** challenge. When I think sementic segmentation, the YOLO family of models comes to mind, although I've actually never trained/fine-tuned any YOLO model before, so this is as good an excuse as any to do so. \n\nYou can find the [inference notebook for this model here](https://www.kaggle.com/code/fnands/a-quick-yolov7-baseline-inference). \n\n### YOLOv7\n\nYOLOv7 (like all the YOLO models) are optimized for inference speed, while still delivering good performance. That being said, I am not sure this model is the absolute best one for this challenge, but it should provide a good baseline, and should help with iterating fast early.\n\nThe provided model training code expects the labels to be formatted as `.txt` files. I've created a dataset that does just that [here](https://www.kaggle.com/datasets/fnands/hubmap-hhv-coco). The [notebook for creating the dataset can be found here](https://www.kaggle.com/code/fnands/convert-training-data-to-coco-format), so you can play around with difference test/train splits, or filter by slide. Realistically, the dataset is small enough that you can just add it to this notebook if you wish. \n\n### Kudos\nI've borrowed liberally from other notebooks, so thanks to:  \n[@leonidkulyk](https://www.kaggle.com/leonidkulyk) for [[EDA] ❤️HuBMAP-HHV ~ Interactive annotations](https://www.kaggle.com/code/leonidkulyk/eda-hubmap-hhv-interactive-annotations)  \n[@averma111](https://www.kaggle.com/averma111) for [🔥PyTorch-HuBMAP-CNN🔥](https://www.kaggle.com/code/averma111/pytorch-hubmap-cnn)","metadata":{}},{"cell_type":"code","source":"import cv2\nimport pandas as pd\nfrom pathlib import Path\nimport json","metadata":{"execution":{"iopub.status.busy":"2023-06-10T19:59:01.017621Z","iopub.execute_input":"2023-06-10T19:59:01.018302Z","iopub.status.idle":"2023-06-10T19:59:01.169182Z","shell.execute_reply.started":"2023-06-10T19:59:01.018275Z","shell.execute_reply":"2023-06-10T19:59:01.168238Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = Path('/kaggle/input/hhv-70-30-split/')","metadata":{"execution":{"iopub.status.busy":"2023-06-10T19:59:01.171217Z","iopub.execute_input":"2023-06-10T19:59:01.171632Z","iopub.status.idle":"2023-06-10T19:59:01.176103Z","shell.execute_reply.started":"2023-06-10T19:59:01.171601Z","shell.execute_reply":"2023-06-10T19:59:01.175129Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone -b u7 --single-branch https://github.com/WongKinYiu/yolov7.git","metadata":{"execution":{"iopub.status.busy":"2023-06-10T19:59:01.177901Z","iopub.execute_input":"2023-06-10T19:59:01.17822Z","iopub.status.idle":"2023-06-10T19:59:03.910155Z","shell.execute_reply.started":"2023-06-10T19:59:01.17819Z","shell.execute_reply":"2023-06-10T19:59:03.908868Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -r yolov7/seg/requirements.txt","metadata":{"execution":{"iopub.status.busy":"2023-06-10T19:59:03.915449Z","iopub.execute_input":"2023-06-10T19:59:03.917834Z","iopub.status.idle":"2023-06-10T19:59:18.271171Z","shell.execute_reply.started":"2023-06-10T19:59:03.917769Z","shell.execute_reply":"2023-06-10T19:59:18.269842Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir yolo_wheel\n!pip wheel -r yolov7/seg/requirements.txt --wheel-dir=yolo_wheel yolov7","metadata":{"execution":{"iopub.status.busy":"2023-06-10T19:59:18.273174Z","iopub.execute_input":"2023-06-10T19:59:18.2739Z","iopub.status.idle":"2023-06-10T20:00:59.892522Z","shell.execute_reply.started":"2023-06-10T19:59:18.273859Z","shell.execute_reply":"2023-06-10T20:00:59.891347Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-seg.pt .","metadata":{"execution":{"iopub.status.busy":"2023-06-10T20:00:59.896521Z","iopub.execute_input":"2023-06-10T20:00:59.896898Z","iopub.status.idle":"2023-06-10T20:01:02.969472Z","shell.execute_reply.started":"2023-06-10T20:00:59.896865Z","shell.execute_reply":"2023-06-10T20:01:02.968226Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from yolov7.seg.segment import train","metadata":{"_kg_hide-input":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Set up weights and biases login for tracking","metadata":{}},{"cell_type":"code","source":"import wandb\nfrom kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\n\n# I have saved my API token with \"WANDB\" as Label. \n# If you use some other Label make sure to change the same below. \nwandb_api = user_secrets.get_secret(\"band\") \n\nwandb.login(key=wandb_api)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T20:01:02.971588Z","iopub.execute_input":"2023-06-10T20:01:02.971989Z","iopub.status.idle":"2023-06-10T20:01:07.008854Z","shell.execute_reply.started":"2023-06-10T20:01:02.971953Z","shell.execute_reply":"2023-06-10T20:01:07.007929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Set hyperparameters","metadata":{}},{"cell_type":"code","source":"# Create a yaml file as expected by YOLOv7 (and others)\nyaml_text = \"\"\"\n# YOLOv5 🚀 by Ultralytics, GPL-3.0 license\n# Hyperparameters for high-augmentation COCO training from scratch\n# python train.py --batch 32 --cfg yolov5m6.yaml --weights '' --data coco.yaml --img 1280 --epochs 300\n# See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials\n\nlr0: 0.0005  # initial learning rate (SGD=1E-2, Adam=1E-3)\nlrf: 0.1  # 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.3  # cls loss gain\ncls_pw: 1.0  # cls BCELoss positive_weight\nobj: 0.7  # 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.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.1  # image translation (+/- fraction)\nscale: 0.9  # 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: 1.0  # image mosaic (probability)\nmixup: 0.1  # image mixup (probability)\ncopy_paste: 0.1  # segment copy-paste (probability)\n\"\"\"\nwith open('/kaggle/working/hyp.yaml', 'w') as text_file:\n    text_file.write(yaml_text)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-10T20:01:19.287573Z","iopub.execute_input":"2023-06-10T20:01:19.28808Z","iopub.status.idle":"2023-06-10T20:01:19.295112Z","shell.execute_reply.started":"2023-06-10T20:01:19.288054Z","shell.execute_reply":"2023-06-10T20:01:19.294098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat hyp.yaml","metadata":{"execution":{"iopub.status.busy":"2023-06-10T20:01:19.2988Z","iopub.execute_input":"2023-06-10T20:01:19.299511Z","iopub.status.idle":"2023-06-10T20:01:20.29486Z","shell.execute_reply.started":"2023-06-10T20:01:19.299478Z","shell.execute_reply":"2023-06-10T20:01:20.29371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Start the training","metadata":{}},{"cell_type":"code","source":"train.run(data='/kaggle/input/hhv-70-30-split/hubmap-coco.yaml',\n          imgsz=512, \n          batch=32,\n          weights='yolov7-seg.pt',\n          cfg='yolov7/seg/models/segment/yolov7-seg.yaml',\n          epochs=25,\n          name='yolov7-fine-tune',\n          project='yolov7-fine-tune',\n          hyp='/kaggle/working/hyp.yaml',\n          optimizer='Adam'\n          )","metadata":{"execution":{"iopub.status.busy":"2023-06-10T20:01:20.296667Z","iopub.execute_input":"2023-06-10T20:01:20.297374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf wandb\n!rm yolov7-seg.pt\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}