什么是accimage
先要介绍PIL,Python Image Library。
这是一个python自带的图片处理库,其保存的图片文件可以叫做PIL_image,是一种特殊结构。
也就是说,哪怕你根本不知道pytorch是什么,在其他的python图像处理任务中也可能用到PIL。
而在pytorch中,开发团队为了解决图片处理的性能瓶颈问题,改写了PIL_image类,
简化了一些信息,就成了acc_image这个类。
//参考https://github.com/pytorch/accimage/issues/12
accimage is a partial replacement for PIL.Image (only RGB JPEG images, and only a subset of image transformations used in torch.vision) on top of JPEG-Turbo and Intel IPP. In the early days of PyTorch I noticed that on desktop processors performance of PIL.Image can easily becomes the bottleneck when training lightweight networks on multiple GPUs, and wrote accimage to work around this bottleneck.
在torchvision的_init_.py文件中也可以看到
def set_image_backend(backend):
"""
Specifies the package used to load images.
Args:
backend (string): Name of the image backend. one of {'PIL', 'accimage'}.
The :mod:`accimage` package uses the Intel IPP library. It is
generally faster than PIL, but does not support as many operations.
"""
global _image_backend
if backend not in ['PIL', 'accimage']:
raise ValueError("Invalid backend '{}'. Options are 'PIL' and 'accimage'"
.format(backend))
_image_backend = backend
accimage使用的是Intel IPP Library,一般情况下会比Python Image Library快,只不过很少有项目支持。
要具体去理解这个问题,我们可以看看PIL_image有多麻烦。
class Image(object):
"""
This class represents an image object. To create
:py:class:`~PIL.Image.Image` objects, use the appropriate factory
functions. There's hardly ever any reason to call the Image constructor
directly.
* :py:func:`~PIL.Image.open`
* :py:func:`~PIL.Image.new`
* :py:func:`~PIL.Image.frombytes`
"""
def __init__(self):
# FIXME: take "new" parameters / other image?
# FIXME: turn mode and size into delegating properties?
self.im = None
self.mode = ""
self._size = (0, 0)
self.palette = None
self.info = {}
self.category = NORMAL
self.readonly = 0
self.pyaccess = None
可以看到除了基本的数据以外,还存了诸如palette,category,pyaccess等很多别的内容。
还有一堆copy,save,dump,__getstate__方法。
简化image对象除了数据以外的无效成员,可以优化空间和处理速度。
IPP库不了解,就不写。