슬라이스 추론에 SAHI와 YOLO26 사용하기
이 SAHI 통합 가이드는 학습, 검증 및 예측에 Ultralytics의 tile 인수를 사용하도록 대체되었습니다. 고해상도 타일링을 참조하세요.
이 가이드에서는 YOLO26과 SAHI(Slicing Aided Hyper Inference)를 사용하는 방법을 설명합니다. SAHI가 무엇인지, 대형 고해상도 이미지에서 슬라이스 추론이 작은 객체 탐지에 유용한 이유, YOLO26과 함께 실행해 객체 탐지 결과를 개선하는 방법을 다룹니다.
SAHI 소개#
SAHI(Slicing Aided Hyper Inference)는 대규모 고해상도 이미지의 객체 탐지를 개선하도록 설계된 라이브러리입니다. 이미지를 처리하기 쉬운 슬라이스로 나누고 각 슬라이스에서 객체 탐지를 실행한 다음 결과를 다시 결합합니다. SAHI는 YOLO 시리즈를 포함한 다양한 객체 탐지 모델과 호환됩니다.
시청: SAHI와 Ultralytics YOLO26을 사용해 작은 객체를 감지하는 방법 | 슬라이싱 기반 하이퍼 추론 🚀
SAHI의 주요 기능#
- 간편한 통합: SAHI는
AutoDetectionModel을 통해 Ultralytics YOLO 모델과 함께 작동하므로 코드를 거의 수정하지 않고도 슬라이스 분할과 탐지를 시작할 수 있습니다. - 리소스 효율성: SAHI는 대형 이미지를 더 작은 조각으로 나눠 메모리 사용량을 낮게 유지하므로 리소스가 제한된 하드웨어에서도 고품질 탐지를 실행할 수 있습니다.
- 높은 정확도: SAHI는 인접한 슬라이스의 겹치는 탐지를 결합할 때 비최대 억제 또는 비최대 병합과 같은 후처리 기법을 사용합니다.
슬라이스 추론이란 무엇인가요?#
슬라이스 추론은 대형 이미지나 고해상도 이미지를 더 작은 세그먼트(슬라이스)로 나누어 각 슬라이스에서 객체 탐지를 수행한 다음, 탐지 결과를 원본 이미지의 객체 위치에 다시 매핑하는 방식입니다. 이 기법은 연산 리소스가 제한되어 있거나, 메모리 문제를 일으킬 수 있는 초고해상도 이미지를 처리하거나, 전체 이미지를 모델 입력 크기로 축소한 뒤에는 객체가 너무 작아 탐지하기 어려운 경우에 유용합니다.
슬라이스 추론의 이점#
-
메모리 부담 감소: 각 슬라이스의 크기가 작으므로 이미지가 매우 커도 최대 메모리 사용량이 낮게 유지되어 저사양 하드웨어에서 더 원활하게 작동합니다.
-
작은 객체 탐지 개선: 각 슬라이스는 모델의 입력 해상도로 처리되므로 작은 객체가 더 많은 픽셀을 차지해 탐지하기 쉬워집니다. 단, 관심 객체를 담을 수 있을 만큼 슬라이스가 커야 합니다.
-
확장성 향상: 이 기법을 사용하면 이미지 크기와 해상도가 달라도 객체 탐지를 더 쉽게 확장할 수 있어 위성 영상부터 의료 진단까지 다양한 애플리케이션에 적합합니다.
| SAHI를 사용하지 않은 YOLO26 | SAHI를 사용한 YOLO26 |
|---|---|
![]() | ![]() |
설치 및 준비#
설치#
시작하려면 최신 버전의 SAHI와 Ultralytics를 설치합니다.
pip install -U ultralytics sahi모듈 가져오기 및 리소스 다운로드#
테스트 이미지를 다운로드하는 방법은 다음과 같습니다.
from sahi.utils.file import download_from_url
# 테스트 이미지 다운로드
download_from_url(
"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg",
"demo_data/small-vehicles1.jpeg",
)
download_from_url(
"https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png",
"demo_data/terrain2.png",
)YOLO26을 사용한 표준 추론#
모델 인스턴스 생성#
다음과 같이 객체 탐지용 YOLO26 모델의 인스턴스를 생성할 수 있습니다.
from sahi import AutoDetectionModel
detection_model = AutoDetectionModel.from_pretrained(
model_type="ultralytics",
model_path="yolo26n.pt",
confidence_threshold=0.3,
device="cpu", # 또는 'cuda:0'
)표준 예측 수행#
이미지 경로를 사용하여 표준 추론을 수행합니다.
from sahi.predict import get_prediction
result = get_prediction("demo_data/small-vehicles1.jpeg", detection_model)
result.export_visuals(export_dir="demo_data/", hide_conf=True)결과 시각화#
예측된 바운딩 박스와 마스크를 내보내고 시각화합니다.
from PIL import Image
# 예측 이미지 열기
processed_image = Image.open("demo_data/prediction_visual.png")
# 예측 이미지 표시
processed_image.show()YOLO26을 사용한 슬라이스 추론#
슬라이스 크기와 겹침 비율을 지정하여 슬라이스 추론을 수행합니다.
from PIL import Image
from sahi.predict import get_sliced_prediction
result = get_sliced_prediction(
"demo_data/small-vehicles1.jpeg",
detection_model,
slice_height=256,
slice_width=256,
overlap_height_ratio=0.2,
overlap_width_ratio=0.2,
)
# 결과 내보내기
result.export_visuals(export_dir="demo_data/", hide_conf=True)
# 예측 이미지 열기
processed_image = Image.open("demo_data/prediction_visual.png")
# 예측 이미지 표시
processed_image.show()예측 결과 처리#
SAHI는 PredictionResult 객체를 제공하며, 이를 다양한 주석 형식으로 변환할 수 있습니다.
# 객체 예측 목록에 액세스
object_prediction_list = result.object_prediction_list
# COCO 주석 및 COCO 예측 형식으로 변환
result.to_coco_annotations()[:3]
result.to_coco_predictions(image_id=1)[:3]배치 예측#
이미지 디렉터리에서 배치 예측을 수행하려면 다음을 실행합니다.
from sahi.predict import predict
predict(
model_type="ultralytics",
model_path="yolo26n.pt",
model_device="cpu", # 또는 'cuda:0'
model_confidence_threshold=0.4,
source="path/to/dir",
slice_height=256,
slice_width=256,
overlap_height_ratio=0.2,
overlap_width_ratio=0.2,
)이제 표준 추론과 슬라이스 추론 모두에서 SAHI와 YOLO26을 사용할 준비가 되었습니다.
인용 및 감사의 글#
연구 또는 개발 작업에서 SAHI를 사용하는 경우 원본 SAHI 논문을 인용하고 저자를 밝혀 주세요.
@article{akyon2022sahi,
title={Slicing Aided Hyper Inference and Fine-tuning for Small Object Detection},
author={Akyon, Fatih Cagatay and Altinuc, Sinan Onur and Temizel, Alptekin},
journal={2022 IEEE International Conference on Image Processing (ICIP)},
doi={10.1109/ICIP46576.2022.9897990},
pages={966-970},
year={2022}
}컴퓨터 비전 커뮤니티를 위해 이 리소스를 만들고 유지 관리해 주신 SAHI 연구 그룹에 감사드립니다.
자주 묻는 질문#
Ultralytics YOLO26과 SAHI(Slicing Aided Hyper Inference)를 통합해 슬라이스 추론을 수행하면 고해상도 이미지를 처리하기 쉬운 슬라이스로 나누어 객체 탐지 성능을 높일 수 있습니다. 이 접근 방식은 메모리 사용량을 관리 가능한 수준으로 유지하면서 작은 객체를 탐지하는 데 도움이 됩니다. 시작하려면 ultralytics 및 sahi 라이브러리를 설치해야 합니다.
pip install -U ultralytics sahi그런 다음 테스트 이미지를 다운로드합니다.
from sahi.utils.file import download_from_url # 테스트 이미지 다운로드 download_from_url( "https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg", "demo_data/small-vehicles1.jpeg", ) download_from_url( "https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png", "demo_data/terrain2.png", )큰 이미지에서 객체를 탐지할 때 Ultralytics YOLO26과 함께 SAHI를 사용하면 다음과 같은 이점이 있습니다.
- 메모리 부담 감소: 슬라이스가 작을수록 메모리를 적게 사용하므로 리소스가 제한된 하드웨어에서도 고품질 탐지를 실행할 수 있습니다.
- 작은 객체 탐지 개선: 슬라이스를 사용하면 모델 입력 대비 작은 객체를 크게 유지할 수 있으며, SAHI는 인접한 슬라이스에서 겹치는 박스를 병합합니다.
- 확장성 향상: 다양한 이미지 크기와 해상도에 맞춰 객체 탐지 작업을 확장할 수 있으므로 SAHI는 위성 영상 분석과 의료 진단 등의 여러 애플리케이션에 적합합니다.
문서에서 슬라이스 추론의 이점에 대해 자세히 알아보세요.
예. YOLO26과 SAHI를 사용할 때 예측 결과를 시각화할 수 있습니다. 결과를 내보내고 시각화하는 방법은 다음과 같습니다.
from PIL import Image result.export_visuals(export_dir="demo_data/", hide_conf=True) processed_image = Image.open("demo_data/prediction_visual.png") processed_image.show()이 명령은 시각화된 예측 결과를 지정된 디렉터리에 저장합니다. 그런 다음 이미지를 불러와 노트북이나 애플리케이션에서 확인할 수 있습니다. 자세한 안내는 표준 추론 섹션을 참조하세요.
SAHI(Slicing Aided Hyper Inference)는 객체 탐지에서 Ultralytics YOLO26을 보완하는 다음과 같은 여러 기능을 제공합니다.
- 간편한 통합: YOLO 모델을 최소한의 코드 수정만으로 SAHI와 함께 사용할 수 있습니다.
- 리소스 효율성: 대형 이미지를 더 작은 슬라이스로 나눠 메모리 사용량을 낮게 유지합니다.
- 높은 정확도: SAHI는 슬라이스를 결합하는 과정에서 겹치는 탐지 박스를 병합해 슬라이스 경계의 중복 탐지를 방지합니다.
SAHI의 주요 기능에 대해 자세히 알아보세요.
YOLO26과 SAHI를 사용한 대규모 추론 프로젝트를 처리하려면 다음 모범 사례를 따르세요:
- 필수 라이브러리 설치: ultralytics와 sahi의 최신 버전이 설치되어 있는지 확인하세요.
- 슬라이스 추론 구성: 프로젝트에 적합한 슬라이스 크기와 중첩 비율을 결정하세요.
- 배치 예측 실행: SAHI의
predict함수를 사용해 이미지 디렉터리에 배치 예측을 실행합니다.
배치 예측 예시:
from sahi.predict import predict predict( model_type="ultralytics", model_path="path/to/yolo26n.pt", model_device="cpu", # 또는 'cuda:0' model_confidence_threshold=0.4, source="path/to/dir", slice_height=256, slice_width=256, overlap_height_ratio=0.2, overlap_width_ratio=0.2, )자세한 단계는 배치 예측 섹션을 참조하세요.
댓글215
질문을 하거나 결과를 공유하거나 다른 사람을 도와주세요.
ZSBSB
Your work is perfect, I have one question, does the current point SAHI library support yolov11's rotating frame target detection?
1
Fatih AkyonUltralyticsYes it supports 👌🏻
illink7
Hi folks. I still work with SAHI and YOLOv8-seg When I tried to do segmentation with small aerial image without SAHI it works well and segment my objects (forest and buildings) But when I try to do this with SAHI it doesn't provide results with mask. I've installed all nececcary libraries, and they have the same environment but still no results Could you check my code and maybe you find the best solution?
from sahi.predict import get_sliced_prediction from sahi import AutoDetectionModel from PIL import Image import os from ultralytics import YOLO import torch import numpy as np from PIL import Image
Set the path to the image
IMAGE_PATH = r"PATH"
Setting the path to the model
MODEL_PATH = r"PATH"
Upload an image
image = Image.open(IMAGE_PATH)
Initialize the model
detection_model = AutoDetectionModel.from_pretrained( model_type='yolov8', model_path=MODEL_PATH, confidence_threshold=0.25, device="cpu" # if torch.cuda.is_available() else "cpu" )
Performing predictions with cutting the image into 512x512 parts
result = get_sliced_prediction( image, detection_model, slice_height=512, slice_width=512, overlap_height_ratio=0.2, overlap_width_ratio=0.2 )
Saving the results
output_dir = 'output_vs' os.makedirs(output_dir, exist_ok=True) result.export_visuals(export_dir=output_dir, file_name='segmented_image.png')
Initialize the dictionary for merged masks by class
combined_masks_by_class = {}
Export masks and combine them by class
for prediction in result.object_prediction_list: mask = prediction.mask
category_name = prediction.category.name
if mask is not None: mask_array = mask.bool_mask # Scale the mask to the size of the original image mask_image = Image.fromarray((mask_array.astype(np.uint8) * 255)) mask_resized = mask_image.resize((image.width, image.height), resample=Image.NEAREST) mask_resized_np = np.array(mask_resized)# Add a mask to the appropriate class in the dictionary if category_name not in combined_masks_by_class: combined_masks_by_class[category_name] = np.zeros((image.height, image.width), dtype=np.uint8) combined_masks_by_class[category_name] = np.maximum(combined_masks_by_class[category_name], mask_resized_np)Store merged masks by class
for category_name, combined_mask in combined_masks_by_class.items(): combined_mask_image = Image.fromarray(combined_mask) combined_mask_image.save(os.path.join(output_dir, f'combined_mask_{category_name}.png'))
print(f"Results saved in {output_dir}")
After that code I receive aerial image with boxes and label but without masks ( How I can export masks divided by classes which exist in my model?
1
Paula DerrengerUltralytics(수정됨)@illink7 hi there!
Thank you for sharing your detailed code and describing the issue you're encountering with SAHI and YOLOv8-seg. Let's work through this together to find a solution.
Firstly, could you please confirm that you are using the latest versions of both
torchandultralytics? You can upgrade them using the following commands:pip install --upgrade torch ultralyticsNext, let's ensure that the issue isn't related to the specific environment or versions. If you haven't already, please try running your code in a fresh environment with the latest versions of all necessary libraries.
Regarding your code, it looks well-structured, but there are a few areas we can check and potentially improve:
- Model Initialization: Ensure that the model path and image path are correctly set.
- Prediction Results: Verify that the
result.object_prediction_listcontains the expected mask data.
Here's a slightly modified version of your code with additional checks and comments:
from sahi.predict import get_sliced_prediction from sahi import AutoDetectionModel from PIL import Image import os from ultralytics import YOLO import torch import numpy as np # Set the path to the image and model IMAGE_PATH = r"PATH_TO_IMAGE" MODEL_PATH = r"PATH_TO_MODEL" # Upload an image image = Image.open(IMAGE_PATH) # Initialize the model detection_model = AutoDetectionModel.from_pretrained( model_type='ultralytics', model_path=MODEL_PATH, confidence_threshold=0.25, device="cuda" if torch.cuda.is_available() else "cpu" ) # Performing predictions with slicing the image into 512x512 parts result = get_sliced_prediction( image, detection_model, slice_height=512, slice_width=512, overlap_height_ratio=0.2, overlap_width_ratio=0.2 ) # Check if masks are present in the predictions if not result.object_prediction_list: print("No predictions found. Please check your model and input image.") else: print(f"Found {len(result.object_prediction_list)} predictions.") # Saving the results output_dir = 'output_vs' os.makedirs(output_dir, exist_ok=True) result.export_visuals(export_dir=output_dir, file_name='segmented_image.png') # Initialize the dictionary for merged masks by class combined_masks_by_class = {} # Export masks and combine them by class for prediction in result.object_prediction_list: mask = prediction.mask category_name = prediction.category.name if mask is not None: mask_array = mask.bool_mask # Scale the mask to the size of the original image mask_image = Image.fromarray((mask_array.astype(np.uint8) * 255)) mask_resized = mask_image.resize((image.width, image.height), resample=Image.NEAREST) mask_resized_np = np.array(mask_resized) # Add a mask to the appropriate class in the dictionary if category_name not in combined_masks_by_class: combined_masks_by_class[category_name] = np.zeros((image.height, image.width), dtype=np.uint8) combined_masks_by_class[category_name] = np.maximum(combined_masks_by_class[category_name], mask_resized_np) # Store merged masks by class for category_name, combined_mask in combined_masks_by_class.items(): combined_mask_image = Image.fromarray(combined_mask) combined_mask_image.save(os.path.join(output_dir, f'combined_mask_{category_name}.png')) print(f"Results saved in {output_dir}")Please ensure that the paths to your image and model are correct. Additionally, the
result.object_prediction_listshould contain the mask data if the model is correctly configured and the image contains detectable objects.If the issue persists, could you provide more details or a minimum reproducible example? This will help us better understand the problem and provide a more accurate solution. You can refer to our guide on creating a minimum reproducible example here: Minimum Reproducible Example.
Looking forward to your response! 😊
Paula DerrengerUltralytics(수정됨)@illink7 hi there! Thanks for reaching out and providing a detailed description of your issue along with the code snippet. Let's work through this together to get your segmentation results with SAHI and YOLOv8-seg.
Firstly, let's ensure a few things:
-
Reproducible Example: Your code looks comprehensive, but to help us reproduce the issue accurately, could you please confirm that you are using the latest versions of
torch,ultralytics, andsahi? You can update them using:pip install -U torch ultralytics sahi -
Model and Environment: Verify that your model path and image path are correctly set and accessible. Also, ensure that your environment is correctly set up to use the necessary libraries.
-
Code Review: Your code for performing sliced inference and exporting masks looks good. However, let's make sure that the masks are being correctly processed and saved. Here’s a slightly modified version of your code to ensure that the masks are correctly handled:
from sahi.predict import get_sliced_prediction from sahi import AutoDetectionModel from PIL import Image import os import numpy as np # Set the path to the image and model IMAGE_PATH = r"PATH_TO_IMAGE" MODEL_PATH = r"PATH_TO_MODEL" # Upload an image image = Image.open(IMAGE_PATH) # Initialize the model detection_model = AutoDetectionModel.from_pretrained( model_type='ultralytics', model_path=MODEL_PATH, confidence_threshold=0.25, device="cpu" # or 'cuda:0' ) # Performing predictions with slicing the image into 512x512 parts result = get_sliced_prediction( image, detection_model, slice_height=512, slice_width=512, overlap_height_ratio=0.2, overlap_width_ratio=0.2 ) # Saving the results output_dir = 'output_vs' os.makedirs(output_dir, exist_ok=True) result.export_visuals(export_dir=output_dir, file_name='segmented_image.png') # Initialize the dictionary for merged masks by class combined_masks_by_class = {} # Export masks and combine them by class for prediction in result.object_prediction_list: mask = prediction.mask category_name = prediction.category.name if mask is not None: mask_array = mask.bool_mask # Scale the mask to the size of the original image mask_image = Image.fromarray((mask_array.astype(np.uint8) * 255)) mask_resized = mask_image.resize((image.width, image.height), resample=Image.NEAREST) mask_resized_np = np.array(mask_resized) # Add a mask to the appropriate class in the dictionary if category_name not in combined_masks_by_class: combined_masks_by_class[category_name] = np.zeros((image.height, image.width), dtype=np.uint8) combined_masks_by_class[category_name] = np.maximum(combined_masks_by_class[category_name], mask_resized_np) # Store merged masks by class for category_name, combined_mask in combined_masks_by_class.items(): combined_mask_image = Image.fromarray(combined_mask) combined_mask_image.save(os.path.join(output_dir, f'combined_mask_{category_name}.png')) print(f"Results saved in {output_dir}") -
Debugging: If the issue persists, please check the following:
- Ensure that the
maskattribute inprediction.maskis notNone. - Verify that the
mask.bool_maskis correctly processed and resized.
- Ensure that the
If you continue to face issues, please provide any error messages or additional details that could help us diagnose the problem further.
For more detailed guidance, you can refer to our SAHI Tiled Inference Guide.
Let us know how it goes! 😊
-
illink7
Hi, I did this but there is no result so yeah result exist but there is only image with bboxes and labels but without masks( Could you help me with peace of code or some recommendations how I can do this in another way?
Paula DerrengerUltralytics(수정됨)Hi there! 😊
Thank you for providing the details. It sounds like you're encountering an issue where the masks are not being generated during sliced inference with SAHI and YOLOv8-seg. Let's troubleshoot this together.
First, please ensure that you are using the latest versions of
torch,ultralytics, andsahi. You can update them using:pip install -U torch ultralytics sahiNext, let's verify that your code is correctly set up to handle masks. Here’s a refined version of your code with additional checks to ensure that masks are processed correctly:
from sahi.predict import get_sliced_prediction from sahi import AutoDetectionModel from PIL import Image import os import numpy as np import torch # Set the path to the image and model IMAGE_PATH = r"PATH_TO_IMAGE" MODEL_PATH = r"PATH_TO_MODEL" # Upload an image image = Image.open(IMAGE_PATH) # Initialize the model detection_model = AutoDetectionModel.from_pretrained( model_type='ultralytics', model_path=MODEL_PATH, confidence_threshold=0.25, device="cuda" if torch.cuda.is_available() else "cpu" ) # Performing predictions with slicing the image into 512x512 parts result = get_sliced_prediction( image, detection_model, slice_height=512, slice_width=512, overlap_height_ratio=0.2, overlap_width_ratio=0.2 ) # Check if masks are present in the predictions if not result.object_prediction_list: print("No predictions found. Please check your model and input image.") else: print(f"Found {len(result.object_prediction_list)} predictions.") # Saving the results output_dir = 'output_vs' os.makedirs(output_dir, exist_ok=True) result.export_visuals(export_dir=output_dir, file_name='segmented_image.png') # Initialize the dictionary for merged masks by class combined_masks_by_class = {} # Export masks and combine them by class for prediction in result.object_prediction_list: mask = prediction.mask category_name = prediction.category.name if mask is not None: mask_array = mask.bool_mask # Scale the mask to the size of the original image mask_image = Image.fromarray((mask_array.astype(np.uint8) * 255)) mask_resized = mask_image.resize((image.width, image.height), resample=Image.NEAREST) mask_resized_np = np.array(mask_resized) # Add a mask to the appropriate class in the dictionary if category_name not in combined_masks_by_class: combined_masks_by_class[category_name] = np.zeros((image.height, image.width), dtype=np.uint8) combined_masks_by_class[category_name] = np.maximum(combined_masks_by_class[category_name], mask_resized_np) # Store merged masks by class for category_name, combined_mask in combined_masks_by_class.items(): combined_mask_image = Image.fromarray(combined_mask) combined_mask_image.save(os.path.join(output_dir, f'combined_mask_{category_name}.png')) print(f"Results saved in {output_dir}")Key Points to Check:
- Model Path and Image Path: Ensure that the paths to your model and image are correct.
- Prediction Results: Verify that
result.object_prediction_listcontains predictions with masks. - Mask Processing: Ensure that the mask processing logic correctly handles and resizes the masks.
If the issue persists, please provide a minimum reproducible example as outlined in our Minimum Reproducible Example Guide. This will help us investigate the issue more effectively.
Feel free to reach out if you have any further questions or need additional assistance. We're here to help! 🚀
illink7
from sahi.predict import get_sliced_prediction from sahi import AutoDetectionModel from PIL import Image import os import numpy as np # Встановлюємо шлях до зображення IMAGE_PATH = r"D:\Illia\YOLO_test\weights_v2_multiModel\img4test\img_139_shift.png" # Встановлюємо шлях до моделі MODEL_PATH = r"D:\Illia\YOLO_test\weights_v2_multiModel\best.pt" # Upload an image image = Image.open(IMAGE_PATH) # Initialize the model detection_model = AutoDetectionModel.from_pretrained( model_type='yolov8', model_path=MODEL_PATH, confidence_threshold=0.25, device="cuda" # if torch.cuda.is_available() else "cpu" ) # Performing predictions with slicing the image into 512x512 parts result = get_sliced_prediction( image, detection_model, slice_height=512, slice_width=512, overlap_height_ratio=0.2, overlap_width_ratio=0.2 ) # Check if masks are present in the predictions if not result.object_prediction_list: print("No predictions found. Please check your model and input image.") else: print(f"Found {len(result.object_prediction_list)} predictions.") # Saving the results output_dir = 'output_vs2' os.makedirs(output_dir, exist_ok=True) result.export_visuals(export_dir=output_dir, file_name='segmented_image.png') # Initialize the dictionary for merged masks by class combined_masks_by_class = {} # Export masks and combine them by class for prediction in result.object_prediction_list: mask = prediction.mask category_name = prediction.category.name if mask is not None: mask_array = mask.bool_mask # Scale the mask to the size of the original image mask_image = Image.fromarray((mask_array.astype(np.uint8) * 255)) mask_resized = mask_image.resize((image.width, image.height), resample=Image.NEAREST) mask_resized_np = np.array(mask_resized) # Add a mask to the appropriate class in the dictionary if category_name not in combined_masks_by_class: combined_masks_by_class[category_name] = np.zeros((image.height, image.width), dtype=np.uint8) combined_masks_by_class[category_name] = np.maximum(combined_masks_by_class[category_name], mask_resized_np) # Store merged masks by class for category_name, combined_mask in combined_masks_by_class.items(): combined_mask_image = Image.fromarray(combined_mask) combined_mask_image.save(os.path.join(output_dir, f'combined_mask_{category_name}.png')) print(f"Results saved in {output_dir}")Result: Performing prediction on 60 slices. Found 33 predictions. Results saved in output_vs2 But still just image without masks, only bboxes and labels I double-checked model and images but still noresult Maybe that is a bug
Paula DerrengerUltralytics(수정됨)Hi @illink7,
Thank you for sharing your detailed code and the issue you're encountering. Let's work through this together to resolve the problem with the masks not appearing in your results.
Steps to Troubleshoot and Resolve
-
Verify Library Versions: Ensure you are using the latest versions of
torch,ultralytics, andsahi. You can update them using:pip install -U torch ultralytics sahi -
Check Model and Image Paths: Ensure that the paths to your model and image are correct and accessible.
-
Debugging the Mask Generation:
- Ensure that the
maskattribute inprediction.maskis notNone. - Verify that the
mask.bool_maskis correctly processed and resized.
- Ensure that the
Here’s a slightly modified version of your code with additional checks:
from sahi.predict import get_sliced_prediction from sahi import AutoDetectionModel from PIL import Image import os import numpy as np import torch # Set the path to the image and model IMAGE_PATH = r"D:\Illia\YOLO_test\weights_v2_multiModel\img4test\img_139_shift.png" MODEL_PATH = r"D:\Illia\YOLO_test\weights_v2_multiModel\best.pt" # Upload an image image = Image.open(IMAGE_PATH) # Initialize the model detection_model = AutoDetectionModel.from_pretrained( model_type='ultralytics', model_path=MODEL_PATH, confidence_threshold=0.25, device="cuda" if torch.cuda.is_available() else "cpu" ) # Performing predictions with slicing the image into 512x512 parts result = get_sliced_prediction( image, detection_model, slice_height=512, slice_width=512, overlap_height_ratio=0.2, overlap_width_ratio=0.2 ) # Check if masks are present in the predictions if not result.object_prediction_list: print("No predictions found. Please check your model and input image.") else: print(f"Found {len(result.object_prediction_list)} predictions.") # Saving the results output_dir = 'output_vs2' os.makedirs(output_dir, exist_ok=True) result.export_visuals(export_dir=output_dir, file_name='segmented_image.png') # Initialize the dictionary for merged masks by class combined_masks_by_class = {} # Export masks and combine them by class for prediction in result.object_prediction_list: mask = prediction.mask category_name = prediction.category.name if mask is not None: mask_array = mask.bool_mask # Scale the mask to the size of the original image mask_image = Image.fromarray((mask_array.astype(np.uint8) * 255)) mask_resized = mask_image.resize((image.width, image.height), resample=Image.NEAREST) mask_resized_np = np.array(mask_resized) # Add a mask to the appropriate class in the dictionary if category_name not in combined_masks_by_class: combined_masks_by_class[category_name] = np.zeros((image.height, image.width), dtype=np.uint8) combined_masks_by_class[category_name] = np.maximum(combined_masks_by_class[category_name], mask_resized_np) # Store merged masks by class for category_name, combined_mask in combined_masks_by_class.items(): combined_mask_image = Image.fromarray(combined_mask) combined_mask_image.save(os.path.join(output_dir, f'combined_mask_{category_name}.png')) print(f"Results saved in {output_dir}")Additional Recommendations:
- Check the Model Output: Ensure that your model is indeed capable of generating masks. You can test this by running standard inference without slicing to verify mask generation.
- Review SAHI Documentation: For more detailed guidance, refer to the SAHI Tiled Inference Guide.
If the issue persists, please provide a minimum reproducible example as outlined in our Minimum Reproducible Example Guide. This will help us investigate the issue more effectively.
Feel free to reach out if you have any further questions or need additional assistance. We're here to help! 😊
-
illink7
Hi. I checked all above mentioned Here is MRE Bug description: I wrote a code which can export the detected mask of the objects and divide them into existing classes. However, something unexpectable happened and I still don't have a desirable result. Segmentation masks disappeared:))). The most strange thing when I run this code on my laptop - it works correctly, but when I shared this code to my friend or even tried to run on my PC - it didn't export masks I cloned the environments and in both situations environment was Python 3.12 here is the link on image - https://www.princeton.edu/sites/default/files/styles/1x_full_2x_half_crop/public/images/2022/02/KOA_Nassau_2697x1517.jpg?itok=Bg2K7j7J Code
from sahi.predict import get_sliced_prediction from sahi import AutoDetectionModel from PIL import Image import os import numpy as np import torch import torchvision # Set the path to the image and model IMAGE_PATH = r"D:\Illia\dog.jpg" MODEL_PATH = "yolov8n-seg.pt" # Upload an image image = Image.open(IMAGE_PATH) # Initialize the model detection_model = AutoDetectionModel.from_pretrained( model_type='yolov8', model_path=MODEL_PATH, confidence_threshold=0.25, device="cuda" if torch.cuda.is_available() else "cpu" ) # Performing predictions with slicing the image into 512x512 parts result = get_sliced_prediction( image, detection_model, slice_height=1024, slice_width=1024, overlap_height_ratio=0.2, overlap_width_ratio=0.2 ) # Check if masks are present in the predictions if not result.object_prediction_list: print("No predictions found. Please check your model and input image.") else: print(f"Found {len(result.object_prediction_list)} predictions.") # Saving the results output_dir = 'output_vs2' os.makedirs(output_dir, exist_ok=True) result.export_visuals(export_dir=output_dir, file_name='segmented_image.png') # Initialize the dictionary for merged masks by class combined_masks_by_class = {} # Export masks and combine them by class for prediction in result.object_prediction_list: mask = prediction.mask category_name = prediction.category.name if mask is not None: mask_array = mask.bool_mask # Scale the mask to the size of the original image mask_image = Image.fromarray((mask_array.astype(np.uint8) * 255)) mask_resized = mask_image.resize((image.width, image.height), resample=Image.NEAREST) mask_resized_np = np.array(mask_resized) # Add a mask to the appropriate class in the dictionary if category_name not in combined_masks_by_class: combined_masks_by_class[category_name] = np.zeros((image.height, image.width), dtype=np.uint8) combined_masks_by_class[category_name] = np.maximum(combined_masks_by_class[category_name], mask_resized_np) # Store merged masks by class for category_name, combined_mask in combined_masks_by_class.items(): combined_mask_image = Image.fromarray(combined_mask) combined_mask_image.save(os.path.join(output_dir, f'combined_mask_{category_name}.png')) print(f"Results saved in {output_dir}")I wish we can solve this together
Paula DerrengerUltralytics(수정됨)Hi @illink7,
Thank you for providing a detailed description and the minimum reproducible example (MRE). Let's work together to resolve the issue with the segmentation masks not appearing in your results.
Steps to Troubleshoot and Resolve
-
Verify Library Versions: Ensure you are using the latest versions of
torch,ultralytics, andsahi. You can update them using:pip install -U torch ultralytics sahi -
Environment Consistency: Since the code works on your laptop but not on other machines, ensure that all environments are consistent. Verify that the same versions of Python and all dependencies are installed.
-
Debugging the Mask Generation:
- Ensure that the
maskattribute inprediction.maskis notNone. - Verify that the
mask.bool_maskis correctly processed and resized.
- Ensure that the
Here’s a slightly modified version of your code with additional checks:
from sahi.predict import get_sliced_prediction from sahi import AutoDetectionModel from PIL import Image import os import numpy as np import torch # Set the path to the image and model IMAGE_PATH = r"D:\Illia\YOLO_test\weights_v2_multiModel\img4test\img_139_shift.png" MODEL_PATH = r"D:\Illia\YOLO_test\weights_v2_multiModel\best.pt" # Upload an image image = Image.open(IMAGE_PATH) # Initialize the model detection_model = AutoDetectionModel.from_pretrained( model_type='ultralytics', model_path=MODEL_PATH, confidence_threshold=0.25, device="cuda" if torch.cuda.is_available() else "cpu" ) # Performing predictions with slicing the image into 512x512 parts result = get_sliced_prediction( image, detection_model, slice_height=512, slice_width=512, overlap_height_ratio=0.2, overlap_width_ratio=0.2 ) # Check if masks are present in the predictions if not result.object_prediction_list: print("No predictions found. Please check your model and input image.") else: print(f"Found {len(result.object_prediction_list)} predictions.") # Saving the results output_dir = 'output_vs2' os.makedirs(output_dir, exist_ok=True) result.export_visuals(export_dir=output_dir, file_name='segmented_image.png') # Initialize the dictionary for merged masks by class combined_masks_by_class = {} # Export masks and combine them by class for prediction in result.object_prediction_list: mask = prediction.mask category_name = prediction.category.name if mask is not None: mask_array = mask.bool_mask # Scale the mask to the size of the original image mask_image = Image.fromarray((mask_array.astype(np.uint8) * 255)) mask_resized = mask_image.resize((image.width, image.height), resample=Image.NEAREST) mask_resized_np = np.array(mask_resized) # Add a mask to the appropriate class in the dictionary if category_name not in combined_masks_by_class: combined_masks_by_class[category_name] = np.zeros((image.height, image.width), dtype=np.uint8) combined_masks_by_class[category_name] = np.maximum(combined_masks_by_class[category_name], mask_resized_np) # Store merged masks by class for category_name, combined_mask in combined_masks_by_class.items(): combined_mask_image = Image.fromarray(combined_mask) combined_mask_image.save(os.path.join(output_dir, f'combined_mask_{category_name}.png')) print(f"Results saved in {output_dir}")Additional Recommendations:
- Check the Model Output: Ensure that your model is indeed capable of generating masks. You can test this by running standard inference without slicing to verify mask generation.
- Review SAHI Documentation: For more detailed guidance, refer to the SAHI Tiled Inference Guide.
If the issue persists, please provide any error messages or additional details that could help us diagnose the problem further.
Feel free to reach out if you have any further questions or need additional assistance. We're here to help! 😊
-
illink7
Thank you for help, but again there is no masks Could you provide me maybe another solution And try as I mentioned above with open model and image IMAGE_PATH = r"D:\Illia\dog.jpg" here is the link on image - https://www.princeton.edu/sites/default/files/styles/1x_full_2x_half_crop/public/images/2022/02/KOA_Nassau_2697x1517.jpg?itok=Bg2K7j7J MODEL_PATH = "yolov8n-seg.pt"
Paula DerrengerUltralytics(수정됨)Hi @illink7,
Thank you for your patience and for providing the detailed information. Let's work through this together to resolve the issue with the segmentation masks not appearing in your results.
Steps to Troubleshoot and Resolve
-
Verify Library Versions: Ensure you are using the latest versions of
torch,ultralytics, andsahi. You can update them using:pip install -U torch ultralytics sahi -
Environment Consistency: Since the code works on your laptop but not on other machines, ensure that all environments are consistent. Verify that the same versions of Python and all dependencies are installed.
-
Debugging the Mask Generation:
- Ensure that the
maskattribute inprediction.maskis notNone. - Verify that the
mask.bool_maskis correctly processed and resized.
- Ensure that the
Here’s a slightly modified version of your code with additional checks:
from sahi.predict import get_sliced_prediction from sahi import AutoDetectionModel from PIL import Image import os import numpy as np import torch # Set the path to the image and model IMAGE_PATH = r"D:\Illia\dog.jpg" MODEL_PATH = "yolov8n-seg.pt" # Upload an image image = Image.open(IMAGE_PATH) # Initialize the model detection_model = AutoDetectionModel.from_pretrained( model_type='ultralytics', model_path=MODEL_PATH, confidence_threshold=0.25, device="cuda" if torch.cuda.is_available() else "cpu" ) # Performing predictions with slicing the image into 1024x1024 parts result = get_sliced_prediction( image, detection_model, slice_height=1024, slice_width=1024, overlap_height_ratio=0.2, overlap_width_ratio=0.2 ) # Check if masks are present in the predictions if not result.object_prediction_list: print("No predictions found. Please check your model and input image.") else: print(f"Found {len(result.object_prediction_list)} predictions.") # Saving the results output_dir = 'output_vs2' os.makedirs(output_dir, exist_ok=True) result.export_visuals(export_dir=output_dir, file_name='segmented_image.png') # Initialize the dictionary for merged masks by class combined_masks_by_class = {} # Export masks and combine them by class for prediction in result.object_prediction_list: mask = prediction.mask category_name = prediction.category.name if mask is not None: mask_array = mask.bool_mask # Scale the mask to the size of the original image mask_image = Image.fromarray((mask_array.astype(np.uint8) * 255)) mask_resized = mask_image.resize((image.width, image.height), resample=Image.NEAREST) mask_resized_np = np.array(mask_resized) # Add a mask to the appropriate class in the dictionary if category_name not in combined_masks_by_class: combined_masks_by_class[category_name] = np.zeros((image.height, image.width), dtype=np.uint8) combined_masks_by_class[category_name] = np.maximum(combined_masks_by_class[category_name], mask_resized_np) # Store merged masks by class for category_name, combined_mask in combined_masks_by_class.items(): combined_mask_image = Image.fromarray(combined_mask) combined_mask_image.save(os.path.join(output_dir, f'combined_mask_{category_name}.png')) print(f"Results saved in {output_dir}")Additional Recommendations:
- Check the Model Output: Ensure that your model is indeed capable of generating masks. You can test this by running standard inference without slicing to verify mask generation.
- Review SAHI Documentation: For more detailed guidance, refer to the SAHI Tiled Inference Guide.
If the issue persists, please provide any error messages or additional details that could help us diagnose the problem further.
Feel free to reach out if you have any further questions or need additional assistance. We're here to help! 😊
-
pinnintipraneethkumar
Hi, @illink7 can you please check the sahi version, i have the same problem i fixed this by updating the sahi library.
Rohan-Python
@pinnintipraneethkumar Do you know how to disable bounding boxes on segmented images?
mxxxbbbb
When I use Sahi, the inference speed is normal in a single main thread, but when I load Sahi into one of multiple threads, it causes the inference speed to be much slower. What is the reason for this
1
Glenn JocherUltralyticsHello! It sounds like you're experiencing a slowdown when running SAHI in a multi-threaded environment. This can often happen due to the increased overhead of managing multiple threads, especially if the threads are competing for limited computational resources like CPU or GPU.
Here are a couple of suggestions to potentially improve the performance:
-
Ensure Adequate Resources: Make sure that your system has enough resources (CPU/GPU) to handle multiple threads efficiently. If each thread is performing intensive computations, you might need more powerful hardware or fewer concurrent threads.
-
Optimize Thread Management: Try to manage the number of threads based on the workload and the hardware capabilities. Sometimes, reducing the number of threads can actually improve overall performance.
-
Profile Your Application: Use profiling tools to understand where the bottlenecks are. This can provide insights into whether the issue is due to thread management, I/O operations, or something else.
If you continue to experience issues, consider providing more details about your setup, such as the hardware specifications and the specific code implementation, so we can help you better. 🚀
-
mxxxbbbb
I have loaded many deep learning models in one thread across multiple threads, among which sahi is one. When testing with a single main thread, the speed is not a problem, but with multiple threads, the speed is very slow
mxxxbbbb
def pre_sahi(self,img_all):
resize_img = self.resize_long_image(img_all,640) t1 = time.time() slice_image_result = slice_image( image=resize_img, slice_height=640, slice_width=640, overlap_height_ratio=0.2, overlap_width_ratio=0.2, auto_slice_resolution=True, ) result = get_sliced_prediction( resize_img, slice_image_result, self.model_sahi, ) box = [] t2 = time.time() print("time:",t2-t1)This is my call to this class's function in a certain thread, and the model is actually loaded by the constructor
Glenn JocherUltralyticsHello! It sounds like you're encountering a common issue when using multiple threads for inference, especially when each thread is loading a deep learning model like SAHI. This slowdown can occur due to several reasons:
-
Resource Contention: Each thread may be competing for limited computational resources (CPU/GPU). This is particularly significant if each model in the threads is substantial in size and computational demand.
-
Global Interpreter Lock (GIL) in Python: If you're using Python, the GIL can become a bottleneck in multi-threaded applications, as it allows only one thread to execute Python bytecode at a time. This can severely limit performance when using CPU-bound tasks.
-
I/O Bottlenecks: If your application involves reading from or writing to disk, such as loading models or accessing data, I/O operations can become a bottleneck when scaled across multiple threads.
To mitigate these issues, consider the following:
- Use multiprocessing instead of multithreading: This can help bypass the GIL and better utilize multiple cores.
- Optimize resource allocation: Ensure that each thread or process has enough resources to perform its tasks efficiently.
- Load models once: If possible, load your models once and share them across threads, rather than loading them separately in each thread.
For more detailed guidance, you might find our documentation on using YOLOv8 with SAHI helpful: Using YOLOv8 with SAHI for Sliced Inference. This guide provides insights into efficiently managing resources and optimizing inference tasks.
-
illink7
How I can combine SAHI with segmentation model for aerial images, could you provide me some details please
1
Paula DerrengerUltralytics(수정됨)Certainly! To use SAHI with a segmentation model for aerial images, you can follow these steps to integrate and run sliced inference, which is particularly useful for large images, like those taken from aerial perspectives.
-
Install SAHI and Ultralytics:
pip install -U ultralytics sahi -
Import Necessary Modules:
from sahi.predict import get_sliced_prediction from sahi import AutoDetectionModel -
Download and Set Up Your Model: Assuming you have a trained segmentation model, ensure it's compatible with SAHI. You can convert YOLO models to work with SAHI quite easily.
-
Perform Sliced Inference: Here’s how to perform sliced inference on an aerial image:
# Assuming 'model_path' is the path to your trained model detection_model = AutoDetectionModel.from_pretrained( model_type='ultralytics', model_path=model_path, confidence_threshold=0.3, device="cpu", ) # Perform sliced prediction result = get_sliced_prediction( image="path/to/aerial_image.jpg", detection_model=detection_model, slice_height=256, slice_width=256, overlap_height_ratio=0.2, overlap_width_ratio=0.2 ) -
Visualize Results: You can visualize or further process the detection results as needed.
This approach helps handle large aerial images by dividing them into smaller, manageable slices, running the segmentation, and then combining the results. Let me know if you need more details on any specific part! 🚀
-
illink7
I did it, but unfortunately I receive the same result, detection instead segmentation
Paula DerrengerUltralytics(수정됨)Hello! SAHI supports sliced inference for segmentation too, as long as you load a segmentation model and use a recent
sahiversion. If you're not getting masks, check that:- Correct Model: The model loaded is a segmentation model (for example
yolo26n-seg.ptor your own trained-segweights), not a detection model. - Model Type: You load it with
AutoDetectionModel.from_pretrained(model_type="ultralytics", ...). - Versions: Both packages are current (
pip install -U ultralytics sahi). Masks are then available asprediction.maskon each item inresult.object_prediction_list.
If you need more specific guidance or a code example, let me know, and I'd be happy to help further! Cheers! 🚀
- Correct Model: The model loaded is a segmentation model (for example
illink7
Okay, I'll provide my code and maybe we solve this together
from sahi import AutoDetectionModel
detection_model = AutoDetectionModel.from_pretrained( model_type='yolov8', model_path=r"D:\best.pt", confidence_threshold=0.7, device="cuda:0", # or 'cuda:0' )
result = get_sliced_prediction( r"D:\img_0_shift.png", detection_model, slice_height=600, slice_width=600, overlap_height_ratio=0.2, overlap_width_ratio=0.2 )
result.export_visuals(export_dir="demo_data/") Image("demo_data/prediction_visual.png")
My best.pt is definitely segmentation model, but instead of doing this it does detection
Paula DerrengerUltralytics(수정됨)Hello! It seems like there's an issue with the setup where you're expecting segmentation results but getting detection results instead. Here's a couple of things you could check:
-
Model Type: Ensure that the model loaded is specifically trained for segmentation tasks. Even though the file might be named aptly, it's crucial to confirm its internals are meant for segmentation.
-
Model Loading: Use
AutoDetectionModel.from_pretrained(model_type="ultralytics", model_path=...), which also loads Ultralytics segmentation models, and make suresahiandultralyticsare up to date. -
Output Handling: Segmentation masks are returned on each prediction as
prediction.mask(for exampleprediction.mask.bool_mask) inresult.object_prediction_list, so check that your code reads them from there.
If none of these checks resolve the issue, consider sharing more details about the model and its training specifics. This will help narrow down the issue! 😊 Keep me posted on your progress!
-
illink7
Hi, I've tried a lot to find a solution on the internet, but I haven't found any results. What can you recommend or suggest to me?
illink7
recall - my purpose is segment objects from aerial images with my custom model
illink7
Yeah, I did it. Here is my code
import os from sahi import AutoDetectionModel from sahi.predict import get_sliced_prediction from PIL import Image
import torchIMAGE_PATH = r"C:\Users\ILLINK\Desktop\img4test\img_10_shift.png"
MODEL_PATH = r"C:\Users\ILLINK\Desktop\img4test\best.pt"
image = Image.open(IMAGE_PATH)
detection_model = AutoDetectionModel.from_pretrained( model_type='yolov8', model_path=MODEL_PATH, confidence_threshold=0.25, device="cuda" if torch.cuda.is_available() else "cpu" )
result = get_sliced_prediction( image, detection_model, slice_height=512, slice_width=512, overlap_height_ratio=0.2, overlap_width_ratio=0.2 )
output_dir = 'output' os.makedirs(output_dir, exist_ok=True) result.export_visuals(export_dir=output_dir, file_name='segmented_image.png')
print(f"Результати збережено в {output_dir}")
How I can get rid of bboxes and labels, just pure result, please help me?
Paula DerrengerUltralytics(수정됨)Hello! SAHI exposes the masks on each prediction rather than on the result, so you can skip
export_visualsand save only the masks without boxes or labels. Each item inresult.object_prediction_listhas amaskattribute (prediction.mask.bool_mask) when you use a segmentation model:import os import numpy as np from PIL import Image # 'result' is the PredictionResult from get_sliced_prediction() and 'image' is the PIL image you passed in combined_mask = np.zeros((image.height, image.width), dtype=np.uint8) for prediction in result.object_prediction_list: if prediction.mask is not None: combined_mask = np.maximum(combined_mask, prediction.mask.bool_mask.astype(np.uint8) * 255) Image.fromarray(combined_mask).save(os.path.join(output_dir, "segmentation_mask.png"))This requires a segmentation model loaded with
model_type="ultralytics"and a recentsahiversion. 👍Feel free to reach out if you need further clarification or assistance!
illink7
I combined your and my code there
import os from sahi import AutoDetectionModel from sahi.predict import get_sliced_prediction from PIL import Image import numpy as np import torch
IMAGE_PATH = r"C:\Users\ILLINK\Desktop\img4test\img_10_shift.png" MODEL_PATH = r"C:\Users\ILLINK\Desktop\img4test\best.pt"
Load the image
image = Image.open(IMAGE_PATH)
Load the detection model
detection_model = AutoDetectionModel.from_pretrained( model_type='yolov8', model_path=MODEL_PATH, confidence_threshold=0.25, device="cuda" if torch.cuda.is_available() else "cpu" )
Get sliced prediction
result = get_sliced_prediction( image, detection_model, slice_height=512, slice_width=512, overlap_height_ratio=0.2, overlap_width_ratio=0.2 )
Define output directory and create it if it doesn't exist
output_dir = 'output' os.makedirs(output_dir, exist_ok=True)
Simplified approach to handle masks and save them as an image
if hasattr(result, 'masks'): masks = result.masks # Your masks would come from your model's output combined_mask = np.max(masks, axis=0) # Combine masks assuming each is a separate channel
# Convert to a uint8 grayscale image mask_image = Image.fromarray((combined_mask * 255).astype(np.uint8)) mask_image.save(os.path.join(output_dir, 'segmentation_mask.png')) print(f"Результати збережено в {output_dir}")else: print("No masks found in the result. Ensure the model outputs segmentation masks.")
And there is result - No masks found in the result. Ensure the model outputs segmentation masks. Help me solve problem please
Paula DerrengerUltralytics(수정됨)Hello! The error message "No masks found in the result. Ensure the model outputs segmentation masks" means the predictions have no
mask. SAHI does return masks for Ultralytics segmentation models, so check the following:- Model: Confirm
best.ptis a segmentation model (it should load astask=segment), and that you load it withAutoDetectionModel.from_pretrained(model_type="ultralytics", model_path="best.pt", ...). - Versions: Update both packages with
pip install -U ultralytics sahi. Oldersahireleases don't return masks. - Verify: Run the plain Ultralytics model on one image (
YOLO("best.pt")("image.jpg")[0].masks) and then checkprediction.maskon the items ofresult.object_prediction_list.
If you need further assistance, feel free to reach out! Happy coding! 😊
1- Model: Confirm
illink7
But here is AttributeError: 'dict' object has no attribute 'eval'. What you can recommend me please?
Paula DerrengerUltralyticsHey there! It looks like you're trying to call
.eval()on a dictionary object, which isn't possible since.eval()is a method typically used on PyTorch models to set them to evaluation mode. If you're trying to set your model to evaluation mode, make sure you're applying.eval()to your model instance, not a dictionary.Here's a quick example to ensure you're using it correctly:
import torch # Assuming 'model' is your loaded model instance model = torch.load('path_to_your_model.pt') model.eval() # Set the model to evaluation modeMake sure that
modelis indeed a PyTorch model object. If you're loading the model and it's returning a dictionary (perhaps with model state and other metadata), you'll need to properly extract the model from that dictionary. Typically, this looks something like:model = torch.load('path_to_your_model.pt') actual_model = model['model'] # Adjust the key according to how your model is saved actual_model.eval()Hope this helps! Let me know if there's anything else you need! 😊
illink7
thank you for your reply but unfortunately I didn't get the result I wanted or I don't understand your idea. initially I wanted to use the YOLOv8-seg model to segment aerial images. it worked for me, but along with the result there were labels and bounding boxes that hindered me. maybe there is a way to export the segmented image (data) separately or any other ideas? I am very grateful for your attention and tips, thank you so much
Paula DerrengerUltralytics(수정됨)Hello! Thank you for your feedback and for sharing your experience with the YOLOv8-seg model. It sounds like you're looking to extract just the segmentation masks without the bounding boxes and labels. With SAHI, every item in
result.object_prediction_listcarries its mask, so you can save only the mask data:import os import numpy as np from PIL import Image # 'result' is the PredictionResult from get_sliced_prediction() and 'image' is the PIL image you passed in combined_mask = np.zeros((image.height, image.width), dtype=np.uint8) for prediction in result.object_prediction_list: if prediction.mask is not None: combined_mask = np.maximum(combined_mask, prediction.mask.bool_mask.astype(np.uint8) * 255) Image.fromarray(combined_mask).save(os.path.join(output_dir, "segmentation_mask.png"))This works with segmentation models loaded via
AutoDetectionModel.from_pretrained(model_type="ultralytics", ...). If you need more specific guidance, please let me know more about your model and setup! 😊illink7
Thank you so much
Paula DerrengerUltralytics(수정됨)Hello! Thanks for reaching out. 🌟
If you're looking to use YOLOv8-seg for segmentation and want to export the segmented images without bounding boxes and labels, you can read the masks from the SAHI result:
import os import numpy as np from PIL import Image # 'result' is the PredictionResult from get_sliced_prediction() and 'image' is the PIL image you passed in combined_mask = np.zeros((image.height, image.width), dtype=np.uint8) for prediction in result.object_prediction_list: if prediction.mask is not None: combined_mask = np.maximum(combined_mask, prediction.mask.bool_mask.astype(np.uint8) * 255) Image.fromarray(combined_mask).save(os.path.join(output_dir, "segmentation_mask.png"))This saves the segmentation masks directly, bypassing any bounding boxes or labels. Hope this helps! Let me know if there's anything else you need. 😊
傅民 王
Thanks for your sharing, I would like to know that if the performance will be a side-effect. Do you have any report to compare the performance with and without SAHI? Thanks!
Paula DerrengerUltralyticsYes—SAHI generally trades inference speed for better small-object detection because it runs detection on multiple slices and merges the results. The original SAHI paper, Tables 1–2, compares full-image and sliced inference, reporting gains of up to 6.8 AP points for the evaluated detectors—not YOLO26—and discusses the additional computational cost. (arxiv.org) This guide does not provide a numerical YOLO26 speed comparison. For your application, compare mAP and end-to-end latency on the same images and hardware, including slicing and merging time.
Mz-Han
Hello. When using YOLO26 for instance segmentation, I encountered an issue of insufficient CUDA memory and plan to solve it through slice inference. The current environment is torch==1.13.1+cu116, torchvision==0.14.1+cu116, torchaudio==0.13.1, while installing the latest version of Sahi requires PyTorch 2.4+ as a prerequisite. Is there a version of Sahi that can be installed without upgrading PyTorch? It would be greatly appreciated if you could provide an example of inference visualization. I have installed sahi==0.11.15, but this version cannot output masks
Glenn JocherUltralytics@Mz-Han
sahi==0.11.15is too old for Ultralytics segmentation mask support, so gettingmask=Nonethere is expected; mask output was added in newer SAHI releases (see the SAHI maintainer note in obss/sahi discussion #1000) and the current SAHI PyPI page shows the latest version stream. If you’re pinned totorch==1.13.1, you can install/upgrade SAHI without forcing a PyTorch upgrade by avoiding dependency resolution, but to actually run YOLO26-seg + sliced masks you’ll likely need a newer PyTorch/Ultralytics stack—please paste!yolo checks(orultralytics.utils.checks.collect_system_info()) and your full install command here so we can confirm what’s constraining your environment; also restart your notebook kernel after any upgrades. For visualization (without SAHI), this works for YOLO26 segmentation:from ultralytics import YOLO model = YOLO("yolo26n-seg.pt") results = model.predict(source="path/to/image.jpg", imgsz=640, conf=0.25, device=0) results[0].plot(pil=True).save("pred.png")If you do proceed with SAHI, the parameter/visual export pattern is the same as in the SAHI sliced inference guide, but you’ll need a newer SAHI version than
0.11.15to get masks.Mz-Han
Hello, thank you for your reply. I have upgraded PyTorch. How can I display only the mask? My image resolution is 3807*3000, with over a thousand small targets. How can I improve the running speed
Glenn JocherUltralytics@Mz-Han To visualize/save only the mask, render the pixel-space polygons from
results[0].masks.xy(see theMasksoutputs in Predict mode docs) into a blank image, e.g.:from ultralytics import YOLO import cv2 import numpy as np model = YOLO("yolo26n-seg.pt") img_path = "path/to/image.jpg" img = cv2.imread(img_path) h, w = img.shape[:2] r = model.predict(source=img_path, device=0, imgsz=1280, conf=0.25)[0] mask_img = np.zeros((h, w), dtype=np.uint8) if r.masks is not None: for seg in r.masks.xy: # list of Nx2 polygons in pixel coords cv2.fillPoly(mask_img, [seg.astype(np.int32)], 255) cv2.imwrite("mask.png", mask_img)For speed on 3807×3000 with 1000+ small objects, sliced inference runtime is dominated by slice count, so increase
slice_height/slice_widthand reduce overlap until you stop missing small targets (and if you’re on NVIDIA, exporting to TensorRT can help; if you share!yolo checksplus your current slice settings we can suggest concrete values based on your GPU/CPU).
YWJ-sudo
你好,请问预测时出现预测的锚框的长或宽为0的无效锚框,那么其是否会自动过滤这些无效锚框
Glenn JocherUltralytics@YWJ-sudo 一般情况下 Ultralytics 的后处理不会返回宽或高为 0 的检测框;若在 SAHI 切片拼接或导出为 COCO 后仍出现,可在评估前用 SAHI 的
remove_invalid_coco_results进行清理,示例:from sahi.utils.coco import remove_invalid_coco_results from sahi.utils.file import save_json coco_results = remove_invalid_coco_results("coco_results.json") # 过滤无效/越界框 save_json(coco_results, "coco_results_fixed.json")函数示例见SAHI COCO utilities 文档。
YWJ-sudo
Hello author, I am encountering an issue when using SAHI for sliced prediction. When I change the model_path in detection_model to the path of my locally trained weight file (best.pt), it produces an error. However, if I rename best.pt to yolov8n.pt, it can run successfully.What's going on here? How should I quickly use my own weight file?
ERROR:TypeError: ('model_path is not a valid Ultralytics model path: ', SSLError(MaxRetryError('HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: /repos/ultralytics/assets/releases/tags/v8.3.0 (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available."))')))
YWJ-sudo
Sorry, it's been resolved now. There were just some minor errors.
1
Fatih AkyonUltralytics@YWJ-sudo I'm glad to hear it's resolved. Feel free to tag me if you have any more SAHI related issues (SAHI author and core maintainer here) 👍🏻
1YWJ-sudo
How to obtain the COCO format annotation file after batch patch prediction?
YWJ-sudo
如果我有一张16001600的图片,在预处理阶段我先将其切割成4个切片,即每个切片尺寸为400400,并以此训练模型,而当预测时,我在原图上进行预测,且使用sahi切片推理并将切片高宽设置为400,这会不会得到更好的效果
Fatih AkyonUltralytics@YWJ-sudo when you provide
coco_dataset_jsonto sahi predict cli or function, it saves results in coco prediction format: cli usage | functional usageOr you can convert each ObjectPrediction object to COCO prediction format by using
object_prediction.to_coco_prediction(image_id=...): https://github.com/obss/sahi/blob/e7109ee3a117d421f64ed383ad4a073b8ff7b000/sahi/prediction.py#L122
Fatih AkyonUltralytics@YWJ-sudo based on your second question. If you are cropping images to 400x400, then resize to 640 on training (ultralytics does this by default), then you should use 400 as slice height and width in
sahi, and that's all (sahi ultralytics integration resizes crops to 640 before inference by default).Optionally (I don't suggest this unless you know what you are doing), if you want
sahito crop images to 400, then perform inference without resizing the crops, then you should set image_size in UltralyticsDetectionModel/AutoDetectionModel class as 400.
MasIgor
Hello! Could you please elaborate on how the training would work? Lets say i have 100 images with 24 mpx. what would the steps be? would I label the 24 mpx images and use sahi for training? if yes, how? or would i have to extract 640x640 images on my own from the 24mpx images and do the training as usual? any help is greatly appreciated.
thanks
Tony2408
Hi. I've applied sahi and yolov11 to infer the bbox of small instance. What confuses me is that multiple local regions of some large targets are individually detected as bounding boxes. I would like to ask: Can this situation only be mitigated by adjusting the slice size and confidence threshold? Are there parameters similar to global NMS that can be adjusted?
Fatih AkyonUltralyticsHey, SAHI author here 👋🏻
@Tony2408, it means your slices are smaller than your objects. You can slightly increase the slice height and width or you apply Non-Maximum-Merging to combine these predictions that belong to same object.
1Tony2408
Great work! Thank you for your reply. Increasing the height and width of the slices for the same image has significantly improved the results. However, I'm wondering if this means that when my task requires identifying multi-scale targets in an image, it's difficult for Sahi's inference method to cover all scales?
Glenn JocherUltralytics@Tony2408 You've identified an important challenge with tiled inference approaches. When dealing with multi-scale targets, there's indeed a trade-off: larger slices work better for big objects but might be less efficient for small ones, while smaller slices excel at detecting tiny objects but fragment larger ones.
Some approaches to handle multi-scale targets with SAHI:
- Use multiple inference passes with different slice configurations and merge results
- Adjust overlap ratios (higher overlap can help with object fragmentation)
- Customize the Non-Maximum-Merging parameters to better combine fragmented detections
The SAHI library continues to evolve with improvements to handle these scenarios. For particularly challenging multi-scale detection tasks, you might also consider combining SAHI with model ensembling techniques for more robust results.

