MXNet - 增强 - 预期uint8,得到float32

时间:2018-04-22 00:19:47

标签: image python-2.7 image-processing mxnet

我正在尝试使用mxnet 1.10/mxnet-cu91进行图像分类。我目前正在尝试使用mxnet.image.ImageIter进行迭代  和预处理图像。我已成功使用Augmenters预处理图像,但在使用Augmenters时遇到以下错误(唯一的例外是ForceResizeAug):

Traceback (most recent call last):
  File "image.py", line 22, in <module>
    for batch in iterator:
  File "/usr/local/lib/python2.7/dist-packages/mxnet/image/image.py", line 1181, in next
    data = self.augmentation_transform(data)
  File "/usr/local/lib/python2.7/dist-packages/mxnet/image/image.py", line 1239, in augmentation_transform
    data = aug(data)
  File "/usr/local/lib/python2.7/dist-packages/mxnet/image/image.py", line 659, in __call__
    src = t(src)
  File "/usr/local/lib/python2.7/dist-packages/mxnet/image/image.py", line 721, in __call__
    gray = src * self.coef
  File "/usr/local/lib/python2.7/dist-packages/mxnet/ndarray/ndarray.py", line 235, in __mul__
    return multiply(self, other)
  File "/usr/local/lib/python2.7/dist-packages/mxnet/ndarray/ndarray.py", line 2566, in multiply
    None)
  File "/usr/local/lib/python2.7/dist-packages/mxnet/ndarray/ndarray.py", line 2379, in _ufunc_helper
    return fn_array(lhs, rhs)
  File "<string>", line 46, in broadcast_mul
  File "/usr/local/lib/python2.7/dist-packages/mxnet/_ctypes/ndarray.py", line 92, in _imperative_invoke
    ctypes.byref(out_stypes)))
  File "/usr/local/lib/python2.7/dist-packages/mxnet/base.py", line 146, in check_call
    raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: [20:02:07] src/operator/contrib/../elemwise_op_common.h:123: Check failed: assign(&dattr, (*vec)[i]) Incompatible attr in node  at 1-th input: expected uint8, got float32

Stack trace returned 10 entries:
[bt] (0) /usr/local/lib/python2.7/dist-packages/mxnet/libmxnet.so(+0x2ab9a8) [0x7f5c873f09a8]
[bt] (1) /usr/local/lib/python2.7/dist-packages/mxnet/libmxnet.so(+0x2abdb8) [0x7f5c873f0db8]
[bt] (2) /usr/local/lib/python2.7/dist-packages/mxnet/libmxnet.so(+0x2d2078) [0x7f5c87417078]
[bt] (3) /usr/local/lib/python2.7/dist-packages/mxnet/libmxnet.so(+0x2d2b83) [0x7f5c87417b83]
[bt] (4) /usr/local/lib/python2.7/dist-packages/mxnet/libmxnet.so(+0x24c4c1e) [0x7f5c89609c1e]
[bt] (5) /usr/local/lib/python2.7/dist-packages/mxnet/libmxnet.so(+0x24c6e59) [0x7f5c8960be59]
[bt] (6) /usr/local/lib/python2.7/dist-packages/mxnet/libmxnet.so(+0x240539b) [0x7f5c8954a39b]
[bt] (7) /usr/local/lib/python2.7/dist-packages/mxnet/libmxnet.so(MXImperativeInvokeEx+0x63) [0x7f5c8954a903]
[bt] (8) /usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_call_unix64+0x4c) [0x7f5cc334ae40]
[bt] (9) /usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_call+0x2eb) [0x7f5cc334a8ab]

复制问题所需的代码如下(为简洁而缩短,与documentation中提供的代码非常相似):

import mxnet as mx
import glob

type1_paths = glob.glob('type1/*.jpg')
type1_list = [[1.0, path] for path in type1_paths]

type2_paths = glob.glob('type2/*.JPG')
type2_list = [[2.0, path] for path in type2_paths]
all_paths = type1_list + type2_list
iterator = mx.image.ImageIter(1, (3, 1000, 1000),
                              imglist=all_paths,
                              aug_list=[
                              mx.image.ColorJitterAug(0.1, 0.1, 0.1),
                              ])
for batch in iterator:
    print batch.data

我不确定错误发生的原因,因为我没有使用任何可能影响dtype差异的自定义增强器。我在使用以下内容时也复制了这个问题:

  • RandomGrayAug
  • HueJitterAug
  • ContrastJitterAug
  • SaturationJitterAug

注意:如果这很重要,我知道加载的jpg/JPG之间的唯一区别是有些照片是使用手机拍摄的,有些是使用数码单反相机拍摄的。

如果我遗漏任何有助于学习的信息,请告诉我。

1 个答案:

答案 0 :(得分:2)

您收到此问题是因为图片加载的数据类型为int8,但扩充期望数据类型为float32。遗憾的是,由于输入图像(int8)与对比度抖动(float32)相乘,因此错误消息会向后读取您在此情况下需要执行的操作。它抱怨对比度抖动的数据类型而不是输入数据。与色调和饱和度增强器相同的问题。

因此,要解决此问题,您需要将输入图像数据类型转换为float32。您可以通过在增强器列表的开头添加mx.image.CastAug(typ='float32')来完成此操作。

iterator = mx.image.ImageIter(1, (3, 100, 100),
                              path_root=".",
                              imglist=all_paths,
                              aug_list=[
                                    mx.image.CastAug(typ='float32'),
                                    mx.image.ColorJitterAug(0.1, 0.1, 0.1),
                                    mx.image.CenterCropAug((100,100))
                              ])

在扩充后可视化您的数据始终是一个好主意,以确认正在按预期应用这些步骤。