Search code examples
pythonmxnet

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


I had a problem, when i apply arcface implementation from this repo, i got error.

From this line code:

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)

And the error:

    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

So anyone ever met this error before? And please show me how to fix it.


Solution

  • You are passing a NDArray to DataBatch when DataBatch is accepting an array of NDArrays, each array being of size batch_size.

    Can you try this?

    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)