使用caffe訓練自己的圖像數據(未完)


參考博客:blog.csdn.net/drrlalala/article/details/47274549

1,首先在網上下載圖片,貓和狗。
直接保存下載該網頁,會生成一個有圖片的文件夾。
caffe-master/data  新建 myself
myself/  新建  train   dog
                                cat
                       test   dog
                                cat
之后將圖片分別復制到文件夾中

2,生成train.txt和test.txt
在train文件夾下運行    find -name *.jpg |cut -d '/' -f2-3 >train.txt
              手動加標簽    sed -i 's/$/ 0/g' train.txt   在末位加0
val.txt同理可得,也要加標簽。

3,圖片轉化為lmdb格式
caffe-master/examples/    新建 myself
復制examples/imagenet/create_imagenet.sh到 examples/myself

修改參照下面,主要是針對路徑的修改

#!/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/myself      ##路徑需要自己修改,默認的相對路徑是在caffe-master下  
                                               
DATA=data/myself          ##是指生成的train.txt和val.txt的路徑                     
TOOLS=build/tools

TRAIN_DATA_ROOT=data/myself/train/   ##注生成的數據最前面就不需要加/了
VAL_DATA_ROOT=data/myself/test/

# Set RESIZE=true to resize the images to 256x256. Leave as false if images have
# already been resized using another tool.
RESIZE=true    ##如果填true說明事先沒有將其轉為256*256格式
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 \           ##之前生成的list
    $EXAMPLE/myself_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/myself_test_lmdb

echo "Done."


之后 cd caffe-master
運行  sudo ./examples/myself/create_imagenet.sh
在examples/myself/   下生成兩個lmdb文件夾

4,計算圖像均值

復制examples/imagenet/make_imagenet_mean.sh到 examples/myself

使用make_imagenet_mean.sh計算圖像均值,在data/myself 下產生imagenet_mean.binaryproto文件
相對路徑仍為 caffe-master下,按照自己的文件修改路徑。
之后 sudo ./examples/myself/make_imagenet_mean.sh

#!/usr/bin/env sh
# Compute the mean image from the imagenet training lmdb
# N.B. this is available in data/ilsvrc12

EXAMPLE=examples/myself
DATA=data/myself
TOOLS=build/tools

$TOOLS/compute_image_mean $EXAMPLE/myself_train_lmdb
$DATA/imagenet_mean.binaryproto

echo "Done."


5,定義網絡
主要是修改下面三個文件:
my_train.prototxt
my_test.prototxt
solver.prototxt
這三個文件可以從caffe的已有例子中復制過來。接下來需要自己修改,修改的地方主要是,一些文件的路徑和輸出層的個數(即類別數目)。


6,寫訓練腳本my_train_lenet.sh 如下:(我這個是根據mnist中的文件改過來的)

#!/usr/bin/env sh
set -e

./build/tools/caffe train --solver=examples/myself/solver.prototxt $@

 未完待續。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM