简体   繁体   English

在Caffe导入错误中加载自定义Python层

[英]Loading custom Python Layer in Caffe-Import Error

I've written my own Caffe layer in Python (maskextractor.py). 我已经在Python(maskextractor.py)中编写了自己的Caffe层。 When training the network from scratch, it worked well. 从头开始训练网络时,它运作良好。 But once I've tried finetuning from the saved network: 但是一旦我尝试从保存的网络进行微调:

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

the error I got is failed import of the new layer: 我得到的错误是新图层的导入失败:

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 ***

Clearly Caffe can't find the new layer. 显然,Caffe找不到新层。 I've added it to the Pythonpath via sys.path.insert and then copied to caffe/python/caffe/ an recompiled pycaffe, but it didn't help either. 我已经通过sys.path.insert将其添加到Pythonpath中,然后将其复制到caffe/python/caffe/重新编译的pycaffe中,但它也无济于事。

EDIT: this only happens when I finetune. 编辑:这只会在我微调时发生。 If I start from solver: 如果我从求解器开始:

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()

everything works fine. 一切正常。 But I want to continue training from he snapshot. 但我想从他的快照继续训练。 In such case I get the error above. 在这种情况下,我得到上面的错误。 PythonPath looks like 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']

EDIT 2: this is how the sys.path and the import looks like 编辑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)'

and sys.path is now 和sys.path现在

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

Yet the exact same problem persists. 然而,完全相同的问题仍然存在。 As I mentioned before, this only comes up when I invoke caffe from the tools dir. 如前所述,仅当我从tools目录调用caffe时才会出现这种情况。 When I create caffe net with the solver rather than solverstate, no problems are reported. 当我使用求解器而不是SolverState创建Caffe Net时,未报告任何问题。

Step 1: Make sure you can import your layer from Python 第1步:确保可以从Python导入图层

To test your python code, you should be able to open python and type from module_name import layer_name where module_name and layer_name is what you use in the prototxt definition. 为了测试您的python代码,您应该能够打开pythonfrom module_name import layer_name中键入from module_name import layer_name其中module_namelayer_name是您在prototxt定义中使用的名称。

As indicated, you passed this step. 如所示,您通过了此步骤。

Step 2: Make sure your PYTHONPATH is valid 第2步:确保您的PYTHONPATH有效

If your PYTHONPATH is valid, you should be able to go to the directory of your caffe excutable and then import your module. 如果您的PYTHONPATH有效,则应该可以转到caffe可执行文件的目录,然后导入模块。 What is the result when you perform this step? 执行此步骤会得到什么结果?

The paths in PYTHONPATH should all be absolute. PYTHONPATH的路径应全部为绝对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM