Instance segmentation training runs _ epochs and hits index error

JohnPolo: <p>I am following the <a href="https://colab.research.google.com/github/pytorch/vision/blob/temp-tutorial/tutorials/torchvision_finetuning_instance_segmentation.ipynb#scrollTo=bX0rqK-A3Nbl" rel="noopener nofollow ugc">Finetuning Instance Segmentation</a> tutorial with my own data.</p>
<p>I am at the point in the tutorial where the model is <a href="https://colab.research.google.com/github/pytorch/vision/blob/temp-tutorial/tutorials/torchvision_finetuning_instance_segmentation.ipynb#scrollTo=bX0rqK-A3Nbl" rel="noopener nofollow ugc">trained for 10 epochs</a>. All the code up to this point seems to work. When I run this section, the results are mixed. Once, it ran for one epoch and then gave an error. Then I tried it again and it gave the same error as previous attempt but before finishing one epoch. Now it ran for two epochs and it returns the error again. In the first two failures this line had “…process 0”. Now in the third attempt:</p>
<blockquote>
<p>IndexError: Caught IndexError in DataLoader worker process 1.</p>
</blockquote>
<p>In all three attempts, the following line is always the same:</p>
<blockquote>
<p>IndexError: too many indices for tensor of dimension 1</p>
</blockquote>
<p>This is the stacktrace:</p>
<pre><code class="lang-auto">Epoch: [0]  [ 0/19]  eta: 0:13:06  lr: 0.000282  loss: 8.1070 (8.1070)  loss_classifier: 0.4619 (0.4619)  loss_box_reg: 0.3113 (0.3113)  loss_mask: 7.3135 (7.3135)  loss_objectness: 0.0185 (0.0185)  loss_rpn_box_reg: 0.0019 (0.0019)  time: 41.3855  data: 3.8544  max mem: 0
Epoch: [0]  [10/19]  eta: 0:04:21  lr: 0.003058  loss: 1.5146 (2.5890)  loss_classifier: 0.3459 (0.3226)  loss_box_reg: 0.2827 (0.2941)  loss_mask: 0.7114 (1.8825)  loss_objectness: 0.0403 (0.0862)  loss_rpn_box_reg: 0.0034 (0.0036)  time: 29.0771  data: 0.3538  max mem: 0

---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

&lt;ipython-input-15-973f272f5759&gt; in &lt;module&gt;()
      4 for epoch in range(num_epochs):
      5     # train for one epoch, printing every 10 iterations
----&gt; 6     train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
      7     # update the learning rate
      8     lr_scheduler.step()

5 frames

/content/engine.py in train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq)
     24         lr_scheduler = utils.warmup_lr_scheduler(optimizer, warmup_iters, warmup_factor)
     25 
---&gt; 26     for images, targets in metric_logger.log_every(data_loader, print_freq, header):
     27         images = list(image.to(device) for image in images)
     28         targets = [{k: v.to(device) for k, v in t.items()} for t in targets]

/content/utils.py in log_every(self, iterable, print_freq, header)
    199         ])
    200         MB = 1024.0 * 1024.0
--&gt; 201         for obj in iterable:
    202             data_time.update(time.time() - end)
    203             yield obj

/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py in __next__(self)
    515             if self._sampler_iter is None:
    516                 self._reset()
--&gt; 517             data = self._next_data()
    518             self._num_yielded += 1
    519             if self._dataset_kind == _DatasetKind.Iterable and \

/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py in _next_data(self)
   1197             else:
   1198                 del self._task_info[idx]
-&gt; 1199                 return self._process_data(data)
   1200 
   1201     def _try_put_index(self):

/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py in _process_data(self, data)
   1223         self._try_put_index()
   1224         if isinstance(data, ExceptionWrapper):
-&gt; 1225             data.reraise()
   1226         return data
   1227 

/usr/local/lib/python3.7/dist-packages/torch/_utils.py in reraise(self)
    427             # have message field
    428             raise self.exc_type(message=msg)
--&gt; 429         raise self.exc_type(msg)
    430 
    431 

IndexError: Caught IndexError in DataLoader worker process 1.
Original Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/worker.py", line 202, in _worker_loop
    data = fetcher.fetch(index)
  File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in &lt;listcomp&gt;
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataset.py", line 330, in __getitem__
    return self.dataset[self.indices[idx]]
  File "&lt;ipython-input-1-4d5623c80b35&gt;", line 62, in __getitem__
    area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
IndexError: too many indices for tensor of dimension 1

</code></pre>
<p>This is not my image exactly, I can’t share, but <a href="https://drive.google.com/file/d/1AXCGE8AGbc_1Rw6LACP6iJoJaXdmCXQF/view?usp=sharing" rel="noopener nofollow ugc">this is</a> close.<br>
And this is one of <a href="https://drive.google.com/file/d/1Ky00zGz5_ChmbG6kbr2ZkVNIk_ag_SEy/view?usp=sharing" rel="noopener nofollow ugc">my mask</a>s.</p>

ptrblck: <p>This error is raised, if the index tensor has a single dimension only while you are indexing it at <code>dim1</code>:</p>
<pre><code class="lang-python">boxes = torch.randn(4)
out = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
&gt; IndexError: too many indices for tensor of dimension 1

boxes = torch.randn(1, 4)
out = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0]) # works
</code></pre>
<p>You could check the shape of <code>boxes</code> inside the <code>Dataset</code> and make sure it has at least 2 dimensions.</p>

JohnPolo: <p>Thanks, <a class="mention" href="/u/ptrblck">@ptrblck</a>. The masks I created had some noise in them, for lack of a better term, and so In the Dataset class creation, it was hard to get the bounding boxes created. Each mask file is supposed to have four masks/bounding boxes. By changing the index selection to “[-4:]”, the problem is fixed.</p>
<pre><code class="lang-auto">mask = Image.open(mask_path)

# Convert from image object to array
mask = np.array(mask)
        
obj_ids = np.unique(mask)

obj_ids = obj_ids[-4:] #Previously, I had [6:] to avoid the noise. This was causing my problem.
</code></pre>