caffe使用之——将自己的数据生成lmdb

1,准备文件目录

在caffe/examples/下新建my_image文件夹

下面在新建data文件夹以存放数据,建lmdbtest文件夹存放lmdb文件

2,数据准备

data文件夹下新建train,val文件夹分别存放用于训练和验证的数据

建立train.txt文件,生成图片的目录+标签,最好打乱一下

home/alice/caffe/examples/my_image/data/train/n02121620_3678.JPEG 0
home/alice/caffe/examples/my_image/data/train/n02084071_13.JPEG 1
home/alice/caffe/examples/my_image/data/train/n02084071_15328.JPEG 1
home/alice/caffe/examples/my_image/data/train/n02084071_23907.JPEG 1
home/alice/caffe/examples/my_image/data/train/n02121620_25290.JPEG 0
home/alice/caffe/examples/my_image/data/train/n02121620_3420.JPEG 0
home/alice/caffe/examples/my_image/data/train/n02084071_7987.JPEG 1
home/alice/caffe/examples/my_image/data/train/n02121620_4769.JPEG 0
home/alice/caffe/examples/my_image/data/train/n02084071_5399.JPEG 1
home/alice/caffe/examples/my_image/data/train/n02121620_4568.JPEG 0
home/alice/caffe/examples/my_image/data/train/n02121620_30404.JPEG 0
home/alice/caffe/examples/my_image/data/train/n02121620_25411.JPEG 0

建立val.txt文件,同上

home/alice/caffe/examples/my_image/data/val/n02121620_293.JPEG 0
home/alice/caffe/examples/my_image/data/val/n02084071_32440.JPEG 1
home/alice/caffe/examples/my_image/data/val/n02121620_1028.JPEG 0
home/alice/caffe/examples/my_image/data/val/n02121620_1395.JPEG 0
home/alice/caffe/examples/my_image/data/val/n02084071_29164.JPEG 1
home/alice/caffe/examples/my_image/data/val/n02512053_929.JPEG 2
home/alice/caffe/examples/my_image/data/val/n02084071_32981.JPEG 1
home/alice/caffe/examples/my_image/data/val/n02512053_2034.JPEG 2
home/alice/caffe/examples/my_image/data/val/n02512053_815.JPEG 2
home/alice/caffe/examples/my_image/data/val/n02121620_1208.JPEG 0
home/alice/caffe/examples/my_image/data/val/n02084071_30600.JPEG 1
home/alice/caffe/examples/my_image/data/val/n02512053_1829.JPEG 2
home/alice/caffe/examples/my_image/data/val/n02084071_35839.JPEG 1
home/alice/caffe/examples/my_image/data/val/n02121620_68.JPEG 0
home/alice/caffe/examples/my_image/data/val/n02512053_38.JPEG 2
建立words。txt文件

0 cat
1 dog
2 fish

3,复制creat_imagenet.sh到my_image目录下

修改参数

#!/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/my_image/lmdbtest    #存放lmdb的文件夹目录
DATA=examples/my_image/data  #tain.txt 等文件所在目录
TOOLS=build/tools #调用caffe工具

TRAIN_DATA_ROOT=/  #此类目录与train.txt val.txt文件下的目录结合为每个图片的完整目录
VAL_DATA_ROOT=/

# 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 $TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --shuffle \
    $TRAIN_DATA_ROOT \
    $DATA/train.txt \
    $EXAMPLE/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/val_lmdb

echo "Done."
运行
 ./examples/my_image/create_myimagenet.sh

成功则

caffe使用之——将自己的数据生成lmdb_第1张图片





你可能感兴趣的:(caffe)