如何修复 python 中的 "IndexError('Slicing stop %d exceeds limit of %d' % (stop, length))" 错误

How to fix "IndexError('Slicing stop %d exceeds limit of %d' % (stop, length))" error in python

我有一个问题,当我从这个 repo 应用 arcface 实现时,我得到了错误。

来自这行代码:

face_imgs_resized = np.array(face_imgs_resized)
face_imgs_resized = np.rollaxis(face_imgs_resized, 3, 1)

data = self.mx.nd.array(face_imgs_resized)
db = self.mx.io.DataBatch(data=(data,))
self.model.forward(db, is_train=False)

错误:

    self.model.forward(db, is_train=False)
  File "/root/miniconda/envs/roy/lib/python3.7/site-packages/mxnet/module/module.py", line 625, in forward
    self.exec_group.forward(data_batch, is_train)
  File "/root/miniconda/envs/roy/lib/python3.7/site-packages/mxnet/module/executor_group.py", line 450, in forward
    load_data(data_batch, self.data_arrays, self.data_layouts)
  File "/root/miniconda/envs/roy/lib/python3.7/site-packages/mxnet/module/executor_group.py", line 74, in _load_data
    _load_general(batch.data, targets, major_axis)
  File "/root/miniconda/envs/roy/lib/python3.7/site-packages/mxnet/module/executor_group.py", line 48, in _load_general
    d_src[slice_idx.start:slice_idx.stop].copyto(d_dst)
  File "/root/miniconda/envs/roy/lib/python3.7/site-packages/mxnet/ndarray/ndarray.py", line 506, in __getitem
    return self._get_nd_basic_indexing(key)
  File "/root/miniconda/envs/roy/lib/python3.7/site-packages/mxnet/ndarray/ndarray.py", line 787, in _get_nd_basic_indexing
    return self._slice(key.start, key.stop)
  File "/root/miniconda/envs/roy/lib/python3.7/site-packages/mxnet/ndarray/ndarray.py", line 902, in _slice
    start, stop, _ = _get_index_range(start, stop, self.shape[0])
  File "/root/miniconda/envs/roy/lib/python3.7/site-packages/mxnet/ndarray/ndarray.py", line 2327, in _get_index_range
    raise IndexError('Slicing stop %d exceeds limit of %d' % (stop, length))
IndexError: Slicing stop 2 exceeds limit of 1

有人以前遇到过这个错误吗?请告诉我如何修复它。

当 DataBatch 接受 NDArray 数组时,您将 NDArray 传递给 DataBatch,每个数组的大小为 batch_size。

你能试试这个吗?

face_imgs_resized = np.ones((128,224,224,3))
face_imgs_resized = np.array(face_imgs_resized)
face_imgs_resized = np.rollaxis(face_imgs_resized, 3, 1)
data = mx.nd.array(face_imgs_resized)
db = mx.io.DataBatch(data=mx.nd.split_v2(data, 4))
self.model.forward(db, is_train=False)