caffe——使用python接口训练

    在ubuntu下编译了原版Caffe,后来又下载便宜了另一个基于Caffe的项目(MyNet)。所以Caffe和MyNet两个文件夹都处于/home/philochan/目录下,按照下面代码利用caffe的python借口对模型进行训练,会出现...TransformationParameter(LayerParameter) has no filed name "..."的错误:
import sys
sys.path.append('/home/philochan/MyNet/python')
import caffe
caffe.set_device(1)
caffe.set_mode_gpu()
solver=caffe.SGDSolver('/home/philochan/MyNet/model/solver.prototxt')
solver.net.copy_from('VOC2012.caffemodel')
solver.solve()

    想了想,觉得可能是导入的caffe模块不正确,我需要的是MyNet项目下便宜好的caffe,但是可能导入了原版的caffe,所以修改了以下train.py,改成下面代码,成功解决了has no filed named ""的错误:

import sys
sys.path.insert(0,'/home/philochan/MyNet/python')
import caffe
caffe.set_device(1)
caffe.set_mode_gpu()
solver=caffe.SGDSolver('/home/philochan/MyNet/model/solver.prototxt')
solver.net.copy_from('VOC2012.caffemodel')
solver.solve()
    总结:在使用caffe的python接口时,头四行代码还是
import sys
caffe_root = ''
sys.path.insert(0,caffe_root + 'python')
import caffe
    关键是使用 sys.path.insert(0, path)而不要用sys.path.append(path),因为sys.path实际是个列表,导入模块搜索路径时会按sys.path的顺序搜索,如果sys.path保存的某个位置也包含了同样名称的模块,就会导致一些莫名其妙的错误。而sys.path.insert(0,path)会将路径插入到这个列表的第一个位置,可以保证导入的模块是程序需要的。


猜你喜欢

转载自blog.csdn.net/chenxjhit/article/details/80348722