I wonder why Pytorch uses PIL not the cv2

I wonder why Pytorch uses PIL not the cv2.
As the article(https://www.kaggle.com/vfdev5/pil-vs-opencv) says, cv2 is three times faster than PIL.
I did an experiment and I really think so.
But why does Pytorch use PIL?

3 Likes

If PyTorch can remove it’s dependency on PIL and use OpenCV 3 instead, it would help me work with multi-channel images that are greater than 3-channels. At the moment, if you pass a 10-ch image or 10-ch mask, PIL truncates it to a 3-ch image.

See these issues:


1 Like

The only thing that uses PIL is the torchvision package. Rewriting it to support cv2 probably it’s not such a huge effort, since you just have to replace functions call here and there - if you have experience with cv2 you could probably try that yourself!

2 Likes

If you want a performance increase, you could install Pillow-SIMD instead of Pillow. This is a drop-in replacement without any code changes.

4 Likes

Thanks for the heads up, I’ll try that out.

If you finish it, will you make it public like on github? That will be beneficial to many others.

Yes, I’ll will. I ran into this issue with PIL last week, trying to work with multi-channel images. Replacing the TorchVision’s dependency on PIL sounds like the right thing to do. It looks like TorchVision library already supports multiple imaging backends, so it should be a matter of adding a cv2 backend.

I’ll let you know once I’m done.

3 Likes

Thank you very much!

I have rewritren the “transforms” in torchvision package with cv2.

4 Likes

I also have the same question.

May be because cv2 is a heavier module, and PIL is a lighter module. Not going for a heaver module, if we could get our work done just by a lighter one.

2 Likes

With the recent addition of torchvision.io, now might be a good time to consider adding an option to use opencv as the image backend.