Train and Test LeNet on your own dataset

the purpose of this topic is:

  1. how to train LeNet (defined in examples/mnist/lenet.prototxt) using your own dataset( jpg images of the same size or preprocessed ) 
  2. test using the trained LeNet.

step 1: convert the image files to lmdb database format

suppose we have prepared all the images in .jpg format under some folders; train.txt and val.txt that contains the [filename label] of the training and testing data (label: 0~#of labels-1)

reference  examples/imagenet/create_imagenet.sh

change the following path:

  • EXAMPLE = /path/to/subfolder/under/examples/to/store/lmdb file
  • DATA = /path/to/folder/containing/train.txt & val.txt
  • TRAIN_DATA_ROOT=/path/to/folder/omitted/in/train.txt
  • TRAIN_DATA_ROOT=/path/to/folder/omitted/in/val.txt
  • GLOG….$EXAMPLE/…_train(val)_lmdb (rename the database for training and testing)
  • if the input images are gray, add ” – -gray \” after ” – -shuffle \”

step 2: modify the lenet model to adapt the input file and filepath

reference examples/mnist/lenet.prototxt, lenet_train_test.prototxt, lenet_solver.prototxt, train_lenet.sh

  • lenet.prototxt and lenet_train_test.prototxt:
  1.       input_dim: (batchsize, #channels, imagesize1,imagesize2)
  2.       inner_product_param: num_output: #labels
  3.       “data” layer: phase: TRAIN/TEST  source: “…”(path to training/testing lmdb database)

! the default path in “data” layer omit caffe_root, and you need to call ./build/tools/caffe at caffe_root !

  • lenet_solver.prototxt:
  1. test_iter:  #test batches ( the total test images evaluated in TRAIN phase = test_iter*TEST batch size )
  2. base_lr: initial learning rate ( default:.01, change to a smaller number if getting NAN loss in training )
  3. snapshot_prefix: “…”(filepath to snapshot saved)
  4. snapshot: # of iterations per snapshot
  5. max_iter:  # of iterations
  • train_lenet.sh:
  1. –solver = *_solver.prototxt

step 3: train the model

run ./examples/…./train_*.sh in the terminal

step 4: test the model

cd to caffe_root, run:

./build/tools/caffe test -model /path/to/*_train_test.prototxt -weights/path/to/trained/caffe/caffemodel -iterations #test images/batch_size


use new dataset for modified network

  • clear lmdb database
  • rename old files: data/*/train.txt val.txt  examples/*/*.caffemodel *.solverstate
  • update the parameters in examples/*/train_*net.sh if necessary
  • create new train.txt val.txt
  • run examples/*/create_*.sh, train_*.sh
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章