IndexError: list index out of range

Description

Traceback (most recent call last):
File “trt_ssd.py”, line 102, in
main()
File “trt_ssd.py”, line 94, in main
loop_and_detect(cam, trt_ssd, conf_th=0.3, vis=vis)
File “trt_ssd.py”, line 64, in loop_and_detect
img = vis.draw_bboxes(img, boxes, confs, clss)
File “/home/hyebin/project/tensorrt_demos/utils/visualization.py”, line 96, in draw_bboxes
color = self.colors[cl]
IndexError: list index out of range

I got this error when I run Tensorrt_demo

while True:
if cv2.getWindowProperty(WINDOW_NAME, 0) < 0:
break
img = cam.read()
if img is not None:
boxes, confs, clss = trt_ssd.detect(img, conf_th)
img = vis.draw_bboxes(img, boxes, confs, clss)
img = show_fps(img, fps)
cv2.imshow(WINDOW_NAME, img)
toc = time.time()
curr_fps = 1.0 / (toc - tic)
# calculate an exponentially decaying average of fps number
fps = curr_fps if fps == 0.0 else (fps0.95 + curr_fps0.05)
tic = toc
key = cv2.waitKey(1)
if key == 27: # ESC key: quit program
break
elif key == ord(‘F’) or key == ord(‘f’): # Toggle fullscreen
full_scrn = not full_scrn
set_display(WINDOW_NAME, full_scrn)

def main():
args = parse_args()
cam = Camera(args)
cam.open()
if not cam.is_opened:
sys.exit(‘Failed to open camera!’)
cls_dict = get_cls_dict(args.model.split(’_’)[-1])
trt_ssd = TrtSSD(args.model, INPUT_HW)
cam.start()
open_window(WINDOW_NAME, args.image_width, args.image_height,
‘Camera TensorRT SSD Demo for Jetson Nano’)
vis = BBoxVisualization(cls_dict)
loop_and_detect(cam, trt_ssd, conf_th=0.3, vis=vis)
cam.stop()
cam.release()
cv2.destroyAllWindows()

This is where trt_ssd.py’s error occurred.

def draw_bboxes(self, img, boxes, confs, clss):
“”“Draw detected bounding boxes on the original image.”""
for bb, cf, cl in zip(boxes, confs, clss):
cl = int(cl)
x_min, y_min, x_max, y_max = bb[0], bb[1], bb[2], bb[3]
color = self.colors[cl]
cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color, 2)
txt_loc = (max(x_min+2, 0), max(y_min+2, 0))
cls_name = self.cls_dict.get(cl, ‘CLS{}’.format(cl))
txt = ‘{} {:.2f}’.format(cls_name, cf)
img = draw_boxed_text(img, txt, txt_loc, color)

And this is where visualization.py’s error occurred.

How to solve it?

Environment

TensorRT Version: 6
GPU Type: Jetson TX2
Nvidia Driver Version:
CUDA Version: 10
CUDNN Version: 7
Operating System + Version: Jetpack 4.3
Python Version (if applicable): 3.6
TensorFlow Version (if applicable): 1.15
PyTorch Version (if applicable):
Baremetal or Container (if container which image + tag):

Hi,

If I am not wrong you are referring to below tensorrt demo code:
https://github.com/jkjung-avt/tensorrt_demos.
Request you to please file issue on https://github.com/jkjung-avt/tensorrt_demos/issues

Thanks

You need to initialize “BBoxVisualization” with the correct “cls_dict” (class dictionary, which contains the correct number of classes).

I got this answer. But I don’t know what he is saying.

Hi,

I think, as suggested above you might have to update the ssd_classes as per your model’s supported classes.

Since cls_dict count is not matching with your model, you might be getting this issue.

In case of further query, would recommend you post it on git issues section

Thanks

This error basically means you are trying to access a value at a List index which is out of bounds i.e greater than the last index of the list or less than the least index in the list. So the first element is 0, second is 1, so on. So if there are n elements in a python list, the last element is n-1 . If you try to access the empty or None element by pointing available index of the list, then you will get the "List index out of range " error. To solve this error, you should make sure that you’re not trying to access a non-existent item in a list.

Gtk-Message: 16:50:34.237: Failed to load module "canberra-gtk-module"
Traceback (most recent call last):
  File "camera_tf_trt.py", line 198, in <module>
    main()
  File "camera_tf_trt.py", line 188, in main
    loop_and_detect(cam, tf_sess, args.conf_th, vis, od_type=od_type)
  File "camera_tf_trt.py", line 120, in loop_and_detect
    img = vis.draw_bboxes(img, box, conf, cls)
  File "/home/shlok/projects/tf_trt_models/utils/visualization.py", line 96, in draw_bboxes
    color = self.colors[cl]
IndexError: list index out of range

I am running https://github.com/jkjung-avt/tf_trt_models using this command
python3 camera_tf_trt.py --latency 200 --model new_model_ssd_colab_v2_mobilenet_2020_29_12 --labelmap data/label_final.pbtxt --num-classes 3

I have only 3 classes in label file(bottle,cardboard,clothing). I am running my custom model was able to create tf-trt graph of my model successfully but as soon as I run it using onboard camera it freezes and gives index out of range errror

{0: 'CLS0', 1: 'bottle', 2: 'cardboard', 3: 'clothing'} this is the list of the classes in cls_dict so I changed --num-classes 4 still it didn’t work. I am not sure about 0: 'CLS0' is that for background? I can see this class gets added here along with the classes from label file /home/s/projects/tf_trt_models/utils/od_utils.py

def read_label_map(path_to_labels):
    """Read from the label map file and return a class dictionary which
    maps class id (int) to the corresponding display name (string).

    Reference:
    https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb
    """
    from object_detection.utils import label_map_util

    category_index = label_map_util.create_category_index_from_labelmap(
        path_to_labels)
    cls_dict = {int(x['id']): x['name'] for x in category_index.values()}
    num_classes = max(c for c in cls_dict.keys()) + 1
    # add missing classes as, say,'CLS12' if any
    return {i: cls_dict.get(i, 'CLS{}'.format(i)) for i in range(num_classes)}

Can anyone guide me how should I run it on jetson tx2 with onboard cam?
thanks