Bulk extract Caffe features (to be continued) (Python, C + +, Matlab)

Source: Internet
Author: User

This article refers to the following:

Instant Recognition with Caffe
Extracting Features

Caffe Python feature Extraction

Caffe Practice 4--Use Python to bulk extract Caffe Compute features--by banana melody
Caffe Exercise 3 Use the C + + function provided by Caffe to extract image features in batches--by banana melody

Caffe Python batch extraction of image features
Caffe Python Batch extraction image feature-sequel
Caffe C + + extract picture features

Shicai C + + Caffe extraction Features

Caffe Source Code Modification: Extract any picture features

Matlab batch extraction of CNN Features

The framework of this article on how to extract features in batches is as follows:
1. Preparation of data and corresponding preparation work
2. Initializing the network
3. Read the image list
4. Extract image features and save them in a specific format

Python method One
There are three main functions:
Initialize () Initialization of the network related
ReadList () Read the Extract image list
Extractfeatre () Extracts the characteristics of an image, saving it as a specified format

Where transformer needs to be set according to its own needs.

#encoding: Utf-8 #详情请查看http://www.cnblogs.com/louyihang-loves-baiyan/p/5078746.html import numpy as NP import Matplotlib.pyplot as PLT import OS import Caffe import sys import pickle import struct import sys,cv2 = '. /' # Run the model's prototxt deployprototxt = '/home/bids/caffe/caffe-master/changmiao/model/deploy.prototxt ' # corresponding loaded modelfile m
Odelfile = '/home/bids/caffe/caffe-master/changmiao/model/bvlc_reference_caffenet.caffemodel ' # meanfile can also be generated by itself Meanfile = ' python/caffe/imagenet/ilsvrc_2012_mean.npy ' # A list of images to extract imagelistfile = '/home/bids/caffe/caffe-master/ Changmiao/data/temp.txt ' Imagebasepath = '/home/bids/caffe/caffe-master/changmiao/data/cat ' #gpuID = 4 # Depending on your computer's GPU situation, postfix = '. Classify_allcar1716_fc6 ' # Initialization functions related to Operations Def initilize (): print ' Initilize ... ' Sys.path.insert (0, caffe_root + ' python ') Caffe.set_mode_gpu () # caffe.set_device (gpuid) net = Caffe. Net (Deployprototxt, Modelfile,caffe. TEST) return NET # extracts features and saves them as appropriate file Def extractfeatUre (imageList, net): # Adjust input data Accordingly, such as channel, dimension, etc. transformer = Caffe.io.Transformer ({' Data ': net.blobs[' data '].data.shap e}) transformer.set_transpose (' Data ', (2,0,1)) Transformer.set_mean (' Data ', np.load (Caffe_root + meanfile). Mean (1)  
    . Mean (1)) # mean pixel transformer.set_raw_scale (' data ', 255) transformer.set_channel_swap (' Data ', (2,1,0)) # Set NET to batch size of 1 if you have more pictures, set the appropriate batchsize net.blobs[' data '].reshape (1,3,227,227) #这里根据需要设定, if the network is inconsistent, you need to To adjust the num=0 #imageList = Os.listdir (Imagebasepath) for imagefile in imagelist:imagefile_abs = OS.PATH.J Oin (Imagebasepath, imagefile) print imagefile_abs net.blobs[' data '].data[...] = transformer.preprocess (' da Ta ', Caffe.io.load_image (imagefile_abs)) out = Net.forward () Fea_file = Imagefile_abs.replace ('. jpg ', POSTF
            (ix) NUM +=1 print ' num ', num, ' extract feature ', Fea_file with open (Fea_file, ' WB ') as F: For x in xrange (0, Net.blobs[' Fc6 '].data.shape[0]): For y in xrange (0, net.blobs[' fc6 '].data.shape[1]): F. Write (Struct.pack (' F ', net.blobs[' Fc6 '].data[x,y]) # Read file list def readimagelist (imagelistfile): ImageList = [] Wit H-Open (Imagelistfile, ' R ') as Fi:while (True): line = Fi.readline (). Strip (). Split () # every line is a IM Age file name if not Line:break imagelist.append (line[0]) print ' Read Imagel ist done Image num ', Len (imageList) return imageList if __name__ = = "__main__": Net = initilize () imageList = Readimagelist (imagelistfile) extractfeature (imageList, net)
Related Article

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.