caffe(9)使用jupyter測試單張圖片代碼實例
来源:互联网 发布:虚拟制造软件 编辑:程序博客网 时间:2024/06/14 22:40
首先,在命令行打開jupyter
sudo ipython notebook --allow-root
Then, open a new python, you can see like this
matplotlib---paint the pictures
#import required packagesimport numpy as npimport matplotlib.pyplot as pltimport os,sys,caffefrom PIL import Image%matplotlib inline#set path to rootcaffe_root='/home/peipei/caffe/'os.chdir(caffe_root)sys.path.insert(0,caffe_root+'python')#set show parametersplt.rcParams['figure.figsize'] = (64, 64)plt.rcParams['image.interpolation'] = 'nearest'plt.rcParams['image.cmap'] = 'gray'#import caffe model, set the model architecture deploy.prototxt and model parameters .caffemodelnet = caffe.Net(caffe_root + 'examples/myfile2/cifar10_quick.prototxt', caffe_root + 'examples/myfile2/cifar10_quick_iter_300.caffemodel', caffe.TEST)#print each layer[(k, v[0].data.shape) for k, v in net.params.items()]net.blobs['data'].data.shape#编写一个函数,用于显示各层的参数def show_feature(data, padsize=1, padval=0): data -= data.min() data /= data.max() # force the number of filters to be square 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)) # tile the filters into an image 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, cmap='gray') plt.axis('off')# first convolution layer, show in imagesweight = net.params["conv1"][0].dataprint weight.shape#show_feature(weight.transpose(0, 2, 3, 1))#set CPU modecaffe.set_mode_cpu()#加载测试图片,并显示im = caffe.io.load_image('/home/peipei/caffe/examples/myfile2/test/FVC2004_fingerprints_544/4/ms_ds2_s0001_fo10.bmp', False)print im.shape#imr = im.resize((64,64)) #resize image required by inputprint im.shape#plt.imshow(im)# 编写一个函数,将二进制的均值转换为python的均值def convert_mean(binMean,npyMean): blob = caffe.proto.caffe_pb2.BlobProto() bin_mean = open(binMean, 'rb' ).read() blob.ParseFromString(bin_mean) arr = np.array( caffe.io.blobproto_to_array(blob) ) npy_mean = arr[0] np.save(npyMean, npy_mean )#generate the python file: mean.npybinMean=caffe_root+'examples/myfile2/mean.binaryproto'npyMean=caffe_root+'examples/myfile2/mean.npy'convert_mean(binMean,npyMean)#将图片载入blob中,并减去均值transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) # 设定图片的shape格式(1,1,64,64) transformer.set_transpose('data', (2,0,1)) # 改变维度的顺序,由原始图片(64,64,1)变为(1,64,64)transformer.set_mean('data', np.load(npyMean).mean(1).mean(1)) # 减去均值transformer.set_raw_scale('data', 255) # 缩放到【0,255】之间 #transformer.set_channel_swap('data', (2,1,0)) #交换通道,将图片由RGB变为BGR net.blobs['data'].data[...] = transformer.preprocess('data',im) #执行上面设置的图片预处理操作,并将图片载入到blob中 inputData=net.blobs['data'].dataout = net.forward()# 编写一个函数,用于显示各层数据def show_data(data, padsize=1, padval=0): data -= data.min() data /= data.max() # force the number of filters to be square 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)) # tile the filters into an image 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.figure() plt.imshow(data,cmap='gray') plt.axis('off')plt.rcParams['figure.figsize'] = (64, 64)plt.rcParams['image.interpolation'] = 'nearest'plt.rcParams['image.cmap'] = 'gray'#显示第一个卷积层的输出数据和权值(filter)show_data(net.blobs['conv1'].data[0])print net.blobs['conv1'].data.shapeshow_data(net.params['conv1'][0].data.reshape(32,5,5))print net.params['conv1'][0].data.shape# 最后一层输入属于某个类的概率prob = net.blobs['prob'].data[0].flatten()print probplt.plot(prob) order=prob.argsort()[2]print order
finanly, show the probability of all the labels:
[ 0.02902012 0.946181 0.02479883]print order show the label with the highest probability.
阅读全文
0 0
- caffe(9)使用jupyter測試單張圖片代碼實例
- jupyter笔记(9/24)-------jupyter基本使用
- jupyter使用
- jupyter使用
- Python:如何使用jupyter绘制caffe训练过程中的loss和accuracy曲线?n
- 安装使用jupyter(原来的notebook)
- IPython notebook(Jupyter Notebook) 的使用
- Windows下anaconda 使用Jupyter(图文)
- Jupyter Notebook使用笔记(一)
- Jupyter Notebook使用笔记(二)
- Jupyter的安装和使用(windows)
- caffe学习(9)LeNet在Caffe上的使用
- Jupyter Notebook的使用
- Jupyter使用积累
- Jupyter notebook使用记录
- jupyter notebook使用技巧
- Jupyter Notebook 使用小结
- jupyter notebook 使用记录
- 配置文件Properties读取类 ResourceBundle
- okhttp
- 最长公共子序列LCS(动态规划法)
- CSS,JS方面几个简单的笔试题
- eclipse查看底层源码
- caffe(9)使用jupyter測試單張圖片代碼實例
- Implicit Analysis(二)
- 大数据工程师必备技能图谱
- spring源码(5)父子beans标签之间的属性关系
- 伪类清除浮动
- 凸包 +模板
- HDU6055-Regular polygon
- 7、取近似值
- Struts2——"token"拦截器(防止表单重复提交)