Yolov3: PyCharm Printing Numpy Arrays (IndexError: tuple index out of range)

Created on 12 Sep 2018  路  5Comments  路  Source: ultralytics/yolov3

when i print (labels) in datasets.py ,row 143, there are a problem:
i can not print (lables), but i can print (labels[0][0]), print(labels0.shape)

# Load labels
if os.path.isfile(label_path):
labels0 = np.loadtxt(label_path, dtype=np.float32).reshape(-1, 5)
print(labels0.shape) #143
print(labels0[0][0]) #144
print(labels) #145
exit()

#

Traceback (most recent call last):
File "train.py", line 193, in
main(opt)
File "train.py", line 116, in main
for i, (imgs, targets) in enumerate(dataloader):
File "/home/chenfei/Downloads/yolov3-master1/utils/datasets.py", line 143, in __next__
print(labels)
File "/home/chenfei/anaconda3/lib/python3.6/site-packages/numpy/core/arrayprint.py", line 1504, in array_str
return array2string(a, max_line_width, precision, suppress_small, ' ', "")
File "/home/chenfei/anaconda3/lib/python3.6/site-packages/numpy/core/arrayprint.py", line 668, in array2string
return _array2string(a, options, separator, prefix)
File "/home/chenfei/anaconda3/lib/python3.6/site-packages/numpy/core/arrayprint.py", line 460, in wrapper
return f(self, args, *kwargs)
File "/home/chenfei/anaconda3/lib/python3.6/site-packages/numpy/core/arrayprint.py", line 495, in _array2string
summary_insert, options['legacy'])
File "/home/chenfei/anaconda3/lib/python3.6/site-packages/numpy/core/arrayprint.py", line 796, in _formatArray
curr_width=line_width)
File "/home/chenfei/anaconda3/lib/python3.6/site-packages/numpy/core/arrayprint.py", line 750, in recurser
word = recurser(index + (-i,), next_hanging_indent, next_width)
File "/home/chenfei/anaconda3/lib/python3.6/site-packages/numpy/core/arrayprint.py", line 704, in recurser
return format_function(a[index])
IndexError: tuple index out of range

bug

Most helpful comment

@CF2220160244 I get the same error on printing numpy arrays, but I think in my case this is a PyCharm problem. In PyCharm if I convert the numpy array to a torch tensor print works fine:

labels0 = np.loadtxt(label_path, dtype=np.float32).reshape(-1, 5)

print(labels0)
Traceback (most recent call last):
...
IndexError: tuple index out of range

print(torch.from_numpy(labels0))
tensor([[76.00000,  0.35609,  0.44456,  0.54970,  0.16180],
        [ 0.00000,  0.48129,  0.40587,  0.94952,  0.80312]])

In Spyder the same operation shows no errors:

ipdb> labels0 = np.loadtxt(label_path, dtype=np.float32).reshape(-1, 5)

ipdb> labels0
array([[76.      ,  0.356086,  0.444563,  0.549703,  0.161803],
       [ 0.      ,  0.481289,  0.405874,  0.949516,  0.803123]], dtype=float32)

ipdb> print(labels0)
[[76.        0.356086  0.444563  0.549703  0.161803]
 [ 0.        0.481289  0.405874  0.949516  0.803123]]

All 5 comments

@CF2220160244 I get the same error on printing numpy arrays, but I think in my case this is a PyCharm problem. In PyCharm if I convert the numpy array to a torch tensor print works fine:

labels0 = np.loadtxt(label_path, dtype=np.float32).reshape(-1, 5)

print(labels0)
Traceback (most recent call last):
...
IndexError: tuple index out of range

print(torch.from_numpy(labels0))
tensor([[76.00000,  0.35609,  0.44456,  0.54970,  0.16180],
        [ 0.00000,  0.48129,  0.40587,  0.94952,  0.80312]])

In Spyder the same operation shows no errors:

ipdb> labels0 = np.loadtxt(label_path, dtype=np.float32).reshape(-1, 5)

ipdb> labels0
array([[76.      ,  0.356086,  0.444563,  0.549703,  0.161803],
       [ 0.      ,  0.481289,  0.405874,  0.949516,  0.803123]], dtype=float32)

ipdb> print(labels0)
[[76.        0.356086  0.444563  0.549703  0.161803]
 [ 0.        0.481289  0.405874  0.949516  0.803123]]

The issue seem to be that the print option for float_kind has a typo (https://github.com/ultralytics/yolov3/blob/master/utils/utils.py#L10).
Try changing
np.set_printoptions(linewidth=320, formatter={'float_kind': '{11.5g}'.format})
to
np.set_printoptions(linewidth=320, formatter={'float_kind': '{:11.5g}'.format})
(format was trying to access the 11th element of the input instead of using 11.5g as formatting)

@simedw you are right, this fixes the problem! I implemented this change in commit bce94f6adec6988c07bf295ea50bf59299974886.

@simedw you are right, this fixes the problem! I implemented this change in commit bce94f6.
But this error is not fixed by using th above suggestion
return a.reshape((1, a.shape[0], a.shape[1]))
IndexError: tuple index out of range

@NaumanKhan665
Hello, thank you for your interest in our work! Please note that most technical problems are due to:

  • Your changes to the default repository. If your issue is not reproducible in a fresh git clone version of this repository we can not debug it. Before going further run this code and ensure your issue persists:
sudo rm -rf yolov3  # remove exising repo
git clone https://github.com/ultralytics/yolov3 && cd yolov3 # git clone latest
python3 detect.py  # verify detection
python3 train.py  # verify training (a few batches only)
# CODE TO REPRODUCE YOUR ISSUE HERE
  • Your custom data. If your issue is not reproducible with COCO data we can not debug it. Visit our Custom Training Tutorial for exact details on how to format your custom data. Examine train_batch0.jpg and test_batch0.jpg for a sanity check of training and testing data.
  • Your environment. If your issue is not reproducible in a GCP Quickstart Guide VM we can not debug it. Ensure you meet the requirements specified in the README: Unix, MacOS, or Windows with Python >= 3.7, Pytorch >= 1.1, etc. You can also use our Google Colab Notebook to test your code in working environment.

If none of these apply to you, we suggest you close this issue and raise a new one using the Bug Report template, providing screenshots and minimum viable code to reproduce your issue. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Alex1101a picture Alex1101a  路  5Comments

Deep-Learner picture Deep-Learner  路  5Comments

Aria20155 picture Aria20155  路  3Comments

leeyunhome picture leeyunhome  路  3Comments

mehrdadazizi72 picture mehrdadazizi72  路  3Comments