目录
-
工具
- miniconda3
- windows 10
- 图片文件夹
- 配置虚拟环境
-
我的情况
- 每个文件夹存储几张甲骨文文字的图片;每个文件夹名不同以作类别区分;图片数据集可以在这里下载 手写甲骨文数据集
-
将数据集划分为训练集、验证集、测试集
-
已配置好虚拟环境,以mxnetcpu命名;将下载好的数据集放到我的pycharm工程目录OBC_reco下面 ,在HWOBC文件夹中新建文件夹——dataset用来存放数据集的idx、lst、rec文件,即我们最后要实现的
- 每个文件夹存储几张甲骨文文字的图片;每个文件夹名不同以作类别区分;图片数据集可以在这里下载 手写甲骨文数据集
-
看我操作
- 管理员身份打开Anaconda Power Shell,激活工程文件所在的虚拟环境(我的是mxcpu),进入图片数据集所在的目录
- 生成lst文件
python E:\miniconda3\envs\mxnetcpu\Lib\site-packages\mxnet\tools\im2rec.py --list --recursive --test-ratio=0.1 --train-ratio=0.8 dataset\data D:\AI\OBC_reco\HWOBC\sample
- lst文件第一列是图像的索引,第二列是图像的label(注意:float类型),第三列是图像路径
- 与 tensorflow 不同,MXNet 也有自己的专属图像数据格式,MXNet 读取图像有两种方式:读 .rec格式文件,包含文件路径、标签和图像信息 ;读.lst 和图像结合方式,.lst 文件其实就是图像路径和标签的对应列表,有点类似 .csv 文件
- 我们打开im2rec.py可查看命令中指令的含义,下面介绍主要用到的
-
没有写--no-shuffle,因为我们需要打乱数据集
cgroup.add_argument('--no-shuffle', dest='shuffle', action='store_false', help='If this is passed, \ im2rec will not randomize the image order in <prefix>.lst')
-
recrusive:为每个子文件夹分配独立标签;不含这一项,则给root folder中的所有图片分配label 0;部分教程要求 --recursive True, 亲测此时会报错;
cgroup.add_argument('--recursive', action='store_true', help='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.')
- --train-ratio、--test-ratio 训练集:测试集:验证集=8:1:1
cgroup.add_argument('--train-ratio', type=float, default=1.0, help='Ratio of images to use for training.') cgroup.add_argument('--test-ratio', type=float, default=0, help='Ratio of images to use for testing.')
- --list
cgroup.add_argument('--list', action='store_true', help='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')
- root------D:\AI\OBC_reco\HWOBC\sample
parser.add_argument('root', help='path to folder containing images.')
- profix------dataset\data
parser.add_argument('prefix', help='prefix of input/output lst and rec files.')
-
- 生成rec、idx文件
python E:\miniconda3\envs\mxnetcpu\Lib\site-packages\mxnet\tools\im2rec.py --num-thread=4 dataset\data D:\AI\OBC_reco\HWOBC\sample
- RecordIO是MXNet用于数据IO的文件格式,文件后缀为.rec,.rec 需要与.idx共同使用。
- 类似的指令也可以在im2rec文件中找到含义,这里不再叙述
- num_thread:用于多线程读取数据,加快处理进程
- prefix-folder:是上一步生成的lst所在文件夹路径-------dataset\data
- 会生成和prefix-folder中.lst 同名的.rec和.idx文件------D:\AI\OBC_reco\HWOBC\sample
- 管理员身份打开Anaconda Power Shell,激活工程文件所在的虚拟环境(我的是mxcpu),进入图片数据集所在的目录
参考文章
错误可能分析
- 命令行没有进入正确的文件夹位置
- 没有管理员权限
- 没有安装opencv,OpenCV is also required by im2rec,需要你手动pip一下