{"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":"none","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"},{"sourceId":158629003,"sourceType":"kernelVersion"}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install ultralytics","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport json\nimport os\nimport cv2\nimport yaml\nimport shutil\n\nfrom ultralytics.data.converter import convert_coco\nfrom yolo_conversion_utils import process_masks, copy_files_with_rename","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-11T21:35:07.584259Z","iopub.execute_input":"2024-01-11T21:35:07.584809Z","iopub.status.idle":"2024-01-11T21:35:07.591193Z","shell.execute_reply.started":"2024-01-11T21:35:07.584766Z","shell.execute_reply":"2024-01-11T21:35:07.590228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p '/kaggle/working/train/images/'\n!mkdir -p '/kaggle/working/train/labels/'","metadata":{"execution":{"iopub.status.busy":"2024-01-11T21:35:07.608906Z","iopub.execute_input":"2024-01-11T21:35:07.60959Z","iopub.status.idle":"2024-01-11T21:35:09.786417Z","shell.execute_reply.started":"2024-01-11T21:35:07.609554Z","shell.execute_reply":"2024-01-11T21:35:09.78501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_name = 'kidney_2'","metadata":{"execution":{"iopub.status.busy":"2024-01-11T21:35:09.78869Z","iopub.execute_input":"2024-01-11T21:35:09.790059Z","iopub.status.idle":"2024-01-11T21:35:09.797073Z","shell.execute_reply.started":"2024-01-11T21:35:09.790009Z","shell.execute_reply":"2024-01-11T21:35:09.795425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# copy images\nimg_destination_directory = '/kaggle/working/train/images/'\nimg_source_directory = f'/kaggle/input/blood-vessel-segmentation/train/{dataset_name}/images'\ncopy_files_with_rename(img_source_directory, img_destination_directory)\n\n# Copy labels\nlab_destination_directory = '/kaggle/working/train/labels/'\nlab_source_directory = f'/kaggle/input/blood-vessel-segmentation/train/{dataset_name}/labels'\ncopy_files_with_rename(lab_source_directory, lab_destination_directory)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T21:35:09.799248Z","iopub.execute_input":"2024-01-11T21:35:09.799767Z","iopub.status.idle":"2024-01-11T21:36:41.953579Z","shell.execute_reply.started":"2024-01-11T21:35:09.799723Z","shell.execute_reply":"2024-01-11T21:36:41.952085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create coco format annotations\nprocess_masks('/kaggle/working/train', f'/kaggle/working/train/{dataset_name}.json')","metadata":{"execution":{"iopub.status.busy":"2024-01-11T21:42:16.802846Z","iopub.execute_input":"2024-01-11T21:42:16.803459Z","iopub.status.idle":"2024-01-11T21:43:00.868207Z","shell.execute_reply.started":"2024-01-11T21:42:16.803416Z","shell.execute_reply":"2024-01-11T21:43:00.866856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remove tif labels\n!rm train/labels/*.tif","metadata":{"execution":{"iopub.status.busy":"2024-01-11T21:43:00.870268Z","iopub.execute_input":"2024-01-11T21:43:00.871053Z","iopub.status.idle":"2024-01-11T21:43:02.759006Z","shell.execute_reply.started":"2024-01-11T21:43:00.871008Z","shell.execute_reply":"2024-01-11T21:43:02.755312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert annotations from coco json to yolo format using ultralytics\nconvert_coco(labels_dir='/kaggle/working/train', \n             use_segments=True,\n             cls91to80=False,)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T21:43:02.766858Z","iopub.execute_input":"2024-01-11T21:43:02.768688Z","iopub.status.idle":"2024-01-11T21:43:30.950805Z","shell.execute_reply.started":"2024-01-11T21:43:02.768522Z","shell.execute_reply":"2024-01-11T21:43:30.949364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp  /kaggle/working/coco_converted/labels/{dataset_name}/*.txt /kaggle/working/train/labels/\n!rm -r /kaggle/working/coco_converted","metadata":{"execution":{"iopub.status.busy":"2024-01-11T21:43:30.952793Z","iopub.execute_input":"2024-01-11T21:43:30.953262Z","iopub.status.idle":"2024-01-11T21:43:33.519123Z","shell.execute_reply.started":"2024-01-11T21:43:30.953219Z","shell.execute_reply":"2024-01-11T21:43:33.517679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !ls -lh /kaggle/input/blood-vessel-segmentation/train/{dataset_name}/images/","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}