caffe学习_1.caffe测试

之前四篇文章简单介绍了怎样配置好环境的,https://mp.csdn.net/postedit/81120722 https://mp.csdn.net/postedit/81120934  https://mp.csdn.net/postedit/81120953  https://mp.csdn.net/postedit/81120987

至此 ,

ubuntu1604+cuda+anaconda2+opencv340+opencv340_contrib+caffe+pycaffe

深度学习环境搭建完成 !

期间即便有遇到很多问题,查找了些博客、论坛能够解决了大部分,小部分就是根据error自己瞎搞搞解决的,就记录了一部分问题在之前的caffe安装四篇文章里面。

接下来就是测试是否成功了,主要根据caffe官网http://caffe.berkeleyvision.org/提供的两个入门指导文件。

一.pycaffe的测试

http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb

我是将其中的命令全部写入到py文件中去了,注意需要把网站里面提到的准备工作都在终端里面执行掉,在python文件里面将其注释掉,不然执行py文件会报错。需要在终端执行的两句:

sudo ./caffe/scripts/download_model_binary.py caffe/models/bvlc_reference_caffenet
#下载caffenet模型文件
sudo ./caffe/data/ilsvrc12/get_ilsvrc_aux.sh #下载标签

这是py文件,将其放在caffe/examples文件夹里面:




# set up Python environment: numpy for numerical routines, and matplotlib for plotting
import numpy as np
import matplotlib.pyplot as plt
# display plots in this notebook
%matplotlib inline

# set display defaults
plt.rcParams['figure.figsize'] = (10, 10)        # large images
plt.rcParams['image.interpolation'] = 'nearest'  # don't interpolate: show square pixels
plt.rcParams['image.cmap'] = 'gray'  # use grayscale output rather than a (potentially misleading) color heatmap

# The caffe module needs to be on the Python path;
#  we'll add it here explicitly.
import sys
caffe_root = '../'  # this file should be run from {caffe_root}/examples (otherwise change this line)
sys.path.insert(0, caffe_root + 'python')

import caffe
# If you get "No module named _caffe", either you have not built pycaffe or you have the wrong path.

import os
if os.path.isfile(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'):
    print 'CaffeNet found.'
#else:
#    print 'Downloading pre-trained CaffeNet model...'
#    !../scripts/download_model_binary.py ../models/bvlc_reference_caffenet

caffe.set_mode_cpu()

model_def = caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt'
model_weights = caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'

net = caffe.Net(model_def,      # defines the structure of the model
                model_weights,  # contains the trained weights
                caffe.TEST)     # use test mode (e.g., don't perform dropout)

 load the mean ImageNet image (as distributed with Caffe) for subtraction
mu = np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy')
mu = mu.mean(1).mean(1)  # average over pixels to obtain the mean (BGR) pixel values
print 'mean-subtracted values:', zip('BGR', mu)

# create transformer for the input called 'data'
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})

transformer.set_transpose('data', (2,0,1))  # move image channels to outermost dimension
transformer.set_mean('data', mu)            # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255)      # rescale from [0, 1] to [0, 255]
transformer.set_channel_swap('data', (2,1,0))  # swap channels from RGB to BGR

# set the size of the input (we can skip this if we're happy
#  with the default; we can also change it later, e.g., for different batch sizes)
net.blobs['data'].reshape(50,        # batch size
                          3,         # 3-channel (BGR) images
                          227, 227)  # image size is 227x227

image = caffe.io.load_image(caffe_root + 'examples/images/cat.jpg')
transformed_image = transformer.preprocess('data', image)
plt.imshow(image)

# copy the image data into the memory allocated for the net
net.blobs['data'].data[...] = transformed_image

### perform classification
output = net.forward()

output_prob = output['prob'][0]  # the output probability vector for the first image in the batch

print 'predicted class is:', output_prob.argmax()

# load ImageNet labels
labels_file = caffe_root + 'data/ilsvrc12/synset_words.txt'
#if not os.path.exists(labels_file):
#    !../data/ilsvrc12/get_ilsvrc_aux.sh
    
labels = np.loadtxt(labels_file, str, delimiter='\t')

print 'output label:', labels[output_prob.argmax()]

# sort top five predictions from softmax output
top_inds = output_prob.argsort()[::-1][:5]  # reverse sort and take five largest items

print 'probabilities and labels:'
zip(output_prob[top_inds], labels[top_inds])

 能输出预测信息,pycaffe配置成功 !

二.caffe(C++)的测试

http://caffe.berkeleyvision.org/gathered/examples/cpp_classification.html

之前测试pycaffe时已经下载过了模型文件和标签信息,只需在caffe目录下执行

./build/examples/cpp_classification/classification.bin models/bvlc_reference_caffenet/deploy.prototxt models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel data/ilsvrc12/imagenet_mean.binaryproto data/ilsvrc12/synset_words.txt examples/images/cat.jpg

 能输出预测信息,caffe(C++)配置成功 !

环境都搭载好了,但可惜在配置过程中遇到乱七八糟问题我ubuntu系统崩过(主要还是第一次接触linux,手忙脚乱),然后修复的时候把引导文件从移动硬盘上写回了默认的电脑C盘,因此我的caffe不能“移动”了,如果能把引导文件重写到移动硬盘而不更改其他,就更好了 !挖坑待填 !

接下来就是1.看看能不能把移动硬盘上的ubuntu的引导文件写回移动硬盘,而不动其他,否则再来安装配置环境,有点烦

                  2.学习一些经典的模型,学会用caffe做fine-tune

挖下两个坑,慢慢填了 ~

回来填第一坑,看了两篇感觉跟我的境遇挺相似的都是ubuntu要重做引导文件到其他地方,

https://www.cnblogs.com/iamnewsea/p/7701436.html

也就是这个操作:先将你的ubuntu-12.04 64位的启动u盘(LiveUSB)打开,复制里面的EFI文件夹(或者到iso中去提取)到移动硬盘或u盘的FAT32分区中。再进到"/boot/efi/EFI/ubuntu/"中,用grubx64.efi覆盖"EFI/BOOT/"下的文件。完成,是的,仅需复制粘贴就可建立移动设备上的引导了。

结果,失败,没用,把移动硬盘插到另外一台电脑上能bios里面能够显示ubuntu,但进不去。然后下一篇:

https://blog.csdn.net/qq_34570910/article/details/78205915

主要是这个操作:

$ sudo su
# mount /dev/sdb9 /mnt(注意先确认自己的 / 分区是 sdaX)
# mount /dev/sdb8 /mnt/boot/efi
# mount -t proc proc /mnt/proc
# mount -t sysfs sys /mnt/sys
# mount -o bind /dev /mnt/dev
# mount -t devpts pts /mnt/dev/pts/
# chroot /mnt
# grub-install /dev/sdb8
# update-grub2

结果失败,还是没用,。。。一顿操作还是无法解决。暂时不搞了,就用原来的机子学习,再说了......再次留坑。

下一篇 名词解释 https://blog.csdn.net/vahalla233/article/details/81874995

猜你喜欢

转载自blog.csdn.net/vahalla233/article/details/81808470
今日推荐