What are the main reasons for receiving RuntimeError: stack expects a non-empty TensorList error for torch.stack?

username: Mona_Jalal
cooked: <p>I am not sure why sometimes this error is shown when using images. Initially, I thought it is happening because of using JFIF standard jpg images vs EXIF standard jpg images but now I found a case that also happens for EXIF standard jpg images. How can I debug this problem and how can that be solved? I am using PyTorch 1.8.1.</p>
<pre><code class="lang-auto">(phosa) [jalal@goku phosa]$ file input/input/bat* | grep -i exif
input/input/bat5.jpg:  JPEG image data, EXIF standard
(phosa) [jalal@goku phosa]$ python demo.py --filename input/input/bat5.jpg --class_name bat
2021-03-31 20:26:59,384 INFO     Calling with args: Namespace(class_name='bat', filename='input/input/bat5.jpg', lw_collision=None, lw_depth=None, lw_inter=None, lw_inter_part=None, lw_scale=None, lw_scale_person=None, lw_sil=None, mesh_index=0, output_dir='output')
2021-03-31 20:27:02,203 INFO     Loading checkpoint from detectron2://PointRend/InstanceSegmentation/pointrend_rcnn_R_50_FPN_3x_coco/164955410/model_final_3c3198.pkl
2021-03-31 20:27:02,213 INFO     URL https://dl.fbaipublicfiles.com/detectron2/PointRend/InstanceSegmentation/pointrend_rcnn_R_50_FPN_3x_coco/164955410/model_final_3c3198.pkl cached in /home/grad3/jalal/.torch/fvcore_cache/detectron2/PointRend/InstanceSegmentation/pointrend_rcnn_R_50_FPN_3x_coco/164955410/model_final_3c3198.pkl
2021-03-31 20:27:02,300 INFO     Reading a file from 'Detectron2 Model Zoo'
WARNING: You are using a SMPL model, with only 10 shape coefficients.
class_name:  bat
Traceback (most recent call last):
  File "demo.py", line 145, in &lt;module&gt;
    main(get_args())
  File "demo.py", line 120, in main
    object_parameters = find_optimal_poses(
  File "/scratch3/research/code/phosa/phosa/pose_optimization.py", line 426, in find_optimal_poses
    object_parameters[k] = torch.stack(v)
RuntimeError: stack expects a non-empty TensorList
</code></pre>
<p>an image that breaks the code:</p>
<pre><code class="lang-auto">(phosa) [jalal@goku phosa]$ identify -verbose input/input/bat5.jpg
Image: input/input/bat5.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: DirectClass
  Geometry: 1417x957+0+0
  Resolution: 72x72
  Print size: 19.6806x13.2917
  Units: PixelsPerInch
  Colorspace: sRGB
  Type: TrueColor
  Base type: Undefined
  Endianess: Undefined
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Pixels: 1356069
    Red:
      min: 4  (0.0156863)
      max: 246 (0.964706)
      mean: 121.121 (0.474984)
      standard deviation: 65.4112 (0.256514)
      kurtosis: -1.42305
      skewness: -0.279998
      entropy: 0.952832
    Green:
      min: 4  (0.0156863)
      max: 248 (0.972549)
      mean: 123.048 (0.482542)
      standard deviation: 63.9564 (0.25081)
      kurtosis: -1.37041
      skewness: -0.31156
      entropy: 0.955039
    Blue:
      min: 4  (0.0156863)
      max: 247 (0.968627)
      mean: 125.684 (0.492879)
      standard deviation: 64.8382 (0.254268)
      kurtosis: -1.35544
      skewness: -0.330874
      entropy: 0.957843
  Image statistics:
    Overall:
      min: 4  (0.0156863)
      max: 248 (0.972549)
      mean: 123.284 (0.483468)
      standard deviation: 64.7353 (0.253864)
      kurtosis: -1.38286
      skewness: -0.307323
      entropy: 0.955238
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 1417x957+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  Quality: 94
  Orientation: Undefined
  Properties:
    date:create: 2021-04-01T00:02:56+00:00
    date:modify: 2021-04-01T00:01:01+00:00
    exif:DateTime: 2019-05-08T22:34:50+02:00
    exif:ExifOffset: 156
    exif:ExifVersion: 48, 50, 50, 48
    exif:PixelXDimension: 1417
    exif:PixelYDimension: 957
    exif:ResolutionUnit: 2
    exif:Software: Adobe Photoshop CS6 Windows
    exif:XResolution: 72/1
    exif:YResolution: 72/1
    jpeg:colorspace: 2
    jpeg:sampling-factor: 1x1,1x1,1x1
    signature: b933305114f74f66bd8c0430f6689843f3287008be2e0663974091322a869040
  Profiles:
    Profile-app12: 15 bytes
    Profile-exif: 210 bytes
    Profile-xmp: 960 bytes
  Artifacts:
    filename: input/input/bat5.jpg
    verbose: true
  Tainted: False
  Filesize: 256664B
  Number pixels: 1.35607M
  Pixels per second: 39.5923MB
  User time: 0.030u
  Elapsed time: 0:01.034
  Version: ImageMagick 6.9.10-68 Q16 x86_64 2021-02-24 https://imagemagick.org
</code></pre>
<p>an image that will work:</p>
<pre><code class="lang-auto">(phosa) [jalal@goku phosa]$ identify -verbose input/bat_sidehold.jpg 
Image: input/bat_sidehold.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: DirectClass
  Geometry: 1156x1600+0+0
  Resolution: 72x72
  Print size: 16.0556x22.2222
  Units: PixelsPerInch
  Colorspace: sRGB
  Type: TrueColor
  Base type: Undefined
  Endianess: Undefined
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Pixels: 1849600
    Red:
      min: 0  (0)
      max: 255 (1)
      mean: 226.177 (0.886968)
      standard deviation: 64.3149 (0.252215)
      kurtosis: 5.41792
      skewness: -2.58614
      entropy: 0.409396
    Green:
      min: 0  (0)
      max: 255 (1)
      mean: 219.381 (0.860319)
      standard deviation: 57.0324 (0.223657)
      kurtosis: 0.586459
      skewness: -1.36916
      entropy: 0.443656
    Blue:
      min: 0  (0)
      max: 255 (1)
      mean: 220.606 (0.86512)
      standard deviation: 54.885 (0.215235)
      kurtosis: 0.68018
      skewness: -1.36917
      entropy: 0.442032
  Image statistics:
    Overall:
      min: 0  (0)
      max: 255 (1)
      mean: 222.055 (0.870803)
      standard deviation: 58.7441 (0.230369)
      kurtosis: 2.75662
      skewness: -1.88089
      entropy: 0.431695
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: JPEG
  Intensity: Undefined
  Compose: Over
  Page geometry: 1156x1600+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  Quality: 65
  Orientation: Undefined
  Properties:
    date:create: 2021-03-26T05:48:14+00:00
    date:modify: 2021-03-26T05:48:14+00:00
    exif:Copyright: (c) Zhagunov | Dreamstime.com
    exif:ResolutionUnit: 2
    exif:XResolution: 72/1
    exif:YCbCrPositioning: 1
    exif:YResolution: 72/1
    jpeg:colorspace: 2
    jpeg:sampling-factor: 2x2,1x1,1x1
    signature: c1017a4d64ee13044fd81942a442cbaf6c28e42c61282ed3ab376d3dd92fd3c1
    xmpRights:WebStatement: https://www.dreamstime.com/about-stock-image-licenses
  Profiles:
    Profile-8bim: 54 bytes
    Profile-exif: 126 bytes
    Profile-iptc: 41 bytes
      Copyright String[2,116]: (c) Zhagunov | Dreamstime.com
      unknown[2,0]: 
    Profile-xmp: 3158 bytes
  Artifacts:
    filename: input/bat_sidehold.jpg
    verbose: true
  Tainted: False
  Filesize: 96559B
  Number pixels: 1849600
  Pixels per second: 35.8514MB
  User time: 0.050u
  Elapsed time: 0:01.051
  Version: ImageMagick 6.9.10-68 Q16 x86_64 2021-02-24 https://imagemagick.org
</code></pre>

username: ptrblck
cooked: <p>I’m not sure how you are loading the images, but it seems that the used library has trouble loading these problematic image files (e.g. <code>PIL</code> or <code>OpenCV</code>).<br>
Based on the error message a <code>None</code> object will be returned which then fails in the <code>torch.stack</code> operation.<br>
Could you try to load the mentioned images using the image lib and check what the output would be?</p>

username: Mona_Jalal
cooked: <p>thanks for the pointers. yeah the problem happens because the bat class is not detected at all in certain classes (please let me know if you might have any suggestions)</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/a/fa5f212c51a3b894c11dcee0e97177fa1c8b3c2e.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/fa5f212c51a3b894c11dcee0e97177fa1c8b3c2e" title="Screenshot from 2021-04-02 01-57-24"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/a/fa5f212c51a3b894c11dcee0e97177fa1c8b3c2e_2_690x469.jpeg" alt="Screenshot from 2021-04-02 01-57-24" data-base62-sha1="zITbgQHb1ryYLYQh0tRT8gYVwtM" width="690" height="469" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/a/fa5f212c51a3b894c11dcee0e97177fa1c8b3c2e_2_690x469.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/f/a/fa5f212c51a3b894c11dcee0e97177fa1c8b3c2e_2_1035x703.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/a/fa5f212c51a3b894c11dcee0e97177fa1c8b3c2e_2_1380x938.jpeg 2x" data-dominant-color="9588A7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2021-04-02 01-57-24</span><span class="informations">2236×1520 422 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/2/e21d480977786405174d7838240fd4b451580bda.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/e21d480977786405174d7838240fd4b451580bda" title="Screenshot from 2021-04-02 01-57-33"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/2/e21d480977786405174d7838240fd4b451580bda_2_670x499.jpeg" alt="Screenshot from 2021-04-02 01-57-33" data-base62-sha1="wgiDTiWCDr5S9ioTPc1V4JTGoLo" width="670" height="499" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/2/e21d480977786405174d7838240fd4b451580bda_2_670x499.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/e/2/e21d480977786405174d7838240fd4b451580bda_2_1005x748.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/2/e21d480977786405174d7838240fd4b451580bda_2_1340x998.jpeg 2x" data-dominant-color="959799"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2021-04-02 01-57-33</span><span class="informations">2160×1610 401 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/1/d1a0a83bf19f14c098b67fff6e5392eec196a7d9.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/d1a0a83bf19f14c098b67fff6e5392eec196a7d9" title="Screenshot from 2021-04-02 01-47-28"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d1a0a83bf19f14c098b67fff6e5392eec196a7d9_2_362x500.jpeg" alt="Screenshot from 2021-04-02 01-47-28" data-base62-sha1="tUrZRcDeZRUF3tiQ8lMaHWLhFq1" width="362" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d1a0a83bf19f14c098b67fff6e5392eec196a7d9_2_362x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d1a0a83bf19f14c098b67fff6e5392eec196a7d9_2_543x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d1a0a83bf19f14c098b67fff6e5392eec196a7d9_2_724x1000.jpeg 2x" data-dominant-color="E6DCE5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2021-04-02 01-47-28</span><span class="informations">982×1356 171 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/7/f72113c56e2304ffb8154cced389601de489d00f.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/f72113c56e2304ffb8154cced389601de489d00f" title="Screenshot from 2021-04-02 01-47-57"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/7/f72113c56e2304ffb8154cced389601de489d00f_2_375x500.jpeg" alt="Screenshot from 2021-04-02 01-47-57" data-base62-sha1="zgcNM7L486EkWNfeWHw6bgkR9HF" width="375" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/7/f72113c56e2304ffb8154cced389601de489d00f_2_375x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/f/7/f72113c56e2304ffb8154cced389601de489d00f_2_562x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/7/f72113c56e2304ffb8154cced389601de489d00f_2_750x1000.jpeg 2x" data-dominant-color="E5E6CE"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2021-04-02 01-47-57</span><span class="informations">902×1202 112 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/6/064aedb1644d91c6f4692d6f4c4d2e7a590637f6.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/064aedb1644d91c6f4692d6f4c4d2e7a590637f6" title="bat26"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/6/064aedb1644d91c6f4692d6f4c4d2e7a590637f6_2_344x500.jpeg" alt="bat26" data-base62-sha1="TFoHkeshS7hTHzay0ACOyNnUqO" width="344" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/6/064aedb1644d91c6f4692d6f4c4d2e7a590637f6_2_344x500.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/0/6/064aedb1644d91c6f4692d6f4c4d2e7a590637f6.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/0/6/064aedb1644d91c6f4692d6f4c4d2e7a590637f6.jpeg 2x" data-dominant-color="DFE2E5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">bat26</span><span class="informations">422×612 23.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>the photo of the girl where bat is detected is the only one phosa doesn’t break.</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Mona_Jalal" data-post="3" data-topic="116659">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mona_jalal/48/25715_2.png" class="avatar"> Mona_Jalal:</div>
<blockquote>
<p>please let me know if you might have any suggestions</p>
</blockquote>
</aside>
<p>Is the problem that the model isn’t able to detect this class and you would like to improve the model itself or would you rather like to fix the <code>None</code> error in case no valid classes were detected by the model?<br>
I would personally try to fix the second issue first, as you might face the same error for other classes in case the model is also unable to detect them.</p>