一、im2rec用法简介

首先看文档:

usage: im2rec.py [-h] [--list] [--exts EXTS [EXTS ...]] [--chunks CHUNKS]
[--train-ratio TRAIN_RATIO] [--test-ratio TEST_RATIO]
[--recursive] [--no-shuffle] [--pass-through]
[--resize RESIZE] [--center-crop] [--quality QUALITY]
[--num-thread NUM_THREAD] [--color {-1,0,1}]
[--encoding {.jpg,.png}] [--pack-label]
prefix root Create an image list or make a record database by reading from an image list positional arguments:
prefix prefix of input/output lst and rec files.
root path to folder containing images. optional arguments:
-h, --help show this help message and exit Options for creating image lists:
--list If this is set im2rec will create image list(s) by
traversing root folder and output to <prefix>.lst.
Otherwise im2rec will read <prefix>.lst and create a
database at <prefix>.rec (default: False)
--exts EXTS [EXTS ...]
list of acceptable image extensions. (default:
['.jpeg', '.jpg', '.png'])
--chunks CHUNKS number of chunks. (default: 1)
--train-ratio TRAIN_RATIO
Ratio of images to use for training. (default: 1.0)
--test-ratio TEST_RATIO
Ratio of images to use for testing. (default: 0)
--recursive If true recursively walk through subdirs and assign an
unique label to images in each folder. Otherwise only
include images in the root folder and give them label
0. (default: False)
--no-shuffle If this is passed, im2rec will not randomize the image
order in <prefix>.lst (default: True) Options for creating database:
--pass-through whether to skip transformation and save image as is
(default: False)
--resize RESIZE resize the shorter edge of image to the newsize,
original images will be packed by default. (default:
0)
--center-crop specify whether to crop the center image to make it
rectangular. (default: False)
--quality QUALITY JPEG quality for encoding, 1-100; or PNG compression
for encoding, 1-9 (default: 95)
--num-thread NUM_THREAD
number of thread to use for encoding. order of images
will be different from the input list if >1. the input
list will be modified to match the resulting order.
(default: 1)
--color {-1,0,1} specify the color mode of the loaded image. 1: Loads a
color image. Any transparency of image will be
neglected. It is the default flag. 0: Loads image in
grayscale mode. -1:Loads image as such including alpha
channel. (default: 1)
--encoding {.jpg,.png}
specify the encoding of the images. (default: .jpg)
--pack-label Whether to also pack multi dimensional label in the
record file (default: False)

必须要填写的参数有prefix、和root两个路径参数,

prefix:生成文件文件夹目录

     可以指定为.lst的路径,这样生成文件会和.lst同一级别,且会根据.lst中的条目生成二进制文件

root:图片文件目录,默认的话里面是类别文件夹,类别名做label,每个文件夹存储图像

如果指定了.lst,则每个图片路径变为root路径+.lst中每个图片的路径

--pack-label:在指定了.lst后很有用,此时允许label为高维度(主要是label可以设置为数组)

实际上我们之前也介绍过,.lst文件并不是必须的,仅有.rec和.idx就可以满足需要(标签存储在.rec中),它是一个辅助(人工生成.lst指导.rec生成),或者作为一个结果展示(自动生成.rec时选择同时生成.lst)。

【注意】,最新版本的pack-label等bool参数已经变成了开关型参数,即输入--pack-label表示原意True。

使用简介

二、通用图像数据存储以及迭代读取方式

1、生成.lst文件

具体格式如下,各列之间使用'\t'分隔第一列为索引最后一列为文件路径,这两列是固定的,中间为标签列,列数不固定。

0    2    5    0    0.0    0.0    0.3    0.3    2007_000129.jpg
1    2    5    1    0.1    0.1    0.4    0.4    2007_000027.jpg
2    2    5    2    0.2    0.2    0.5    0.5    2007_000123.jpg
3    2    5    3    0.3    0.3    0.6    0.6    2007_000063.jpg
4    2    5    4    0.4    0.4    0.7    0.7    2007_000033.jpg
5    2    5    5    0.5    0.5    0.8    0.8    2007_000121.jpg
6    2    5    6    0.6    0.6    0.9    0.9    2007_000042.jpg
7    2    5    7    0.7    0.7    1.0    1.0    2007_000039.jpg
8    2    5    8    0.8    0.8    1.1    1.1    2007_000032.jpg
9    2    5    9    0.9    0.9    1.2    1.2    2007_000061.jpg
10    2    5    10    1.0    1.0    1.3    1.3    2007_000068.jpg
11    2    5    11    1.1    1.1    1.4    1.4    2007_000170.jpg

代码如下,

import os
name_list = [f for f in os.listdir('./') if f.endswith('jpg')]
with open('my_test.lst', 'w+') as f:
for i, n in enumerate(name_list):
f.write(
str(i) + '\t' + # idx
'2' + '\t' + '5' + '\t' + # 头信息长度(不含索引), 每个obj长度
str(i) + '\t' + # class
str((i / 10)) + '\t' + str((i / 10)) + '\t' + str(((i + 3) / 10)) + '\t' +str(((i + 3) / 10)) + '\t' +
# xmin, ymin, xmax, ymax
n + '\n'
# image path
)

2、创建.rec

调用子进程创建rec文件:

import subprocess
import mxnet as mx im2rec_path = os.path.join(mx.__path__[0], 'tools/im2rec.py') # 寻找im2rec.py路径
# final validation - sometimes __path__ (or __file__) gives 'mxnet/python/mxnet' instead of 'mxnet'
if not os.path.exists(im2rec_path):
im2rec_path = os.path.join(os.path.dirname(os.path.dirname(mx.__path__[0])), 'tools/im2rec.py') subprocess.check_call(["python", im2rec_path,
os.path.abspath('my_test.lst'), os.path.abspath('./'), "--pack-label"])

返回0表示进程顺利结束。

3、读取.rec

这里我门介绍一下几种读取API使用方式,ImageIter要求标签列数固定,label_width=7表示中间7列为标签列,我们打印了标签作示范:

data_iter = mx.image.ImageIter(batch_size=4,
resize=30,
label_width=7,
data_shape=(3, 30, 60),# depth,height,width
path_imgrec="./my_test.rec",
path_imgidx="./my_test.idx" )
data_iter.reset()
batch = data_iter.next()
img, labels = batch.data[0], batch.label[0]
print(labels)
[[ 2.          5.          0.          0.          0.          0.30000001  0.30000001]
[ 2. 5. 1. 0.1 0.1 0.40000001 0.40000001]
[ 2. 5. 2. 0.2 0.2 0.5 0.5 ]
[ 2. 5. 3. 0.30000001 0.30000001 0.60000002 0.60000002]]
<NDArray 4x7 @cpu(0)>

简明易懂,不过对于很多目标检测任务来说,object数目并不一致,中间的label列也就不一致,此时下面的API泛用性更好:

rec = mx.image.ImageDetIter(
path_imgrec = './my_test.rec',
path_imglist = '',
batch_size = 4,
data_shape = (3, 300, 300))
rec.next().label[0]

InageDetIter没有label_width参数,其扣除首末两列,中间都作为待定标签列:0列-索引列,1列-头信息列数,2列-每个对象信息列数,3~(n-1)列-标签列,n列-图像路径。

输出时会在1~n-1列中先扣除第二列数字的列数(本例中1、2两列被扣除),之后的列数才是标签:

[[[ 0.          0.          0.          0.30000001  0.30000001]]

 [[ 1.          0.1         0.1         0.40000001  0.40000001]]

 [[ 2.          0.2         0.2         0.5         0.5       ]]

 [[ 3.          0.30000001  0.30000001  0.60000002  0.60000002]]]
<NDArray 4x1x5 @cpu(0)>

下面我们测试一下各张图片label长度不等的情况:

im2rec_path = os.path.join(mx.__path__[0], 'tools/im2rec.py')  # 寻找im2rec.py路径
# final validation - sometimes __path__ (or __file__) gives 'mxnet/python/mxnet' instead of 'mxnet'
if not os.path.exists(im2rec_path):
im2rec_path = os.path.join(os.path.dirname(os.path.dirname(mx.__path__[0])), 'tools/im2rec.py') subprocess.check_call(["python", im2rec_path,
os.path.abspath('my_test.lst'), os.path.abspath('./'), "--pack-label"]) rec = mx.image.ImageDetIter(
path_imgrec = './my_test.rec',
path_imglist = '',
batch_size = 4,
data_shape = (3, 300, 300))
rec.next().label[0]

label长度不足的图片使用-1进行了补齐:

[[[ 0.          0.          0.          0.30000001  0.30000001]
[ 1. 2. 3. 4. 5. ]] [[ 1. 0.1 0.1 0.40000001 0.40000001]
[-1. -1. -1. -1. -1. ]] [[ 2. 0.2 0.2 0.5 0.5 ]
[-1. -1. -1. -1. -1. ]] [[ 3. 0.30000001 0.30000001 0.60000002 0.60000002]
[-1. -1. -1. -1. -1. ]]]
<NDArray 4x2x5 @cpu(0)>

这一点上反倒是TensorFlow宽松一点,如果是None位置,在同一个会话中也可以通过不同的形状:

import tensorflow as tf

input_ = tf.placeholder(dtype=tf.int8, shape=(None,))

with tf.Session() as sess:
print(sess.run(input_, feed_dict={input_:(10,1)}))
print(sess.run(input_, feed_dict={input_:(10,1,5)}))

[10 1]

[10 1 5]

也即是说在标签读取中,TensorFlow不需要补全-1,不同图片的标签形状不需要进行统一(见前面的TensorFlow-SSD标签处理一节)。

『MXNet』im2rec脚本使用以及数据读取的更多相关文章

  1. 『MXNet』专题汇总

    MXNet文档 MXNet官方教程 持久化模型 框架介绍 『MXNet』第一弹_基础架构及API 『MXNet』第二弹_Gluon构建模型 『MXNet』第三弹_Gluon模型参数 『MXNet』第四 ...

  2. 『MXNet』第四弹_Gluon自定义层

    一.不含参数层 通过继承Block自定义了一个将输入减掉均值的层:CenteredLayer类,并将层的计算放在forward函数里, from mxnet import nd, gluon from ...

  3. 『MXNet』第八弹_数据处理API_下_Image IO专题

    想学习MXNet的同学建议看一看这位博主的博客,受益良多. 在本节中,我们将学习如何在MXNet中预处理和加载图像数据. 在MXNet中加载图像数据有4种方式. 使用 mx.image.imdecod ...

  4. 『006』Shell脚本

    『003』索引-Linux Shell Script Shel脚本-初步入门 [001]- 点我快速打开文章[<01 什么是 Shell>] [002]- 点我快速打开文章[<02 ...

  5. 『MXNet』第八弹_数据处理API_上

    一.Gluon数据加载 下面的两个dataset处理类一般会成对出现,两个都可做预处理,但是由于后面还可能用到原始图片,.ImageFolderDataset不加预处理的话可以满足,所以建议在.Dat ...

  6. 『MXNet』第十一弹_符号式编程初探

    一.符号分类 符号对我们想要进行的计算进行了描述, 下图展示了符号如何对计算进行描述. 我们定义了符号变量A, 符号变量B, 生成了符号变量C, 其中, A, B为参数节点, C为内部节点! mxne ...

  7. 『MXNet』第十弹_物体检测SSD

    全流程地址 一.辅助API介绍 mxnet.image.ImageDetIter 图像检测迭代器, from mxnet import image from mxnet import nd data_ ...

  8. 『MXNet』第九弹_分类器以及迁移学习DEMO

    解压文件命令: with zipfile.ZipFile('../data/kaggle_cifar10/' + fin, 'r') as zin: zin.extractall('../data/k ...

  9. 『MXNet』第六弹_Gluon性能提升

    一.符号式编程 1.命令式编程和符号式编程 命令式: def add(a, b): return a + b def fancy_func(a, b, c, d): e = add(a, b) f = ...

随机推荐

  1. js选择排序

    选择排序 平均时间复杂度O(n*n) 最好情况O(n*n) 最差情况O(n*n) 空间复杂度O(1) 稳定性:不稳定 function chooseSort (arr) { var temp; var ...

  2. 关于COM类工厂80070005和8000401a错误分析及解决办法

    关于COM类工厂80070005和8000401a错误分析及解决办法 看到很多相关的文章,第一次配置配置时没有啥作用,让别人来解决的,可惜不晓得他怎么解决的,当我再次遇到时,不得不硬着头皮去解决. 总 ...

  3. Spring核心简介

    Spring简介 Spring是一个开源.轻量级框架.在诞生之初,创建Spring的主要目的是用来替代更加重量级的企业级Java技术,尤其是EJB(Enterprise JavaBean).从最初的挑 ...

  4. P1948 [USACO08JAN]电话线Telephone Lines(二分答案+最短路)

    思路 考虑题目要求求出最小的第k+1大的边权,想到二分答案 然后二分第k+1大的边权wx 把所有边权<=wx的边权变为0,边权>wx的边权变为0,找出最短路之后,如果dis[T]<= ...

  5. 题解——loj6280 数列分块入门4 (分块)

    分块维护一个区间和 然后记得更新的时候左边角块的tag不要打错到右边角块 #include <cstdio> #include <algorithm> #include < ...

  6. ngnix简介以及如何实现负载均衡原理

    1 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况.那么负载均衡的前提就是要有多台服务器才能实现, ...

  7. HDU 6212 Zuma(区间dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=6212 题意:有一行的祖玛,只由1和0组成,每次出现连续三个及以上的就会消去,问你最少需要发射多少个球才能消完. ...

  8. 批量Excel数据导入Oracle数据库 导入excel错误:外部表不是预期的格式 解决方案

    在asp.net网站中导出Excel文件后,再把文件导入到数据库中. 读取Excel文件时,打开连接出错. 错误为:外部表不是预期的格式 解决:检查了一下,导出的Excel是标准文件不是html,没错 ...

  9. Codeforces 785E. Anton and Permutation

    题目链接:http://codeforces.com/problemset/problem/785/E 其实可以CDQ分治... 我们只要用一个数据结构支持单点修改,区间查询比一个数大(小)的数字有多 ...

  10. idea中git pull push需要反复输入密码

    在使用idea开发的过程中,在终端terminal中git pull和git push时遇到一个问题,一个是 每次提交都需要输入用户名和密码,,从网上找了下解决方案,记录一下. 解决: 打开git终端 ...