python - 类型错误 Python 类

标签 python caffe

我有一个具有以下定义的库(caffe):

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() 调用它时,出现以下错误:

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

TypeError: must be type, not None

我检查了SO-9698614 , SO-576169SO-489269但他们并没有找到解决办法。我的类(class)是一个新类型的类(class),我不明白为什么它不起作用。

完整跟踪:

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

最佳答案

不知何故,您设法将 NetSpec 绑定(bind)到 None 某处:

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

该错误表明 NetSpec 全局绑定(bind)到 None

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

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

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

from .layers import layers, params, NetSpec

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

关于python - 类型错误 Python 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29774793/

相关文章:

neural-network - 挽救发散的神经网络

python - 理解 Pandas 中的 any() 和 nan

python - 了解 Python 配置文件输出

python - 如何将annotation.xml文件转换为train.txt和val.txt以进行对象检测?

computer-vision - 三重态损失反向传播梯度公式是什么?

python - 当我尝试在 Windows 10 上安装 caffe 时,脚本找不到 python 库和 numpy

python - 确定天空中的 eclipse

python - 旧 Plone 产品的自动化蛋化

python - 解码并打印转义字符

python - 无法导入咖啡