question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IndexError: list index out of range

See original GitHub issue

🐛 Bug

To Reproduce

Steps to reproduce the behavior:

  1. Run the TorchVision Object Detection Finetuning Tutorial.
  2. For the model, I used the instructions for
  1. Modifying the model to add a different backbone

But I keep getting the following error:

`--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-16-159df024665a> in <module> 4 for epoch in range(num_epochs): 5 # train for one epoch, printing every 10 iterations ----> 6 train_one_epoch(model, optimizer, train_loader, device, epoch, print_freq=10) 7 # update the learning rate 8 lr_scheduler.step()

/Volumes/Samsung_T5/OneDrive - Coventry University/detector/faster_rcnn_v23/engine.py in train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq) 28 targets = [{k: v.to(device) for k, v in t.items()} for t in targets] 29 —> 30 loss_dict = model(imgs1, targets) 31 32 losses = sum(loss for loss in loss_dict.values())

~/opt/miniconda3/envs/torch/lib/python3.8/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs) 530 result = self._slow_forward(*input, **kwargs) 531 else: –> 532 result = self.forward(*input, **kwargs) 533 for hook in self._forward_hooks.values(): 534 hook_result = hook(self, input, result)

~/opt/miniconda3/envs/torch/lib/python3.8/site-packages/torchvision/models/detection/generalized_rcnn.py in forward(self, images, targets) 69 features = OrderedDict([(‘0’, features)]) 70 proposals, proposal_losses = self.rpn(images, features, targets) —> 71 detections, detector_losses = self.roi_heads(features, proposals, images.image_sizes, targets) 72 detections = self.transform.postprocess(detections, images.image_sizes, original_image_sizes) 73

~/opt/miniconda3/envs/torch/lib/python3.8/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs) 530 result = self._slow_forward(*input, **kwargs) 531 else: –> 532 result = self.forward(*input, **kwargs) 533 for hook in self._forward_hooks.values(): 534 hook_result = hook(self, input, result)

~/opt/miniconda3/envs/torch/lib/python3.8/site-packages/torchvision/models/detection/roi_heads.py in forward(self, features, proposals, image_shapes, targets) 754 matched_idxs = None 755 –> 756 box_features = self.box_roi_pool(features, proposals, image_shapes) 757 box_features = self.box_head(box_features) 758 class_logits, box_regression = self.box_predictor(box_features)

~/opt/miniconda3/envs/torch/lib/python3.8/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs) 530 result = self._slow_forward(*input, **kwargs) 531 else: –> 532 result = self.forward(*input, **kwargs) 533 for hook in self._forward_hooks.values(): 534 hook_result = hook(self, input, result)

~/opt/miniconda3/envs/torch/lib/python3.8/site-packages/torchvision/ops/poolers.py in forward(self, x, boxes, image_shapes) 186 rois = self.convert_to_roi_format(boxes) 187 if self.scales is None: –> 188 self.setup_scales(x_filtered, image_shapes) 189 190 scales = self.scales

~/opt/miniconda3/envs/torch/lib/python3.8/site-packages/torchvision/ops/poolers.py in setup_scales(self, features, image_shapes) 159 # get the levels in the feature map by leveraging the fact that the network always 160 # downsamples by a factor of 2 at each level. –> 161 lvl_min = -torch.log2(torch.tensor(scales[0], dtype=torch.float32)).item() 162 lvl_max = -torch.log2(torch.tensor(scales[-1], dtype=torch.float32)).item() 163 self.scales = scales

IndexError: list index out of range`

I tried different models and adjusted the actors and scales, but keep getting this error.

Environment

PyTorch version: 1.4.0
Is debug build: No
CUDA used to build PyTorch: None

OS: Mac OSX 10.15.3
GCC version: Could not collect
CMake version: Could not collect

Python version: 3.8
Is CUDA available: No
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA

Versions of relevant libraries:
[pip] numpy==1.18.1
[pip] torch==1.4.0
[pip] torchvision==0.5.0
[conda] blas                      1.0                         mkl  
[conda] mkl                       2019.4                      233  
[conda] mkl-service               2.3.0            py38hfbe908c_0  
[conda] mkl_fft                   1.0.15           py38h5e564d8_0  
[conda] mkl_random                1.1.0            py38h6440ff4_0  
[conda] pytorch                   1.4.0                   py3.8_0    pytorch
[conda] torchvision               0.5.0                  py38_cpu    pytorch

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
EMCPcommented, May 4, 2020

Had this same error occur while attempting to perform the same tutorial… but with ResNet101 … perhaps the docs should be updated or a note added to reflect this ?

I fixed the same error with just changing from

roi_pooler = torchvision.ops.MultiScaleRoIAlign(featmap_names=[0],
                                                        output_size=rpn_pooling_size,
                                                        sampling_ratio=2)

into

roi_pooler = torchvision.ops.MultiScaleRoIAlign(featmap_names=["0"],
                                                        output_size=rpn_pooling_size,
                                                        sampling_ratio=2)
``
6reactions
fmassacommented, Mar 30, 2020

Hi,

Make sure that the feature map names returned by your backbone are compatible with the RoIPooler, see https://github.com/pytorch/vision/blob/1c7aa0c0910d518971e694620b58485704a006a8/torchvision/models/detection/faster_rcnn.py#L202

I believe there might be a mismatch there (like, strings vs ints?)

Read more comments on GitHub >

github_iconTop Results From Across the Web

List Index Out of Range – Python Error Message Solved
You'll get the Indexerror: list index out of range error when iterating through a list and trying to access an item that doesn't...
Read more >
Indexerror: list Index Out of Range in Python - STechies
In python “list index out of range” error occurs when we try to access an undefined element from the list. List index out...
Read more >
Index Error: list index out of range (Python) - Stack Overflow
Generally it means that you are providing an index for which a list element does not exist. E.g, if your list was [1,...
Read more >
Python IndexError: List Index Out of Range [Easy Fix] - Finxter
The error “list index out of range” arises if you access invalid indices in your ...
Read more >
How to debug list index out of range error in python? - Flexiple
To foolproof your loop of such instances, the range() function along with len() would return the length of the list dynamically. Subsequently ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found