caffe如何将图片数据写成lmdb格式

来源:互联网 发布:lol网吧代理软件 编辑:程序博客网 时间:2024/04/30 21:38

本人也才开始使用caffe 配好环境跑了mnist和另外一个例子,当时就蒙了   如果是图片的例子应该怎么导入,后来看了两天终于整明白了 给大家讲述一下具体过程很简单。我用minst数据库做个实验。

首先我们要得到两个txt一个是train.txt另一个是test.txt

内容如下:



前面代表是图像所在目录下的名字  第二个数字代表标签  test.txt   train.txt

下面是matlab代码

clc;
clear;
load('mnist_uint8.mat')
num=size(test_x,1);
fid = fopen('test_minst.txt','wt'); 
savepath='testImage/';
%train_y=interge(train_y);
for i=1:num
    image=reshape(test_x(i,:),[28 28]);
    label=find(test_y(i,:)~=0)-1;
    if i<10
    imageName=strcat('test_0000',num2str(i));
    end
    if i<100&&i>9
    imageName=strcat('test_000',num2str(i));
    end
    if i<1000&&i>99
    imageName=strcat('test_00',num2str(i));
    end
    if i<10000&&i>999
    imageName=strcat('test_0',num2str(i));
    end
    if i>9999
    imageName=strcat('test_',num2str(i));
    end
    imageName=strcat(imageName,'.jpg');
    imagepath=strcat(savepath,imageName);
    fprintf(fid,'%s\t',imagepath);
    fprintf(fid,'%s\n',num2str(label));
   imwrite(image,imagepath,'jpg');
    
end
fclose(fid);

这样我们得到



这样我们得到两个文件夹和两个txt

下一步在caffe example下新建一个目录 至于叫什么  你自己决定我的叫newmnist文件夹  之后我将测试图像集和训练图像集和两个txt导入caffe目录下的data目录下的一个子文件夹,这个我忘截图了  ,

在example/newmnist文件夹新建一个sh

目的是将图片集写成lmdb格式

sh如下:

#!/usr/bin/env sh
# Create the imagenet lmdb inputs
# N.B. set the path to the imagenet train + val data dirs


EXAMPLE=examples/newmnist
DATA=data/testmnist
TOOLS=build/tools


TRAIN_DATA_ROOT=data/testmnist/Image/
VAL_DATA_ROOT=data/testmnist/testImage
/


# 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=28
  RESIZE_WIDTH=28
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_minst.txt \
    $EXAMPLE/mnist_train_lmdb


echo "Creating test lmdb..."


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


echo "Done."




我标红的地方就是需要改的地方

EXAMPLE就是example你新建的目录

DATA就是你在data文件夹下新建目录,里面有两个图片集(训练和测试训练集)及上面所说的两个txt

TRAIN_DATA_ROOT就是训练图片集路径

VAL_DATA_ROOT就是训练图片集路径

运行sh example/newmnist/creatmnistLmdb.sh

就可以在example/newmnist/下找到mnist_test_lmdb和mnist_train_lmdb两个文件夹



为了验证我们是否正确得到lmdb文件

我们进行mnist数据集训练

第一个sh文件是train_lenet,sh

#!/usr/bin/env sh


./build/tools/caffe train --solver=examples/newmnist/lenet_solver.prototxt

修改路径

创建lenet_solver.prototxt文件

# The train/test net protocol buffer definition
net: "examples/newmnist/lenet_train_test.prototxt"
# test_iter specifies how many forward passes the test should carry out.
# In the case of MNIST, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100
# Carry out testing every 500 training iterations.
test_interval: 500
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01
momentum: 0.9
weight_decay: 0.0005
# The learning rate policy
lr_policy: "inv"
gamma: 0.0001
power: 0.75
# Display every 100 iterations
display: 100
# The maximum number of iterations
max_iter: 1000
# snapshot intermediate results
snapshot: 500
snapshot_prefix: "examples/newmnist/lenet"
# solver mode: CPU or GPU
solver_mode: GPU


标红都是需要修改的路径 

lenet_train_test.prototxt复制从mnist文件夹到当前文件夹下

修改路径

name: "LeNet"
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "examples/newmnist/mnist_train_lmdb"
    batch_size: 64
    backend: LMDB
  }
}
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "examples/newmnist/mnist_test_lmdb"
    batch_size: 100
    backend: LMDB
  }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 20
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool2"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 500
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 10
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

标红地方修改

最后  sh example/newmnist/train_lenet,sh


yun标红的都是

1 0