{"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-06-26T09:43:27.256803Z","iopub.execute_input":"2023-06-26T09:43:27.257701Z","iopub.status.idle":"2023-06-26T09:43:27.430570Z","shell.execute_reply.started":"2023-06-26T09:43:27.257636Z","shell.execute_reply":"2023-06-26T09:43:27.429491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = Path('/kaggle/input/hubmap-hhv-coco/')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-06-26T09:43:27.433087Z","iopub.execute_input":"2023-06-26T09:43:27.433491Z","iopub.status.idle":"2023-06-26T09:43:27.438158Z","shell.execute_reply.started":"2023-06-26T09:43:27.433454Z","shell.execute_reply":"2023-06-26T09:43:27.437145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ultralytics","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-06-26T09:43:27.439811Z","iopub.execute_input":"2023-06-26T09:43:27.440527Z","iopub.status.idle":"2023-06-26T09:43:45.412935Z","shell.execute_reply.started":"2023-06-26T09:43:27.440492Z","shell.execute_reply":"2023-06-26T09:43:45.411802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8m-seg.pt .","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-06-26T09:43:45.416951Z","iopub.execute_input":"2023-06-26T09:43:45.418967Z","iopub.status.idle":"2023-06-26T09:43:47.739787Z","shell.execute_reply.started":"2023-06-26T09:43:45.418935Z","shell.execute_reply":"2023-06-26T09:43:47.738695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-06-26T09:43:47.742659Z","iopub.execute_input":"2023-06-26T09:43:47.743131Z","iopub.status.idle":"2023-06-26T09:43:53.376045Z","shell.execute_reply.started":"2023-06-26T09:43:47.743088Z","shell.execute_reply":"2023-06-26T09:43:53.375116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":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(\"WANDB\") \n\nwandb.login(key=wandb_api)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T09:43:53.377530Z","iopub.execute_input":"2023-06-26T09:43:53.378134Z","iopub.status.idle":"2023-06-26T09:43:56.673045Z","shell.execute_reply.started":"2023-06-26T09:43:53.378100Z","shell.execute_reply":"2023-06-26T09:43:56.671966Z"},"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)\n# change 1 : mixup 0.1=>0 copy paste 0.1=>0\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  # image mixup (probability)\ncopy_paste: 0  # 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-26T09:43:56.685276Z","iopub.execute_input":"2023-06-26T09:43:56.685601Z","iopub.status.idle":"2023-06-26T09:43:56.699939Z","shell.execute_reply.started":"2023-06-26T09:43:56.685577Z","shell.execute_reply":"2023-06-26T09:43:56.699050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat hyp.yaml","metadata":{"execution":{"iopub.status.busy":"2023-06-26T09:43:56.701293Z","iopub.execute_input":"2023-06-26T09:43:56.701895Z","iopub.status.idle":"2023-06-26T09:43:57.653400Z","shell.execute_reply.started":"2023-06-26T09:43:56.701862Z","shell.execute_reply":"2023-06-26T09:43:57.652292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Start the training","metadata":{}},{"cell_type":"code","source":"# train.run(data='/kaggle/input/hubmap-hhv-coco/hubmap-coco.yaml',\n#           imgsz=512, \n#           batch=16,\n#           weights='yolov8-seg.pt',\n#           cfg='yolov8/seg/models/segment/yolov8-seg.yaml',\n#           epochs=25,\n#           name='yolov8-fine-tune',\n#           project='yolov8-fine-tune',\n#           hyp='/kaggle/working/hyp.yaml',\n#           optimizer='Adam'\n#           )","metadata":{"execution":{"iopub.status.busy":"2023-06-26T09:43:57.657785Z","iopub.execute_input":"2023-06-26T09:43:57.658099Z","iopub.status.idle":"2023-06-26T09:43:57.662753Z","shell.execute_reply.started":"2023-06-26T09:43:57.658070Z","shell.execute_reply":"2023-06-26T09:43:57.661710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = YOLO('/kaggle/working/yolov8m-seg.pt')\nmodel.train(data='/kaggle/input/hubmap-hhv-coco/hubmap-coco.yaml',epochs=25,imgsz=512,optimizer='Adam')","metadata":{"execution":{"iopub.status.busy":"2023-06-26T09:43:57.664453Z","iopub.execute_input":"2023-06-26T09:43:57.664830Z","iopub.status.idle":"2023-06-26T10:02:33.391410Z","shell.execute_reply.started":"2023-06-26T09:43:57.664798Z","shell.execute_reply":"2023-06-26T10:02:33.388007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf wandb\n!rm yolov8m-seg.pt\n","metadata":{"execution":{"iopub.status.busy":"2023-06-26T10:02:33.394744Z","iopub.status.idle":"2023-06-26T10:02:33.397082Z","shell.execute_reply.started":"2023-06-26T10:02:33.396812Z","shell.execute_reply":"2023-06-26T10:02:33.396842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}