MXNet源码解读:数据读取高级类(2)— mxnet.image.ImageIter

mxnet.image.ImageIter接口同样继承自MXNet框架下的基础数据迭代器构造类mxnet.io.DataIter,该接口是python代码实现的图像数据迭代器,且既可读取.rec文件,也可以以图像+.lst方式来读取数据,这是和mxnet.io.ImageRecordIter接口不同的地方。由于mxnet.image.ImageIter接口在以原图像+.lst文件形式读取数据时是基于python代码实现的,因此在速度上会比基于C++代码实现的mxnet.io.ImageRecordIter接口效率低,尤其是当数据是存储在机械硬盘上时。当然python实现版本可读性还是比较强的,接下来就介绍下该接口的源码,基本上代码都在github的~mxnet/python/mxnet/image/image.py脚本中。因此该博客是介绍该接口的源码,而关于如何使用该接口读取数据并应用在实际模型训练中可以参考另一篇博客:MXNet如何用mxnet.image.ImageIter直接导入图像

mxnet.image.ImageIter类源码地址

源码如下。由另一篇博客MXNet源码解读:数据读取基础类—mxnet.io.DataIter中关于mxnet.io.DataIter基础类的介绍可以看出主要关注的方法是初始化init和next方法。其他方法一般是这两个方法的辅助方法。

class ImageIter(io.DataIter):
    """Image data iterator with a large number of augmentation choices.
    This iterator supports reading from both .rec files and raw image files.

    To load input images from .rec files, use `path_imgrec` parameter and to load from raw image
    files, use `path_imglist` and `path_root` parameters.

    To use data partition (for distributed training) or shuffling, specify `path_imgidx` parameter.

    Parameters
    ----------
    batch_size : int
        Number of examples per batch.
    data_shape : tuple
        Data shape in (channels, height, width) format.
        For now, only RGB image with 3 channels is supported.
    label_width : int, optional
        Number of labels per example. The default label width is 1.
    path_imgrec : str
        Path to image record file (.rec).
        Created with tools/im2rec.py or bin/im2rec.
    path_imglist : str
        Path to image list (.lst).
        Created with tools/im2rec.py or with custom script.
        Format: Tab separated record of index, one or more labels and relative_path_from_root.
    imglist: list
        A list of images with the label(s).
        Each item is a list [imagelabel: float or list of float, imgpath].
    path_root : str
        Root folder of image files.
    path_imgidx : str
        Path to image index file. Needed for partition and shuffling when using .rec source.
    shuffle : bool
        Whether to shuffle all images at the start of each iteration or not.
        Can be slow for HDD.
    part_index : int
        Partition index.
    num_parts : int
        Total number of partitions.
    data_name : str
        Data name for provided symbols.
    label_name : str
        Label name for provided symbols.
    kwargs : ...
        More arguments for creating augmenter. See mx.image.CreateAugmenter.
    """

# 数据读取操作是在初始化方法init上进行的。这里主要分两种情况:1、读取的是record文
# 件(后缀是.rec),那么读取数据就通过recordio.MXIndexedRecordIO接口或
# recordio.MXRecordIO接口来读取,取决于你是否提供了.idx文件,而.idx文件是在生
# 成.rec文件时附带生成的。不管是recordio.MXIndexedRecordIO接口还是
# recordio.MXRecordIO接口,都是调用底层的C++代码来读取数据,具体可以参看
# ~mxnet/python/mxnet/recordio.py脚本,因此效率跟和mxnet.io.ImageRecordIter
# 接口没有太大差别。2、读取的是原图像+.lst文件,那么读取数据就通过python的open语
# 句来读取,维护了一个imglist字典来存放读取到的.lst文件内容,其中键是index,值是
# 标签和图像路径组成的tuple。在初始化中还有一步比较重要:            
# if aug_list is None:
#    self.auglist = CreateAugmenter(data_shape, **kwargs)
# else:
#    self.auglist = aug_list
# 这个if语句是关于数据预处理的设置,可以看出如果你要添加一些数据预处理操作,可以在
# mxnet.image.ImageIter接口的aug_list参数处设置,如果不设置的话就采用默认的
# CreateAugmenter(data_shape, **kwargs)函数,而这个默认的函数基本上没进行什么
# 预处理操作。那么aug_list参数要怎么给呢?参考CreateAugmenter函数的返回值可以看
# 出(CreateAugmenter函数的介绍可以参考博客:),aug_list是一个列表,列表中的每
# 个值存放一种数据处理类&#
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值