{"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":158682423,"sourceType":"kernelVersion"}],"dockerImageVersionId":30636,"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-18T09:46:05.163932Z","iopub.execute_input":"2024-01-18T09:46:05.164221Z","iopub.status.idle":"2024-01-18T09:46:34.326267Z","shell.execute_reply.started":"2024-01-18T09:46:05.164194Z","shell.execute_reply":"2024-01-18T09:46:34.324886Z"},"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-18T09:46:34.329112Z","iopub.execute_input":"2024-01-18T09:46:34.329452Z","iopub.status.idle":"2024-01-18T09:46:38.356295Z","shell.execute_reply.started":"2024-01-18T09:46:34.329425Z","shell.execute_reply":"2024-01-18T09:46:38.355081Z"},"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","metadata":{"execution":{"iopub.status.busy":"2024-01-18T09:46:38.358013Z","iopub.execute_input":"2024-01-18T09:46:38.358403Z","iopub.status.idle":"2024-01-18T09:48:23.255053Z","shell.execute_reply.started":"2024-01-18T09:46:38.358364Z","shell.execute_reply":"2024-01-18T09:48:23.253889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Copy only images 500-899 for test (these are approximately the labels that are available for kidney_3_dense as well)","metadata":{}},{"cell_type":"code","source":"!cp /kaggle/input/kidney-3-dense-to-yolo/train/labels/kidney_3_dense_0[5-8]*.txt /kaggle/temp/val/labels\n!cp /kaggle/input/kidney-3-dense-to-yolo/train/images/kidney_3_dense_0[5-8]*.tif /kaggle/temp/val/images","metadata":{"execution":{"iopub.status.busy":"2024-01-18T09:48:23.257584Z","iopub.execute_input":"2024-01-18T09:48:23.257907Z","iopub.status.idle":"2024-01-18T09:48:55.018295Z","shell.execute_reply.started":"2024-01-18T09:48:23.257881Z","shell.execute_reply":"2024-01-18T09:48:55.016958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Explore image sizes","metadata":{"execution":{"iopub.status.busy":"2024-01-18T09:32:59.323033Z","iopub.execute_input":"2024-01-18T09:32:59.323576Z","iopub.status.idle":"2024-01-18T09:32:59.332617Z","shell.execute_reply.started":"2024-01-18T09:32:59.323536Z","shell.execute_reply":"2024-01-18T09:32:59.330993Z"}}},{"cell_type":"code","source":"import cv2\nimport os\n\nsets = ['train','test']\nfor s in sets:\n    dirs = os.listdir(f'/kaggle/input/blood-vessel-segmentation/{s}/')\n    for d in dirs:\n        if d != 'kidney_3_dense':\n            prefix = f'/kaggle/input/blood-vessel-segmentation/{s}/{d}/images/'\n            image_file = os.listdir(prefix)[0]\n            image_path = f'{prefix}{image_file}'\n            print(f'shape for {d} is {cv2.imread(image_path).shape}')","metadata":{"execution":{"iopub.status.busy":"2024-01-18T09:48:55.019866Z","iopub.execute_input":"2024-01-18T09:48:55.020178Z","iopub.status.idle":"2024-01-18T09:48:55.816256Z","shell.execute_reply.started":"2024-01-18T09:48:55.020148Z","shell.execute_reply":"2024-01-18T09:48:55.815286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kidney_1_dense_sample_image_path = '/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/images/0000.tif'\ntrain_img_size = cv2.imread(kidney_1_dense_sample_image_path).shape[0]","metadata":{"execution":{"iopub.status.busy":"2024-01-18T09:48:55.817419Z","iopub.execute_input":"2024-01-18T09:48:55.817741Z","iopub.status.idle":"2024-01-18T09:48:55.856212Z","shell.execute_reply.started":"2024-01-18T09:48:55.817714Z","shell.execute_reply":"2024-01-18T09:48:55.855354Z"},"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-18T09:48:55.857912Z","iopub.execute_input":"2024-01-18T09:48:55.858296Z","iopub.status.idle":"2024-01-18T09:48:55.889821Z","shell.execute_reply.started":"2024-01-18T09:48:55.858259Z","shell.execute_reply":"2024-01-18T09:48:55.888799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir train","metadata":{"execution":{"iopub.status.busy":"2024-01-18T09:48:55.891177Z","iopub.execute_input":"2024-01-18T09:48:55.891573Z","iopub.status.idle":"2024-01-18T09:48:56.895730Z","shell.execute_reply.started":"2024-01-18T09:48:55.891531Z","shell.execute_reply":"2024-01-18T09:48:56.894551Z"},"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-18T09:48:56.897225Z","iopub.execute_input":"2024-01-18T09:48:56.897566Z","iopub.status.idle":"2024-01-18T09:48:56.905835Z","shell.execute_reply.started":"2024-01-18T09:48:56.897510Z","shell.execute_reply":"2024-01-18T09:48:56.904899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat {yaml_path}","metadata":{"execution":{"iopub.status.busy":"2024-01-18T09:48:56.909046Z","iopub.execute_input":"2024-01-18T09:48:56.909375Z","iopub.status.idle":"2024-01-18T09:48:57.908402Z","shell.execute_reply.started":"2024-01-18T09:48:56.909348Z","shell.execute_reply":"2024-01-18T09:48:57.907171Z"},"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-18T09:48:57.909810Z","iopub.execute_input":"2024-01-18T09:48:57.910108Z","iopub.status.idle":"2024-01-18T09:49:00.978503Z","shell.execute_reply.started":"2024-01-18T09:48:57.910080Z","shell.execute_reply":"2024-01-18T09:49:00.977582Z"},"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-18T09:49:00.979761Z","iopub.execute_input":"2024-01-18T09:49:00.980193Z","iopub.status.idle":"2024-01-18T09:49:05.753495Z","shell.execute_reply.started":"2024-01-18T09:49:00.980165Z","shell.execute_reply":"2024-01-18T09:49:05.752430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"project = 'hacking_human_vasculature'\nname = 'train_kidney_1_dense_val_kidney_3_dense_size_1303'\n\nresults = model.train(data=yaml_path,\n                      project=project,\n                      name=name,\n                      epochs=200,\n                      time=4,\n                      patience=30, \n                      batch=16,\n                      imgsz=train_img_size,\n                      device=[0, 1],\n                      save_period=1,\n                      cache=True\n                     )","metadata":{"execution":{"iopub.status.busy":"2024-01-18T09:52:06.867697Z","iopub.execute_input":"2024-01-18T09:52:06.868368Z","iopub.status.idle":"2024-01-18T09:56:21.302672Z","shell.execute_reply.started":"2024-01-18T09:52:06.868332Z","shell.execute_reply":"2024-01-18T09:56:21.300956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.finish()","metadata":{"execution":{"iopub.status.busy":"2024-01-18T09:51:43.230957Z","iopub.status.idle":"2024-01-18T09:51:43.231385Z","shell.execute_reply.started":"2024-01-18T09:51:43.231175Z","shell.execute_reply":"2024-01-18T09:51:43.231212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}