From 77751a57b587632ad2f90dbb82d1e375be768180 Mon Sep 17 00:00:00 2001 From: Hamza Ahmouny Date: Thu, 28 Mar 2024 11:20:00 +0000 Subject: [PATCH 1/3] Added yolov9 compatibility --- sahi/auto_model.py | 1 + sahi/models/yolov9.py | 163 ++++++++++++++++++++++++++++++++++++++++++ sahi/utils/yolov9.py | 38 ++++++++++ 3 files changed, 202 insertions(+) create mode 100644 sahi/models/yolov9.py create mode 100644 sahi/utils/yolov9.py diff --git a/sahi/auto_model.py b/sahi/auto_model.py index 7970dd056..870085f88 100644 --- a/sahi/auto_model.py +++ b/sahi/auto_model.py @@ -4,6 +4,7 @@ MODEL_TYPE_TO_MODEL_CLASS_NAME = { "yolov8": "Yolov8DetectionModel", + "yolov9": "Yolov9DetectionModel", "mmdet": "MmdetDetectionModel", "yolov5": "Yolov5DetectionModel", "detectron2": "Detectron2DetectionModel", diff --git a/sahi/models/yolov9.py b/sahi/models/yolov9.py new file mode 100644 index 000000000..9c1686626 --- /dev/null +++ b/sahi/models/yolov9.py @@ -0,0 +1,163 @@ +# OBSS SAHI Tool +# Code written by AnNT, 2023 and adapted by Hamzalopode 2024. + +import logging +from typing import Any, Dict, List, Optional + +import numpy as np + +logger = logging.getLogger(__name__) + +from sahi.models.base import DetectionModel +from sahi.prediction import ObjectPrediction +from sahi.utils.compatibility import fix_full_shape_list, fix_shift_amount_list +from sahi.utils.import_utils import check_requirements + + +class Yolov9DetectionModel(DetectionModel): + def check_dependencies(self) -> None: + check_requirements(["ultralytics"]) + + def load_model(self): + """ + Detection model is initialized and set to self.model. + """ + + from ultralytics import YOLO + + try: + model = YOLO(self.model_path) + model.to(self.device) + self.set_model(model) + except Exception as e: + raise TypeError("model_path is not a valid yolov9 model path: ", e) + + def set_model(self, model: Any): + """ + Sets the underlying YOLOv9 model. + Args: + model: Any + A YOLOv9 model + """ + + self.model = model + + # set category_mapping + if not self.category_mapping: + category_mapping = {str(ind): category_name for ind, category_name in enumerate(self.category_names)} + self.category_mapping = category_mapping + + def perform_inference(self, image: np.ndarray): + """ + Prediction is performed using self.model and the prediction result is set to self._original_predictions. + Args: + image: np.ndarray + A numpy array that contains the image to be predicted. 3 channel image should be in RGB order. + """ + + # Confirm model is loaded + if self.model is None: + raise ValueError("Model is not loaded, load it by calling .load_model()") + if self.image_size is not None: # ADDED IMAGE SIZE OPTION FOR YOLOV9 MODELS: + prediction_result = self.model( + image[:, :, ::-1], imgsz=self.image_size, verbose=False, device=self.device + ) # YOLOv9 expects numpy arrays to have BGR + else: + prediction_result = self.model( + image[:, :, ::-1], verbose=False, device=self.device + ) # YOLOv9 expects numpy arrays to have BGR + prediction_result = [ + result.boxes.data[result.boxes.data[:, 4] >= self.confidence_threshold] for result in prediction_result + ] + + self._original_predictions = prediction_result + + @property + def category_names(self): + return self.model.names.values() + + @property + def num_categories(self): + """ + Returns number of categories + """ + return len(self.model.names) + + @property + def has_mask(self): + """ + Returns if model output contains segmentation mask + """ + return False # fix when yolov5 supports segmentation models + + def _create_object_prediction_list_from_original_predictions( + self, + shift_amount_list: Optional[List[List[int]]] = [[0, 0]], + full_shape_list: Optional[List[List[int]]] = None, + ): + """ + self._original_predictions is converted to a list of prediction.ObjectPrediction and set to + self._object_prediction_list_per_image. + Args: + shift_amount_list: list of list + To shift the box and mask predictions from sliced image to full sized image, should + be in the form of List[[shift_x, shift_y],[shift_x, shift_y],...] + full_shape_list: list of list + Size of the full image after shifting, should be in the form of + List[[height, width],[height, width],...] + """ + original_predictions = self._original_predictions + + # compatilibty for sahi v0.8.15 + shift_amount_list = fix_shift_amount_list(shift_amount_list) + full_shape_list = fix_full_shape_list(full_shape_list) + + # handle all predictions + object_prediction_list_per_image = [] + for image_ind, image_predictions_in_xyxy_format in enumerate(original_predictions): + shift_amount = shift_amount_list[image_ind] + full_shape = None if full_shape_list is None else full_shape_list[image_ind] + object_prediction_list = [] + + # process predictions + for prediction in image_predictions_in_xyxy_format.cpu().detach().numpy(): + x1 = prediction[0] + y1 = prediction[1] + x2 = prediction[2] + y2 = prediction[3] + bbox = [x1, y1, x2, y2] + score = prediction[4] + category_id = int(prediction[5]) + category_name = self.category_mapping[str(category_id)] + + # fix negative box coords + bbox[0] = max(0, bbox[0]) + bbox[1] = max(0, bbox[1]) + bbox[2] = max(0, bbox[2]) + bbox[3] = max(0, bbox[3]) + + # fix out of image box coords + if full_shape is not None: + bbox[0] = min(full_shape[1], bbox[0]) + bbox[1] = min(full_shape[0], bbox[1]) + bbox[2] = min(full_shape[1], bbox[2]) + bbox[3] = min(full_shape[0], bbox[3]) + + # ignore invalid predictions + if not (bbox[0] < bbox[2]) or not (bbox[1] < bbox[3]): + logger.warning(f"ignoring invalid prediction with bbox: {bbox}") + continue + + object_prediction = ObjectPrediction( + bbox=bbox, + category_id=category_id, + score=score, + bool_mask=None, + category_name=category_name, + shift_amount=shift_amount, + full_shape=full_shape, + ) + object_prediction_list.append(object_prediction) + object_prediction_list_per_image.append(object_prediction_list) + + self._object_prediction_list_per_image = object_prediction_list_per_image diff --git a/sahi/utils/yolov9.py b/sahi/utils/yolov9.py new file mode 100644 index 000000000..e52926687 --- /dev/null +++ b/sahi/utils/yolov9.py @@ -0,0 +1,38 @@ +import urllib.request +from os import path +from pathlib import Path +from typing import Optional + + +class Yolov9TestConstants: + YOLOV9C_MODEL_URL = "https://github.com/ultralytics/assets/releases/download/v8.1.0/yolov9c.pt" + YOLOV9C_MODEL_PATH = "tests/data/models/yolov9/yolov9c.pt" + + YOLOV9E_MODEL_URL = "https://github.com/ultralytics/assets/releases/download/v8.1.0/yolov9e.pt" + YOLOV9E_MODEL_PATH = "tests/data/models/yolov9/yolov9e.pt" + + + +def download_yolov9c_model(destination_path: Optional[str] = None): + if destination_path is None: + destination_path = Yolov9TestConstants.YOLOV9C_MODEL_PATH + + Path(destination_path).parent.mkdir(parents=True, exist_ok=True) + + if not path.exists(destination_path): + urllib.request.urlretrieve( + Yolov9TestConstants.YOLOV9C_MODEL_URL, + destination_path, + ) + +def download_yolov9e_model(destination_path: Optional[str] = None): + if destination_path is None: + destination_path = Yolov9TestConstants.YOLOV9E_MODEL_PATH + + Path(destination_path).parent.mkdir(parents=True, exist_ok=True) + + if not path.exists(destination_path): + urllib.request.urlretrieve( + Yolov9TestConstants.YOLOV9E_MODEL_URL, + destination_path, + ) From 971acda0fcd2b4d56e34e548576d2e9119ab159d Mon Sep 17 00:00:00 2001 From: Hamza Ahmouny Date: Mon, 8 Apr 2024 16:18:49 +0000 Subject: [PATCH 2/3] Fixed code style errors raised in tests. - fixed Demo/*.ipynb - fixed yolov9.py --- demo/inference_for_detectron2.ipynb | 24 +++--- demo/inference_for_huggingface.ipynb | 22 +++-- demo/inference_for_mmdetection.ipynb | 31 ++++--- demo/inference_for_sparse_yolov5.ipynb | 27 ++++--- demo/inference_for_torchvision.ipynb | 28 ++++--- demo/inference_for_yolonas.ipynb | 32 +++++--- demo/inference_for_yolov5.ipynb | 26 +++--- demo/inference_for_yolov8.ipynb | 24 +++--- demo/inference_for_yolov8_onnx.ipynb | 107 ++++++++++++++++++++++--- demo/slicing.ipynb | 21 ++--- sahi/utils/yolov9.py | 2 +- 11 files changed, 241 insertions(+), 103 deletions(-) diff --git a/demo/inference_for_detectron2.ipynb b/demo/inference_for_detectron2.ipynb index f39e7be4a..31c622cc8 100644 --- a/demo/inference_for_detectron2.ipynb +++ b/demo/inference_for_detectron2.ipynb @@ -92,6 +92,7 @@ ], "source": [ "import os\n", + "\n", "os.getcwd()" ] }, @@ -157,8 +158,13 @@ "model_path = Detectron2TestConstants.FASTERCNN_MODEL_ZOO_NAME\n", "\n", "# download test images into demo_data folder\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", + " \"demo_data/small-vehicles1.jpeg\",\n", + ")\n", + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", + ")" ] }, { @@ -213,12 +219,12 @@ ], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type='detectron2',\n", + " model_type=\"detectron2\",\n", " model_path=model_path,\n", " config_path=model_path,\n", " confidence_threshold=0.5,\n", " image_size=640,\n", - " device=\"cpu\", # or 'cuda:0'\n", + " device=\"cpu\", # or 'cuda:0'\n", ")" ] }, @@ -394,10 +400,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height = 256,\n", - " slice_width = 256,\n", - " overlap_height_ratio = 0.2,\n", - " overlap_width_ratio = 0.2,\n", + " slice_height=256,\n", + " slice_width=256,\n", + " overlap_height_ratio=0.2,\n", + " overlap_width_ratio=0.2,\n", ")" ] }, @@ -775,7 +781,7 @@ "model_type = \"detectron2\"\n", "model_path = model_path\n", "model_config_path = model_path\n", - "model_device = \"cpu\" # or 'cuda:0'\n", + "model_device = \"cpu\" # or 'cuda:0'\n", "model_confidence_threshold = 0.5\n", "\n", "slice_height = 480\n", diff --git a/demo/inference_for_huggingface.ipynb b/demo/inference_for_huggingface.ipynb index 9addaf742..986ff9db7 100644 --- a/demo/inference_for_huggingface.ipynb +++ b/demo/inference_for_huggingface.ipynb @@ -80,6 +80,7 @@ "outputs": [], "source": [ "import os\n", + "\n", "os.getcwd()" ] }, @@ -150,8 +151,13 @@ "model_path = \"facebook/detr-resnet-50\"\n", "\n", "# download test images into demo_data folder\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", + " \"demo_data/small-vehicles1.jpeg\",\n", + ")\n", + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", + ")" ] }, { @@ -246,12 +252,12 @@ ], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type='huggingface',\n", + " model_type=\"huggingface\",\n", " model_path=model_path,\n", " config_path=model_path,\n", " confidence_threshold=0.5,\n", " image_size=640,\n", - " device=\"cpu\", # or 'cuda'\n", + " device=\"cpu\", # or 'cuda'\n", ")" ] }, @@ -429,10 +435,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height = 512,\n", - " slice_width = 512,\n", - " overlap_height_ratio = 0.2,\n", - " overlap_width_ratio = 0.2,\n", + " slice_height=512,\n", + " slice_width=512,\n", + " overlap_height_ratio=0.2,\n", + " overlap_width_ratio=0.2,\n", ")" ] }, diff --git a/demo/inference_for_mmdetection.ipynb b/demo/inference_for_mmdetection.ipynb index e5fd961c8..0ab2b016a 100644 --- a/demo/inference_for_mmdetection.ipynb +++ b/demo/inference_for_mmdetection.ipynb @@ -37,6 +37,7 @@ "outputs": [], "source": [ "import os\n", + "\n", "os.getcwd()" ] }, @@ -81,13 +82,21 @@ "outputs": [], "source": [ "# download cascade mask rcnn model&config\n", - "model_path = 'models/cascade_mask_rcnn.pth'\n", + "model_path = \"models/cascade_mask_rcnn.pth\"\n", "download_mmdet_cascade_mask_rcnn_model(model_path)\n", - "config_path = download_mmdet_config(model_name=\"cascade_rcnn\", config_file_name=\"cascade-mask-rcnn_r50_fpn_1x_coco.py\",)\n", + "config_path = download_mmdet_config(\n", + " model_name=\"cascade_rcnn\",\n", + " config_file_name=\"cascade-mask-rcnn_r50_fpn_1x_coco.py\",\n", + ")\n", "\n", "# download test images into demo_data folder\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", + " \"demo_data/small-vehicles1.jpeg\",\n", + ")\n", + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", + ")" ] }, { @@ -119,12 +128,12 @@ ], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type='mmdet',\n", + " model_type=\"mmdet\",\n", " model_path=model_path,\n", " config_path=config_path,\n", " confidence_threshold=0.4,\n", " image_size=640,\n", - " device=\"cpu\", # or 'cuda:0'\n", + " device=\"cpu\", # or 'cuda:0'\n", ")" ] }, @@ -221,10 +230,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height = 320,\n", - " slice_width = 320,\n", - " overlap_height_ratio = 0.2,\n", - " overlap_width_ratio = 0.2\n", + " slice_height=320,\n", + " slice_width=320,\n", + " overlap_height_ratio=0.2,\n", + " overlap_width_ratio=0.2,\n", ")" ] }, @@ -636,7 +645,7 @@ "model_type = \"mmdet\"\n", "model_path = model_path\n", "model_config_path = config_path\n", - "model_device = \"cpu\" # or 'cuda:0'\n", + "model_device = \"cpu\" # or 'cuda:0'\n", "model_confidence_threshold = 0.4\n", "\n", "slice_height = 640\n", diff --git a/demo/inference_for_sparse_yolov5.ipynb b/demo/inference_for_sparse_yolov5.ipynb index ddf3285b9..900729935 100644 --- a/demo/inference_for_sparse_yolov5.ipynb +++ b/demo/inference_for_sparse_yolov5.ipynb @@ -128,8 +128,13 @@ "outputs": [], "source": [ "# download test images into demo_data folder\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", + " \"demo_data/small-vehicles1.jpeg\",\n", + ")\n", + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", + ")" ] }, { @@ -160,11 +165,11 @@ "source": [ "model_path = \"zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned-aggressive_96\"\n", "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type='yolov5sparse',\n", + " model_type=\"yolov5sparse\",\n", " confidence_threshold=0.3,\n", - " image_size = 640,\n", - " model_path = model_path ,\n", - " device=\"cpu\", # or 'cuda:0'\n", + " image_size=640,\n", + " model_path=model_path,\n", + " device=\"cpu\", # or 'cuda:0'\n", ")" ] }, @@ -324,10 +329,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height = 256,\n", - " slice_width = 256,\n", - " overlap_height_ratio = 0.2,\n", - " overlap_width_ratio = 0.2\n", + " slice_height=256,\n", + " slice_width=256,\n", + " overlap_height_ratio=0.2,\n", + " overlap_width_ratio=0.2,\n", ")" ] }, @@ -718,7 +723,7 @@ "outputs": [], "source": [ "model_type = \"yolov5sparse\"\n", - "model_device = \"cpu\" # or 'cuda:0'\n", + "model_device = \"cpu\" # or 'cuda:0'\n", "model_confidence_threshold = 0.4\n", "\n", "slice_height = 256\n", diff --git a/demo/inference_for_torchvision.ipynb b/demo/inference_for_torchvision.ipynb index 719d639ba..61290e86e 100644 --- a/demo/inference_for_torchvision.ipynb +++ b/demo/inference_for_torchvision.ipynb @@ -39,6 +39,7 @@ "outputs": [], "source": [ "import os\n", + "\n", "os.getcwd()" ] }, @@ -92,8 +93,13 @@ "model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=FasterRCNN_ResNet50_FPN_Weights.DEFAULT)\n", "\n", "# download test images into demo_data folder\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", + " \"demo_data/small-vehicles1.jpeg\",\n", + ")\n", + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", + ")" ] }, { @@ -133,13 +139,13 @@ "outputs": [], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type='torchvision',\n", + " model_type=\"torchvision\",\n", " model=model,\n", " confidence_threshold=0.5,\n", " image_size=640,\n", - " device=\"cpu\", # or \"cuda:0\"\n", + " device=\"cpu\", # or \"cuda:0\"\n", " load_at_init=True,\n", - ")\n" + ")" ] }, { @@ -302,10 +308,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height = 320,\n", - " slice_width = 320,\n", - " overlap_height_ratio = 0.2,\n", - " overlap_width_ratio = 0.2,\n", + " slice_height=320,\n", + " slice_width=320,\n", + " overlap_height_ratio=0.2,\n", + " overlap_width_ratio=0.2,\n", ")" ] }, @@ -665,11 +671,11 @@ "source": [ "model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)\n", "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type='torchvision',\n", + " model_type=\"torchvision\",\n", " model=model,\n", " confidence_threshold=0.4,\n", " image_size=640,\n", - " device=\"cpu\", # or \"cuda:0\"\n", + " device=\"cpu\", # or \"cuda:0\"\n", " load_at_init=True,\n", ")\n", "\n", diff --git a/demo/inference_for_yolonas.ipynb b/demo/inference_for_yolonas.ipynb index 87986b30d..0b68a7b5d 100644 --- a/demo/inference_for_yolonas.ipynb +++ b/demo/inference_for_yolonas.ipynb @@ -53,6 +53,7 @@ ], "source": [ "import os\n", + "\n", "os.getcwd()" ] }, @@ -128,8 +129,13 @@ "download_yolonas_s_model(yolonas_model_path)\n", "\n", "# download test images into demo_data folder\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", + " \"demo_data/small-vehicles1.jpeg\",\n", + ")\n", + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", + ")" ] }, { @@ -183,12 +189,12 @@ ], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type='yolonas',\n", + " model_type=\"yolonas\",\n", " model_name=\"yolo_nas_s\",\n", - " class_names_yaml_path=\"tests/data/coco_utils/coco_class_names.yaml\", # required if model_path is provided\n", - " model_path=yolonas_model_path, # If model_path is not provided, model is downloaded from Deci-AI remote client automatically\n", + " class_names_yaml_path=\"tests/data/coco_utils/coco_class_names.yaml\", # required if model_path is provided\n", + " model_path=yolonas_model_path, # If model_path is not provided, model is downloaded from Deci-AI remote client automatically\n", " confidence_threshold=0.3,\n", - " device=\"cpu\", # or 'cuda:0'\n", + " device=\"cpu\", # or 'cuda:0'\n", ")" ] }, @@ -290,10 +296,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height = 256,\n", - " slice_width = 256,\n", - " overlap_height_ratio = 0.2,\n", - " overlap_width_ratio = 0.2\n", + " slice_height=256,\n", + " slice_width=256,\n", + " overlap_height_ratio=0.2,\n", + " overlap_width_ratio=0.2,\n", ")" ] }, @@ -575,9 +581,9 @@ "outputs": [], "source": [ "model_type = \"yolonas\"\n", - "model_name=\"yolo_nas_s\"\n", + "model_name = \"yolo_nas_s\"\n", "model_path = yolonas_model_path\n", - "model_device = \"cpu\" # or 'cuda:0'\n", + "model_device = \"cpu\" # or 'cuda:0'\n", "model_confidence_threshold = 0.4\n", "class_names_yaml_path = \"tests/data/coco_utils/coco_class_names.yaml\"\n", "\n", @@ -676,7 +682,7 @@ " model_type=model_type,\n", " model_name=model_name,\n", " model_path=model_path,\n", - " class_names_yaml_path = class_names_yaml_path,\n", + " class_names_yaml_path=class_names_yaml_path,\n", " model_device=model_device,\n", " model_confidence_threshold=model_confidence_threshold,\n", " source=source_image_dir,\n", diff --git a/demo/inference_for_yolov5.ipynb b/demo/inference_for_yolov5.ipynb index 13f744b86..4f2c4f3a3 100644 --- a/demo/inference_for_yolov5.ipynb +++ b/demo/inference_for_yolov5.ipynb @@ -37,6 +37,7 @@ "outputs": [], "source": [ "import os\n", + "\n", "os.getcwd()" ] }, @@ -80,12 +81,17 @@ "outputs": [], "source": [ "# download YOLOV5S6 model to 'models/yolov5s6.pt'\n", - "yolov5_model_path = 'models/yolov5s6.pt'\n", + "yolov5_model_path = \"models/yolov5s6.pt\"\n", "download_yolov5s6_model(destination_path=yolov5_model_path)\n", "\n", "# download test images into demo_data folder\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", + " \"demo_data/small-vehicles1.jpeg\",\n", + ")\n", + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", + ")" ] }, { @@ -109,10 +115,10 @@ "outputs": [], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type='yolov5',\n", + " model_type=\"yolov5\",\n", " model_path=yolov5_model_path,\n", " confidence_threshold=0.3,\n", - " device=\"cpu\", # or 'cuda:0'\n", + " device=\"cpu\", # or 'cuda:0'\n", ")" ] }, @@ -209,10 +215,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height = 256,\n", - " slice_width = 256,\n", - " overlap_height_ratio = 0.2,\n", - " overlap_width_ratio = 0.2\n", + " slice_height=256,\n", + " slice_width=256,\n", + " overlap_height_ratio=0.2,\n", + " overlap_width_ratio=0.2,\n", ")" ] }, @@ -515,7 +521,7 @@ "source": [ "model_type = \"yolov5\"\n", "model_path = yolov5_model_path\n", - "model_device = \"cpu\" # or 'cuda:0'\n", + "model_device = \"cpu\" # or 'cuda:0'\n", "model_confidence_threshold = 0.4\n", "\n", "slice_height = 256\n", diff --git a/demo/inference_for_yolov8.ipynb b/demo/inference_for_yolov8.ipynb index f339ba3b5..bf145d8b2 100644 --- a/demo/inference_for_yolov8.ipynb +++ b/demo/inference_for_yolov8.ipynb @@ -49,6 +49,7 @@ ], "source": [ "import os\n", + "\n", "os.getcwd()" ] }, @@ -96,8 +97,13 @@ "download_yolov8s_model(yolov8_model_path)\n", "\n", "# download test images into demo_data folder\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", + " \"demo_data/small-vehicles1.jpeg\",\n", + ")\n", + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", + ")" ] }, { @@ -122,10 +128,10 @@ "outputs": [], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type='yolov8',\n", + " model_type=\"yolov8\",\n", " model_path=yolov8_model_path,\n", " confidence_threshold=0.3,\n", - " device=\"cpu\", # or 'cuda:0'\n", + " device=\"cpu\", # or 'cuda:0'\n", ")" ] }, @@ -234,10 +240,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height = 256,\n", - " slice_width = 256,\n", - " overlap_height_ratio = 0.2,\n", - " overlap_width_ratio = 0.2\n", + " slice_height=256,\n", + " slice_width=256,\n", + " overlap_height_ratio=0.2,\n", + " overlap_width_ratio=0.2,\n", ")" ] }, @@ -485,7 +491,7 @@ "source": [ "model_type = \"yolov8\"\n", "model_path = yolov8_model_path\n", - "model_device = \"cpu\" # or 'cuda:0'\n", + "model_device = \"cpu\" # or 'cuda:0'\n", "model_confidence_threshold = 0.4\n", "\n", "slice_height = 256\n", diff --git a/demo/inference_for_yolov8_onnx.ipynb b/demo/inference_for_yolov8_onnx.ipynb index ee7878fcb..eab72561d 100644 --- a/demo/inference_for_yolov8_onnx.ipynb +++ b/demo/inference_for_yolov8_onnx.ipynb @@ -48,6 +48,7 @@ ], "source": [ "import os\n", + "\n", "os.getcwd()" ] }, @@ -91,8 +92,13 @@ "download_yolov8n_onnx_model(yolov8_onnx_model_path)\n", "\n", "# download test images into demo_data folder\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", - "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", + " \"demo_data/small-vehicles1.jpeg\",\n", + ")\n", + "download_from_url(\n", + " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", + ")" ] }, { @@ -115,7 +121,88 @@ "metadata": {}, "outputs": [], "source": [ - "category_mapping = {'0': 'person','1': 'bicycle','2': 'car','3': 'motorcycle','4': 'airplane','5': 'bus','6': 'train','7': 'truck','8': 'boat','9': 'traffic light','10': 'fire hydrant','11': 'stop sign','12': 'parking meter','13': 'bench','14': 'bird','15': 'cat','16': 'dog','17': 'horse','18': 'sheep','19': 'cow','20': 'elephant','21': 'bear','22': 'zebra','23': 'giraffe','24': 'backpack','25': 'umbrella','26': 'handbag','27': 'tie','28': 'suitcase','29': 'frisbee','30': 'skis','31': 'snowboard','32': 'sports ball','33': 'kite','34': 'baseball bat','35': 'baseball glove','36': 'skateboard','37': 'surfboard','38': 'tennis racket','39': 'bottle','40': 'wine glass','41': 'cup','42': 'fork','43': 'knife','44': 'spoon','45': 'bowl','46': 'banana','47': 'apple','48': 'sandwich','49': 'orange','50': 'broccoli','51': 'carrot','52': 'hot dog','53': 'pizza','54': 'donut','55': 'cake','56': 'chair','57': 'couch','58': 'potted plant','59': 'bed','60': 'dining table','61': 'toilet','62': 'tv','63': 'laptop','64': 'mouse','65': 'remote','66': 'keyboard','67': 'cell phone','68': 'microwave','69': 'oven','70': 'toaster','71': 'sink','72': 'refrigerator','73': 'book','74': 'clock','75': 'vase','76': 'scissors','77': 'teddy bear','78': 'hair drier','79': 'toothbrush'}" + "category_mapping = {\n", + " \"0\": \"person\",\n", + " \"1\": \"bicycle\",\n", + " \"2\": \"car\",\n", + " \"3\": \"motorcycle\",\n", + " \"4\": \"airplane\",\n", + " \"5\": \"bus\",\n", + " \"6\": \"train\",\n", + " \"7\": \"truck\",\n", + " \"8\": \"boat\",\n", + " \"9\": \"traffic light\",\n", + " \"10\": \"fire hydrant\",\n", + " \"11\": \"stop sign\",\n", + " \"12\": \"parking meter\",\n", + " \"13\": \"bench\",\n", + " \"14\": \"bird\",\n", + " \"15\": \"cat\",\n", + " \"16\": \"dog\",\n", + " \"17\": \"horse\",\n", + " \"18\": \"sheep\",\n", + " \"19\": \"cow\",\n", + " \"20\": \"elephant\",\n", + " \"21\": \"bear\",\n", + " \"22\": \"zebra\",\n", + " \"23\": \"giraffe\",\n", + " \"24\": \"backpack\",\n", + " \"25\": \"umbrella\",\n", + " \"26\": \"handbag\",\n", + " \"27\": \"tie\",\n", + " \"28\": \"suitcase\",\n", + " \"29\": \"frisbee\",\n", + " \"30\": \"skis\",\n", + " \"31\": \"snowboard\",\n", + " \"32\": \"sports ball\",\n", + " \"33\": \"kite\",\n", + " \"34\": \"baseball bat\",\n", + " \"35\": \"baseball glove\",\n", + " \"36\": \"skateboard\",\n", + " \"37\": \"surfboard\",\n", + " \"38\": \"tennis racket\",\n", + " \"39\": \"bottle\",\n", + " \"40\": \"wine glass\",\n", + " \"41\": \"cup\",\n", + " \"42\": \"fork\",\n", + " \"43\": \"knife\",\n", + " \"44\": \"spoon\",\n", + " \"45\": \"bowl\",\n", + " \"46\": \"banana\",\n", + " \"47\": \"apple\",\n", + " \"48\": \"sandwich\",\n", + " \"49\": \"orange\",\n", + " \"50\": \"broccoli\",\n", + " \"51\": \"carrot\",\n", + " \"52\": \"hot dog\",\n", + " \"53\": \"pizza\",\n", + " \"54\": \"donut\",\n", + " \"55\": \"cake\",\n", + " \"56\": \"chair\",\n", + " \"57\": \"couch\",\n", + " \"58\": \"potted plant\",\n", + " \"59\": \"bed\",\n", + " \"60\": \"dining table\",\n", + " \"61\": \"toilet\",\n", + " \"62\": \"tv\",\n", + " \"63\": \"laptop\",\n", + " \"64\": \"mouse\",\n", + " \"65\": \"remote\",\n", + " \"66\": \"keyboard\",\n", + " \"67\": \"cell phone\",\n", + " \"68\": \"microwave\",\n", + " \"69\": \"oven\",\n", + " \"70\": \"toaster\",\n", + " \"71\": \"sink\",\n", + " \"72\": \"refrigerator\",\n", + " \"73\": \"book\",\n", + " \"74\": \"clock\",\n", + " \"75\": \"vase\",\n", + " \"76\": \"scissors\",\n", + " \"77\": \"teddy bear\",\n", + " \"78\": \"hair drier\",\n", + " \"79\": \"toothbrush\",\n", + "}" ] }, { @@ -125,11 +212,11 @@ "outputs": [], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type='yolov8onnx',\n", + " model_type=\"yolov8onnx\",\n", " model_path=yolov8_onnx_model_path,\n", " confidence_threshold=0.3,\n", " category_mapping=category_mapping,\n", - " device=\"cpu\", # or 'cuda:0'\n", + " device=\"cpu\", # or 'cuda:0'\n", ")" ] }, @@ -226,10 +313,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height = 256,\n", - " slice_width = 256,\n", - " overlap_height_ratio = 0.2,\n", - " overlap_width_ratio = 0.2\n", + " slice_height=256,\n", + " slice_width=256,\n", + " overlap_height_ratio=0.2,\n", + " overlap_width_ratio=0.2,\n", ")" ] }, @@ -477,7 +564,7 @@ "source": [ "model_type = \"yolov8onnx\"\n", "model_path = yolov8_onnx_model_path\n", - "model_device = \"cpu\" # or 'cuda:0'\n", + "model_device = \"cpu\" # or 'cuda:0'\n", "model_confidence_threshold = 0.4\n", "model_category_mapping = category_mapping\n", "\n", diff --git a/demo/slicing.ipynb b/demo/slicing.ipynb index 63de1a1df..d60d6f7d2 100644 --- a/demo/slicing.ipynb +++ b/demo/slicing.ipynb @@ -14,6 +14,7 @@ "outputs": [], "source": [ "import os\n", + "\n", "os.getcwd()" ] }, @@ -85,14 +86,14 @@ "f, axarr = plt.subplots(1, 1, figsize=(12, 12))\n", "# read image\n", "img_ind = 0\n", - "img = Image.open(\"demo_data/\" + coco_dict[\"images\"][img_ind][\"file_name\"]).convert('RGBA')\n", + "img = Image.open(\"demo_data/\" + coco_dict[\"images\"][img_ind][\"file_name\"]).convert(\"RGBA\")\n", "# iterate over all annotations\n", "for ann_ind in range(len(coco_dict[\"annotations\"])):\n", " # convert coco bbox to pil bbox\n", " xywh = coco_dict[\"annotations\"][ann_ind][\"bbox\"]\n", - " xyxy = [xywh[0], xywh[1], xywh[0]+xywh[2], xywh[1]+xywh[3]]\n", + " xyxy = [xywh[0], xywh[1], xywh[0] + xywh[2], xywh[1] + xywh[3]]\n", " # visualize bbox over image\n", - " ImageDraw.Draw(img, 'RGBA').rectangle(xyxy, width=5)\n", + " ImageDraw.Draw(img, \"RGBA\").rectangle(xyxy, width=5)\n", "axarr.imshow(img)" ] }, @@ -166,7 +167,7 @@ " overlap_height_ratio=0.2,\n", " overlap_width_ratio=0.2,\n", " min_area_ratio=0.1,\n", - " verbose=True\n", + " verbose=True,\n", ")" ] }, @@ -196,7 +197,7 @@ } ], "source": [ - "f, axarr = plt.subplots(4, 5, figsize=(13,13))\n", + "f, axarr = plt.subplots(4, 5, figsize=(13, 13))\n", "img_ind = 0\n", "for ind1 in range(4):\n", " for ind2 in range(5):\n", @@ -231,23 +232,23 @@ } ], "source": [ - "f, axarr = plt.subplots(4, 5, figsize=(13,13))\n", + "f, axarr = plt.subplots(4, 5, figsize=(13, 13))\n", "img_ind = 0\n", "for row_ind in range(4):\n", " for column_ind in range(5):\n", " # read image\n", - " img = Image.open(\"demo_data/sliced/\" + coco_dict[\"images\"][img_ind][\"file_name\"]).convert('RGBA')\n", + " img = Image.open(\"demo_data/sliced/\" + coco_dict[\"images\"][img_ind][\"file_name\"]).convert(\"RGBA\")\n", " # iterate over all annotations\n", " for ann_ind in range(len(coco_dict[\"annotations\"])):\n", " # find annotations that belong the selected image\n", " if coco_dict[\"annotations\"][ann_ind][\"image_id\"] == coco_dict[\"images\"][img_ind][\"id\"]:\n", " # convert coco bbox to pil bbox\n", " xywh = coco_dict[\"annotations\"][ann_ind][\"bbox\"]\n", - " xyxy = [xywh[0], xywh[1], xywh[0]+xywh[2], xywh[1]+xywh[3]]\n", + " xyxy = [xywh[0], xywh[1], xywh[0] + xywh[2], xywh[1] + xywh[3]]\n", " # visualize bbox over image\n", - " ImageDraw.Draw(img, 'RGBA').rectangle(xyxy, width=5)\n", + " ImageDraw.Draw(img, \"RGBA\").rectangle(xyxy, width=5)\n", " axarr[row_ind, column_ind].imshow(img)\n", - " img_ind += 1\n" + " img_ind += 1" ] }, { diff --git a/sahi/utils/yolov9.py b/sahi/utils/yolov9.py index e52926687..11f406d4f 100644 --- a/sahi/utils/yolov9.py +++ b/sahi/utils/yolov9.py @@ -12,7 +12,6 @@ class Yolov9TestConstants: YOLOV9E_MODEL_PATH = "tests/data/models/yolov9/yolov9e.pt" - def download_yolov9c_model(destination_path: Optional[str] = None): if destination_path is None: destination_path = Yolov9TestConstants.YOLOV9C_MODEL_PATH @@ -25,6 +24,7 @@ def download_yolov9c_model(destination_path: Optional[str] = None): destination_path, ) + def download_yolov9e_model(destination_path: Optional[str] = None): if destination_path is None: destination_path = Yolov9TestConstants.YOLOV9E_MODEL_PATH From 7aae396b5b1883940b81b5f9b33f6e124aa8b16e Mon Sep 17 00:00:00 2001 From: Hamzalopode <46407388+Hamzalopode@users.noreply.github.com> Date: Tue, 9 Apr 2024 02:34:30 +0100 Subject: [PATCH 3/3] Remove notebook refactor with isort. --- demo/inference_for_detectron2.ipynb | 24 +++--- demo/inference_for_huggingface.ipynb | 22 ++--- demo/inference_for_mmdetection.ipynb | 31 +++---- demo/inference_for_sparse_yolov5.ipynb | 27 +++---- demo/inference_for_torchvision.ipynb | 28 +++---- demo/inference_for_yolonas.ipynb | 32 +++----- demo/inference_for_yolov5.ipynb | 26 +++--- demo/inference_for_yolov8.ipynb | 24 +++--- demo/inference_for_yolov8_onnx.ipynb | 107 +++---------------------- demo/slicing.ipynb | 21 +++-- 10 files changed, 102 insertions(+), 240 deletions(-) diff --git a/demo/inference_for_detectron2.ipynb b/demo/inference_for_detectron2.ipynb index 31c622cc8..f39e7be4a 100644 --- a/demo/inference_for_detectron2.ipynb +++ b/demo/inference_for_detectron2.ipynb @@ -92,7 +92,6 @@ ], "source": [ "import os\n", - "\n", "os.getcwd()" ] }, @@ -158,13 +157,8 @@ "model_path = Detectron2TestConstants.FASTERCNN_MODEL_ZOO_NAME\n", "\n", "# download test images into demo_data folder\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", - " \"demo_data/small-vehicles1.jpeg\",\n", - ")\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", - ")" + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" ] }, { @@ -219,12 +213,12 @@ ], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type=\"detectron2\",\n", + " model_type='detectron2',\n", " model_path=model_path,\n", " config_path=model_path,\n", " confidence_threshold=0.5,\n", " image_size=640,\n", - " device=\"cpu\", # or 'cuda:0'\n", + " device=\"cpu\", # or 'cuda:0'\n", ")" ] }, @@ -400,10 +394,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height=256,\n", - " slice_width=256,\n", - " overlap_height_ratio=0.2,\n", - " overlap_width_ratio=0.2,\n", + " slice_height = 256,\n", + " slice_width = 256,\n", + " overlap_height_ratio = 0.2,\n", + " overlap_width_ratio = 0.2,\n", ")" ] }, @@ -781,7 +775,7 @@ "model_type = \"detectron2\"\n", "model_path = model_path\n", "model_config_path = model_path\n", - "model_device = \"cpu\" # or 'cuda:0'\n", + "model_device = \"cpu\" # or 'cuda:0'\n", "model_confidence_threshold = 0.5\n", "\n", "slice_height = 480\n", diff --git a/demo/inference_for_huggingface.ipynb b/demo/inference_for_huggingface.ipynb index 986ff9db7..9addaf742 100644 --- a/demo/inference_for_huggingface.ipynb +++ b/demo/inference_for_huggingface.ipynb @@ -80,7 +80,6 @@ "outputs": [], "source": [ "import os\n", - "\n", "os.getcwd()" ] }, @@ -151,13 +150,8 @@ "model_path = \"facebook/detr-resnet-50\"\n", "\n", "# download test images into demo_data folder\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", - " \"demo_data/small-vehicles1.jpeg\",\n", - ")\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", - ")" + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" ] }, { @@ -252,12 +246,12 @@ ], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type=\"huggingface\",\n", + " model_type='huggingface',\n", " model_path=model_path,\n", " config_path=model_path,\n", " confidence_threshold=0.5,\n", " image_size=640,\n", - " device=\"cpu\", # or 'cuda'\n", + " device=\"cpu\", # or 'cuda'\n", ")" ] }, @@ -435,10 +429,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height=512,\n", - " slice_width=512,\n", - " overlap_height_ratio=0.2,\n", - " overlap_width_ratio=0.2,\n", + " slice_height = 512,\n", + " slice_width = 512,\n", + " overlap_height_ratio = 0.2,\n", + " overlap_width_ratio = 0.2,\n", ")" ] }, diff --git a/demo/inference_for_mmdetection.ipynb b/demo/inference_for_mmdetection.ipynb index 0ab2b016a..e5fd961c8 100644 --- a/demo/inference_for_mmdetection.ipynb +++ b/demo/inference_for_mmdetection.ipynb @@ -37,7 +37,6 @@ "outputs": [], "source": [ "import os\n", - "\n", "os.getcwd()" ] }, @@ -82,21 +81,13 @@ "outputs": [], "source": [ "# download cascade mask rcnn model&config\n", - "model_path = \"models/cascade_mask_rcnn.pth\"\n", + "model_path = 'models/cascade_mask_rcnn.pth'\n", "download_mmdet_cascade_mask_rcnn_model(model_path)\n", - "config_path = download_mmdet_config(\n", - " model_name=\"cascade_rcnn\",\n", - " config_file_name=\"cascade-mask-rcnn_r50_fpn_1x_coco.py\",\n", - ")\n", + "config_path = download_mmdet_config(model_name=\"cascade_rcnn\", config_file_name=\"cascade-mask-rcnn_r50_fpn_1x_coco.py\",)\n", "\n", "# download test images into demo_data folder\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", - " \"demo_data/small-vehicles1.jpeg\",\n", - ")\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", - ")" + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" ] }, { @@ -128,12 +119,12 @@ ], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type=\"mmdet\",\n", + " model_type='mmdet',\n", " model_path=model_path,\n", " config_path=config_path,\n", " confidence_threshold=0.4,\n", " image_size=640,\n", - " device=\"cpu\", # or 'cuda:0'\n", + " device=\"cpu\", # or 'cuda:0'\n", ")" ] }, @@ -230,10 +221,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height=320,\n", - " slice_width=320,\n", - " overlap_height_ratio=0.2,\n", - " overlap_width_ratio=0.2,\n", + " slice_height = 320,\n", + " slice_width = 320,\n", + " overlap_height_ratio = 0.2,\n", + " overlap_width_ratio = 0.2\n", ")" ] }, @@ -645,7 +636,7 @@ "model_type = \"mmdet\"\n", "model_path = model_path\n", "model_config_path = config_path\n", - "model_device = \"cpu\" # or 'cuda:0'\n", + "model_device = \"cpu\" # or 'cuda:0'\n", "model_confidence_threshold = 0.4\n", "\n", "slice_height = 640\n", diff --git a/demo/inference_for_sparse_yolov5.ipynb b/demo/inference_for_sparse_yolov5.ipynb index 900729935..ddf3285b9 100644 --- a/demo/inference_for_sparse_yolov5.ipynb +++ b/demo/inference_for_sparse_yolov5.ipynb @@ -128,13 +128,8 @@ "outputs": [], "source": [ "# download test images into demo_data folder\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", - " \"demo_data/small-vehicles1.jpeg\",\n", - ")\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", - ")" + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" ] }, { @@ -165,11 +160,11 @@ "source": [ "model_path = \"zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned-aggressive_96\"\n", "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type=\"yolov5sparse\",\n", + " model_type='yolov5sparse',\n", " confidence_threshold=0.3,\n", - " image_size=640,\n", - " model_path=model_path,\n", - " device=\"cpu\", # or 'cuda:0'\n", + " image_size = 640,\n", + " model_path = model_path ,\n", + " device=\"cpu\", # or 'cuda:0'\n", ")" ] }, @@ -329,10 +324,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height=256,\n", - " slice_width=256,\n", - " overlap_height_ratio=0.2,\n", - " overlap_width_ratio=0.2,\n", + " slice_height = 256,\n", + " slice_width = 256,\n", + " overlap_height_ratio = 0.2,\n", + " overlap_width_ratio = 0.2\n", ")" ] }, @@ -723,7 +718,7 @@ "outputs": [], "source": [ "model_type = \"yolov5sparse\"\n", - "model_device = \"cpu\" # or 'cuda:0'\n", + "model_device = \"cpu\" # or 'cuda:0'\n", "model_confidence_threshold = 0.4\n", "\n", "slice_height = 256\n", diff --git a/demo/inference_for_torchvision.ipynb b/demo/inference_for_torchvision.ipynb index 61290e86e..719d639ba 100644 --- a/demo/inference_for_torchvision.ipynb +++ b/demo/inference_for_torchvision.ipynb @@ -39,7 +39,6 @@ "outputs": [], "source": [ "import os\n", - "\n", "os.getcwd()" ] }, @@ -93,13 +92,8 @@ "model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=FasterRCNN_ResNet50_FPN_Weights.DEFAULT)\n", "\n", "# download test images into demo_data folder\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", - " \"demo_data/small-vehicles1.jpeg\",\n", - ")\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", - ")" + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" ] }, { @@ -139,13 +133,13 @@ "outputs": [], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type=\"torchvision\",\n", + " model_type='torchvision',\n", " model=model,\n", " confidence_threshold=0.5,\n", " image_size=640,\n", - " device=\"cpu\", # or \"cuda:0\"\n", + " device=\"cpu\", # or \"cuda:0\"\n", " load_at_init=True,\n", - ")" + ")\n" ] }, { @@ -308,10 +302,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height=320,\n", - " slice_width=320,\n", - " overlap_height_ratio=0.2,\n", - " overlap_width_ratio=0.2,\n", + " slice_height = 320,\n", + " slice_width = 320,\n", + " overlap_height_ratio = 0.2,\n", + " overlap_width_ratio = 0.2,\n", ")" ] }, @@ -671,11 +665,11 @@ "source": [ "model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)\n", "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type=\"torchvision\",\n", + " model_type='torchvision',\n", " model=model,\n", " confidence_threshold=0.4,\n", " image_size=640,\n", - " device=\"cpu\", # or \"cuda:0\"\n", + " device=\"cpu\", # or \"cuda:0\"\n", " load_at_init=True,\n", ")\n", "\n", diff --git a/demo/inference_for_yolonas.ipynb b/demo/inference_for_yolonas.ipynb index 0b68a7b5d..87986b30d 100644 --- a/demo/inference_for_yolonas.ipynb +++ b/demo/inference_for_yolonas.ipynb @@ -53,7 +53,6 @@ ], "source": [ "import os\n", - "\n", "os.getcwd()" ] }, @@ -129,13 +128,8 @@ "download_yolonas_s_model(yolonas_model_path)\n", "\n", "# download test images into demo_data folder\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", - " \"demo_data/small-vehicles1.jpeg\",\n", - ")\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", - ")" + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" ] }, { @@ -189,12 +183,12 @@ ], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type=\"yolonas\",\n", + " model_type='yolonas',\n", " model_name=\"yolo_nas_s\",\n", - " class_names_yaml_path=\"tests/data/coco_utils/coco_class_names.yaml\", # required if model_path is provided\n", - " model_path=yolonas_model_path, # If model_path is not provided, model is downloaded from Deci-AI remote client automatically\n", + " class_names_yaml_path=\"tests/data/coco_utils/coco_class_names.yaml\", # required if model_path is provided\n", + " model_path=yolonas_model_path, # If model_path is not provided, model is downloaded from Deci-AI remote client automatically\n", " confidence_threshold=0.3,\n", - " device=\"cpu\", # or 'cuda:0'\n", + " device=\"cpu\", # or 'cuda:0'\n", ")" ] }, @@ -296,10 +290,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height=256,\n", - " slice_width=256,\n", - " overlap_height_ratio=0.2,\n", - " overlap_width_ratio=0.2,\n", + " slice_height = 256,\n", + " slice_width = 256,\n", + " overlap_height_ratio = 0.2,\n", + " overlap_width_ratio = 0.2\n", ")" ] }, @@ -581,9 +575,9 @@ "outputs": [], "source": [ "model_type = \"yolonas\"\n", - "model_name = \"yolo_nas_s\"\n", + "model_name=\"yolo_nas_s\"\n", "model_path = yolonas_model_path\n", - "model_device = \"cpu\" # or 'cuda:0'\n", + "model_device = \"cpu\" # or 'cuda:0'\n", "model_confidence_threshold = 0.4\n", "class_names_yaml_path = \"tests/data/coco_utils/coco_class_names.yaml\"\n", "\n", @@ -682,7 +676,7 @@ " model_type=model_type,\n", " model_name=model_name,\n", " model_path=model_path,\n", - " class_names_yaml_path=class_names_yaml_path,\n", + " class_names_yaml_path = class_names_yaml_path,\n", " model_device=model_device,\n", " model_confidence_threshold=model_confidence_threshold,\n", " source=source_image_dir,\n", diff --git a/demo/inference_for_yolov5.ipynb b/demo/inference_for_yolov5.ipynb index 4f2c4f3a3..13f744b86 100644 --- a/demo/inference_for_yolov5.ipynb +++ b/demo/inference_for_yolov5.ipynb @@ -37,7 +37,6 @@ "outputs": [], "source": [ "import os\n", - "\n", "os.getcwd()" ] }, @@ -81,17 +80,12 @@ "outputs": [], "source": [ "# download YOLOV5S6 model to 'models/yolov5s6.pt'\n", - "yolov5_model_path = \"models/yolov5s6.pt\"\n", + "yolov5_model_path = 'models/yolov5s6.pt'\n", "download_yolov5s6_model(destination_path=yolov5_model_path)\n", "\n", "# download test images into demo_data folder\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", - " \"demo_data/small-vehicles1.jpeg\",\n", - ")\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", - ")" + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" ] }, { @@ -115,10 +109,10 @@ "outputs": [], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type=\"yolov5\",\n", + " model_type='yolov5',\n", " model_path=yolov5_model_path,\n", " confidence_threshold=0.3,\n", - " device=\"cpu\", # or 'cuda:0'\n", + " device=\"cpu\", # or 'cuda:0'\n", ")" ] }, @@ -215,10 +209,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height=256,\n", - " slice_width=256,\n", - " overlap_height_ratio=0.2,\n", - " overlap_width_ratio=0.2,\n", + " slice_height = 256,\n", + " slice_width = 256,\n", + " overlap_height_ratio = 0.2,\n", + " overlap_width_ratio = 0.2\n", ")" ] }, @@ -521,7 +515,7 @@ "source": [ "model_type = \"yolov5\"\n", "model_path = yolov5_model_path\n", - "model_device = \"cpu\" # or 'cuda:0'\n", + "model_device = \"cpu\" # or 'cuda:0'\n", "model_confidence_threshold = 0.4\n", "\n", "slice_height = 256\n", diff --git a/demo/inference_for_yolov8.ipynb b/demo/inference_for_yolov8.ipynb index bf145d8b2..f339ba3b5 100644 --- a/demo/inference_for_yolov8.ipynb +++ b/demo/inference_for_yolov8.ipynb @@ -49,7 +49,6 @@ ], "source": [ "import os\n", - "\n", "os.getcwd()" ] }, @@ -97,13 +96,8 @@ "download_yolov8s_model(yolov8_model_path)\n", "\n", "# download test images into demo_data folder\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", - " \"demo_data/small-vehicles1.jpeg\",\n", - ")\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", - ")" + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" ] }, { @@ -128,10 +122,10 @@ "outputs": [], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type=\"yolov8\",\n", + " model_type='yolov8',\n", " model_path=yolov8_model_path,\n", " confidence_threshold=0.3,\n", - " device=\"cpu\", # or 'cuda:0'\n", + " device=\"cpu\", # or 'cuda:0'\n", ")" ] }, @@ -240,10 +234,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height=256,\n", - " slice_width=256,\n", - " overlap_height_ratio=0.2,\n", - " overlap_width_ratio=0.2,\n", + " slice_height = 256,\n", + " slice_width = 256,\n", + " overlap_height_ratio = 0.2,\n", + " overlap_width_ratio = 0.2\n", ")" ] }, @@ -491,7 +485,7 @@ "source": [ "model_type = \"yolov8\"\n", "model_path = yolov8_model_path\n", - "model_device = \"cpu\" # or 'cuda:0'\n", + "model_device = \"cpu\" # or 'cuda:0'\n", "model_confidence_threshold = 0.4\n", "\n", "slice_height = 256\n", diff --git a/demo/inference_for_yolov8_onnx.ipynb b/demo/inference_for_yolov8_onnx.ipynb index eab72561d..ee7878fcb 100644 --- a/demo/inference_for_yolov8_onnx.ipynb +++ b/demo/inference_for_yolov8_onnx.ipynb @@ -48,7 +48,6 @@ ], "source": [ "import os\n", - "\n", "os.getcwd()" ] }, @@ -92,13 +91,8 @@ "download_yolov8n_onnx_model(yolov8_onnx_model_path)\n", "\n", "# download test images into demo_data folder\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg\",\n", - " \"demo_data/small-vehicles1.jpeg\",\n", - ")\n", - "download_from_url(\n", - " \"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png\", \"demo_data/terrain2.png\"\n", - ")" + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')\n", + "download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')" ] }, { @@ -121,88 +115,7 @@ "metadata": {}, "outputs": [], "source": [ - "category_mapping = {\n", - " \"0\": \"person\",\n", - " \"1\": \"bicycle\",\n", - " \"2\": \"car\",\n", - " \"3\": \"motorcycle\",\n", - " \"4\": \"airplane\",\n", - " \"5\": \"bus\",\n", - " \"6\": \"train\",\n", - " \"7\": \"truck\",\n", - " \"8\": \"boat\",\n", - " \"9\": \"traffic light\",\n", - " \"10\": \"fire hydrant\",\n", - " \"11\": \"stop sign\",\n", - " \"12\": \"parking meter\",\n", - " \"13\": \"bench\",\n", - " \"14\": \"bird\",\n", - " \"15\": \"cat\",\n", - " \"16\": \"dog\",\n", - " \"17\": \"horse\",\n", - " \"18\": \"sheep\",\n", - " \"19\": \"cow\",\n", - " \"20\": \"elephant\",\n", - " \"21\": \"bear\",\n", - " \"22\": \"zebra\",\n", - " \"23\": \"giraffe\",\n", - " \"24\": \"backpack\",\n", - " \"25\": \"umbrella\",\n", - " \"26\": \"handbag\",\n", - " \"27\": \"tie\",\n", - " \"28\": \"suitcase\",\n", - " \"29\": \"frisbee\",\n", - " \"30\": \"skis\",\n", - " \"31\": \"snowboard\",\n", - " \"32\": \"sports ball\",\n", - " \"33\": \"kite\",\n", - " \"34\": \"baseball bat\",\n", - " \"35\": \"baseball glove\",\n", - " \"36\": \"skateboard\",\n", - " \"37\": \"surfboard\",\n", - " \"38\": \"tennis racket\",\n", - " \"39\": \"bottle\",\n", - " \"40\": \"wine glass\",\n", - " \"41\": \"cup\",\n", - " \"42\": \"fork\",\n", - " \"43\": \"knife\",\n", - " \"44\": \"spoon\",\n", - " \"45\": \"bowl\",\n", - " \"46\": \"banana\",\n", - " \"47\": \"apple\",\n", - " \"48\": \"sandwich\",\n", - " \"49\": \"orange\",\n", - " \"50\": \"broccoli\",\n", - " \"51\": \"carrot\",\n", - " \"52\": \"hot dog\",\n", - " \"53\": \"pizza\",\n", - " \"54\": \"donut\",\n", - " \"55\": \"cake\",\n", - " \"56\": \"chair\",\n", - " \"57\": \"couch\",\n", - " \"58\": \"potted plant\",\n", - " \"59\": \"bed\",\n", - " \"60\": \"dining table\",\n", - " \"61\": \"toilet\",\n", - " \"62\": \"tv\",\n", - " \"63\": \"laptop\",\n", - " \"64\": \"mouse\",\n", - " \"65\": \"remote\",\n", - " \"66\": \"keyboard\",\n", - " \"67\": \"cell phone\",\n", - " \"68\": \"microwave\",\n", - " \"69\": \"oven\",\n", - " \"70\": \"toaster\",\n", - " \"71\": \"sink\",\n", - " \"72\": \"refrigerator\",\n", - " \"73\": \"book\",\n", - " \"74\": \"clock\",\n", - " \"75\": \"vase\",\n", - " \"76\": \"scissors\",\n", - " \"77\": \"teddy bear\",\n", - " \"78\": \"hair drier\",\n", - " \"79\": \"toothbrush\",\n", - "}" + "category_mapping = {'0': 'person','1': 'bicycle','2': 'car','3': 'motorcycle','4': 'airplane','5': 'bus','6': 'train','7': 'truck','8': 'boat','9': 'traffic light','10': 'fire hydrant','11': 'stop sign','12': 'parking meter','13': 'bench','14': 'bird','15': 'cat','16': 'dog','17': 'horse','18': 'sheep','19': 'cow','20': 'elephant','21': 'bear','22': 'zebra','23': 'giraffe','24': 'backpack','25': 'umbrella','26': 'handbag','27': 'tie','28': 'suitcase','29': 'frisbee','30': 'skis','31': 'snowboard','32': 'sports ball','33': 'kite','34': 'baseball bat','35': 'baseball glove','36': 'skateboard','37': 'surfboard','38': 'tennis racket','39': 'bottle','40': 'wine glass','41': 'cup','42': 'fork','43': 'knife','44': 'spoon','45': 'bowl','46': 'banana','47': 'apple','48': 'sandwich','49': 'orange','50': 'broccoli','51': 'carrot','52': 'hot dog','53': 'pizza','54': 'donut','55': 'cake','56': 'chair','57': 'couch','58': 'potted plant','59': 'bed','60': 'dining table','61': 'toilet','62': 'tv','63': 'laptop','64': 'mouse','65': 'remote','66': 'keyboard','67': 'cell phone','68': 'microwave','69': 'oven','70': 'toaster','71': 'sink','72': 'refrigerator','73': 'book','74': 'clock','75': 'vase','76': 'scissors','77': 'teddy bear','78': 'hair drier','79': 'toothbrush'}" ] }, { @@ -212,11 +125,11 @@ "outputs": [], "source": [ "detection_model = AutoDetectionModel.from_pretrained(\n", - " model_type=\"yolov8onnx\",\n", + " model_type='yolov8onnx',\n", " model_path=yolov8_onnx_model_path,\n", " confidence_threshold=0.3,\n", " category_mapping=category_mapping,\n", - " device=\"cpu\", # or 'cuda:0'\n", + " device=\"cpu\", # or 'cuda:0'\n", ")" ] }, @@ -313,10 +226,10 @@ "result = get_sliced_prediction(\n", " \"demo_data/small-vehicles1.jpeg\",\n", " detection_model,\n", - " slice_height=256,\n", - " slice_width=256,\n", - " overlap_height_ratio=0.2,\n", - " overlap_width_ratio=0.2,\n", + " slice_height = 256,\n", + " slice_width = 256,\n", + " overlap_height_ratio = 0.2,\n", + " overlap_width_ratio = 0.2\n", ")" ] }, @@ -564,7 +477,7 @@ "source": [ "model_type = \"yolov8onnx\"\n", "model_path = yolov8_onnx_model_path\n", - "model_device = \"cpu\" # or 'cuda:0'\n", + "model_device = \"cpu\" # or 'cuda:0'\n", "model_confidence_threshold = 0.4\n", "model_category_mapping = category_mapping\n", "\n", diff --git a/demo/slicing.ipynb b/demo/slicing.ipynb index d60d6f7d2..63de1a1df 100644 --- a/demo/slicing.ipynb +++ b/demo/slicing.ipynb @@ -14,7 +14,6 @@ "outputs": [], "source": [ "import os\n", - "\n", "os.getcwd()" ] }, @@ -86,14 +85,14 @@ "f, axarr = plt.subplots(1, 1, figsize=(12, 12))\n", "# read image\n", "img_ind = 0\n", - "img = Image.open(\"demo_data/\" + coco_dict[\"images\"][img_ind][\"file_name\"]).convert(\"RGBA\")\n", + "img = Image.open(\"demo_data/\" + coco_dict[\"images\"][img_ind][\"file_name\"]).convert('RGBA')\n", "# iterate over all annotations\n", "for ann_ind in range(len(coco_dict[\"annotations\"])):\n", " # convert coco bbox to pil bbox\n", " xywh = coco_dict[\"annotations\"][ann_ind][\"bbox\"]\n", - " xyxy = [xywh[0], xywh[1], xywh[0] + xywh[2], xywh[1] + xywh[3]]\n", + " xyxy = [xywh[0], xywh[1], xywh[0]+xywh[2], xywh[1]+xywh[3]]\n", " # visualize bbox over image\n", - " ImageDraw.Draw(img, \"RGBA\").rectangle(xyxy, width=5)\n", + " ImageDraw.Draw(img, 'RGBA').rectangle(xyxy, width=5)\n", "axarr.imshow(img)" ] }, @@ -167,7 +166,7 @@ " overlap_height_ratio=0.2,\n", " overlap_width_ratio=0.2,\n", " min_area_ratio=0.1,\n", - " verbose=True,\n", + " verbose=True\n", ")" ] }, @@ -197,7 +196,7 @@ } ], "source": [ - "f, axarr = plt.subplots(4, 5, figsize=(13, 13))\n", + "f, axarr = plt.subplots(4, 5, figsize=(13,13))\n", "img_ind = 0\n", "for ind1 in range(4):\n", " for ind2 in range(5):\n", @@ -232,23 +231,23 @@ } ], "source": [ - "f, axarr = plt.subplots(4, 5, figsize=(13, 13))\n", + "f, axarr = plt.subplots(4, 5, figsize=(13,13))\n", "img_ind = 0\n", "for row_ind in range(4):\n", " for column_ind in range(5):\n", " # read image\n", - " img = Image.open(\"demo_data/sliced/\" + coco_dict[\"images\"][img_ind][\"file_name\"]).convert(\"RGBA\")\n", + " img = Image.open(\"demo_data/sliced/\" + coco_dict[\"images\"][img_ind][\"file_name\"]).convert('RGBA')\n", " # iterate over all annotations\n", " for ann_ind in range(len(coco_dict[\"annotations\"])):\n", " # find annotations that belong the selected image\n", " if coco_dict[\"annotations\"][ann_ind][\"image_id\"] == coco_dict[\"images\"][img_ind][\"id\"]:\n", " # convert coco bbox to pil bbox\n", " xywh = coco_dict[\"annotations\"][ann_ind][\"bbox\"]\n", - " xyxy = [xywh[0], xywh[1], xywh[0] + xywh[2], xywh[1] + xywh[3]]\n", + " xyxy = [xywh[0], xywh[1], xywh[0]+xywh[2], xywh[1]+xywh[3]]\n", " # visualize bbox over image\n", - " ImageDraw.Draw(img, \"RGBA\").rectangle(xyxy, width=5)\n", + " ImageDraw.Draw(img, 'RGBA').rectangle(xyxy, width=5)\n", " axarr[row_ind, column_ind].imshow(img)\n", - " img_ind += 1" + " img_ind += 1\n" ] }, {