我的问题非常接近这一个。
我在google Colab上安装了Caffe,我正在尝试运行这个开源CNN模型。预训练模型和测试可以在这里下载。
我遵循这些步骤:
我重复了this notebook中的所有说明,以便在我的google Colab上安装Caffe。
然后我下载了预训练的模型,包括test.py 从这个链接(也在上面提到),使用以下代码行:
!wgethttp://opensurfaces.cs.cornell.edu/static/minc/minc-model.tar.gz
我运行运行test.py,其中包括这行代码:
net=caffe。分类器('deploy-{}.prototxt'。格式(arch),'minc-{}。caffemodel'。格式(arch),channel_swap=(2,1,0),mean=numpy。阵列([104,117,124]))
但我得到以下错误:
AttributeError:模块'caffe'没有属性'分类器'
我明白了"classifier.py"在google云端硬盘上的caffe文件夹中,它们是同一件事吗? 如果是的话,我如何将地址实现到上面提到的代码行中?
先谢谢你。
这是我之前运行的代码test.py 要在colab上安装caffe。
!ls
!git clone https://github.com/BVLC/caffe.git
!git reset --hard 9b891540183ddc834a02b2bd81b31afae71b2153 #reset to the newest revision that worked OK on 27.03.2021
# !sudo apt-cache search libhdf5-
# !sudo apt-cache search gflags
# !sudo apt --fix-broken install
!sudo apt-get install libgflags2.2
!sudo apt-get install libgflags-dev
!sudo apt-get install libgoogle-glog-dev
# !sudo apt-get install libhdf5-10 - replaced with 100
!sudo apt-get install libhdf5-100
!sudo apt-get install libhdf5-serial-dev
!sudo apt-get install libhdf5-dev
# !sudo apt-get install libhdf5-cpp-11 - replaced with 100
!sudo apt-get install libhdf5-cpp-100
!sudo apt-get install libprotobuf-dev protobuf-compiler
!find /usr -iname "*hdf5.so"
# got: /usr/lib/x86_64-linux-gnu/hdf5/serial
!find /usr -iname "*hdf5_hl.so"
!ln -s /usr/lib/x86_64-linux-gnu/libhdf5_serial.so /usr/lib/x86_64-linux-gnu/libhdf5.so
!ln -s /usr/lib/x86_64-linux-gnu/libhdf5_serial_hl.so /usr/lib/x86_64-linux-gnu/libhdf5_hl.so
#!find /usr -iname "*hdf5.h*" # got:
# /usr/include/hdf5/serial/hdf5.h
# /usr/include/opencv2/flann/hdf5.h
# Let's try the first one.
%env CPATH="/usr/include/hdf5/serial/"
#fatal error: hdf5.h: No such file or directory
!sudo apt-get install libleveldb-dev
!sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
!sudo apt-get install libsnappy-dev
!echo $CPATH
%cd caffe
!ls
!make clean
!cp Makefile.config.example Makefile.config
!sed -i 's/-gencode arch=compute_20/#-gencode arch=compute_20/' Makefile.config #old cuda versions won't compile
!sed -i 's/\/usr\/local\/include/\/usr\/local\/include \/usr\/include\/hdf5\/serial\//' Makefile.config #one of the 4 things needed to fix hdf5 issues
!sed -i 's/# OPENCV_VERSION := 3/OPENCV_VERSION := 3/' Makefile.config #We actually use opencv 4.1.2, but it's similar enough to opencv 3.
!sed -i 's/code=compute_61/code=compute_61 - gencode=arch=compute_70,code=sm_70 - gencode=arch=compute_75,code=sm_75 -gencode=arch=compute_75,code=compute_75/' Makefile.config #support for new GPUs
!make all -j 4 # -j would use all availiable cores, but RAM related errors occur
import cv2
print(cv2.__version__)
import caffe
# !./data/mnist/get_mnist.sh #Yann Lecun's hosting sometimes fails with 503 error
# So we use alternative source of mnist dataset
!wget www.di.ens.fr/~lelarge/MNIST.tar.gz
!tar -zxvf MNIST.tar.gz
!cp -rv MNIST/raw/* data/mnist/
!./examples/mnist/create_mnist.sh
这是我运行的test.py
(起初我得到错误"将图像放置在图像/brick/中进行分类。jpg,图像/地毯/。jpg,。..'",我创建一个名为'images
'的文件夹,我把example.jpg
放在里面,然后它得到解决。
#!/usr/bin/env python2
from __future__ import division
from __future__ import with_statement
from __future__ import print_function
import caffe
import numpy
import glob
import os.path
import sys
if __name__=='__main__':
if not os.path.exists('images'):
print('Place images to be classified in images/brick/*.jpg, images/carpet/*.jpg, ...')
sys.exit(1)
categories=[x.strip() for x in open('categories.txt').readlines()]
arch='googlenet' # googlenet, vgg16 or alexnet
net=caffe.Classifier('deploy-{}.prototxt'.format(arch),'minc-{}.caffemodel'.format(arch),channel_swap=(2,1,0),mean=numpy.array([104,117,124]))
result={}
for i,x in enumerate(categories):
result[x]=[]
for j,y in enumerate(sorted(glob.glob('images/{}/*'.format(x)))):
z=net.predict([caffe.io.load_image(y)*255.0])[0]
k=z.argmax()
print(arch,y,categories[k],z[k],k==i)
result[x].append(k==i)
for i,x in enumerate(categories):
print(arch,x,sum(result[x])/len(result[x]))
print(arch,sum(sum(x) for x in result.values())/sum(len(x) for x in result.values()))
我收到这个错误 :
AttributeError Traceback(最近的调用 最后)在() 18 19arch='googlenet'#googlenet,vgg16或alexnet --->20net=caffe。分类器('deploy-{}.prototxt'。格式(arch),'minc-{}。caffemodel'。格式(arch),channel_swap=(2,1,0),mean=numpy。阵列([104,117,124])) 21 22结果={}
AttributeError:模块'caffe'没有属性'分类器'