在Caffe-Import错误中加载自定义Python层

时间:2017-11-27 13:58:26

标签: python caffe pycaffe

我用Python编写了自己的Caffe图层(maskextractor.py)。从头开始训练网络时,它运作良好。但是,一旦我尝试从保存的网络中进行微调:

../caffe/build/tools/caffe train -solver solverFCN8s_MCN_newmodule.prototxt -snapshot snapshot/train8MCNs_borders_pascal_maskextractor_iter_1.solverstate

我得到的错误是导入新图层失败:

I1127 09:38:40.254966  3102 layer_factory.hpp:77] Creating layer maskextractor
ImportError: No module named mask_extractor
terminate called after throwing an instance of 'boost::python::error_already_set'
*** Aborted at 1511775520 (unix time) try "date -d @1511775520" if you are using GNU date ***

很明显,Caffe无法找到新图层。我已经通过sys.path.insert将其添加到Python路径中,然后将其复制到caffe/python/caffe/重新编译的pycaffe,但它也没有帮助。

编辑:这只有在我微调时才会发生。如果我从求解器开始:

import numpy as np
from PIL import Image
import os, sys
caffe_dir = "/home/ICTDOMAIN/453615/Downloads/caffe/python"
sys.path.insert(0,caffe_dir)
import caffe
newmodule_dir = "../lib/mask_extractor"
sys.path.insert(0, newmodule_dir)
import mask_extractor
#
caffe.set_mode_gpu()
caffe.set_device(0)

# continue from the saved weights
weights = 'snapshot/train8MCNs_borders_pascal_adadelta_maskextractor_new_iter_1.caffemodel'
solver=caffe.get_solver('solverFCN8s_MCN_adadelta_maskextractor_new.prototxt')
solver.net.copy_from(weights)

solver.solve()
一切正常。但我想继续他的快照培训。在这种情况下,我得到上面的错误。 PythonPath看起来像

print sys.path

 ['/home/ICTDOMAIN/453615/Downloads/caffe/python', '../lib/mask_extractor', '/home/ICTDOMAIN/453615/Downloads/caffe/python', '/home/ICTDOMAIN/453615/Downloads/fcn.berkeleyvision.org/voc-fcn8s', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/dist-packages/mxnet-0.9.5-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/fast_rcnn-0.0.0-py2.7-linux-x86_64.egg', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

编辑2:这是sys.path和导入的样子

from subprocess import call
import numpy as np
from PIL import Image
import os, sys
#caffe_dir = "/home/ICTDOMAIN/453615/Downloads/caffe-crfrnn/python"
#caffe_dir = "/home/ICTDOMAIN/453615/Downloads/caffe/python"
#sys.path.insert(0,caffe_dir)
#import caffe
newmodule_dir = "/home/ICTDOMAIN/453615/Downloads/fcn.berkeleyvision.org/lib/mask_extractor"
sys.path.insert(0, newmodule_dir)
import mask_extractor
#import caffe
#
caffe.set_mode_gpu()
caffe.set_device(0)
print sys.path
# continue from the saved weights

call('/home/ICTDOMAIN/453615/Downloads/fcn.berkeleyvision.org/voc-fcn8s/run_ft.sh', shell=True)'

和sys.path现在是

'/home/ICTDOMAIN/453615/Downloads/caffe/python', '/home/ICTDOMAIN/453615/Downloads/fcn.berkeleyvision.org/lib/mask_extractor'

然而,同样的问题仍然存在。正如我之前提到的,当我从tools目录调用caffe时,这只会出现。当我使用求解器创建caffe网而不是solverstate时,没有报告任何问题。

1 个答案:

答案 0 :(得分:2)

第1步:确保您可以从Python

导入图层

要测试您的python代码,您应该能够打开python并输入from module_name import layer_name,其中module_namelayer_name是您在原型文本定义中使用的内容。

如上所示,您通过了此步骤。

步骤2:确保您的PYTHONPATH有效

如果您的PYTHONPATH有效,您应该可以转到caffe excutable的目录,然后导入您的模块。执行此步骤时的结果是什么?

PYTHONPATH中的路径都应该是绝对的。