RetinaNet Out of Bounds Error

Dicko: <p>Hello folks, I’m having some trouble with my model. When I train it I get the error below.</p>
<p>If anyone know how to resolve this that would be great and thanks in advance.</p>
<p>I’ve been trying to follow this implementation of RetinaNet with a Resnet-101 backbone:</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/yhenon/pytorch-retinanet">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/yhenon/pytorch-retinanet" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/345;"><img src="https://opengraph.githubassets.com/a1e289dfa2628b245978cc76964659559371ef8abd2a9c0421f8f84290496cc8/yhenon/pytorch-retinanet" class="thumbnail" width="690" height="345"></div>

<h3><a href="https://github.com/yhenon/pytorch-retinanet" target="_blank" rel="noopener nofollow ugc">GitHub - yhenon/pytorch-retinanet: Pytorch implementation of RetinaNet object...</a></h3>

  <p>Pytorch implementation of RetinaNet object detection. - GitHub - yhenon/pytorch-retinanet: Pytorch implementation of RetinaNet object detection.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>`<br>
mAP:<br>
myClass: 0.0</p>
<p>pytorch-retinanet/train.py", line 218, in <br>
main()<br>
pytorch-retinanet/train.py", line 205, in main<br>
mAP = csv_eval.evaluate(dataset_val, retinanet)</p>
<p>pytorch-retinanet/retinanet/csv_eval.py", line 240, in evaluate<br>
print("Precision: ",precision[-1])<br>
IndexError: index -1 is out of bounds for axis 0 with size 0<br>
`</p>
<p>My losses with a lr=1e-9 started off like:</p>
<pre><code class="lang-auto">Epoch: 0 | Iteration: 0 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 1 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 2 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 3 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 4 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 5 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 6 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 7 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 8 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 9 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 10 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 11 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 12 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 13 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 14 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 15 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 16 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 17 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 18 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 19 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783

</code></pre>

ptrblck: <p><code>precision</code> is computed as:</p>
<pre><code class="lang-python">precision = true_positives / np.maximum(true_positives + false_positives, np.finfo(np.float64).eps)
</code></pre>
<p>while <code>true_positives</code> is defined as an array containing ones and zeros depending on the <code>detections</code>:</p>
<pre><code class="lang-python">            for d in detections:
                scores = np.append(scores, d[4])

                if annotations.shape[0] == 0:
                    false_positives = np.append(false_positives, 1)
                    true_positives  = np.append(true_positives, 0)
                    continue

                overlaps            = compute_overlap(np.expand_dims(d, axis=0), annotations)
                assigned_annotation = np.argmax(overlaps, axis=1)
                max_overlap         = overlaps[0, assigned_annotation]

                if max_overlap &gt;= iou_threshold and assigned_annotation not in detected_annotations:
                    false_positives = np.append(false_positives, 0)
                    true_positives  = np.append(true_positives, 1)
                    detected_annotations.append(assigned_annotation)
                else:
                    false_positives = np.append(false_positives, 1)
                    true_positives  = np.append(true_positives, 0)
</code></pre>
<p>Based on this it seems <code>detections</code> might be empty or just a single value, so check why this would be the case and add a few conditions if needed.</p>

Dicko: <p>Thank you very much <a class="mention" href="/u/ptrblck">@ptrblck</a> for trying to help, I’ve been stuck on these problems for weeks and weeks and every time I think I’m getting a little closer … something happens.</p>
<p>I’ve managed to upload my files to GitHub and wondered if you saw anything obvious that might be causing the problem, I’d love to learn how to solve the issue.</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/Dickoabc123/RetinaNet">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/Dickoabc123/RetinaNet" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/345;"><img src="https://opengraph.githubassets.com/7fc40bffb3f76c8245611d5b882cf6f2a44dd3d19a758d69147ccd607fcd9628/Dickoabc123/RetinaNet" class="thumbnail" width="690" height="345"></div>

<h3><a href="https://github.com/Dickoabc123/RetinaNet" target="_blank" rel="noopener nofollow ugc">GitHub - Dickoabc123/RetinaNet: Trying to Create a RetinaNet Model with a...</a></h3>

  <p>Trying to Create a RetinaNet Model with a Resnet-101 backbone - GitHub - Dickoabc123/RetinaNet: Trying to Create a RetinaNet Model with a Resnet-101 backbone</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>I just wanted to train a custom RetinaNet model with a Resnet-101 backbone on my own data which has only 1 class.</p>

Dicko: <p>So I tried to do some print statements like you kindly suggested:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/a/9/a95525a13512cc41ff898462af3ea2a5a06bc933.png" alt="image" data-base62-sha1="o9Z7nNbc7bh8sZE6GxT8QV5kxcT" width="583" height="320"><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/3/4/34f8e7b8dd029d53055b81332c6bc99a8ebb0863.png" alt="image" data-base62-sha1="7yC6wQrVz32bnuLDSNU8xr2tQaf" width="362" height="108"></p>

Dicko: <p>I’ve logged everything here:</p><aside class="onebox githubblob" data-onebox-src="https://github.com/Dickoabc123/RetinaNet/blob/master/mylog.txt">
  <header class="source">

      <a href="https://github.com/Dickoabc123/RetinaNet/blob/master/mylog.txt" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/Dickoabc123/RetinaNet/blob/master/mylog.txt" target="_blank" rel="noopener nofollow ugc">Dickoabc123/RetinaNet/blob/master/mylog.txt</a></h4>


      <pre><code class="lang-txt">+ python train.py --dataset csv --csv_train /Data/train.csv --csv_classes /Data/classes.csv --csv_val /Data/val.csv --depth 101 --epochs 200
CUDA available: True
Num training images: 2920
Epoch: 0 | Iteration: 0 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 1 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 2 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 3 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 4 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 5 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 6 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 7 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 8 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 9 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 10 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 11 | Classification loss: 0.05783 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 12 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 13 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 14 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 15 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783
Epoch: 0 | Iteration: 16 | Classification loss: 0.05782 | Regression loss: 0.00000 | Running loss: 0.05783
</code></pre>



  This file has been truncated. <a href="https://github.com/Dickoabc123/RetinaNet/blob/master/mylog.txt" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>I printed out the detections, annotations, generator, retinanet, score threshold (0.05), max detections (100).</p>
<p>even changing the score threshold to 0.01 gives me the same output.</p>
<p>I’ve even printed the data as a tensor which is in that mylog.txt file</p>

Dicko: <p>I even used the anchor optomisation tool to get the best anchors just in case that was the issue and using the recommended anchors did nothing to improve the result, the identical problem still reamins.</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/martinzlocha/anchor-optimization">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/martinzlocha/anchor-optimization" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/345;"><img src="https://opengraph.githubassets.com/f163bb6a1c840425443c8fbb619e60214b7e8d20f9a427b9632a0be96c73f9d2/martinzlocha/anchor-optimization" class="thumbnail" width="690" height="345"></div>

<h3><a href="https://github.com/martinzlocha/anchor-optimization" target="_blank" rel="noopener nofollow ugc">GitHub - martinzlocha/anchor-optimization: Anchor optimization for RetinaNet.</a></h3>

  <p>Anchor optimization for RetinaNet. Contribute to martinzlocha/anchor-optimization development by creating an account on GitHub.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>