Caffe training to test your own data set

Source: Internet
Author: User

Simply record your own use of the Caffe process and some of the problems encountered.

Download Caffe and installation is not described in detail, you can refer to http://caffe.berkeleyvision.org/installation.html.


Here's the process of preparing the dataset and training reference Imagenet: refer to Http://drubiano.github.io/2014/06/18/caffe-custom-data.html

1. Divide the DataSet into train and validate, written in Train.txt and Val.txt, respectively. Format each line file name + Space + label (label is starting from 0, and continuous)

00001.jpg 0

00002.jpg 1

00004.jpg 1

00003.jpg 2

2. Put the prepared two txt into caffe_root/data/myfile/(Caffe_root is the root of the Caffe, MyFile own name).

3. Go to the caffe_root/examples/imagenet/directory and modify the create_imagenet.sh

Data=data/myfile

train_data_root=/img_full_dir1(Img_full_dir1 store the catalogue of training pictures)

val_data_root=/img_full_dir2 (img_full_dir2 stores the catalog of test pictures)

Executed under Caffe_root./examples/imagenet/create_imagenet.sh, can be found in examples/imagenet directory, Ilsvrc12_train_lmdb ilsvrc12_val _lmdb of two directories

4. Execute under Caffe_root./examples/imagenet/make_imagenet_mean.sh, results saved in Data/ilsvrc12/imagenet_mean.binaryproto

5. Modify the Solver.prototxt and Train_val.prototxt under Models/bvlc_reference_caffenet, if you do not modify the structure of the network, just modify the size of the picture Crop_size < ImageWidth.

6. Execute under Caffe_root./examples/imagenet/train_caffenet.sh, train, train a good model on the Models/bvlc_reference_caffenet

Test networks and Models

1. Execute under Python, first run in the root directory make Pycaffe make distribute

2. Convert Data/ilsvrc12/imagenet_mean.binaryproto to NumPy format, create a Mycode folder in Caffe_root, and create a convertmean.py with the following content:

#!/usr/bin/pythonimport NumPy as Npimport syssys.path.append ('/caffe_root/python ') <span style= "White-space:pre"  ></span> #caffe_root is your caffe root dir need changeimport Caffeif len (sys.argv)! = 3:    print "Usage:python convert_protomean.py proto.mean out.npy "    sys.exit () blob = Caffe.proto.caffe_pb2. Blobproto () data = open (Sys.argv[1], ' RB '). Read () blob. Parsefromstring (data) arr = Np.array (Caffe.io.blobproto_to_array (BLOB)) out = Arr[0]np.save (sys.argv[2], out)

3. Execute Python convertmean.py/caffe_root/data/ilsvrc12/imagenet_mean.binaryproto mean.npy

4. Place a picture in the Mycode directory, vim testcode.py

#!/usr/bin/pythonimport numpy as Npcaffe_root = '/caffe_root/' <span style= ' White-space:pre ' ></span>#< Span style= "font-family:arial, Helvetica, Sans-serif;" >caffe_root is your caffe root dir need change</span>import Syssys.path.insert (0,caffe_root+ ' python ') import Caffemodel_file = caffe_root + '/models/bvlc_reference_caffenet/deploy.prototxt ' <span style= "White-space:pre" ></span> #your netpretrained = caffe_root + '/models/bvlc_reference_caffenet/caffenet_train.caffemodel ' < Span style= "White-space:pre" ></span> #your model image_file = caffe_root + ' mycode/imagename.jpg ' <span Style= "White-space:pre" ></span> #your imageimport osif not Os.path.isfile (pretrained): print ("Downloading pre-trained caffenet model ... ") caffe.set_mode_cpu () #net = Caffe. Classifier (Model_file, pretrained,# mean=np.load (caffe_root + ' mycode/mean.npy '). Mean (1). Mean (1), # channel_swap= ( 2,1,0), # raw_scale=255,# image_dims= (()) NET = Caffe. Classifier(Model_file, pretrained) net.set_raw_scale (' data ', 255) net.set_channel_swap (' Data ', (2,1,0)) Net.set_mean (' Data ', Np.load (caffe_root + ' mycode/mean.npy ')) Input_image = Caffe.io.load_image (image_file) prediction = net.predict ([input _image]) print ' prediction shape: ', Prediction[0].shapeprint ' predicted class: ', Prediction[0].argmax ()


The above is the whole process, for reference only. Many of the code on the Web is quoted.


Caffe training to test your own data set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.