如何修复'ImportError:无法在Python中导入名称'numpy_type_map''?

时间:2019-01-27 11:31:31

标签: python pytorch object-detection

我已经按照Detectron中的说明进行了多次配置:代码会按需编译。在运行代码时,出现此错误:

    Traceback (most recent call last):
    File "tools/train_net_step.py", line 21, in <module>
    import nn as mynn
    File "/home/federico/PycharmProjects/Detectron.pytorch/lib/nn/__init__.py", line 2, in <module>
    from .parallel import DataParallel
    File "/home/federico/PycharmProjects/Detectron.pytorch/lib/nn/parallel/__init__.py", line 3, in <module>
    from .data_parallel import DataParallel, data_parallel
    File "/home/federico/PycharmProjects/Detectron.pytorch/lib/nn/parallel/data_parallel.py", line 4, in <module>
    from .scatter_gather import scatter_kwargs, gather
    File "/home/federico/PycharmProjects/Detectron.pytorch/lib/nn/parallel/scatter_gather.py", line 8, in <module>
    from torch.utils.data.dataloader import numpy_type_map
    ImportError: cannot import name 'numpy_type_map'

我也尝试过用Google搜索很多次,但是找不到解决方法。我能做什么?我正在使用PyTorch 0.4.1和pytorch每晚1.0.0-dev。

编辑:感谢sancelot,我设法解决了该错误(PyTorch 0.4.0完成了该操作)。无论如何,现在我又遇到了另一个错误:

    Traceback (most recent call last):
    File "tools/train_net_step.py", line 27, in <module>
    from modeling.model_builder import Generalized_RCNN
    File "/home/federico/PycharmProjects/Detectron.pytorch/lib/modeling/model_builder.py", line 11, in <module>
    from model.roi_pooling.functions.roi_pool import RoIPoolFunction
    File "/home/federico/PycharmProjects/Detectron.pytorch/lib/model/roi_pooling/functions/roi_pool.py", line 3, in <module>
    from .._ext import roi_pooling
    File "/home/federico/PycharmProjects/Detectron.pytorch/lib/model/roi_pooling/_ext/roi_pooling/__init__.py", line 3, in <module>
    from ._roi_pooling import lib as _lib, ffi as _ffi
    ImportError: /home/federico/PycharmProjects/Detectron.pytorch/lib/model/roi_pooling/_ext/roi_pooling/_roi_pooling.so: undefined symbol: PyInt_FromLong

这次我不能得到的是:这是外部库给出的错误吗?我正在使用以前由我的教授制作的anaconda环境,他曾将它用于Detectron ...所以我无法猜出为什么得到这个。

2 个答案:

答案 0 :(得分:1)

是的,这是由于Pytorch版本不匹配造成的。解决方案取决于您愿意采取的程度,有时,如果您对骇客没事,只是让它开始运行,则只需复制粘贴旧版本中的#include <Python.h> #include <uv.h> typedef struct Pod_s { uv_rwlock_t lock; int number; } Pod; typedef struct PyPod_s { PyObject_HEAD Pod pod; } PyPod; static PyMemberDef[] PyPod_members = { {"number", T_INT, offsetof(PyPod, pod) + offsetof(Pod, number)}, {0}, }; static PyObject *PyPod_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyPod *self; self = (PyPod *) type->tp_alloc(type, 0); if(self != NULL) uv_rwlock_init(&self->pod.lock); // In real life this would be error-checked return (PyObject *) self; } // Is this getattro/setattro safe? static PyObject *PyPod_getattro(PyObject *self, PyObject *attr) { PyPod *pypod = (PyPod *) self; uv_rwlock_rdlock(&pypod->pod.lock); PyObject *ret = PyObject_GenericGetAttr(self, attr); uv_rwlock_rdunlock(&pypod->pod.lock); return ret; } static int PyPod_setattro(PyObject *self, PyObject *attr, PyObject *value) { PyPod *pypod = (PyPod *) self; uv_rwlock_wrlock(&pypod->pod.lock); int ret = PyObject_GenericSetAttr(self, attr, value); uv_rwlock_wrunlock(&pypod->pod.lock); return ret; } static PyTypeObject PyPodType = { PyObject_HEAD_INIT(NULL) .tp_name = "PodModule.Pod", .tp_basicsize = sizeof(PyPod), .tp_flags = Py_TPFLAGS_DEFAULT, .tp_new = PyPod_new, .tp_members = PyPod_members, .tp_getattro = PyPod_getattro, .tp_setattro = PyPod_setattro, };

numpy_type_map

或者,在1.1.0版之前,您可以尝试替换import语句

来自

numpy_type_map = {
     'float64': torch.DoubleTensor,
     'float32': torch.FloatTensor,
     'float16': torch.HalfTensor,
     'int64': torch.LongTensor,
     'int32': torch.IntTensor,
     'int16': torch.ShortTensor,
     'int8': torch.CharTensor,
     'uint8': torch.ByteTensor,
 }

from torch.utils.data.dataloader import numpy_type_map

仍会在较新的版本中出现。同样,这是一个 hacky 快速修复解决方案。

答案 1 :(得分:0)

我想在detectron和您使用的所需pytorch版本之间版本不匹配。 如果您查看最新的pytorch源代码,则没有numpy_type_map组件。 https://github.com/pytorch/pytorch/blob/master/torch/utils/data/dataloader.py