Flat edges on segmentation masks? #8230
Replies: 2 comments 16 replies
-
Hi there! 👋 Thanks for bringing this to our attention. The flat edges you're observing on segmentation masks are likely due to the limitations of the model's architecture in capturing the finer details at the boundaries of objects, especially for smaller objects. One potential way to mitigate this is to experiment with different segmentation head architectures or loss functions that may be more sensitive to boundary details. Additionally, post-processing techniques like CRF (Conditional Random Fields) can sometimes help refine the edges of segmentation masks. Here's a quick example of how you might apply CRF post-processing: from ultralytics import YOLO
import pydensecrf.densecrf as dcrf
from pydensecrf.utils import unary_from_softmax
# Load your model
model = YOLO('yolov8m-seg.pt')
# Perform prediction
results = model('path/to/image.jpg')
# Assuming you have a single image and mask
predicted_mask = results[0].masks.data[0] # Get the first predicted mask
# Prepare the unary potentials and run CRF
softmax = predicted_mask.softmax(dim=0).cpu().numpy() # Convert to softmax
unary = unary_from_softmax(softmax)
d = dcrf.DenseCRF2D(predicted_mask.shape[1], predicted_mask.shape[0], 2) # width, height, n_classes
d.setUnaryEnergy(unary)
# You can set your own hyperparameters for CRF here
d.addPairwiseGaussian(sxy=3, compat=3)
d.addPairwiseBilateral(sxy=80, srgb=13, rgbim=image, compat=10)
# Run inference
q = d.inference(5)
refined_mask = np.argmax(np.array(q), axis=0).reshape((predicted_mask.shape[1], predicted_mask.shape[0]))
# Now `refined_mask` is your CRF-refined mask Please note that the above code is just a starting point and you'll need to install We're always working on improving our models, and your feedback is valuable in this process. If you have any more insights or questions, feel free to share! 🚀 |
Beta Was this translation helpful? Give feedback.
-
Have you solved this problem? |
Beta Was this translation helpful? Give feedback.
-
I've noticed that YOLOv8 segmentation masks have flat edges where the mask is adjacent to the bounding box of the detected instance. This is most noticeable on the top edge of the mask, but it occurs on all four edges of the mask. These flat edges result in an incorrect segmentation mask, and lead to incorrect measurements of object size and shape, especially for small objects.
The issue exists both with the YOLOv8 pretrained models, as well as models I've trained using transfer learning with one of the pretrained models as a backbone.
What is the cause of this segmentation mask error? Is there any way to fix or improve it?
Example (note the flat tops of all the puppies heads):
Things I've tried:
imgsz
: I've gone as far as to run prediction on an image the same size as the model, so that no resizing takes place. Issue still persists.retina_masks=True
during prediction. This does not seem to affect the output at all.This issue can be easily reproduced with the CLI like this:
yolo predict task=segment model=yolov8m-seg.pt imgsz=640 source=https://ultralytics.com/images/zidane.jpg show_boxes=False
Output (cropped, to show the issue):
Beta Was this translation helpful? Give feedback.
All reactions