1. 程式人生 > >caffe 訓練自己的分類模型

caffe 訓練自己的分類模型

學習caffe的最終目的,是可以利用自己的資料集,訓練模型,並解決實際問題。

所以在前面跑通了mnist和cifar-10例程的基礎上,嘗試訓練自己的模型,從頭到尾走一遍所有的流程。準備資料、訓練並得到模型,利用模型進行分類預測。

一、準備資料

1、在網上找了一些圖片,分為五類,編號為0,1,2,3,4,每類訓練資料50張,測試資料20張。博主將所有的train圖片都放在了train資料夾下,編號為0-49,100-149,200-249,300-349,400-449,把所有的test圖片都放在了test資料夾下,編號為0-19,100-119,200-219,300-319,400-419。

2、得到train.txt和test.txt清單檔案。網上有很多方法,也可以自己用C++寫程式碼實現。部分清單如下所示:

3、將train.txt和test.txt放在 與 train和test資料夾同一目錄下。我的為:caffe-master\examples\myExample。

0.jpg 0
1.jpg 0
2.jpg 0
3.jpg 0
4.jpg 0
5.jpg 0
6.jpg 0
7.jpg 0
8.jpg 0
9.jpg 0
10.jpg 0
11.jpg 0
12.jpg 0
13.jpg 0
14.jpg 0
15.jpg 0
16.jpg 0
17.jpg 0
18.jpg 0
19.jpg 0
100.jpg 1
101.jpg 1
102.jpg 1

二、轉換為lmdb格式

1、安裝git客戶端,方便後面執行.sh檔案。  安裝教程

 

2、找到caffe-master\examples\imagenet目錄下的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
DATA=E:/NotFineshed/caffeWin10/caffe-master/examples/myExample
TOOLS=E:/NotFineshed/caffeWin10/caffe-master/Build/x64/Release

TRAIN_DATA_ROOT=E:/NotFineshed/caffeWin10/caffe-master/examples/myExample/train/
VAL_DATA_ROOT=E:/NotFineshed/caffeWin10/caffe-master/examples/myExample/test/

# 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 \
    --backend=lmdb \
    --shuffle \
    $TRAIN_DATA_ROOT \
    $DATA/train.txt \
    $EXAMPLE/mydata_train_lmdb

echo "Creating val lmdb..."

GLOG_logtostderr=1 $TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --backend=lmdb \
    --shuffle \
    $VAL_DATA_ROOT \
    $DATA/test.txt \
    $EXAMPLE/mydata_test_lmdb

echo "Done."

3、執行完成後,在git的檔案位置目錄下,得到兩個資料夾 madata_train_lmdb 和 mydata_test_lmdb

三、生成影象均值

1、修改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/imagenet
DATA=E:/NotFineshed/caffeWin10/caffe-master/examples/myExample
TOOLS=E:/NotFineshed/caffeWin10/caffe-master/Build/x64/Release

$TOOLS/compute_image_mean.exe $DATA/mydata_train_lmdb $DATA/mydata_mean.binaryproto --backend=lmdb

echo "Done."
2、執行完成後,在caffe-master\examples\myExample目錄下,生成mydata_mean.binaryproto檔案。

四、建立模型並編寫配置檔案

1、模型就用caffe自帶的caffenet模型,位置在 caffe-master\models\bvlc_reference_caffenet/資料夾下, 我們需要的三個檔案是solver.prototxt 和train_val.prototxt、deploy.prototxt。博主將train_val.prototxt改名為train_test.prototxt。

2、修改其中的solver.prototxt,如下所示

net: "E:/NotFineshed/caffeWin10/caffe-master/examples/myExample/train_test.prototxt"
test_iter: 5
test_interval: 10
base_lr: 0.001
lr_policy: "step"
gamma: 0.1
stepsize: 100
display: 20
max_iter: 1000
momentum: 0.9
weight_decay: 0.005
solver_mode: GPU
snapshot: 500
snapshot_prefix: "E:/NotFineshed/caffeWin10/caffe-master/examples/myExample/minemodel"
3、修改train_test.protxt,需要修改兩個階段的data層。

    實際上就是修改兩個data layer的mean_file和source這兩個地方,其它都沒有變化 。

    然後修改全連線層fc8的輸出num,即分類數,我的為五類,所以改為5.

name: "CaffeNet"
layer {
  name: "data"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    mirror: true
    crop_size: 227
    mean_file: "E:/NotFineshed/caffeWin10/caffe-master/examples/myExample/mydata_mean.binaryproto"
  }
# mean pixel / channel-wise mean instead of mean image
#  transform_param {
#    crop_size: 227
#    mean_value: 104
#    mean_value: 117
#    mean_value: 123
#    mirror: true
#  }
  data_param {
    source: "E:/NotFineshed/caffeWin10/caffe-master/examples/myExample/mydata_train_lmdb"
    batch_size: 25
    backend: LMDB
  }
}
layer {
  name: "data"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    mirror: false
    crop_size: 227
    mean_file: "E:/NotFineshed/caffeWin10/caffe-master/examples/myExample/mydata_mean.binaryproto"
  }
# mean pixel / channel-wise mean instead of mean image
#  transform_param {
#    crop_size: 227
#    mean_value: 104
#    mean_value: 117
#    mean_value: 123
#    mirror: false
#  }
  data_param {
    source: "E:/NotFineshed/caffeWin10/caffe-master/examples/myExample/mydata_test_lmdb"
    batch_size: 20
    backend: LMDB
  }
}

4、修改deploy.prototxt檔案

 同樣的修改num_output為5(5個類別)。

五、開始訓練

1、在caffe-master目錄下,新建train-my-caffe-model.txt,並輸新增以下程式碼,新增完成後,再將字尾名改為.bat

Build\x64\Release\caffe.exe train --solver=examples/myExample/solver.prototxt
pause

另一種方式:新建一個train-my-caffe-model.sh檔案,新增一下程式碼,用git執行也可以的。

DATA=E:/NotFineshed/caffeWin10/caffe-master/examples/myExample
TOOLS=E:/NotFineshed/caffeWin10/caffe-master/Build/x64/Release
$TOOLS/caffe.exe train --solver=$DATA/solver.prototxt
read -p "press Enter to continue"

2、雙擊.bat檔案,即可開始訓練。


六、各種報錯及解決辦法

1、F0108 11:44:23.991556  8376 db_lmdb.hpp:15] Check failed: mdb_status == 0 (3 vs. 0) 系統找不到指定的路徑。

2、F0108 13:05:50.865810  9376 syncedmem.cpp:56] Check failed: error == cudaSuccess (2 vs. 0)  out of memory

解決辦法:GPU視訊記憶體不夠,修改batch_size的值,改為25

七、結果

如下圖所示。


感覺訓練的結果不夠滿意啊,接下來,需要了解模型網路引數,修改引數以得到更好的訓練結果。

八、輸入單張圖片,進行分類預測

1、新建.sh檔案,輸入以下程式碼即可,然後用git執行

#EXAMPLE=examples/imagenet
DATA=E:/NotFineshed/caffeWin10/caffe-master/examples/myExample
TOOLS=E:/NotFineshed/caffeWin10/caffe-master/Build/x64/Release

$TOOLS/classification.exe $DATA/deploy.prototxt $DATA/minemodel_iter_1000.caffemodel $DATA/mydata_mean.binaryproto $DATA/synset_words.txt $DATA/1.jpg
read -p "press Enter to continue"


相關推薦

caffe訓練自己模型步驟

1.準備資料 建立目錄mkdir mydata,mkdir mydata/train,mkdir mydata/val,在train下放訓練資料,在val下放驗證資料。 生成train.txt、val.txt、test.txt 1.1生成val.txt:find  -na

深度學習(一)學會用CAFFE訓練自己模型

利用caffe訓練手寫圖片資料集 bat批處理命令的書寫 在windows平臺學習caffe必須要學會寫批處理檔案,這樣就減少了在CMD命令列下操作檔案的麻煩。bat檔案類似於Linux下的sh指令碼檔案,作用都是簡化命令列操作,筆者目前主要在win

caffe 訓練自己分類模型

學習caffe的最終目的,是可以利用自己的資料集,訓練模型,並解決實際問題。 所以在前面跑通了mnist和cifar-10例程的基礎上,嘗試訓練自己的模型,從頭到尾走一遍所有的流程。準備資料、訓練並得到模型,利用模型進行分類預測。 一、準備資料 1、在網上找了一些圖片,分

caffe訓練自己模型時候出現這個錯誤。

caffe/My_Files/lenet_train_test.prototxt [libprotobuf ERROR google/protobuf/text_format.cc:298] Error parsing text-format caffe.NetParameter: 227:7: M

caffe----訓練自己的圖片caffenet模型

    學習的caffe的目的,不是簡單的做幾個練習,而是最終落實到自己的專案或科研中去。因此,本文介紹一下,從自己的原始圖片到lmdb資料,再到訓練和測試的整個流程。 一、資料的準備     有條件的同學,可以去ImageNet的官網點選開啟連結,下載ImageNet

利用caffe訓練好的模型測試自己的手寫字型圖片

轉載地址: http://blog.csdn.net/xunan003/article/details/73126425 一、前沿         寫這篇博文,是因為一開始在做《21天學習caffe》第6天6.4練習題1的時候看著自己搜尋的博文,在不理解其根本的情況下做的

caffe訓練自己的圖片進行分類預測

搭建好caffe環境後,就需要用自己的圖片進行分類預測,主要步驟如下,主要參照http://www.cnblogs.com/denny402/p/5083300.html,感謝博主: 1、資料準備,下載待訓練的圖片集,共5類400張,測試集100張,目錄分別為

Ubuntu16.04下 用Caffe訓練自己的網路和模型並測試

1.準備圖片(訓練太久就不放那麼多圖片了)在caffe根目錄下data中新建資料夾6class(意思是6類),在6class資料夾下新建兩個資料夾train和val。train用來存放訓練的圖片,在train資料夾下新建6個資料夾0-5 。圖片有6類,杯子(資料夾0)、書包(

有關Caffe訓練好的模型在Python介面下使用分類不準確的問題解決

之前使用caffe訓練了1k個自己的資料,有3個分類,在consol下面訓練加驗證的結果是85%左右的準確率,還是可以的. 但是問題是,當使用了Python介面,匯入caffemodel檔案和npy均值檔案後,分類結果完全慘不忍睹,全部都偏向第一分類. 經過不懈的googl

caffe訓練自己的資料集(三)

本文主要參考了:https://blog.csdn.net/heimu24/article/details/53581362                     https://blog.csd

caffe訓練自己的資料集(二)

本文主要參考了:https://blog.csdn.net/heimu24/article/details/53581362                     https://blog.c

caffe訓練自己的資料集(一)

本文主要參考了:https://blog.csdn.net/heimu24/article/details/53581362                     https://blog.csd

Caffe:使用 Caffe 訓練自己的 Alexnet

使用 Caffe 訓練自己的 Alexnet 1. 資料準備 2. 計算影象均值 3. 定義網路 3.1 修改 solver.prototxt 3.2 修改 train_val.proto

caffe訓練自己的資料集

https://www.cnblogs.com/wktwj/p/6715110.html 預設caffe已經編譯好了,並且編譯好了pycaffe 1 資料準備 首先準備訓練和測試資料集,這裡準備兩類資料,分別放在資料夾0和資料夾1中(之所以使用0和1命名資料類別,是因為方便標註資料類別,直接用資料夾的名

Ubuntu 16.04下利用tf-faster-rcnn在VOC或其他資料集上訓練自己模型

暑期的時候有朋友問我如何配置tf-faster-rcnn,那會簡單寫了個steps.pdf。正好最近閒了下來,就把它彙總一下。 Introduction 這是一篇介紹如何使用tf-faster-rcnn在VOC和其他資料集訓練自己模型的筆記. 筆記中所有檔案的地

image—retraining模型載入訓練自己模型時報錯

在使用image-retraining訓練自己的影象分類時候有時會報TypeError: Cannot interpret feed_dict key as Tensor: The name ‘DecodeJpeg/contents:0’ refers to a

[AI] 使用object detection訓練自己模型用於物體識別

軟體環境 tensorflow (1.8.0) libprotoc 3.0.0/3.3.0 tensorflow models models/research/object_detection (2018/dec版本) models git clone https://

實踐二:caffe環境配置以及使用ssd-caffe訓練自己的資料集

1:環境配置 首先,我們把專案程式碼clone下來, 然後編譯: git clone https://github.com/weiliu89/caffe.git cd caffe git checkout ssd 檢視有沒有安裝opencv pkg-co

用Deformable Part Model(DPM)voc-release3.1訓練自己模型

配置了一上午,終於能訓練自己的DPM模型,實屬不易 我的環境 DPM原始碼版本:voc-release3.1 VOC開發包版本:VOC2007_devkit_08-Jun 使用的訓練資料集:VOC2007                            

Tensorflow + ResNet101 + fasterRcnn 訓練自己模型 資料(一)

一、資料準備: 1、PASCAL VOC資料集格式 2、資料擴充:做了旋轉【0, 90,180,270】(備註:這裡可以不做那麼多許旋轉,fasterrcnn在訓練的時候要做圖片的映象變換)、降取樣 降取樣: import os import cv2 import nu