Pytorch 中的accimage

什么是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库不了解,就不写。

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值