Stack expects each tensor to be equal size, but got [1, 48, 60, 48] at entry 0 and [3, 48, 60, 48] at entry 1

username: anto
cooked: <p>I’m having this problem while i’m creating my dataset using torchio using a medical imaging dataset. It seems like i have some data with 3 channels and other one with a single channel. How can i fix this problem?<br>
The dataset is created in this mode:</p>
<pre><code class="lang-auto">dataset_dir_name = 'BUSI'
dataset_dir = Path(dataset_dir_name)
images_dir = dataset_dir / 'image'
labels_dir = dataset_dir / 'label'
image_paths = sorted(images_dir.glob('*.png'))
label_paths = sorted(labels_dir.glob('*.png'))
++
subjects = []
for (image_path, label_path) in zip(image_paths, label_paths):
    subject = tio.Subject(
        mri=tio.ScalarImage(image_path),
        brain=tio.LabelMap(label_path),
    )
    subjects.append(subject)
dataset = tio.SubjectsDataset(subjects)
print('Dataset size:', len(dataset), 'subjects')
</code></pre>
<p>In this mode i simply compute the transform.</p>
<pre><code class="lang-auto">training_transform = tio.Compose([
    tio.ToCanonical(),
    tio.Resize(1),
    tio.Resample(4),
    tio.CropOrPad((48, 60, 48)),
    tio.RandomMotion(p=0.2),
    tio.RandomBiasField(p=0.3),
    tio.RandomNoise(p=0.5),
    tio.RandomFlip(),
    tio.OneOf({
        tio.RandomAffine(): 0.8,
        tio.RandomElasticDeformation(): 0.2,
    }),
    #tio.OneHot(),
])

validation_transform = tio.Compose([
    tio.ToCanonical(),
    tio.Resize(1),
    tio.Resample(4),
    tio.CropOrPad((48, 60, 48)),
    #tio.OneHot(),
])

num_subjects = len(dataset)
num_training_subjects = int(training_split_ratio * num_subjects)
num_validation_subjects = num_subjects - num_training_subjects

num_split_subjects = num_training_subjects, num_validation_subjects
training_subjects, validation_subjects = torch.utils.data.random_split(subjects, num_split_subjects)

training_set = tio.SubjectsDataset(
    training_subjects, transform=training_transform)

validation_set = tio.SubjectsDataset(
    validation_subjects, transform=validation_transform)

print('Training set:', len(training_set), 'subjects')
print('Validation set:', len(validation_set), 'subjects')
</code></pre>
<p>I generate the dataLoader.</p>
<pre><code class="lang-auto">training_batch_size = 16
validation_batch_size = 2 * training_batch_size

training_loader = torch.utils.data.DataLoader(
    training_set,
    batch_size=training_batch_size,
    shuffle=True,
    num_workers=num_workers,
)

validation_loader = torch.utils.data.DataLoader(
    validation_set,
    batch_size=validation_batch_size,
    num_workers=num_workers,
)
</code></pre>
<p>The error.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/4/342540848934f1d41a686abc57305c64a2073cbc.png" data-download-href="https://discuss.pytorch.org/uploads/default/342540848934f1d41a686abc57305c64a2073cbc" title="Screenshot from 2022-02-10 18-32-27"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/4/342540848934f1d41a686abc57305c64a2073cbc_2_690x323.png" alt="Screenshot from 2022-02-10 18-32-27" data-base62-sha1="7riDIhK8XIbzVAxHneGVjGSN4Ze" width="690" height="323" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/4/342540848934f1d41a686abc57305c64a2073cbc_2_690x323.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/4/342540848934f1d41a686abc57305c64a2073cbc_2_1035x484.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/4/342540848934f1d41a686abc57305c64a2073cbc.png 2x" data-dominant-color="F7F5F5"><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 2022-02-10 18-32-27</span><span class="informations">1131×531 89.8 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>

username: thecho7
cooked: <p>If you want to handle those tensors together,<br>
<code>torch.cat</code> instead of <code>torch.stack</code> along the dim 0</p>