百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

mxnet-im2rec_tutorial

> 编程语言
开源

本简单的教程将介绍如何使用 im2rec 来处理 mx.image.ImageIter、ImageDetIter 以及如何使用 im2rec 处理 COCO 数据集。

100 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

本简单的教程将介绍如何使用 im2rec 来处理 mx.image.ImageIter、ImageDetIter 以及如何使用 im2rec 处理 COCO 数据集。

mxnet data IO im2rec tutorial

update 2018/8/21
Prepare custom datasets for gluonCV and mxnet
https://gluon-cv.mxnet.io/build/examples_datasets/detection_custom.html

this simple tutorial will introduce how to use im2rec for mx.image.ImageIter and ImageDetIter and how to use im2rec for COCO DataSet ok let's start but prepare your im2rec first https://github.com/apache/incubator-mxnet/blob/master/tools/im2rec.py

im2rec

step 1. you should make a .lst file run in terminal

bash
python im2rec.py --list testlst Dataset

--list     => make .lst file 
              you should set True when your folder didnt have any .lst file
              
test       => name of your .lst  name what you want 
DataSet    => your image dataset folder

check your folder path correct

use .lst for imageIter

python

data_iter = mx.image.ImageIter(
    batch_size=4, 
    data_shape=(3,816, 1232),
    label_width=1,
    path_imglist='test.lst',
    path_root='DataSet')  

for data in data_iter:  
    d = data.data[0]  
    break  
  
img =nd.transpose(d,(0,2,3,1))  
print(img.shape)  
io.imshow(img[0].asnumpy().astype(np.uint8))  
io.show()

step 2. make .rec file

bash
python im2rec.py testlst.lst Dataset

testlst     => name of your .rec  name what you want
Dataset     => your image dataset folder

use .rec for imageiter

python
train_iter = mx.image.ImageIter(  
    batch_size=32,  
   data_shape=(3, 816, 1232),  
   path_imgrec='data.rec',  
   path_imgidx='data.idx',  #help shuffle performance
   shuffle=True,  
   aug_list=[mx.image.HorizontalFlipAug(0.5)]  
)

  
train_iter.reset()  
for batch in train_iter:  
    x = batch.data[0]  
    break  
  
img =nd.transpose(x,(0,2,3,1))  
print(img.shape)  
io.imshow(img[0].asnumpy().astype(np.uint8))  
io.show()

other feature

…

ImageDetIter

make own .lst for detection

A B [extra header] [(object0), (object1), ... (objectN)]

  1. you should write the .lst with your image information Where A is the width of header (2 + length of extra header), B is the width of each object. Extra header is optional and used for inserting helper information such as (width, height). Each object is usually 5 or 6 numbers describing the object properties, for example: [id, xmin, ymin, xmax, ymax, difficulty] Putting all together, we have a lst file for object detection:

ref: https://mxnet.incubator.apache.org/api/python/image/image.html

python
with open('dataset.lst', 'w+') as f:  
    for i in range(3):  
        f.write(  
            str(i) + '\t' +  
            # idx  
            str(4) + '\t' + str(5) + '\t' +  
            # width of header and width of each object.  
            str(256) + '\t' + str(256) + '\t' +  
            # (width, height)  
            str(1) + '\t' +  
            # class  
            str((i / 10)) + '\t' + str((i / 10)) + '\t' + str(((i + 3) / 10)) + '\t' +str(((i + 3) / 10)) + '\t' +  
            # xmin, ymin, xmax, ymax  
           str(i) + '.jpg\n'
        )

you will see like this format in .lst

idx  A    B    [extra header]   [(object0), (object1), ... (objectN)]
0    4  5      256    256       1  0.1    0.1    0.3    0.3    0.jpg  
1    4  5      256    256       1  1.1    1.1    1.3    1.3    1.jpg  
2    4  5      256    256       1  2.1    2.1    2.3    2.3    2.jpg

 

must '\t' between data information

make own dataset .rec for ImageDetIter

bash
python im2rec.py --pack-label dataset.lst Dataset

--pack-label  => when you have label
dataset.lst     => name for .rec  type for your self in .lst
Dataset       => DataSet image folder
…

How to use im2rec on COCO DataSet

1. download cococ dataset first!!! 2. make own .json or use COCO.json
3. .json => .lst
4. .lst => .rec 5. .rec for ImageDetIter

how to load COCO DataSet from .json

python
import json  

with open('annotations/instances_train2017.json', 'r') as f:  
    DataSets = json.load(f)  
print(DataSets['annotations'][0])

>>{'segmentation': [[239.97, 260.24, 222.04, 270.49, 199.84, 253.41, 213.5, 227.79, 259.62, 200.46, 274.13, 202.17, 277.55, 210.71, 249.37, 253.41, 237.41, 264.51, 242.54, 261.95, 228.87, 271.34]],
 'area': 2765.1486500000005, 
 'iscrowd': 0, 
 'image_id': 558840, 
 'bbox': [199.84, 200.46, 77.71, 70.88], 
 'category_id': 58, 'id': 156}

as you see, these annotations about image 558840 , boundingbox,class.... in detail you can check coco website

COCO.json => own.json

make own dataset (class:bird) from COCO dataset

…

{'img_name': '000000202273.jpg', 'height': 640, 'width': 480, 'bbox': [0.6530625000000001, 0.089296875, 0.33064583333333336, 0.075390625], 'class': 16}

as you see this is your own dataset annotations

own data (.json) to .lst format

1.How to use own own dataset.json and make own dataset .lst

…

you will see your .lst like this format

use im2rec for COCO dataSet

this step just use 'bird' class (Previous) to show you, .lst => .rec

bash
python im2rec.py --pack-label ownSet.lst ownSet

ownSet.lst => last step you make
ownSet     => image folder

OK now we can use own dataSet ownSet.rec for ImageDetIter

…

result

make very very example for multilabeling

will generate dataset.lst

…

check dataset.lst

step2: use im2rec

bash
python im2rec.py --pack-label dataset.lst data

step3: use ImageDetIter show our multilabel

…

if you feel useful pls give a star!!!

Issues· 6 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Pythoncocodatasetgluonim2recmxnet

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言