Mac上运行FCN自带的模型
1、从github上下载FCN的源码放在caffe根目录下
2、在voc-fcn8s文件中,可以看到caffemodel-url文件,根据里面的地址下载模型放在FCN文件夹下
3、复制fcn根目录下的infer.py文件到voc-fcn8s文件目录下改名为infer-fcn8s
4、修改infer-fcn8s.py文件如下,注意更改为自己的地址
1、从github上下载FCN的源码放在caffe根目录下
2、在voc-fcn8s文件中,可以看到caffemodel-url文件,根据里面的地址下载模型放在FCN文件夹下
3、复制fcn根目录下的infer.py文件到voc-fcn8s文件目录下改名为infer-fcn8s
4、修改infer-fcn8s.py文件如下,注意更改为自己的地址
# import package import numpy as np from PIL import Image import caffe import matplotlib.pyplot as plt # display plots in this notebook # %matplotlib inline # 初始化地址 caffe_root = '/Users/zhouyang/caffe/fcn.berkeleyvision.org-master/voc-fcn8s/' model_def = caffe_root + 'deploy.prototxt' # 模型文件 model_weights = caffe_root + 'fcn8s-heavy-pascal.caffemodel' #模型权重值 test_image = caffe_root + 'image.jpg' #测试图片 # load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe im = Image.open(test_image) in_ = np.array(im, dtype=np.float32) in_ = in_[:,:,::-1] # change RGB image to BGR image in_ -= np.array((104.00698793,116.66876762,122.67891434)) in_ = in_.transpose((2,0,1)) # Reshape the image from (500, 334, 3) to (3, 500, 334) net = caffe.Net(model_def, model_weights, caffe.TEST) #导入模型 net.blobs['data'].reshape(1, *in_.shape) net.blobs['data'].data[...] = in_ #读入图像 net.forward() #图片进入前馈卷积神经网络 out = net.blobs['score'].data[0].argmax(axis=0) #最后得到的图片 print net.blobs['score'].data[0].shape #(21, 500, 334) print net.blobs['score'].data[0].argmax(axis=0) print out.shape plt.imshow(out) plt.imshow(out,cmap='gray') plt.axis('off') plt.savefig('/Users/zhouyang/caffe/fcn.berkeleyvision.org-master/voc-fcn8s/test.png') plt.show()5、运行该文件。