Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add yolov8 to SAHI #833

Merged
merged 6 commits into from
Feb 23, 2023
Merged

Add yolov8 to SAHI #833

merged 6 commits into from
Feb 23, 2023

Conversation

NguyenTheAn
Copy link
Contributor

Add Yolov8DetectionModel class to infer Yolov8 with SAHI

@fcakyon
Copy link
Collaborator

fcakyon commented Feb 21, 2023

@NguyenTheAn thanks a lot!

Can you please reformat your code following the steps in https://github.com/obss/sahi#contributing ?

And can you also add some tests in a file named test_yolov8model.py? You can take the yolov5 tests as reference: https://github.com/obss/sahi/blob/main/tests/test_yolov5model.py

@NguyenTheAn
Copy link
Contributor Author

Yes, I will do that

requirements.txt Outdated Show resolved Hide resolved
@NguyenTheAn NguyenTheAn requested a review from fcakyon February 21, 2023 13:43
.github/workflows/ci.yml Outdated Show resolved Hide resolved
@fcakyon
Copy link
Collaborator

fcakyon commented Feb 21, 2023

All tests pass, thank you!

One final request: Would you mind adding a demo notebook for yolov8 as https://github.com/obss/sahi/blob/main/demo/inference_for_yolov5.ipynb

@NguyenTheAn
Copy link
Contributor Author

yes, I added it

@fcakyon fcakyon added this pull request to the merge queue Feb 23, 2023
Merged via the queue into obss:main with commit 906dd4d Feb 23, 2023
@mayrajeo
Copy link
Contributor

mayrajeo commented Feb 27, 2023

Correct me if I'm wrong, but I think that perform_inference should convert the image to BGR before predicting. My reasoning is that when we pass numpy array or PIL image to the model for inference, YOLOv8 uses LoadPilAndNumpy for processing them. This class has functions

    @staticmethod
    def _single_check(im):
        assert isinstance(im, (Image.Image, np.ndarray)), f'Expected PIL/np.ndarray image type, but got {type(im)}'
        if isinstance(im, Image.Image):
            if im.mode != 'RGB':
                im = im.convert('RGB')
            im = np.asarray(im)[:, :, ::-1]
            im = np.ascontiguousarray(im)  # contiguous
        return im
        
    def _single_preprocess(self, im, auto):
        if self.transforms:
            im = self.transforms(im)  # transforms
        else:
            im = LetterBox(self.imgsz, auto=auto, stride=self.stride)(image=im)
            im = im.transpose((2, 0, 1))[::-1]  # HWC to CHW, BGR to RGB
            im = np.ascontiguousarray(im)  # contiguous
        return im

First, the image is checked with _single_check which converts the image to BGR only if its Image.Image. Then, before the model performs inference, _single_preprocess converts it from BGR to RGB unless there are transforms to apply (default=None). However, since im here is a numpy array with channel ordering RGB, _single_preprocess converts it to BGR.

Example notebook: https://nbviewer.org/gist/mayrajeo/033e842b7593e58135c7694944905937

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants