This is the fourth example in the official Caffe document notebook examples, link address: http://nbviewer.jupyter.org/github/bvlc/caffe/blob/master/examples/03- Fine-tuning.ipynb
This example is used to fine-tune flickr_style data on a trained network. Fine-tune your data with a trained Caffe network. The advantage of this approach is that with the training network from a large number of image data focused on learning, its middle layer can capture the general visual performance of the "semantics" can be seen as a strong feature of the black box, we need only a few layers to get good data characteristics.
First, we need to save the data, including the following steps: Get imagenet ILSVRC and training model download a subset of the Flickr style dataset compiles the downloaded data into a format that Caffe can use
1. Package Required by Importer:
Import os
caffe_root = '/home/sindyz/caffe-master/'
os.chdir (caffe_root)
import sys
Sys.path.insert (0, './python ')
import Caffe
import NumPy as NP from
Pylab Import *
2. Importing data and Models
# This downloads the ILSVRC auxiliary data (mean file, etc), # and a subset of \ Images for the
style recognition T Ask.
! data/ilsvrc12/get_ilsvrc_aux.sh
!scripts/download_model_binary.py models/bvlc_reference_caffenet
! Python examples/finetune_flickr_style/assemble_data.py \
--workers=-1--images=2000--seed=1701--label=5
3. Compare the differences between the two models
!diff Models/bvlc_reference_caffenet/train_val.prototxt Models/finetune_flickr_style/train_val.prototxt
Output here omitted
4. Learn with Python, compare the results of fine-tuning with the results of direct training
Niter = # Losses would also be stored in the log Train_loss = Np.zeros (niter) Scratch_train_loss = Np.zeros (niter) CAF
Fe.set_device (0) Caffe.set_mode_gpu () # We Create a solver that fine-tunes from a previously trained. Solver = Caffe. Sgdsolver (' Models/finetune_flickr_style/solver.prototxt ') solver.net.copy_from (' models/bvlc_reference_caffenet/
Bvlc_reference_caffenet.caffemodel ') # for reference, we also create a solver that does no finetuning. Scratch_solver = Caffe. Sgdsolver (' Models/finetune_flickr_style/solver.prototxt ') # We run the solver for Niter times, and record the training lo SS. For it in range (Niter): Solver.step (1) # SGD by Caffe Scratch_solver.step (1) # Store the train loss T Rain_loss[it] = solver.net.blobs[' loss '].data scratch_train_loss[it] = scratch_solver.net.blobs[' loss '].data if it % = = 0:print ' iter%d, finetune_loss=%f, scratch_loss=%f '% (it, train_loss[it], scratch_train_loss[it]) prin T ' done '
Output omitted ...
5. View training Losses
Visible, the fine tuning method produces a smooth loss fluctuation, and less than the loss of direct use of the model.
6. Enlarge the smaller value part:
Plot (Np.vstack ([Train_loss, Scratch_train_loss]). Clip (0, 4). T
7. After viewing the test accuracy rate after 200 iterations, we see that there are 5 categories in the classification task, the accuracy of the random test is 20%, and as we expected, the result of fine-tuning is better than the direct use of the model.
Test_iters =
accuracy = 0
scratch_accuracy = 0 for
it in Arange (test_iters):
solver.test_nets[0]. Forward ()
accuracy + + solver.test_nets[0].blobs[' accuracy '].data
scratch_solver.test_nets[0].forward ()
scratch_accuracy + + scratch_solver.test_nets[0].blobs[' accuracy '].data
accuracy/= test_iters Scratch
_accuracy/= test_iters
print ' Accuracy for fine-tuning: ', accuracy
print ' accuracy for training to scratch: ', Scratch_accuracy
Accuracy for fine-tuning:0.547999998927
Accuracy for training from scratch:0.218000002205
Resources:
Http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/03-fine-tuning.ipynb