python接口,我的理解主要是兩個,一個是原來caffe官方給的,這里是接口介紹。另一個是SSD框架加的model_libs.py,主要是添加了一些base_network和一些相關的函數。這兩個如果能夠用得很好的話,兄弟,恭喜你!你已經精通caffe的python接口了。其實不用說的,這個接口很方便,從此你不再需要手動去寫什么網絡,單獨去執行什么bat又或者是sh的命令訓練測試了,接口的可移植性非常好,我們將這些必要的設置寫在python文件里,完成上面的一套工作。因此,在我看來,SSD的python接口就是上面兩個的完美結合。
第一個就是官方給的,官方是這么介紹的:
Python
The Python interface – pycaffe – is the caffe
module and its scripts in caffe/python. import caffe
to load models, do forward and backward, handle IO, visualize networks, and even instrument model solving. All model data, derivatives, and parameters are exposed for reading and writing.
caffe.Net
is the central interface for loading, configuring, and running models.caffe.Classifier
andcaffe.Detector
provide convenience interfaces for common tasks.caffe.SGDSolver
exposes the solving interface.caffe.io
handles input / output with preprocessing and protocol buffers.caffe.draw
visualizes network architectures.- Caffe blobs are exposed as numpy ndarrays for ease-of-use and efficiency.
Tutorial IPython notebooks are found in caffe/examples: do ipython notebook caffe/examples
to try them. For developer reference docstrings can be found throughout the code.
Compile pycaffe by make pycaffe
. Add the module directory to your $PYTHONPATH
by export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
or the like for import caffe
.
上面也說了,使用python接口應該要編譯pycaffe,同時應該export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
,避免no moudle named caffe這樣類似的錯誤。這里給出了一個例子,地址在這:例子,這個例子最主要是說明了怎么查看一個CNN網絡中間層的輸出和特征可視化。對此有不少博客給出了代碼注釋,給一個參考:鏈接。
其實對於中間層的可視化應該是我們在第一個問題中比較關注的,另外py文件如何訓練、測試應該參考SSD模型相應的代碼,在此不再贅述。
下面主要來看看中間層的可視化,代碼如下(需要修改必要的路徑):
import numpy as np import matplotlib.pyplot as plt import pylab import os import caffe import sys import pickle import cv2 caffe_root = os.getcwd() deploy = 'models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt' weight = 'models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel' def initilize(): print 'initilize ... ' sys.path.insert(0, caffe_root + 'python') caffe.set_mode_gpu() caffe.set_device(0) net = caffe.Net(deploy, weight,caffe.TEST) return net def getNetDetails(image, net): transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) #changing blob from H*W*C to C*H*W transformer.set_transpose('data', (2,0,1)) #ensure the pixel scale is range from (0,255) transformer.set_raw_scale('data', 255) #change channel order from RGB to BGR transformer.set_channel_swap('data', (2,1,0)) #reshape data net.blobs['data'].reshape(1,3,300,300) #input data and preprocess net.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image(image)) #testing model is just a forward process out = net.forward() filters = net.params['conv1_1'][0].data with open('FirstLayerFilter.pickle','wb') as f: pickle.dump(filters,f) vis_square(filters.transpose(0, 2, 3, 1)) feat = net.blobs['conv4_4'].data[0, :36] with open('FirstLayerOutput.pickle','wb') as f: pickle.dump(feat,f) vis_square(feat,padval=1) pool = net.blobs['conv8_2'].data[0, :81] with open('pool1.pickle','wb') as f: pickle.dump(pool,f) vis_square(pool,padval=1) def vis_square(data, padsize=1, padval=0 ): data -= data.min() data /= data.max() n = int(np.ceil(np.sqrt(data.shape[0]))) padding = ((0, n ** 2 - data.shape[0]), (0, padsize), (0, padsize)) + ((0, 0),) * (data.ndim - 3) data = np.pad(data, padding, mode='constant', constant_values=(padval, padval)) data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1))) data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:]) plt.imshow(data) pylab.show() print data.shape if __name__ == "__main__": net = initilize() testimage = 'examples/images/person.jpg' getNetDetails(testimage, net)
另一個就是model_libs.py,之所以出現它是因為我們不想自己寫好了prototxt,然后導入,更希望是在一個base_network的基礎下去修改,更具有普適性,同時效率高移植性好,出於這樣的目的,寫了model_libs.py這個腳本,當然這些腳本都是可以根據自己需要修改的,另外可以自己寫個腳本實現項目的其它需求。
兩者一結合,那就實現的太完美了,屢試不爽,從此就多了這么一套有趣的工具。