Caffe Picture feature extraction (python/c++)

Source: Internet
Author: User

Caffe Picture feature Extraction (python/c++) 1. Caffe feature Extraction (c + + implementation)

The Caffe framework provides the appropriate tools (Build/tools/extract_features.bin) tool extract features, the official tutorial, using the following methods:

Extract_features.bin Xxx.caffemodel xxxx.prototxt layer-name output-path mini-batches Db-style
Xxx.caffemodel: Well-trained model parameters
Xxxx.prototxt: Model definition (including the path of the picture to be extracted, mean-file, etc.)
Layer_name: The name of the feature to extract (eg. Fc6 fc7), separated by a space in the middle
Output-path: Extracted feature save path
Mini-batches: The size of each batch_size
Db_-style:feature saved Format (leveldb/lmdb)

For details, please refer to the official tutorials, which are written in detail.
- Advantages: Simple, directly available
- Insufficient: Flexibility is not high, you need to write code to parse the corresponding feature

2.Caffe feature extraction (Python implementation)

This article refers to the Caffe official tutorial feature visualization part of the code, using Python to achieve, calculate the image mean (mean-file), feature extraction and preservation, the specific process is as follows.
1) Calculate the mean value of the input picture to save in NPY format.

#**input**#work_path: Working path containing the input file#list_name: Every line containing ' image_path label '#save_name: Where to save the mean file#image_size: The Mean_file ' s size#channel: The Mean_file ' s Channnel#**output**#mean_file Value (3*227*227) def compute_image_mean(work_path,list_name = ' Train.txt ', save_name = ' 227 _227_mean.npy ', image_size = (227,227), Channnel = 3):mean = Np.zeros ((channnel,) + image_size, dtype=np.float64) List = Os.path.join (work_path,list_name) withOpen (list,' R ') asF:lines = F.readlines () sample_size = Len (lines) Count =0     forI, lineinchEnumerate (lines):if(count% +==0):Print(' finish:%d\r\n '%count) image_name, label = line[:-1].split () img = Cv2.imread (image_name). Astype (np.float32) res = Cv2.resize (IMG, image_size) res = res . Transpose (2,0,1) Mean + = Res count + =1mean = Mean/sample_size Save_file = Os.path.join (work_path,save_name) np.save (save_file, mean)returnMean

2) Extract feature by forward propagation and save as NPY format

#**input**#net: Caffe net#mean_file: Image mean File (c*h*w)#input_file: Input image file (Image_path label)#output_prefix_file (Output_path), every image has a output_path#layer_name: Eg. ' Fc6 fc7 ' def computer_features(Net,mean_file,input_file,output_prefix_file, Layer_name s = ' fc6 fc7 '):    # Load the mean image for subtractionmu = np.load (mean_file)# Create transformer for the input called ' data 'Transformer = Caffe.io.Transformer ({' Data ': net.blobs[' Data '].data.shape}) Transformer.set_transpose (' Data ', (2,0,1))# Move image channels to outermost dimensionTransformer.set_mean (' Data ', Mu)# Subtract the Dataset-mean value in each channelTransformer.set_raw_scale (' Data ',255)# Rescale from [0, 1] to [0, 255]Transformer.set_channel_swap (' Data ', (2,1,0))# Swap channels from RGB to BGR    # Set the size of the input (we can skip this if we ' re happy    # with the default; we can also change it later, e.g., for different batch sizes)net.blobs[' Data '].reshape (Batch_size,# Batch Size                              3,# 3-channel (BGR) Images                              227,227)# image size is 227x227features = Layer_names.split () Inputlines = [] Outputlines = [] withOpen (Input_file,' R ') asF: forLineinchF:inputlines.append (line) withOpen (Output_prefix_file,' R ') asF: forLineinchF:outputlines.append (line)assertLen (inputlines) = = Len (outputlines) i =0     forInput,outputinchZip (inputlines,outputlines): input = Input.strip (' \n\t ') output = Output.strip (' \n\t ') Image_path,label = Input.split ()# print Image_pathInput_image = Caffe.io.load_image (image_path) transformed_image = transformer.preprocess (' Data ', Input_image) net.blobs[' Data '].data[...] = transformed_image Net.forward ()if  notOs.path.isdir (Output): os.makedirs (Output) forFeatureinchFeatures:output_file = Os.path.join (output,feature+'. Npy ') Np.save (Output_file, net.blobs[feature].data[0])if(i% +==0):Print(' finish:%d\r\n '% i) i + =1Model_def =' Models/bvlc_reference_caffenet/deploy.prototxt 'Model_weights =' Models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel 'NET = Caffe.net (Model_def,# Defines the structure of the modelModel_weights,# contains the trained weightsCaffe. TEST)# Use test mode (e.g., don ' t perform dropout)Mean_file = args.root + Args.mean input_file = args.root + args.inputs output_file = args.root + args.outputs Caffe.set_mo DE_CPU () computer_features (Net,mean_file,input_file,output_file)

The above code processing only 1 pictures at a time, if the amount of data is large, will be slow, the proposed batch model to calculate, play the advantages of the GPU, changes are relatively simple, if you need batch-size version, can be privately communicated.
Using Python to extract Caffe features, the key is:net.blobs[' fc6 '].data, the secondary parameters can be extracted and saved

Caffe Picture feature extraction (python/c++)

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.