Pytorch: RuntimeError: torch.cat(): input types can't be cast to the desired output type Byte

Wendy_Moira: <p>Hi everyone,<br>
I’m trying to implement the U2Net (<a href="https://github.com/xuebinqin/U-2-Net" class="inline-onebox" rel="noopener nofollow ugc">GitHub - xuebinqin/U-2-Net: The code for our newly accepted paper in Pattern Recognition 2020: "U^2-Net: Going Deeper with Nested U-Structure for Salient Object Detection."</a>) on my custom data. The code was run on 24 epochs until I got the error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
 File "C:\Users\Admin\U-2-Net-master\u2net_train.py", line 318, in &lt;module&gt;
   train_loss, valid_loss = train_model(net, salobj_dataloader_train, salobj_dataloader_valid, batch_size_train, patience, epoch_num, train_num=len(X_train))
 File "C:\Users\Admin\U-2-Net-master\u2net_train.py", line 203, in train_model
   for i, data in enumerate(salobj_dataloader_train):
 File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\utils\data\dataloader.py", line 634, in __next__
   data = self._next_data()
 File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\utils\data\dataloader.py", line 1346, in _next_data
   return self._process_data(data)
 File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\utils\data\dataloader.py", line 1372, in _process_data
   data.reraise()
 File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\_utils.py", line 644, in reraise
   raise exception
RuntimeError: Caught RuntimeError in DataLoader worker process 0.
Original Traceback (most recent call last):
 File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\utils\data\_utils\worker.py", line 308, in _worker_loop
   data = fetcher.fetch(index)
 File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\utils\data\_utils\fetch.py", line 54, in fetch
   return self.collate_fn(data)
 File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\utils\data\_utils\collate.py", line 264, in default_collate
   return collate(batch, collate_fn_map=default_collate_fn_map)
 File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\utils\data\_utils\collate.py", line 127, in collate
   return elem_type({key: collate([d[key] for d in batch], collate_fn_map=collate_fn_map) for key in elem})
 File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\utils\data\_utils\collate.py", line 127, in &lt;dictcomp&gt;
   return elem_type({key: collate([d[key] for d in batch], collate_fn_map=collate_fn_map) for key in elem})
 File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\utils\data\_utils\collate.py", line 119, in collate
   return collate_fn_map[elem_type](batch, collate_fn_map=collate_fn_map)
 File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\utils\data\_utils\collate.py", line 162, in collate_tensor_fn
   return torch.stack(batch, 0, out=out)
RuntimeError: torch.cat(): input types can't be cast to the desired output type Byte
</code></pre>
<p>The dataset contains image and its mask like this:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/3/7/37f2b8e7d47c45591161127f0bd11037517779aa.jpeg" alt="image" data-base62-sha1="7YWi2w0P8w802CJHw5WqrDU5YDg" width="563" height="409"><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/6/4/647c7d175ab7b2fb758ed231fe71be1e4dc78785.png" alt="image" data-base62-sha1="ekWsGHly19P7AOn5JPOBxvD9EfX" width="553" height="407"></p>
<p>My code is running on GPU. After trying to run all the code again, I got the same issue after 5 epochs. So, I have 2 questions:</p>
<ol>
<li>Why does the error appear at different times? (5th epoch and 24th epoch)</li>
<li>I’ve been trying to fix this but I am still stuck and I’m running out of ideas on how to figure out what causes this error. How I can approach this error?</li>
</ol>
<p>Thank you very much!!!</p>

eqy: <p>Since it is a data loading issue, it could be that specific conditions are required for a batch of input data to be invalid, and this might occur after different durations if you are shuffling your dataset or applying random data augmentations with some probability. You might want to check if you can reproduce the issue in a deterministic way by turning off the dataset shuffle and setting a manual seed to reproduce the data augmentations. It might make sense to inspect the individual samples if e.g., the same batch causes the issue every time.</p>
<p>See also: <a href="https://pytorch.org/docs/stable/notes/randomness.html" class="inline-onebox" rel="noopener nofollow ugc">Reproducibility — PyTorch 2.0 documentation</a></p>

Jothi_Jakhotia: <p>Hello, I’m having the same problem as you. Did you manage to resolve this problem? If so, kindly share the resolution with me.</p>

xiemeilong: <p>Same issue here. any help?</p>

pravin382: <aside class="quote no-group" data-username="xiemeilong" data-post="4" data-topic="179099">
<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/xiemeilong/48/68404_2.png" class="avatar"> xiemeilong:</div>
<blockquote>
<p>ame issue here. any help?</p>
</blockquote>
</aside>
<p><a href="https://discuss.pytorch.org/t/input-types-cant-be-cast-to-the-desired-output-type-long/168099" class="inline-onebox">Input types can't be cast to the desired output type Long</a> helped me resolve my issue.</p>

xiemeilong: <p>Thanks for your help</p>