{"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":158586749,"sourceType":"kernelVersion"},{"sourceId":158731138,"sourceType":"kernelVersion"}],"dockerImageVersionId":30627,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install --no-index -f /kaggle/input/download-ultralytics /kaggle/input/download-ultralytics/ultralytics-8.1.0-py3-none-any.whl","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-11T19:20:49.640569Z","iopub.execute_input":"2024-01-11T19:20:49.641168Z","iopub.status.idle":"2024-01-11T19:21:05.327024Z","shell.execute_reply.started":"2024-01-11T19:20:49.641125Z","shell.execute_reply":"2024-01-11T19:21:05.326132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\n\nfrom ultralytics.engine.results import Results","metadata":{"execution":{"iopub.status.busy":"2024-01-11T19:21:05.329123Z","iopub.execute_input":"2024-01-11T19:21:05.329425Z","iopub.status.idle":"2024-01-11T19:21:18.081091Z","shell.execute_reply.started":"2024-01-11T19:21:05.329399Z","shell.execute_reply":"2024-01-11T19:21:18.080339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir trained_model\n!cp /kaggle/input/continue-train-yolo-all-the-data-except-kidney-3/hacking_human_vasculature/kidney_3_dense_val_rest_train_2gpus_10_epochs/weights/* /kaggle/working/trained_model","metadata":{"execution":{"iopub.status.busy":"2024-01-11T19:21:18.082193Z","iopub.execute_input":"2024-01-11T19:21:18.082609Z","iopub.status.idle":"2024-01-11T19:21:20.284293Z","shell.execute_reply.started":"2024-01-11T19:21:18.082582Z","shell.execute_reply":"2024-01-11T19:21:20.283009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_masks(masks):\n    result = 255*(np.sum(masks, axis=0))\n    result = result.clip(0, 255).astype(\"uint8\")\n    return result","metadata":{"execution":{"iopub.status.busy":"2024-01-11T19:21:20.409372Z","iopub.execute_input":"2024-01-11T19:21:20.409754Z","iopub.status.idle":"2024-01-11T19:21:20.415299Z","shell.execute_reply.started":"2024-01-11T19:21:20.409718Z","shell.execute_reply":"2024-01-11T19:21:20.414214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode(mask):\n    pixel = mask.flatten()\n    pixel = np.concatenate([[0], pixel, [0]])\n    run = np.where(pixel[1:] != pixel[:-1])[0] + 1\n    run[1::2] -= run[::2]\n    rle = ' '.join(str(r) for r in run)\n    if rle == '':\n        rle = '1 0'\n    return rle","metadata":{"execution":{"iopub.status.busy":"2024-01-11T19:21:20.41648Z","iopub.execute_input":"2024-01-11T19:21:20.416836Z","iopub.status.idle":"2024-01-11T19:21:20.428935Z","shell.execute_reply.started":"2024-01-11T19:21:20.416809Z","shell.execute_reply":"2024-01-11T19:21:20.428183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_id_from_result(result: Results):\n    dataset_name = result.path.split('/')[-3]\n    file_name = result.path.split('/')[-1].split('.')[0]\n    return f'{dataset_name}_{file_name}'","metadata":{"execution":{"iopub.status.busy":"2024-01-11T19:21:20.429951Z","iopub.execute_input":"2024-01-11T19:21:20.430254Z","iopub.status.idle":"2024-01-11T19:21:20.439703Z","shell.execute_reply.started":"2024-01-11T19:21:20.430229Z","shell.execute_reply":"2024-01-11T19:21:20.438911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_rle_from_result(result: Results):\n    if not result.masks:\n        return '1 0'\n    else:\n        masks_array = result.masks.data.cpu().numpy()\n        combined_mask = add_masks(masks_array)\n        return rle_encode(combined_mask)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T19:21:20.440968Z","iopub.execute_input":"2024-01-11T19:21:20.441341Z","iopub.status.idle":"2024-01-11T19:21:20.452363Z","shell.execute_reply.started":"2024-01-11T19:21:20.44131Z","shell.execute_reply":"2024-01-11T19:21:20.451563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimgsz = cv2.imread('/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0000.tif').shape[:2]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = YOLO('/kaggle/working/trained_model/best.pt')\n","metadata":{"execution":{"iopub.status.busy":"2024-01-11T19:21:20.286184Z","iopub.execute_input":"2024-01-11T19:21:20.286627Z","iopub.status.idle":"2024-01-11T19:21:20.406236Z","shell.execute_reply.started":"2024-01-11T19:21:20.286588Z","shell.execute_reply":"2024-01-11T19:21:20.405335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source = '/kaggle/input/blood-vessel-segmentation/test/**/*.tif'\nresults = model.predict(source, \n                        stream=True, \n                        device=[0,1], \n                        retina_masks=True, \n                        imgsz=imgsz,\n                        conf=0.1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T19:22:17.607725Z","iopub.execute_input":"2024-01-11T19:22:17.608124Z","iopub.status.idle":"2024-01-11T19:22:17.613384Z","shell.execute_reply.started":"2024-01-11T19:22:17.608084Z","shell.execute_reply":"2024-01-11T19:22:17.612349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_list = []\nfor result in results:\n    img_id = extract_id_from_result(result)\n    rle = get_rle_from_result(result)\n    submission_list.append({\n        'id': img_id,\n        'rle': rle\n    })\n\ndf = pd.DataFrame(submission_list, columns=['id', 'rle'])\n\ndf.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-11T19:22:17.800559Z","iopub.execute_input":"2024-01-11T19:22:17.801379Z","iopub.status.idle":"2024-01-11T19:22:18.153973Z","shell.execute_reply.started":"2024-01-11T19:22:17.801346Z","shell.execute_reply":"2024-01-11T19:22:18.153229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}