diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b6dc839c8..e1634a97f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -85,6 +85,10 @@ jobs: run: > pip install yolov5==6.1.3 norfair==1.0.0 + - name: Install Transformers(4.20.0) + run: > + pip install transformers==4.20.0 + - name: Install Layer (0.10.*) run: > pip install -U "layer>=0.10.0,<0.11.0" diff --git a/.github/workflows/ci_torch1.10.yml b/.github/workflows/ci_torch1.10.yml index 350cfe5c1..1131dfc64 100644 --- a/.github/workflows/ci_torch1.10.yml +++ b/.github/workflows/ci_torch1.10.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: operating-system: [ubuntu-latest, windows-latest, macos-latest] - python-version: [3.7, 3.8, 3.9] + python-version: [3.6, 3.7, 3.8, 3.9] fail-fast: false steps: @@ -73,9 +73,9 @@ jobs: pip install torch==1.10.2+cpu torchvision==0.11.3+cpu -f https://download.pytorch.org/whl/torch_stable.html - - name: Install PyTorch on MacOS + - name: Install PyTorch(1.10.1) and TorchVision(0.11.2) on MacOS if: matrix.operating-system == 'macos-latest' - run: pip install torch==1.10.2 torchvision==0.11.3 + run: pip install torch==1.10.1 torchvision==0.11.2 - name: Install MMDetection(2.25.0) with MMCV(1.5.3) run: > @@ -90,7 +90,15 @@ jobs: run: > python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cpu/torch1.10/index.html + - name: Install Transformers(4.20.0) + if: > + matrix.python-version != '3.6' + run: > + pip install transformers==4.20.0 + - name: Install Layer (0.10.*) + if: > + matrix.python-version != '3.6' run: > pip install -U "layer>=0.10.0,<0.11.0" diff --git a/.github/workflows/package_testing.yml b/.github/workflows/package_testing.yml index efe783325..6098e18f0 100644 --- a/.github/workflows/package_testing.yml +++ b/.github/workflows/package_testing.yml @@ -11,7 +11,7 @@ jobs: strategy: matrix: operating-system: [ubuntu-latest, windows-latest, macos-latest] - python-version: [3.7, 3.8, 3.9] + python-version: [3.6, 3.7, 3.8, 3.9] fail-fast: false steps: @@ -50,7 +50,7 @@ jobs: - name: Update pip run: python -m pip install --upgrade pip - - name: Install PyTorch on Linux and Windows + - name: Install PyTorch(1.10.2) and TorchVision(0.11.3) on Linux and Windows if: > matrix.operating-system == 'ubuntu-latest' || matrix.operating-system == 'windows-latest' @@ -58,9 +58,9 @@ jobs: pip install torch==1.10.2+cpu torchvision==0.11.3+cpu -f https://download.pytorch.org/whl/torch_stable.html - - name: Install PyTorch on MacOS + - name: Install PyTorch(1.10.1) and TorchVision(0.11.2) on MacOS if: matrix.operating-system == 'macos-latest' - run: pip install torch==1.10.2 torchvision==0.11.3 + run: pip install torch==1.10.1 torchvision==0.11.2 - name: Install MMDetection(2.25.0) with MMCV(1.5.3) run: > @@ -75,7 +75,15 @@ jobs: run: > python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cpu/torch1.10/index.html + - name: Install Transformers(4.20.0) + if: > + matrix.python-version != '3.6' + run: > + pip install transformers==4.20.0 + - name: Install Layer (0.10.*) + if: > + matrix.python-version != '3.6' run: > pip install -U "layer>=0.10.0,<0.11.0" diff --git a/requirements.txt b/requirements.txt index a90307e7c..15fb6e702 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,9 +2,9 @@ opencv-python>=4.2.0.32 shapely>=1.8.0 tqdm>=4.48.2 pillow>=8.2.0 -pybboxes +pybboxes==0.1.1 pyyaml fire terminaltables requests -click==8.0.4 \ No newline at end of file +click==8.0.4 diff --git a/sahi/auto_model.py b/sahi/auto_model.py index b88e1d356..b7ed1ce21 100644 --- a/sahi/auto_model.py +++ b/sahi/auto_model.py @@ -7,6 +7,7 @@ TorchVisionDetectionModel, Yolov5DetectionModel, ) +from sahi.utils.import_utils import check_requirements MODEL_TYPE_TO_MODEL_CLASS_NAME = { "mmdet": MmdetDetectionModel, @@ -78,6 +79,7 @@ def from_local( ) @staticmethod + @check_requirements(["layer"]) def from_layer( model_path: str, no_cache: bool = False, @@ -115,10 +117,7 @@ def from_layer( ImportError: If Layer is not installed in your environment ValueError: If model path does not match expected pattern: organization_name/project_name/models/model_name """ - try: - import layer - except ImportError: - raise ImportError('Please run "pip install layer -U" ' "to load models from Layer.") + import layer layer_model = layer.get_model(name=model_path, no_cache=no_cache).get_train() if layer_model.__class__.__module__ in ["yolov5.models.common", "models.common"]: diff --git a/setup.py b/setup.py index 0e80f31e1..ae8cd84b9 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ def get_version(): long_description_content_type="text/markdown", url="https://github.com/obss/sahi", packages=setuptools.find_packages(exclude=["demo", "docs", "resources", "tests"]), - python_requires=">=3.7", + python_requires=">=3.6", install_requires=get_requirements(), extras_require={ "tests": ["pytest", "mmdet", "norfair", "layer"], diff --git a/tests/test_huggingfacemodel.py b/tests/test_huggingfacemodel.py index 828cf2676..c7bf772cc 100644 --- a/tests/test_huggingfacemodel.py +++ b/tests/test_huggingfacemodel.py @@ -1,6 +1,7 @@ # OBSS SAHI Tool # Code written by Devrim Cavusoglu, 2022. +import sys import unittest import pybboxes.functional as pbf @@ -9,135 +10,284 @@ from sahi.utils.huggingface import HuggingfaceTestConstants MODEL_DEVICE = "cpu" -CONFIDENCE_THRESHOLD = 0.3 +CONFIDENCE_THRESHOLD = 0.5 IMAGE_SIZE = 320 -class TestHuggingfaceDetectionModel(unittest.TestCase): - def test_load_model(self): - from sahi.model import HuggingfaceDetectionModel - - huggingface_detection_model = HuggingfaceDetectionModel( - model_path=HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH, - confidence_threshold=CONFIDENCE_THRESHOLD, - device=MODEL_DEVICE, - category_remapping=None, - load_at_init=True, - ) - - self.assertNotEqual(huggingface_detection_model.model, None) - - def test_set_model(self): - from transformers import AutoFeatureExtractor, AutoModelForObjectDetection - - from sahi.model import HuggingfaceDetectionModel - - huggingface_model = AutoModelForObjectDetection.from_pretrained(HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH) - huggingface_feature_extractor = AutoFeatureExtractor.from_pretrained( - HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH - ) - - huggingface_detection_model = HuggingfaceDetectionModel( - model=huggingface_model, - feature_extractor=huggingface_feature_extractor, - confidence_threshold=CONFIDENCE_THRESHOLD, - device=MODEL_DEVICE, - category_remapping=None, - load_at_init=True, - ) - - self.assertNotEqual(huggingface_detection_model.model, None) - - def test_perform_inference(self): - from sahi.model import HuggingfaceDetectionModel - - huggingface_detection_model = HuggingfaceDetectionModel( - model_path=HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH, - confidence_threshold=CONFIDENCE_THRESHOLD, - device=MODEL_DEVICE, - category_remapping=None, - load_at_init=True, - image_size=IMAGE_SIZE, - ) - - # prepare image - image_path = "tests/data/small-vehicles1.jpeg" - image = read_image(image_path) - - # perform inference - huggingface_detection_model.perform_inference(image) - original_predictions = huggingface_detection_model.original_predictions - - scores, cat_ids, boxes = huggingface_detection_model.get_valid_predictions( - logits=original_predictions.logits[0], pred_boxes=original_predictions.pred_boxes[0] - ) - - # find box of first car detection with conf greater than 0.5 - for i, box in enumerate(boxes): - if huggingface_detection_model.category_mapping[cat_ids[i].item()] == "car": # if category car - break - - image_height, image_width, _ = huggingface_detection_model.image_shapes[0] - box = list( - pbf.convert_bbox( - box.tolist(), - from_type="yolo", - to_type="voc", - image_size=(image_width, image_height), - return_values=True, +# huggignface/yolos is not available for python<3.7 +if sys.version_info >= (3, 7): + + class TestHuggingfaceDetectionModel(unittest.TestCase): + def test_load_model(self): + from sahi.model import HuggingfaceDetectionModel + + huggingface_detection_model = HuggingfaceDetectionModel( + model_path=HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH, + confidence_threshold=CONFIDENCE_THRESHOLD, + device=MODEL_DEVICE, + category_remapping=None, + load_at_init=True, + ) + + self.assertNotEqual(huggingface_detection_model.model, None) + + def test_set_model(self): + from transformers import AutoFeatureExtractor, AutoModelForObjectDetection + + from sahi.model import HuggingfaceDetectionModel + + huggingface_model = AutoModelForObjectDetection.from_pretrained( + HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH + ) + huggingface_feature_extractor = AutoFeatureExtractor.from_pretrained( + HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH + ) + + huggingface_detection_model = HuggingfaceDetectionModel( + model=huggingface_model, + feature_extractor=huggingface_feature_extractor, + confidence_threshold=CONFIDENCE_THRESHOLD, + device=MODEL_DEVICE, + category_remapping=None, + load_at_init=True, + ) + + self.assertNotEqual(huggingface_detection_model.model, None) + + def test_perform_inference(self): + from sahi.model import HuggingfaceDetectionModel + + huggingface_detection_model = HuggingfaceDetectionModel( + model_path=HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH, + confidence_threshold=CONFIDENCE_THRESHOLD, + device=MODEL_DEVICE, + category_remapping=None, + load_at_init=True, + image_size=IMAGE_SIZE, + ) + + # prepare image + image_path = "tests/data/small-vehicles1.jpeg" + image = read_image(image_path) + + # perform inference + huggingface_detection_model.perform_inference(image) + original_predictions = huggingface_detection_model.original_predictions + + scores, cat_ids, boxes = huggingface_detection_model.get_valid_predictions( + logits=original_predictions.logits[0], pred_boxes=original_predictions.pred_boxes[0] + ) + + # find box of first car detection with conf greater than 0.5 + for i, box in enumerate(boxes): + if huggingface_detection_model.category_mapping[cat_ids[i].item()] == "car": # if category car + break + + image_height, image_width, _ = huggingface_detection_model.image_shapes[0] + box = list( + pbf.convert_bbox( + box.tolist(), + from_type="yolo", + to_type="voc", + image_size=(image_width, image_height), + return_values=True, + ) + ) + + # compare + desired_bbox = [639, 198, 663, 218] + predicted_bbox = list(map(int, box[:4])) + margin = 2 + for ind, point in enumerate(predicted_bbox): + assert point < desired_bbox[ind] + margin and point > desired_bbox[ind] - margin + for score in scores: + self.assertGreaterEqual(score.item(), CONFIDENCE_THRESHOLD) + + def test_convert_original_predictions(self): + from sahi.model import HuggingfaceDetectionModel + + huggingface_detection_model = HuggingfaceDetectionModel( + model_path=HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH, + confidence_threshold=CONFIDENCE_THRESHOLD, + device=MODEL_DEVICE, + category_remapping=None, + load_at_init=True, + image_size=IMAGE_SIZE, + ) + + # prepare image + image_path = "tests/data/small-vehicles1.jpeg" + image = read_image(image_path) + + # perform inference + huggingface_detection_model.perform_inference(image) + + # convert predictions to ObjectPrediction list + huggingface_detection_model.convert_original_predictions() + object_prediction_list = huggingface_detection_model.object_prediction_list + + # compare + self.assertEqual(len(object_prediction_list), 28) + self.assertEqual(object_prediction_list[0].category.id, 3) + self.assertEqual(object_prediction_list[0].category.name, "car") + desired_bbox = [639, 198, 24, 20] + predicted_bbox = object_prediction_list[0].bbox.to_coco_bbox() + margin = 2 + for ind, point in enumerate(predicted_bbox): + assert point < desired_bbox[ind] + margin and point > desired_bbox[ind] - margin + self.assertEqual(object_prediction_list[2].category.id, 3) + self.assertEqual(object_prediction_list[2].category.name, "car") + desired_bbox = [745, 169, 15, 14] + predicted_bbox = object_prediction_list[2].bbox.to_coco_bbox() + for ind, point in enumerate(predicted_bbox): + assert point < desired_bbox[ind] + margin and point > desired_bbox[ind] - margin + + for object_prediction in object_prediction_list: + self.assertGreaterEqual(object_prediction.score.value, CONFIDENCE_THRESHOLD) + + def test_get_prediction_huggingface(self): + from sahi.model import HuggingfaceDetectionModel + from sahi.predict import get_prediction + from sahi.utils.huggingface import HuggingfaceTestConstants + + huggingface_detection_model = HuggingfaceDetectionModel( + model_path=HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH, + confidence_threshold=CONFIDENCE_THRESHOLD, + device=MODEL_DEVICE, + category_remapping=None, + load_at_init=False, + image_size=IMAGE_SIZE, + ) + huggingface_detection_model.load_model() + + # prepare image + image_path = "tests/data/small-vehicles1.jpeg" + image = read_image(image_path) + + # get full sized prediction + prediction_result = get_prediction( + image=image, + detection_model=huggingface_detection_model, + shift_amount=[0, 0], + full_shape=None, + postprocess=None, + ) + object_prediction_list = prediction_result.object_prediction_list + + # compare + self.assertEqual(len(object_prediction_list), 28) + num_person = num_truck = num_car = 0 + for object_prediction in object_prediction_list: + if object_prediction.category.name == "person": + num_person += 1 + elif object_prediction.category.name == "truck": + num_truck += 1 + elif object_prediction.category.name == "car": + num_car += 1 + self.assertEqual(num_person, 0) + self.assertEqual(num_truck, 1) + self.assertEqual(num_car, 27) + + def test_get_prediction_automodel_huggingface(self): + from sahi.auto_model import AutoDetectionModel + from sahi.predict import get_prediction + from sahi.utils.huggingface import HuggingfaceTestConstants + + huggingface_detection_model = AutoDetectionModel.from_local( + model_type="huggingface", + model_path=HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH, + confidence_threshold=CONFIDENCE_THRESHOLD, + device=MODEL_DEVICE, + category_remapping=None, + load_at_init=False, + image_size=IMAGE_SIZE, + ) + huggingface_detection_model.load_model() + + # prepare image + image_path = "tests/data/small-vehicles1.jpeg" + image = read_image(image_path) + + # get full sized prediction + prediction_result = get_prediction( + image=image, + detection_model=huggingface_detection_model, + shift_amount=[0, 0], + full_shape=None, + postprocess=None, ) - ) - - # compare - desired_bbox = [639, 198, 663, 218] - predicted_bbox = list(map(int, box[:4])) - margin = 2 - for ind, point in enumerate(predicted_bbox): - assert point < desired_bbox[ind] + margin and point > desired_bbox[ind] - margin - for score in scores: - self.assertGreaterEqual(score.item(), CONFIDENCE_THRESHOLD) - - def test_convert_original_predictions(self): - from sahi.model import HuggingfaceDetectionModel - - huggingface_detection_model = HuggingfaceDetectionModel( - model_path=HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH, - confidence_threshold=CONFIDENCE_THRESHOLD, - device=MODEL_DEVICE, - category_remapping=None, - load_at_init=True, - image_size=IMAGE_SIZE, - ) - - # prepare image - image_path = "tests/data/small-vehicles1.jpeg" - image = read_image(image_path) - - # perform inference - huggingface_detection_model.perform_inference(image) - - # convert predictions to ObjectPrediction list - huggingface_detection_model.convert_original_predictions() - object_prediction_list = huggingface_detection_model.object_prediction_list - - # compare - self.assertEqual(len(object_prediction_list), 46) - self.assertEqual(object_prediction_list[0].category.id, 3) - self.assertEqual(object_prediction_list[0].category.name, "car") - desired_bbox = [639, 198, 24, 20] - predicted_bbox = object_prediction_list[0].bbox.to_coco_bbox() - margin = 2 - for ind, point in enumerate(predicted_bbox): - assert point < desired_bbox[ind] + margin and point > desired_bbox[ind] - margin - self.assertEqual(object_prediction_list[2].category.id, 3) - self.assertEqual(object_prediction_list[2].category.name, "car") - desired_bbox = [663, 187, 17, 16] - predicted_bbox = object_prediction_list[2].bbox.to_coco_bbox() - for ind, point in enumerate(predicted_bbox): - assert point < desired_bbox[ind] + margin and point > desired_bbox[ind] - margin - - for object_prediction in object_prediction_list: - self.assertGreaterEqual(object_prediction.score.value, CONFIDENCE_THRESHOLD) + object_prediction_list = prediction_result.object_prediction_list + + # compare + self.assertEqual(len(object_prediction_list), 28) + num_person = num_truck = num_car = 0 + for object_prediction in object_prediction_list: + if object_prediction.category.name == "person": + num_person += 1 + elif object_prediction.category.name == "truck": + num_truck += 1 + elif object_prediction.category.name == "car": + num_car += 1 + self.assertEqual(num_person, 0) + self.assertEqual(num_truck, 1) + self.assertEqual(num_car, 27) + + def test_get_sliced_prediction_huggingface(self): + from sahi.model import HuggingfaceDetectionModel + from sahi.predict import get_sliced_prediction + from sahi.utils.huggingface import HuggingfaceTestConstants + + huggingface_detection_model = HuggingfaceDetectionModel( + model_path=HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH, + confidence_threshold=CONFIDENCE_THRESHOLD, + device=MODEL_DEVICE, + category_remapping=None, + load_at_init=False, + image_size=IMAGE_SIZE, + ) + huggingface_detection_model.load_model() + + # prepare image + image_path = "tests/data/small-vehicles1.jpeg" + + slice_height = 512 + slice_width = 512 + overlap_height_ratio = 0.1 + overlap_width_ratio = 0.2 + postprocess_type = "GREEDYNMM" + match_metric = "IOS" + match_threshold = 0.5 + class_agnostic = True + + # get sliced prediction + prediction_result = get_sliced_prediction( + image=image_path, + detection_model=huggingface_detection_model, + slice_height=slice_height, + slice_width=slice_width, + overlap_height_ratio=overlap_height_ratio, + overlap_width_ratio=overlap_width_ratio, + perform_standard_pred=False, + postprocess_type=postprocess_type, + postprocess_match_threshold=match_threshold, + postprocess_match_metric=match_metric, + postprocess_class_agnostic=class_agnostic, + ) + object_prediction_list = prediction_result.object_prediction_list + + # compare + self.assertEqual(len(object_prediction_list), 53) + num_person = num_truck = num_car = 0 + for object_prediction in object_prediction_list: + if object_prediction.category.name == "person": + num_person += 1 + elif object_prediction.category.name == "truck": + num_truck += 1 + elif object_prediction.category.name == "car": + num_car += 1 + self.assertEqual(num_person, 0) + self.assertEqual(num_truck, 5) + self.assertEqual(num_car, 48) if __name__ == "__main__": diff --git a/tests/test_layer.py b/tests/test_layer.py index 3c31d999a..43a9fb4ff 100644 --- a/tests/test_layer.py +++ b/tests/test_layer.py @@ -1,56 +1,56 @@ # OBSS SAHI Tool # Code written by Fatih C Akyon, 2020. +import sys import unittest from unittest import mock -from layer.flavors.base import ModelRuntimeObjects - from sahi.auto_model import AutoDetectionModel -from sahi.model import Yolov5DetectionModel - -class TestLayerDetectionModel(unittest.TestCase): - @mock.patch("layer.get_model") - def test_load_layer_model(self, mock_layer_get_model): - import yolov5 - from layer import Model +# layer is not available for python<3.7 +if sys.version_info >= (3, 7): - from sahi.model import DetectionModel + class TestLayerDetectionModel(unittest.TestCase): + @mock.patch("layer.get_model") + def test_load_layer_model(self, mock_layer_get_model): + import yolov5 + from layer import Model + from layer.flavors.base import ModelRuntimeObjects - layer_model_path = "sahi/yolo/models/yolov5s" + from sahi.model import Yolov5DetectionModel - # Return a YOLO model once mocked `layer.get_model()` is called - yolo_model = yolov5.load("tests/data/models/yolov5n.pt") - layer_model = Model(layer_model_path, model_runtime_objects=ModelRuntimeObjects(yolo_model)) - mock_layer_get_model.return_value = layer_model + layer_model_path = "sahi/yolo/models/yolov5s" - # Make the call - detection_model = AutoDetectionModel.from_layer(layer_model_path) + # Return a YOLO model once mocked `layer.get_model()` is called + yolo_model = yolov5.load("tests/data/models/yolov5n.pt") + layer_model = Model(layer_model_path, model_runtime_objects=ModelRuntimeObjects(yolo_model)) + mock_layer_get_model.return_value = layer_model - # Run assertions - mock_layer_get_model.assert_called_once_with(name=layer_model_path, no_cache=False) + # Make the call + detection_model = AutoDetectionModel.from_layer(layer_model_path) - self.assertIsInstance(detection_model, type(Yolov5DetectionModel())) - self.assertIsInstance(detection_model.model, type(yolo_model)) + # Run assertions + mock_layer_get_model.assert_called_once_with(name=layer_model_path, no_cache=False) - @mock.patch("layer.get_model") - def test_load_layer_fails(self, mock_layer_get_model): - import torch - from layer import Model + self.assertIsInstance(detection_model, type(Yolov5DetectionModel())) + self.assertIsInstance(detection_model.model, type(yolo_model)) - from sahi.model import DetectionModel + @mock.patch("layer.get_model") + def test_load_layer_fails(self, mock_layer_get_model): + import torch + from layer import Model + from layer.flavors.base import ModelRuntimeObjects - layer_model_path = "sahi/yolo/models/yolov5s" + layer_model_path = "sahi/yolo/models/yolov5s" - # Return a basic Torch model once mocked `layer.get_model()` is called - torch_model = torch.nn.Sequential() - layer_model = Model(layer_model_path, model_runtime_objects=ModelRuntimeObjects(torch_model)) - mock_layer_get_model.return_value = layer_model + # Return a basic Torch model once mocked `layer.get_model()` is called + torch_model = torch.nn.Sequential() + layer_model = Model(layer_model_path, model_runtime_objects=ModelRuntimeObjects(torch_model)) + mock_layer_get_model.return_value = layer_model - # Make the call expecting an exception - with self.assertRaises(Exception): - AutoDetectionModel.from_layer(layer_model_path) + # Make the call expecting an exception + with self.assertRaises(Exception): + AutoDetectionModel.from_layer(layer_model_path) if __name__ == "__main__": diff --git a/tests/test_predict.py b/tests/test_predict.py index 06de7e995..bbb415deb 100644 --- a/tests/test_predict.py +++ b/tests/test_predict.py @@ -165,93 +165,6 @@ def test_get_prediction_automodel_yolov5(self): num_car += 1 self.assertEqual(num_car, 2) - def test_get_prediction_huggingface(self): - from sahi.model import HuggingfaceDetectionModel - from sahi.predict import get_prediction - from sahi.utils.huggingface import HuggingfaceTestConstants - - huggingface_detection_model = HuggingfaceDetectionModel( - model_path=HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH, - confidence_threshold=CONFIDENCE_THRESHOLD, - device=MODEL_DEVICE, - category_remapping=None, - load_at_init=False, - image_size=IMAGE_SIZE, - ) - huggingface_detection_model.load_model() - - # prepare image - image_path = "tests/data/small-vehicles1.jpeg" - image = read_image(image_path) - - # get full sized prediction - prediction_result = get_prediction( - image=image, - detection_model=huggingface_detection_model, - shift_amount=[0, 0], - full_shape=None, - postprocess=None, - ) - object_prediction_list = prediction_result.object_prediction_list - - # compare - self.assertEqual(len(object_prediction_list), 28) - num_person = num_truck = num_car = 0 - for object_prediction in object_prediction_list: - if object_prediction.category.name == "person": - num_person += 1 - elif object_prediction.category.name == "truck": - num_truck += 1 - elif object_prediction.category.name == "car": - num_car += 1 - self.assertEqual(num_person, 0) - self.assertEqual(num_truck, 1) - self.assertEqual(num_car, 27) - - def test_get_prediction_automodel_huggingface(self): - from sahi.auto_model import AutoDetectionModel - from sahi.predict import get_prediction - from sahi.utils.huggingface import HuggingfaceTestConstants - - huggingface_detection_model = AutoDetectionModel.from_local( - model_type="huggingface", - model_path=HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH, - confidence_threshold=CONFIDENCE_THRESHOLD, - device=MODEL_DEVICE, - category_remapping=None, - load_at_init=False, - image_size=IMAGE_SIZE, - ) - huggingface_detection_model.load_model() - - # prepare image - image_path = "tests/data/small-vehicles1.jpeg" - image = read_image(image_path) - - # get full sized prediction - prediction_result = get_prediction( - image=image, - detection_model=huggingface_detection_model, - shift_amount=[0, 0], - full_shape=None, - postprocess=None, - ) - object_prediction_list = prediction_result.object_prediction_list - - # compare - self.assertEqual(len(object_prediction_list), 28) - num_person = num_truck = num_car = 0 - for object_prediction in object_prediction_list: - if object_prediction.category.name == "person": - num_person += 1 - elif object_prediction.category.name == "truck": - num_truck += 1 - elif object_prediction.category.name == "car": - num_car += 1 - self.assertEqual(num_person, 0) - self.assertEqual(num_truck, 1) - self.assertEqual(num_car, 27) - def test_get_sliced_prediction_mmdet(self): from sahi.model import MmdetDetectionModel from sahi.predict import get_sliced_prediction @@ -381,63 +294,6 @@ def test_get_sliced_prediction_yolov5(self): num_car += 1 self.assertEqual(num_car, 10) - def test_get_sliced_prediction_huggingface(self): - from sahi.model import HuggingfaceDetectionModel - from sahi.predict import get_sliced_prediction - from sahi.utils.huggingface import HuggingfaceTestConstants - - huggingface_detection_model = HuggingfaceDetectionModel( - model_path=HuggingfaceTestConstants.YOLOS_TINY_MODEL_PATH, - confidence_threshold=CONFIDENCE_THRESHOLD, - device=MODEL_DEVICE, - category_remapping=None, - load_at_init=False, - image_size=IMAGE_SIZE, - ) - huggingface_detection_model.load_model() - - # prepare image - image_path = "tests/data/small-vehicles1.jpeg" - - slice_height = 512 - slice_width = 512 - overlap_height_ratio = 0.1 - overlap_width_ratio = 0.2 - postprocess_type = "GREEDYNMM" - match_metric = "IOS" - match_threshold = 0.5 - class_agnostic = True - - # get sliced prediction - prediction_result = get_sliced_prediction( - image=image_path, - detection_model=huggingface_detection_model, - slice_height=slice_height, - slice_width=slice_width, - overlap_height_ratio=overlap_height_ratio, - overlap_width_ratio=overlap_width_ratio, - perform_standard_pred=False, - postprocess_type=postprocess_type, - postprocess_match_threshold=match_threshold, - postprocess_match_metric=match_metric, - postprocess_class_agnostic=class_agnostic, - ) - object_prediction_list = prediction_result.object_prediction_list - - # compare - self.assertEqual(len(object_prediction_list), 53) - num_person = num_truck = num_car = 0 - for object_prediction in object_prediction_list: - if object_prediction.category.name == "person": - num_person += 1 - elif object_prediction.category.name == "truck": - num_truck += 1 - elif object_prediction.category.name == "car": - num_car += 1 - self.assertEqual(num_person, 0) - self.assertEqual(num_truck, 5) - self.assertEqual(num_car, 48) - def test_coco_json_prediction(self): from sahi.predict import predict from sahi.utils.mmdet import MmdetTestConstants, download_mmdet_yolox_tiny_model