RuntimeError: stack expects each tensor to be equal size, but got [182, 193] at entry 0 and [] at entry 1

111555: <blockquote>
<h2>========== fold: 0 training ==========<br>
========== fold: 0 training ==========<br>
CQT kernels created, time used = 0.0120 seconds<br>
CQT kernels created, time used = 0.0118 seconds<br>
Downloading: “<a href="https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_b7_ns-1dbc32de.pth" rel="noopener nofollow ugc">https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_efficientnet_b7_ns-1dbc32de.pth</a>” to /root/.cache/torch/hub/checkpoints/tf_efficientnet_b7_ns-1dbc32de.pth</h2>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
1 if <strong>name</strong> == ‘<strong>main</strong>’:<br>
----&gt; 2     main()</p>
<p> in main()<br>
19         for fold in range(CFG.n_fold):<br>
20             if fold in CFG.trn_fold:<br>
—&gt; 21                 _oof_df = train_loop(train, fold)<br>
22                 oof_df = pd.concat([oof_df, _oof_df])<br>
23                 LOGGER.info(f"========== fold: {fold} result ==========")</p>
<p> in train_loop(folds, fold)<br>
71<br>
72         # eval<br>
—&gt; 73         avg_val_loss, preds = valid_fn(valid_loader, model, criterion, device)<br>
74<br>
75         if isinstance(scheduler, ReduceLROnPlateau):</p>
<p> in valid_fn(valid_loader, model, criterion, device)<br>
103     preds = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
104     start = end = time.time()<br>
 → 105     for step, (images, labels) in enumerate(valid_loader):<br>
106         # measure data loading time<br>
107         data_time.update(time.time() - end)</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in <strong>next</strong>(self)<br>
433         if self._sampler_iter is None:<br>
434             self._reset()<br>
 → 435         data = self._next_data()<br>
436         self._num_yielded += 1<br>
437         if self._dataset_kind == _DatasetKind.Iterable and \</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _next_data(self)<br>
1083             else:<br>
1084                 del self._task_info[idx]<br>
 → 1085                 return self._process_data(data)<br>
1086<br>
1087     def _try_put_index(self):</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _process_data(self, data)<br>
1109         self._try_put_index()<br>
1110         if isinstance(data, ExceptionWrapper):<br>
 → 1111             data.reraise()<br>
1112         return data<br>
1113</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/_utils.py in reraise(self)<br>
426             # have message field<br>
427             raise self.exc_type(message=msg)<br>
 → 428         raise self.exc_type(msg)<br>
429<br>
430</p>
<p>RuntimeError: Caught RuntimeError in DataLoader worker process 0.<br>
Original Traceback (most recent call last):<br>
File “/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py”, line 198, in _worker_loop<br>
data = fetcher.fetch(index)<br>
File “/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py”, line 47, in fetch<br>
return self.collate_fn(data)<br>
File “/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py”, line 63, in default_collate<br>
return default_collate([torch.as_tensor(b) for b in batch])<br>
File “/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py”, line 55, in default_collate<br>
return torch.stack(batch, 0, out=out)<br>
RuntimeError: stack expects each tensor to be equal size, but got [182, 193] at entry 0 and <span class="chcklst-box fa fa-square-o fa-fw"></span> at entry 1</p>
</blockquote>
<p>Thank you very much for your help.<br>
It may be a little difficult to understand. I will explain step by step, so if you don’t understand, please ask me right away.</p>
<p>The conclusion is that I would like to do the preprocessing individually and then train the Model at once in the pth and npy files without reading the class, init, or file.</p>
<p>1， “bins_per_octave”: I want to change the 8 part to 32 and clean it up.<br>
2，There is not enough memory. (This is an image.)</p>
<p>3，I can’t clear the cache for some reason. (I don’t know)<br>
4、Let’s save this image as npy,pth and try to train it.<br>
5、So I saved it, and when I tried to train it, I got this error.</p>
<p>Preliminaries<br>
Here is the model. ＝＞The model is here.</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://www.kaggle.com/yasufuminakama/g2net-efficientnet-b7-baseline-training?scriptVersionId=69947710">
  <header class="source">
      <img src="https://www.kaggle.com/static/images/favicon.ico" class="site-icon" width="32" height="32">

      <a href="https://www.kaggle.com/yasufuminakama/g2net-efficientnet-b7-baseline-training?scriptVersionId=69947710" target="_blank" rel="noopener nofollow ugc">kaggle.com</a>
  </header>

  <article class="onebox-body">
    <img width="100" height="100" src="https://storage.googleapis.com/kaggle-avatars/thumbnails/1695531-kg.jpeg" class="thumbnail onebox-avatar">

<h3><a href="https://www.kaggle.com/yasufuminakama/g2net-efficientnet-b7-baseline-training?scriptVersionId=69947710" target="_blank" rel="noopener nofollow ugc">G2Net / efficientnet_b7 / baseline [training]</a></h3>

  <p>Explore and run machine learning code with Kaggle Notebooks | Using data from multiple data sources</p>


  </article>

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

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

<p>1、The data is the class traindataset</p>
<h1><a name="h-1" class="anchor" href="#h-1"></a>====================================================</h1>
<h1><a name="dataset-2" class="anchor" href="#dataset-2"></a>Dataset</h1>
<h1><a name="h-3" class="anchor" href="#h-3"></a>====================================================</h1>
<p>class TrainDataset(Dataset):<br>
def <strong>init</strong>(self, df, transform=None):<br>
self.df = df<br>
self.file_names = df[‘file_path’].values<br>
self.labels = df[CFG.target_col].values<br>
self.wave_transform = CQT1992v2(**CFG.qtransform_params)<br>
self.transform = transform</p>
<pre><code>def __len__(self):
    return len(self.df)

def apply_qtransform(self, waves, transform):
    waves = np.hstack(waves)
    waves = waves / np.max(waves)
    waves = torch.from_numpy(waves).float()
    image = transform(waves)
    return image

def __getitem__(self, idx):
    file_path = self.file_names[idx].
    waves = np.load(file_path)
    image = self.apply_qtransform(waves, self.wave_transform)
    image = image.squeeze().numpy()
    if self.transform:
        image = self.transform(image=image)['image'].
    label = torch.tensor(self.labels[idx]).float()
    return image, label
</code></pre>
<p>These are the return values of image and label of</p>
<p>I saved both of these in validation and train.</p>
<p>In the second example, we have<br>
Loaded.</p>
<p>def get_path():<br>
valid_image = np.load(‘…/input/train-bandpass/test_bandpass.npy’)<br>
train_image = np.load(‘…/input/train-bandpass/train_bandpass.npy’)<br>
valid_label = torch.load(‘…/input/train-bandpass/test_bandpass.pth’)<br>
train_label = torch.load(‘…/input/train-bandpass/train_f_bandpass.pth’)</p>
<pre><code>    return train_image, train_label
</code></pre>
<p>def get_path_va():<br>
valid_image = np.load(‘…/input/train-bandpass/test_bandpass.npy’)<br>
train_image = np.load(‘…/input/train-bandpass/train_bandpass.npy’)<br>
valid_label = torch.load(‘…/input/train-bandpass/test_bandpass.pth’)<br>
train_label = torch.load(‘…/input/train-bandpass/train_f_bandpass.pth’)</p>
<pre><code>train_dataset = TrainDataset(train_folds, transform=get_transforms(data='train'))
valid_dataset = TrainDataset(valid_folds, transform=get_transforms(data='train'))
</code></pre>
<p><span class="hashtag-raw">#This</span> is the individual part!<br>
train_dataset=get_path()<br>
valid_dataset=get_path_va()</p>
<pre><code>train_loader = DataLoader(train_dataset,
                          batch_size=CFG.batch_size, 
                          shuffle=True, 
                          num_workers=CFG.num_workers, pin_memory=True, drop_last=True)
valid_loader = DataLoader(valid_dataset, 
                          batch_size=CFG.batch_size * 2, 
                          shuffle=False, 
                          num_workers=CFG.num_workers, pin_memory=True, drop_last=False)
</code></pre>
<p>3, I got an error. (The question is. ;.</p>
<p>4，I would like to know how I can read this.<br>
Normally, I would do class and init, read the whole file, and do the preprocessing there, but it became too heavy, and the preprocessing alone took about 10 hours, so I want to do it separately.<br>
If we can do this, we can save the human race.<br>
Please help me.</p>
<p>5、The way to save, image, and label is like this.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/5/d59936061526848436a1d54000389335dca70579.png" data-download-href="https://discuss.pytorch.org/uploads/default/d59936061526848436a1d54000389335dca70579" title="Screenshot_2021-08-28 17.04.19_9n8olH"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/5/d59936061526848436a1d54000389335dca70579_2_548x500.png" alt="Screenshot_2021-08-28 17.04.19_9n8olH" data-base62-sha1="utzXaqb8WcLmWGdiyCJRiU2wkVX" width="548" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/5/d59936061526848436a1d54000389335dca70579_2_548x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/5/d59936061526848436a1d54000389335dca70579_2_822x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/d/5/d59936061526848436a1d54000389335dca70579_2_1096x1000.png 2x" data-dominant-color="333433"><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_2021-08-28 17.04.19_9n8olH</span><span class="informations">1800×1640 462 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>

ptrblck: <p>The error message indicates that you are trying to <code>torch.stack</code> tensors with a different shape, which is not possible and you would need to make sure all tensors have the same shape.<br>
Your code is not really readable, but it seems the <code>DataLoader</code> throws this error in its <code>collate_fn</code>, which would indicate that your <code>Dataset</code> is returning samples with a different shape.</p>