天天看点

【AI】caffe使用步骤(一):将标注数据生成lmdb或leveldb

1、简述

caffe使用工具 convert_imageset 将标注数据转换成lmdb或leveldb格式,convert_imageset 使用方法可以参考脚本examples/imagenet/create_imagenet.sh。

convert_imageset 在./build/tools/中。

2、convert_imageset命令行参数
./build/tools/convert_imageset 
convert_imageset: 将一组图像转换为leveldb/lmdb格式用为Caffe输入的格式。
用法:
    convert_imageset [参数] 保存图片的路径或图片列表 生成文件的名字

Flags from tools/convert_imageset.cpp:
    -backend 选择生成的数据格式(lmdb, leveldb),默认是: "lmdb"
    -check_size 检查图片大小是否一致,默认不检查: false
    -encode_type 编码图片格式(jpg、png)
    -encoded 启用此选项后,已编码的图像将保存在数据中,默认是: false
    -gray 将图片视为灰度图片,默认是: false
    -resize_height 调整图片高度,默认是0,不调整
    -resize_width 调整图片宽度,默认是0,不调整
    -shuffle 随机打乱图片和标签的顺序,默认是: false
           
3、create_imagenet.sh脚本说明
#!/usr/bin/env sh
# Create the imagenet lmdb inputs
# N.B. set the path to the imagenet train + val data dirs
set -e

EXAMPLE=examples/imagenet	# 保存生成lmdb的路径
DATA=data/ilsvrc12			# 标注文件路径
TOOLS=build/tools			# convert_imageset工具路径

TRAIN_DATA_ROOT=examples/imagenet/train_data # 训练图片集路径
VAL_DATA_ROOT=examples/imagenet/val_data	 # 验证图片集路径

# Set RESIZE=true to resize the images to 256x256. Leave as false if images have
# already been resized using another tool. 是否调整图片大小
RESIZE=true
if $RESIZE; then
  RESIZE_HEIGHT=256
  RESIZE_WIDTH=256
else
  RESIZE_HEIGHT=0
  RESIZE_WIDTH=0
fi

if [ ! -d "$TRAIN_DATA_ROOT" ]; then
  echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT"
  echo "Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path" \
       "where the ImageNet training data is stored."
  exit 1
fi

if [ ! -d "$VAL_DATA_ROOT" ]; then
  echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT"
  echo "Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path" \
       "where the ImageNet validation data is stored."
  exit 1
fi

echo "Creating train lmdb..."
# GLOG_logtostderr=1,设置环境变量GLOG_logtostderr,参考glog的使用方法
GLOG_logtostderr=1 $TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --shuffle \
    $TRAIN_DATA_ROOT \
    $DATA/train.txt \
    $EXAMPLE/ilsvrc12_train_lmdb

echo "Creating val lmdb..."

GLOG_logtostderr=1 $TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --shuffle \
    $VAL_DATA_ROOT \
    $DATA/val.txt \
    $EXAMPLE/ilsvrc12_val_lmdb

echo "Done."

           

继续阅读