#-*- coding: UTF-8 -*-
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
caffe_root ='/home/nvidia/caffe/'
# os.chdir(caffe_root) #os.chdir()用于改变当前工作目录到指定的路径
sys.path.insert(0,caffe_root+'python')
import caffe
MODEL_FILE='/home/nvidia/caffe/examples/mnist/lenet.prototxt'
PRETRAINED = '/home/nvidia/caffe/examples/mnist/lenet_iter_10000.caffemodel'
IMAGE_FILE='/home/nvidia/caffe/examples/images/0.bmp'
input_image = caffe.io.load_image(IMAGE_FILE, color=False)
net = caffe.Classifier(MODEL_FILE, PRETRAINED)
# 预测图片分类,没有crop时,oversample过采样为false
prediction = net.predict([input_image], oversample = False)
caffe.set_mode_gpu()
# 打印分类结果
print 'predicted class:', prediction[0].argmax()
出现如下问题: ImportError: No module named _caffe
from caffe import layers as L, params as P, to_proto
ImportError: No module named caffe
这是由于没有将caffe的python模块添加到python的引用目录中导致的,解决方法:
sudo vim ~/.bashrc
在文件最后写入如下内容:
export PYTHONPATH=~/caffe/python:$PYTHONPATH1
该问题解决,再运行,出现如下问题:
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
ImportError: No module named _caffe
这是由于caffe的python模块没有编译的原因,解决方法,重新编译pycaffe
sudo make pycaffe
出现如下信息:
LD -o .build_release/lib/libcaffe.so.1.0.0
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
touch python/caffe/proto/__init__.py
PROTOC (python) src/caffe/proto/caffe.proto
最后在CAFFE_ROOT/python/caffe/
中生成_caffe.so
才可以成功导入_caffe
模块。
#-*- coding: UTF-8 -*-
#!/usr/bin/python
#!/usr/bin/env python
import numpy as np
import struct
import matplotlib.pyplot as plt
# from PIL import Image
import Image
# 确认已经下载并解压得到MNIST_data/t10k-images.idx3-ubyte文件
# filename = 'MNIST_data/t10k-images.idx3-ubyte'
filename = 't10k-images-idx3-ubyte'
# filename = '/home/nvidia/caffe/data/mnist/t10k-images-idx3-ubyte'
binfile = open(filename, 'rb')
buf = binfile.read()
index = 0
magic, numImages, numRows, numColumns = struct.unpack_from('>IIII', buf, index)
index += struct.calcsize('>IIII')
for img in range(0, numImages):
im = struct.unpack_from('>784B', buf, index)
index += struct.calcsize('>784B')
im = np.array(im, dtype='uint8')
im = im.reshape(28,28)
'''
fig = plt.figure()
plotwindow = fig.add_subplot(111)
plt.imshow(im, cmap='gray')
plt.show()
'''
im = Image.fromarray(im)
im.save('mnist_test/train_%s.bmp' %img, 'bmp') # 需要在当前目录下存在mnist_test目录
nvidia@tegra-ubuntu:~/caffe/data/mnist$ sudo chmod -R 777 mnistimg.py
nvidia@tegra-ubuntu:~/caffe/data/mnist$ ./mnistimg.py
http://blog.csdn.net/baggio1006/article/details/6332769
先将终端所在路径切换到python脚本文件的目录下nvidia@tegra-ubuntu:~/caffe/data/mnist$ ./mnistimg.py
然后给脚本文件运行权限,一般755就OK,如果完全是自己的私人电脑,也不做服务器什么的,给777的权限问题也不大(具体权限含义参考chmod指令的介绍,就不赘述了):
chmod 755 ./*.py
然后执行。
如果在脚本内容的开头已经给出了类似于如下的注释:
#!/usr/bin/env python
那就可以直接在终端里运行:
./*.py
如果没有这个注释
就在终端中执行:
python ./*.py
SyntaxError: Non-ASCII character '\xe7' in file demo.py on line 15, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
出现这种错的解决办法:
在文件头上加上:
# -*- coding: cp936 -*-
或者
# -*- coding: utf-8 -*
针对上面的规则,下面给出各种,合法的,非法的,例子,供参考:
1
2
3
4
|
#!/usr/bin/python
# -*- coding: latin-1 -*-
import
os, sys
...
|
1
2
3
4
|
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import
os, sys
...
|
1
2
3
4
|
#!/usr/bin/python
# -*- coding: ascii -*-
import
os, sys
...
|
1
2
3
|
# This Python file uses the following encoding: utf-8
import
os, sys
...
|
1
2
3
4
|
#!/usr/local/bin/python
# coding: latin-1
import
os, sys
...
|
1
2
3
|
#!/usr/local/bin/python
import
os, sys
...
|
1
2
3
4
|
#!/usr/local/bin/python
# latin-1
import
os, sys
...
|
1
2
3
4
5
|
#!/usr/local/bin/python
#
# -*- coding: latin-1 -*-
import
os, sys
...
|
1
2
3
4
|
#!/usr/local/bin/python
# -*- coding: utf-42 -*-
import
os, sys
...
|