If you use the formula y=f(wx+b)
To represent the whole operation process , that w and b That's what we need to train ,w Called weight , stay cnn Can also be called convolution kernel (filter),b Is a bias item. .f Is the activation function , Yes sigmoid、relu etc. .x It's the input data .
After data training , The saved caffemodel Inside , In fact, it's on all levels w and b value .
We run the code :
deploy=root + 'mnist/deploy.prototxt' #deploy file caffe_model=root + 'mnist/lenet_iter_9380.caffemodel' # Well trained caffemodelnet = caffe.Net(net_file,caffe_model,caffe.TEST) # load model and network
Just load all the parameters and data into one net In the variable , however net It's a very complicated object, It is impossible to show it directly . among :
net.params: Save the parameter values of each layer (w and b)
net.blobs: Save the data values of each layer
Orders available :
[(k,v[0].data) for k,v in net.params.items()]
View the parameter values of each layer , among k Indicates the name of the layer ,v[0].data It's on each floor W value , and v[1].data It's on each floor b value . Be careful : Not all layers have parameters , Only the convolution layer and the full connection layer have .
You can also not view the specific value , Just want to have a look shape, Orders available
[(k,v[0].data.shape) for k,v in net.params.items()]
Suppose we know the name of the first convolution layer 'Convolution1', Then we can extract the parameters of this layer :
w1=net.params['Convolution1'][0].datab1=net.params['Convolution1'][1].data
Enter these codes , Actually check , Understanding you network Very helpful .
Empathy , In addition to viewing parameters , We can also view the data , But here's the thing ,net There was no data in it at first , Need to run :
net.forward()
Then there will be data . We can use code :
[(k,v.data.shape) for k,v in net.blobs.items()]
or
[(k,v.data) for k,v in net.blobs.items()]
To view the data of each layer . Note the difference between the above parameters , One is net.params, One is net.blobs.
In fact, when the data is just input , We call it picture data , After convolution, we call it characteristic .
If you want to extract the features of the first fully connected layer , You can use the command :
fea=net.blobs['InnerProduct1'].data
Just know the name of a layer , You can extract the features of this layer .
I recommend that you spyder in , Run all the code above , Deep understanding of the model layers .
Last , Summarize a code :
import caffeimport numpy as nproot='/home/xxx/' # root directory deploy=root + 'mnist/deploy.prototxt' #deploy file caffe_model=root + 'mnist/lenet_iter_9380.caffemodel' # Well trained caffemodelnet = caffe.Net(deploy,caffe_model,caffe.TEST) # load model and network[(k,v[0].data.shape) for k,v in net.params.items()] # View the parameter scale of each layer w1=net.params['Convolution1'][0].data # Extract parameters wb1=net.params['Convolution1'][1].data # Extract parameters bnet.forward() # Run the test [(k,v.data.shape) for k,v in net.blobs.items()] # View the data scale of each layer fea=net.blobs['InnerProduct1'].data # Extract a layer of data ( features )
That's all caffe Of python Interface caffemodel Details of parameter and feature extraction , More about python caffemodel For information on parameter feature extraction, please pay attention to other relevant articles on the software development network !