{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"},{"sourceId":158630420,"sourceType":"kernelVersion"},{"sourceId":158630632,"sourceType":"kernelVersion"},{"sourceId":158630893,"sourceType":"kernelVersion"},{"sourceId":158631065,"sourceType":"kernelVersion"},{"sourceId":158682423,"sourceType":"kernelVersion"}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install ultralytics\n!pip install -U ipywidgets","metadata":{"_uuid":"6fe71b12-5fd1-4e35-9e8e-03816ec3b539","_cell_guid":"92f3a220-139d-4c55-a6f3-e90c068395bb","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-12T09:18:13.097329Z","iopub.execute_input":"2024-01-12T09:18:13.097816Z","iopub.status.idle":"2024-01-12T09:18:46.715888Z","shell.execute_reply.started":"2024-01-12T09:18:13.097773Z","shell.execute_reply":"2024-01-12T09:18:46.714282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /kaggle/temp/train/labels\n!mkdir -p /kaggle/temp/train/images\n\n!mkdir -p /kaggle/temp/val/labels\n!mkdir -p /kaggle/temp/val/images","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:14:47.643475Z","iopub.execute_input":"2024-01-12T09:14:47.644835Z","iopub.status.idle":"2024-01-12T09:14:52.014788Z","shell.execute_reply.started":"2024-01-12T09:14:47.644792Z","shell.execute_reply":"2024-01-12T09:14:52.013000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp /kaggle/input/kidney-1-dense-to-yolo/train/labels/* /kaggle/temp/train/labels\n!cp /kaggle/input/kidney-1-dense-to-yolo/train/images/* /kaggle/temp/train/images\n\n!cp /kaggle/input/kidney-1-voi-to-yolo/train/labels/* /kaggle/temp/train/labels\n!cp /kaggle/input/kidney-1-voi-to-yolo/train/images/* /kaggle/temp/train/images\n\n!cp /kaggle/input/kidney-2-to-yolo/train/labels/* /kaggle/temp/train/labels\n!cp /kaggle/input/kidney-2-to-yolo/train/images/* /kaggle/temp/train/images\n\n!cp /kaggle/input/kidney-3-sparse-to-yolo/train/labels/* /kaggle/temp/train/labels\n!cp /kaggle/input/kidney-3-sparse-to-yolo/train/images/* /kaggle/temp/train/images","metadata":{"execution":{"iopub.status.busy":"2024-01-12T08:23:57.405910Z","iopub.execute_input":"2024-01-12T08:23:57.406382Z","iopub.status.idle":"2024-01-12T08:29:36.371993Z","shell.execute_reply.started":"2024-01-12T08:23:57.406341Z","shell.execute_reply":"2024-01-12T08:29:36.369529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp /kaggle/input/kidney-3-dense-to-yolo/train/labels/* /kaggle/temp/val/labels\n!cp /kaggle/input/kidney-3-dense-to-yolo/train/images/* /kaggle/temp/val/images","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:15:27.831846Z","iopub.execute_input":"2024-01-12T09:15:27.832338Z","iopub.status.idle":"2024-01-12T09:16:32.536841Z","shell.execute_reply.started":"2024-01-12T09:15:27.832299Z","shell.execute_reply":"2024-01-12T09:16:32.533946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tail /kaggle/input/kidney-1-voi-to-yolo/train/kidney_1_voi.json","metadata":{"execution":{"iopub.status.busy":"2024-01-12T08:45:21.985627Z","iopub.execute_input":"2024-01-12T08:45:21.989019Z","iopub.status.idle":"2024-01-12T08:45:23.091395Z","shell.execute_reply.started":"2024-01-12T08:45:21.988891Z","shell.execute_reply":"2024-01-12T08:45:23.089572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Write yolo dataset yaml","metadata":{}},{"cell_type":"code","source":"import yaml","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:17:20.128025Z","iopub.execute_input":"2024-01-12T09:17:20.128556Z","iopub.status.idle":"2024-01-12T09:17:20.179348Z","shell.execute_reply.started":"2024-01-12T09:17:20.128514Z","shell.execute_reply":"2024-01-12T09:17:20.177904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir train","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:17:01.731182Z","iopub.execute_input":"2024-01-12T09:17:01.732240Z","iopub.status.idle":"2024-01-12T09:17:02.853698Z","shell.execute_reply.started":"2024-01-12T09:17:01.732134Z","shell.execute_reply":"2024-01-12T09:17:02.852050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/temp/train'\nval_path = '/kaggle/temp/val'\nnames = {0: 'Vessel'}\nyaml_path = 'train/train.yaml'\n\n# Number of classes\nnc = len(names)\n\n# Create a dictionary with the required content\nyaml_data = {\n    'names': names,\n    'nc': nc,\n    'test': '',\n    'train': train_path,\n    'val': val_path\n}\n\n# Write the dictionary to a YAML file\nwith open(yaml_path, 'w') as file:\n    yaml.dump(yaml_data, file, default_flow_style=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:17:21.937918Z","iopub.execute_input":"2024-01-12T09:17:21.939380Z","iopub.status.idle":"2024-01-12T09:17:21.949228Z","shell.execute_reply.started":"2024-01-12T09:17:21.939317Z","shell.execute_reply":"2024-01-12T09:17:21.947883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat {yaml_path}","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:17:24.055577Z","iopub.execute_input":"2024-01-12T09:17:24.056025Z","iopub.status.idle":"2024-01-12T09:17:25.168304Z","shell.execute_reply.started":"2024-01-12T09:17:24.055992Z","shell.execute_reply":"2024-01-12T09:17:25.167221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb\nfrom kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nwandb_api_key = user_secrets.get_secret(\"wandb-api-key\")\nwandb.login(key=wandb_api_key)","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:18:00.547266Z","iopub.execute_input":"2024-01-12T09:18:00.547687Z","iopub.status.idle":"2024-01-12T09:18:04.479080Z","shell.execute_reply.started":"2024-01-12T09:18:00.547657Z","shell.execute_reply":"2024-01-12T09:18:04.477804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO\n\nmodel = YOLO('yolov8n-seg.pt')","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:18:46.718308Z","iopub.execute_input":"2024-01-12T09:18:46.719471Z","iopub.status.idle":"2024-01-12T09:18:53.964812Z","shell.execute_reply.started":"2024-01-12T09:18:46.719432Z","shell.execute_reply":"2024-01-12T09:18:53.963830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"project = 'hacking_human_vasculature'\nname = 'kidney_3_dense_val_rest_train_2gpus_10_epochs'\n\nresults = model.train(data=yaml_path,\n                      project=project,\n                      name=name,\n                      epochs=10,\n                      patience=0, #I am setting patience=0 to disable early stopping.\n                      batch=8,\n                      imgsz=1928,\n                      device=[0, 1]\n                     )","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:19:14.176503Z","iopub.execute_input":"2024-01-12T09:19:14.177494Z","iopub.status.idle":"2024-01-12T09:27:50.994564Z","shell.execute_reply.started":"2024-01-12T09:19:14.177440Z","shell.execute_reply":"2024-01-12T09:27:50.990746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.finish()","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:27:54.773629Z","iopub.execute_input":"2024-01-12T09:27:54.774116Z","iopub.status.idle":"2024-01-12T09:27:59.992755Z","shell.execute_reply.started":"2024-01-12T09:27:54.774083Z","shell.execute_reply":"2024-01-12T09:27:59.991479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}