the purpose of this topic is:
- how to train LeNet (defined in examples/mnist/lenet.prototxt) using your own dataset( jpg images of the same size or preprocessed )
- 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:
- input_dim: (batchsize, #channels, imagesize1,imagesize2)
- inner_product_param: num_output: #labels
- “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:
- test_iter: #test batches ( the total test images evaluated in TRAIN phase = test_iter*TEST batch size )
- base_lr: initial learning rate ( default:.01, change to a smaller number if getting NAN loss in training )
- snapshot_prefix: “…”(filepath to snapshot saved)
- snapshot: # of iterations per snapshot
- max_iter: # of iterations
- train_lenet.sh:
- –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