关于caffe:typeerror python类

TypeError Python Class

我有一个图书馆(CAffe),它有以下定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class NetSpec(object):
    def __init__(self):
        super(NetSpec, self).__setattr__('tops', OrderedDict())

    def __setattr__(self, name, value):
        self.tops[name] = value

    def __getattr__(self, name):
        return self.tops[name]

    def to_proto(self):
        names = {v: k for k, v in self.tops.iteritems()}
        autonames = {}
        layers = OrderedDict()
        for name, top in self.tops.iteritems():
            top.fn._to_proto(layers, names, autonames)
        net = caffe_pb2.NetParameter()
        net.layer.extend(layers.values())
        return net

当我试图使用n = caffe.NetSpec()调用它时,我得到以下错误:

1
2
3
4
  File"../../python/caffe/layers.py", line 84, in __init__
    super(NetSpec, self).__setattr__('tops', OrderedDict())

TypeError: must be type, not None

我检查了SO-9698614、SO-576169和SO-489269,但没有找到解决方案。我的班级是一个新型的班级,我不明白它为什么不起作用。

全迹:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Traceback (most recent call last):

  File"<ipython-input-72-694741de221d>", line 1, in <module>
    runfile('/home/shaunak/caffe-pr2086/examples/wine/classify.py', wdir='/home/shaunak/caffe-pr2086/examples/wine')

  File"/home/shaunak/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 682, in runfile
    execfile(filename, namespace)

  File"/home/shaunak/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
    builtins.execfile(filename, *where)

  File"/home/shaunak/caffe-pr2086/examples/wine/classify.py", line 26, in <module>
    f.write(str(logreg('examples/hdf5_classification/data/train.txt', 10)))

  File"/home/shaunak/caffe-pr2086/examples/wine/classify.py", line 18, in logreg
    n = caffe.NetSpec()

  File"../../python/caffe/layers.py", line 84, in __init__
    super(NetSpec, self).__setattr__('tops', OrderedDict())

TypeError: must be type, not None


不知怎么的,你设法把NetSpecNone绑定在某个地方:

1
2
3
4
>>> super(None, object())
Traceback (most recent call last):
  File"<stdin>", line 1, in <module>
TypeError: must be type, not None

错误表明NetSpecglobal与None绑定。

您还可以通过直接转到实例__dict__属性来绕过NetSpec.__setattr__方法:

1
2
3
class NetSpec(object):
    def __init__(self):
       self.__dict__['tops'] = OrderedDict()

从你分享的代码来看,这可能是罪魁祸首:

1
from .layers import layers, params, NetSpec

这将导入caffe.layers,但将caffe.layers重新绑定到Layers()实例。然后,这可能会触发python再次删除模块,因为还没有其他对它的引用(取决于创建sys.modules引用的时间和方式),从而导致所有全局反弹到None(包括NetSpec)。