{"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"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Create COCO annotations from masks","metadata":{}},{"cell_type":"markdown","source":"Based on https://github.com/bnsreenu/python_for_microscopists/blob/master/332%20-%20All%20about%20image%20annotations%E2%80%8B/binary_to_coco_V3.0.py\n\nYT video with explanation: https://www.youtube.com/watch?v=NYeJvxe5nYw","metadata":{}},{"cell_type":"code","source":"import glob\nimport json\nimport os\nimport cv2\nimport yaml\nimport shutil","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-09T18:54:04.628086Z","iopub.execute_input":"2024-01-09T18:54:04.628532Z","iopub.status.idle":"2024-01-09T18:54:04.887758Z","shell.execute_reply.started":"2024-01-09T18:54:04.628497Z","shell.execute_reply":"2024-01-09T18:54:04.886641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MASK_EXT = 'tif'\nORIGINAL_EXT = 'tif'\nMASK_PATH = 'labels'\nIMG_PATH = 'images'","metadata":{"execution":{"iopub.status.busy":"2024-01-09T18:57:59.190030Z","iopub.execute_input":"2024-01-09T18:57:59.190488Z","iopub.status.idle":"2024-01-09T18:57:59.196927Z","shell.execute_reply.started":"2024-01-09T18:57:59.190449Z","shell.execute_reply":"2024-01-09T18:57:59.195471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_annotation_for_contour(contour, annotation_id: int, image_id):\n    bbox = cv2.boundingRect(contour)\n    area = cv2.contourArea(contour)\n    segmentation = contour.flatten().tolist()\n\n    annotation = {\n        \"iscrowd\": 0,\n        \"id\": annotation_id,\n        \"image_id\": image_id,\n        \"category_id\": 1,\n        \"bbox\": bbox,\n        \"area\": area,\n        \"segmentation\": [segmentation],\n    }\n\n    return annotation","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:22:51.191384Z","iopub.execute_input":"2024-01-09T17:22:51.191795Z","iopub.status.idle":"2024-01-09T17:22:51.198684Z","shell.execute_reply.started":"2024-01-09T17:22:51.191763Z","shell.execute_reply":"2024-01-09T17:22:51.197320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def contours_from_mask_image(mask_image_open):\n    # Find contours in the mask image\n    gray = cv2.cvtColor(mask_image_open, cv2.COLOR_BGR2GRAY)\n    _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\n    contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]\n    return contours","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:22:51.622769Z","iopub.execute_input":"2024-01-09T17:22:51.625516Z","iopub.status.idle":"2024-01-09T17:22:51.631781Z","shell.execute_reply.started":"2024-01-09T17:22:51.625474Z","shell.execute_reply":"2024-01-09T17:22:51.630658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def images_annotations_info(path):\n    \"\"\"\n    Process the binary masks and generate images and annotations information.\n\n    :param path: Path to the directory containing images and binary masks\n    :return: Tuple containing images info, annotations info, and annotation count\n    \"\"\"\n    global image_id, annotation_id\n    annotations = []\n    images = []\n\n\n    for mask_image in glob.glob(os.path.join(path, MASK_PATH, f'*.{MASK_EXT}')):\n        original_file_name = f'{os.path.basename(mask_image).split(\".\")[0]}.{ORIGINAL_EXT}'\n        mask_image_open = cv2.imread(mask_image)\n\n        # Get image dimensions\n        height, width, _ = mask_image_open.shape\n\n        # Create or find existing image annotation\n        if original_file_name not in map(lambda img: img['file_name'], images):\n            image = {\n                \"id\": image_id + 1,\n                \"width\": width,\n                \"height\": height,\n                \"file_name\": original_file_name,\n            }\n            images.append(image)\n            image_id += 1\n        else:\n            image = [element for element in images if element['file_name'] == original_file_name][0]\n\n        contours = contours_from_mask_image(mask_image_open)\n\n        # Create annotation for each contour\n        for contour in contours:\n            annotation = create_annotation_for_contour(contour, annotation_id, image['id'])\n\n            # Add annotation if area is greater than zero\n#             if annotation[\"area\"] > 0:\n            annotations.append(annotation)\n            annotation_id += 1\n\n    return images, annotations, annotation_id\n","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:22:54.077293Z","iopub.execute_input":"2024-01-09T17:22:54.077673Z","iopub.status.idle":"2024-01-09T17:22:54.087469Z","shell.execute_reply.started":"2024-01-09T17:22:54.077639Z","shell.execute_reply":"2024-01-09T17:22:54.086581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_masks(mask_path, dest_json):\n    # Initialize the COCO JSON format with categories\n    coco_format = {\n        \"info\": {},\n        \"licenses\": [],\n        \"images\": [],\n        \"categories\": [{\"id\": 1, \"name\": 'Vessel', \"supercategory\": 'Vessel'}],\n        \"annotations\": [],\n    }\n\n    # Create images and annotations sections\n    coco_format[\"images\"], coco_format[\"annotations\"], annotation_cnt = images_annotations_info(mask_path)\n\n    # Save the COCO JSON to a file\n    with open(dest_json, \"w\") as outfile:\n        json.dump(coco_format, outfile, sort_keys=True, indent=4)\n\n    print(\"Created %d annotations for images in folder: %s\" % (annotation_cnt, mask_path))\n","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:22:55.477039Z","iopub.execute_input":"2024-01-09T17:22:55.477936Z","iopub.status.idle":"2024-01-09T17:22:55.484200Z","shell.execute_reply.started":"2024-01-09T17:22:55.477892Z","shell.execute_reply":"2024-01-09T17:22:55.483079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"global image_id, annotation_id\nimage_id = 0\nannotation_id = 0","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:22:56.035276Z","iopub.execute_input":"2024-01-09T17:22:56.035951Z","iopub.status.idle":"2024-01-09T17:22:56.039804Z","shell.execute_reply.started":"2024-01-09T17:22:56.035915Z","shell.execute_reply":"2024-01-09T17:22:56.038958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kidney1_dense_path = \"/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense\"\nkidney1_dense_json_path = \"/kaggle/working/kidney1_dense_coco.json\"\nkidney3_sparse_path = \"/kaggle/input/blood-vessel-segmentation/train/kidney_3_sparse\"\nkidney3_sparse_json_path = \"/kaggle/working/kidney3_sparse_coco.json\"","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:22:56.682891Z","iopub.execute_input":"2024-01-09T17:22:56.683590Z","iopub.status.idle":"2024-01-09T17:22:56.688164Z","shell.execute_reply.started":"2024-01-09T17:22:56.683550Z","shell.execute_reply":"2024-01-09T17:22:56.686967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"process_masks(kidney1_dense_path, kidney1_dense_json_path)\nprocess_masks(kidney3_sparse_path, kidney3_sparse_json_path)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T17:23:04.996182Z","iopub.execute_input":"2024-01-09T17:23:04.996566Z","iopub.status.idle":"2024-01-09T17:25:07.120706Z","shell.execute_reply.started":"2024-01-09T17:23:04.996538Z","shell.execute_reply":"2024-01-09T17:25:07.119472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!grep -C 2 '\"image_id\": 902' kidney1_dense_coco.json\n","metadata":{"execution":{"iopub.status.busy":"2024-01-09T06:58:07.747653Z","iopub.execute_input":"2024-01-09T06:58:07.748116Z","iopub.status.idle":"2024-01-09T06:58:08.919104Z","shell.execute_reply.started":"2024-01-09T06:58:07.748084Z","shell.execute_reply":"2024-01-09T06:58:08.917121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tail /kaggle/working/kidney3_sparse_coco.json","metadata":{"execution":{"iopub.status.busy":"2024-01-09T18:13:37.279805Z","iopub.execute_input":"2024-01-09T18:13:37.280313Z","iopub.status.idle":"2024-01-09T18:13:38.392066Z","shell.execute_reply.started":"2024-01-09T18:13:37.280276Z","shell.execute_reply":"2024-01-09T18:13:38.390470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm /kaggle/working/yolov8n.pt","metadata":{"execution":{"iopub.status.busy":"2024-01-10T05:33:06.182709Z","iopub.execute_input":"2024-01-10T05:33:06.183225Z","iopub.status.idle":"2024-01-10T05:33:07.279032Z","shell.execute_reply.started":"2024-01-10T05:33:06.183187Z","shell.execute_reply":"2024-01-10T05:33:07.277334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create a small subset for experiments and visualizations","metadata":{}},{"cell_type":"markdown","source":"Create directory","metadata":{}},{"cell_type":"code","source":"!mkdir -p small_train/images\n!mkdir -p small_train/labels","metadata":{"execution":{"iopub.status.busy":"2024-01-08T18:52:08.016256Z","iopub.execute_input":"2024-01-08T18:52:08.016832Z","iopub.status.idle":"2024-01-08T18:52:10.245320Z","shell.execute_reply.started":"2024-01-08T18:52:08.016785Z","shell.execute_reply":"2024-01-08T18:52:10.243848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Copy images","metadata":{}},{"cell_type":"code","source":"! cp /kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/images/04**.tif /kaggle/working/small_train/images/","metadata":{"execution":{"iopub.status.busy":"2024-01-08T18:50:31.787033Z","iopub.execute_input":"2024-01-08T18:50:31.787599Z","iopub.status.idle":"2024-01-08T18:50:36.157713Z","shell.execute_reply.started":"2024-01-08T18:50:31.787550Z","shell.execute_reply":"2024-01-08T18:50:36.155846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Copy masks","metadata":{}},{"cell_type":"code","source":"! cp /kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/labels/04**.tif small_train/labels/","metadata":{"execution":{"iopub.status.busy":"2024-01-08T18:52:43.788634Z","iopub.execute_input":"2024-01-08T18:52:43.789200Z","iopub.status.idle":"2024-01-08T18:52:45.264199Z","shell.execute_reply.started":"2024-01-08T18:52:43.789129Z","shell.execute_reply":"2024-01-08T18:52:45.262090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Create coco json for the small training subset","metadata":{}},{"cell_type":"code","source":"small_train_path = \"small_train\"\nsmall_train_json_path = \"small_train_coco.json\"\nprocess_masks(small_train_path, small_train_json_path)","metadata":{"execution":{"iopub.status.busy":"2024-01-08T18:54:04.266916Z","iopub.execute_input":"2024-01-08T18:54:04.268495Z","iopub.status.idle":"2024-01-08T18:54:06.189905Z","shell.execute_reply.started":"2024-01-08T18:54:04.268435Z","shell.execute_reply":"2024-01-08T18:54:06.188604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Convert images to png and modify the annotations to be able to visualize them on roboflow\n\n```shell\n# from the folder where tif image files are\nmkdir ../png\n# convert tif files to png\nsips -s format png *.tif --out ../png\ncd ..\n# copy coco annotation json\ncp small_train_coco.json png\ncd png\n#replace the filenames in the annotation\nsed -i '' 's/\\.tif/\\.png/g' small_train_coco.json\n```\n\nNow the `png` folder is ready to be uploaded and visualized with roboflow\n\nhttps://app.roboflow.com/\n\n","metadata":{}},{"cell_type":"markdown","source":"# Convert COCO annotations to YOLO\n\nLooks like for YOLO\n> ALL images are used during training. If no labels are found then the image simply has no labels.\n\nhttps://github.com/ultralytics/yolov5/discussions/7148","metadata":{}},{"cell_type":"code","source":"# Function to convert images to YOLO format\ndef convert_to_yolo(input_images_path, input_json_path, output_images_path, output_labels_path):\n    # Open JSON file containing image annotations\n    with open(input_json_path) as f:\n        coco_json = json.load(f)\n\n    # Create directories for output labels\n    os.makedirs(output_labels_path, exist_ok=True)\n    os.makedirs(output_images_path, exist_ok=True)\n\n    # List to store filenames\n    file_names = []\n    for filename in os.listdir(input_images_path):\n        if filename.endswith(f\".{ORIGINAL_EXT}\"):\n            source = os.path.join(input_images_path, filename)\n            destination = os.path.join(output_images_path, filename)\n            shutil.copy(source, destination)\n            file_names.append(filename)\n\n    # Function to get image annotations\n    def get_img_ann(image_id):\n        return [ann for ann in coco_json['annotations'] if ann['image_id'] == image_id]\n\n    # Function to get image coco_json\n    def get_img(filename):\n        return next((img for img in coco_json['images'] if img['file_name'] == filename), None)\n\n    # Iterate through filenames and process each image\n    for filename in file_names:\n        img = get_img(filename)\n        img_id = img['id']\n        img_w = img['width']\n        img_h = img['height']\n        img_ann = get_img_ann(img_id)\n\n        # Write normalized polygon data to a text file\n        if img_ann:\n            with open(os.path.join(output_labels_path, f\"{os.path.splitext(filename)[0]}.txt\"), \"a\") as file_object:\n                for ann in img_ann:\n                    current_category = ann['category_id'] - 1\n                    polygon = ann['segmentation'][0]\n                    normalized_polygon = [format(coord / img_w if i % 2 == 0 else coord / img_h, '.6f') for i, coord in enumerate(polygon)]\n                    file_object.write(f\"{current_category} \" + \" \".join(normalized_polygon) + \"\\n\")\n","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:00:31.704543Z","iopub.execute_input":"2024-01-09T19:00:31.704991Z","iopub.status.idle":"2024-01-09T19:00:31.719999Z","shell.execute_reply.started":"2024-01-09T19:00:31.704956Z","shell.execute_reply":"2024-01-09T19:00:31.718368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_images_path = '/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/images'\ninput_json_path = '/kaggle/working/kidney1_dense_coco.json'\noutput_labels_path = 'kidney1_dense_yolo/labels'\noutput_images_path = 'kidney1_dense_yolo/images'","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:00:33.069888Z","iopub.execute_input":"2024-01-09T19:00:33.070372Z","iopub.status.idle":"2024-01-09T19:00:33.077146Z","shell.execute_reply.started":"2024-01-09T19:00:33.070335Z","shell.execute_reply":"2024-01-09T19:00:33.075822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_input_images_path = '/kaggle/input/blood-vessel-segmentation/train/kidney_3_sparse/images'\nval_input_json_path = '/kaggle/working/kidney3_sparse_coco.json'\nval_output_labels_path = 'kidney3_sparse_yolo/labels'\nval_output_images_path = 'kidney3_sparse_yolo/images'","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:00:33.324774Z","iopub.execute_input":"2024-01-09T19:00:33.325524Z","iopub.status.idle":"2024-01-09T19:00:33.331285Z","shell.execute_reply.started":"2024-01-09T19:00:33.325487Z","shell.execute_reply":"2024-01-09T19:00:33.329918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"convert_to_yolo(input_images_path, input_json_path, output_images_path, output_labels_path)\nconvert_to_yolo(val_input_images_path, val_input_json_path, val_output_images_path, val_output_labels_path)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:01:01.468652Z","iopub.execute_input":"2024-01-09T19:01:01.469128Z","iopub.status.idle":"2024-01-09T19:06:46.635965Z","shell.execute_reply.started":"2024-01-09T19:01:01.469094Z","shell.execute_reply":"2024-01-09T19:06:46.634105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to create a YAML file for the dataset\ndef create_yaml(input_json_path, output_yaml_path, train_path, val_path, test_path=None):\n    with open(input_json_path) as f:\n        data = json.load(f)\n    \n    # Extract the category names\n    names = [category['name'] for category in data['categories']]\n    \n    # Number of classes\n    nc = len(names)\n\n    # Create a dictionary with the required content\n    yaml_data = {\n        'names': names,\n        'nc': nc,\n        'test': test_path if test_path else '',\n        'train': train_path,\n        'val': val_path\n    }\n\n    # Write the dictionary to a YAML file\n    with open(output_yaml_path, 'w') as file:\n        yaml.dump(yaml_data, file, default_flow_style=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:06:46.639335Z","iopub.execute_input":"2024-01-09T19:06:46.639957Z","iopub.status.idle":"2024-01-09T19:06:46.651218Z","shell.execute_reply.started":"2024-01-09T19:06:46.639904Z","shell.execute_reply":"2024-01-09T19:06:46.649697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/working/kidney1_dense_yolo/images'\nval_path = '/kaggle/working/kidney3_sparse_yolo/images'","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:06:46.653231Z","iopub.execute_input":"2024-01-09T19:06:46.656311Z","iopub.status.idle":"2024-01-09T19:06:46.665930Z","shell.execute_reply.started":"2024-01-09T19:06:46.656239Z","shell.execute_reply":"2024-01-09T19:06:46.664621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_yaml(input_json_path, 'kidney_1_dense.yaml', train_path=train_path, val_path=val_path)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:06:46.670895Z","iopub.execute_input":"2024-01-09T19:06:46.671332Z","iopub.status.idle":"2024-01-09T19:06:52.606199Z","shell.execute_reply.started":"2024-01-09T19:06:46.671298Z","shell.execute_reply":"2024-01-09T19:06:52.604621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat kidney_1_dense.yaml","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:06:52.607853Z","iopub.execute_input":"2024-01-09T19:06:52.608235Z","iopub.status.idle":"2024-01-09T19:06:53.795453Z","shell.execute_reply.started":"2024-01-09T19:06:52.608202Z","shell.execute_reply":"2024-01-09T19:06:53.794451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nimport cv2\n\ndef display_image_with_annotations(image_path, annotation_path, colors=None):\n    # Load image using OpenCV and convert it from BGR to RGB color space\n    image = cv2.imread(image_path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    img_h, img_w, _ = image.shape\n    \n    # Create a figure and axis to display the image\n    fig, ax = plt.subplots(1)\n    ax.imshow(image)\n    ax.axis('off')  # Turn off the axes\n\n    # Define a default color map if none is provided\n    if colors is None:\n        colors = plt.cm.get_cmap('tab10')\n\n    # Open the annotation file and process each line\n    with open(annotation_path, 'r') as file:\n        for line in file:\n            parts = line.strip().split()\n            category_id = int(parts[0])\n            # Choose color based on category ID, looping through color map if more than 10 categories\n            color = colors(category_id % 10)\n            # Extract normalized polygon coordinates and denormalize them\n            polygon = [float(coord) for coord in parts[1:]]\n            polygon = [coord * img_w if i % 2 == 0 else coord * img_h for i, coord in enumerate(polygon)]\n            # Reshape into (num_points, 2) array\n            polygon = [(polygon[i], polygon[i+1]) for i in range(0, len(polygon), 2)]\n            # Create a Polygon patch using the denormalized coordinates\n            patch = patches.Polygon(polygon, closed=True, edgecolor=color, fill=False)\n            # Add the patch to the plot to display the annotated region\n            ax.add_patch(patch)\n\n    plt.show()  # Display the image with annotations","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:27:50.665622Z","iopub.execute_input":"2024-01-09T19:27:50.669808Z","iopub.status.idle":"2024-01-09T19:27:50.692018Z","shell.execute_reply.started":"2024-01-09T19:27:50.669646Z","shell.execute_reply":"2024-01-09T19:27:50.690420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = \"/kaggle/working/kidney3_sparse_yolo/images/0312.tif\"\nannotation_path = \"/kaggle/working/kidney3_sparse_yolo/labels/0312.txt\"\ndisplay_image_with_annotations(image_path, annotation_path)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:28:28.209786Z","iopub.execute_input":"2024-01-09T19:28:28.210276Z","iopub.status.idle":"2024-01-09T19:28:29.113811Z","shell.execute_reply.started":"2024-01-09T19:28:28.210241Z","shell.execute_reply":"2024-01-09T19:28:29.112110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train YOLOv8 on the custom data","metadata":{}},{"cell_type":"code","source":"# Install the ultralytics package using pip\n!pip install ultralytics==8.0.186\n!pip install wandb\n!pip install -U ipywidgets","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-01-09T19:46:15.186263Z","iopub.execute_input":"2024-01-09T19:46:15.186728Z","iopub.status.idle":"2024-01-09T19:46:42.511841Z","shell.execute_reply.started":"2024-01-09T19:46:15.186701Z","shell.execute_reply":"2024-01-09T19:46:42.510742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb \nwandb.login()","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:46:42.513879Z","iopub.execute_input":"2024-01-09T19:46:42.514206Z","iopub.status.idle":"2024-01-09T19:54:58.731119Z","shell.execute_reply.started":"2024-01-09T19:46:42.514180Z","shell.execute_reply":"2024-01-09T19:54:58.730118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO\nfrom matplotlib import pyplot as plt\nfrom PIL import Image\n\nfrom wandb.integration.ultralytics import add_wandb_callback","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:54:59.392796Z","iopub.execute_input":"2024-01-09T19:54:59.393272Z","iopub.status.idle":"2024-01-09T19:55:05.569271Z","shell.execute_reply.started":"2024-01-09T19:54:59.393244Z","shell.execute_reply":"2024-01-09T19:55:05.568552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Import a model and populate it with pre-trained weights.","metadata":{}},{"cell_type":"code","source":"#Instance\nmodel = YOLO('yolov8n-seg.yaml')  # build a new model from YAML\nmodel = YOLO('yolov8n-seg.pt')  # Transfer the weights from a pretrained model (recommended for training)\nadd_wandb_callback(model, enable_model_checkpointing=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:55:05.570811Z","iopub.execute_input":"2024-01-09T19:55:05.571227Z","iopub.status.idle":"2024-01-09T19:55:06.284180Z","shell.execute_reply.started":"2024-01-09T19:55:05.571201Z","shell.execute_reply":"2024-01-09T19:55:06.283303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat /kaggle/working/kidney_1_dense.yaml","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:55:12.627027Z","iopub.execute_input":"2024-01-09T19:55:12.627375Z","iopub.status.idle":"2024-01-09T19:55:13.593493Z","shell.execute_reply.started":"2024-01-09T19:55:12.627347Z","shell.execute_reply":"2024-01-09T19:55:13.592250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train the model","metadata":{}},{"cell_type":"code","source":"project = 'hacking_human_vasculature'\nname = 'kidney_1_dense_train_kidney_3_sparse_val'","metadata":{"execution":{"iopub.status.busy":"2024-01-09T19:55:22.212663Z","iopub.execute_input":"2024-01-09T19:55:22.213446Z","iopub.status.idle":"2024-01-09T19:55:22.217919Z","shell.execute_reply.started":"2024-01-09T19:55:22.213410Z","shell.execute_reply":"2024-01-09T19:55:22.216987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nresults = model.train(data='/kaggle/working/kidney_1_dense.yaml',\n                      project=project,\n                      name=name,\n                      epochs=10,\n                      patience=0, #I am setting patience=0 to disable early stopping.\n                      batch=4,\n                      imgsz=800,\n#                       device=[0, 1]\n                     )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Finish the W&B run\nwandb.finish()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Image\n","metadata":{"execution":{"iopub.status.busy":"2024-01-09T21:05:19.494719Z","iopub.execute_input":"2024-01-09T21:05:19.495145Z","iopub.status.idle":"2024-01-09T21:05:19.501271Z","shell.execute_reply.started":"2024-01-09T21:05:19.495115Z","shell.execute_reply":"2024-01-09T21:05:19.499960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}