Height and Width of image, mask or masks should be equal. You can disable shapes check by setting a parameter is_check_shapes=False of Compose class (do it only if you are sure about your data consistency)

username: Lohith_Kumaar
cooked: <blockquote>
<p>I am Unable to match the height and width of image, mask.<br>
Here is my code can anyone help me with this</p>
</blockquote>
<pre><code class="lang-auto">!pip install segmentation-models-pytorch
!pip install -U git+https://github.com/albumentations-team/albumentations
!pip install --upgrade opencv-contrib-python

!git clone https://github.com/parth1620/Human-Segmentation-Dataset-master.git

import sys
sys.path.append('/content/Human-Segmentation-Dataset-master')

import torch 
import cv2

import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt 

from sklearn.model_selection import train_test_split
from tqdm import tqdm

import helper

CSV_FILE = '/content/Human-Segmentation-Dataset-master/train.csv'
DATA_DIR = '/content/'

DEVICE = 'cuda'

EPOCHS = 25
LR = 0.003
IMG_SIZE = 320
BATCH_SIZE = 16

ENCODER = 'timm-efficientnet-b0'
WEIGHT = 'imagenet'

train_df, valid_df = train_test_split(df, test_size = 0.2, random_state = 42)

import albumentations as A

def get_train_augs():
  return A.Compose([
      A.Resize(IMG_SIZE, IMG_SIZE),
      A.HorizontalFlip(p = 0.5),
      A.VerticalFlip(p = 0.5),
  ])

def get_valid_augs():
  return A.Compose([
      A.Resize(IMG_SIZE, IMG_SIZE),
  ])

from torch.utils.data import Dataset

class SegmentationDataset(Dataset):

  def __init__(self, df, augmentations):

    self.df = df
    self.augmentations = augmentations
    
  def __len__(self):
    return len(self.df)
  
  def __getitem__(self, idx):
    
    row = self.df.iloc[idx]

    image_path = row.images
    mask_path = row.masks

    image = cv2.imread(image_path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
    mask = np.expand_dims(mask, axis = -1)
    
    if self.augmentations:
      data = self.augmentations(image = image, mask = mask)
      image = data['image']
      mask = data['mask']


    image = np.transpose(image, (2,0,1)).astype(np.float32)
    mask = np.transpose(mask, (2,0,1)).astype(np.float32)


    image = torch.Tensor(image) / 255.0
    mask = torch.round(torch.Tensor(mask) / 255.0)

    return image, mask

trainset = SegmentationDataset(train_df, get_train_augs())
validset = SegmentationDataset(valid_df, get_valid_augs())

print(f"Size of Trainset : {len(trainset)}")
print(f"Size of Validset : {len(validset)}")

idx = 3

image, mask = trainset[idx]
helper.show_image(image, mask)

from torch.utils.data import DataLoader

trainloader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle = True)
validloader = DataLoader(validset, batch_size=BATCH_SIZE)

print(f"Total no. of batches in trainloader : {len(trainloader)}")
print(f"Total no. of batches in validloader : {len(validloader)}")

for image, mask in trainloader:
  break

print(f"One batch images shape : {image.shape}")
print(f"One batch masks shape : {mask.shape}")
</code></pre>
<blockquote>
<p>I am facing the error in the last for loop, the error is<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/7/678a67c652e0d33d7ea1912be0e20db209a194d2.png" data-download-href="https://discuss.pytorch.org/uploads/default/678a67c652e0d33d7ea1912be0e20db209a194d2" title="Screenshot_20230218_114506"><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/7/678a67c652e0d33d7ea1912be0e20db209a194d2.png" alt="Screenshot_20230218_114506" data-base62-sha1="eLXI7mI8QsS9aR70oSI5T4Lt6ZI" width="690" height="155" data-dominant-color="3C3D3F"><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_20230218_114506</span><span class="informations">1507×340 23.4 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>
</blockquote>
<blockquote>
<p>Can anyone please help me with this</p>
</blockquote>

username: ptrblck
cooked: <p>Could you print the shape of <code>image</code> and <code>mask</code> inside the <code>Dataset.__getitem__</code> method before and after applying the transformation, please?</p>

username: Lohith_Kumaar
cooked: <blockquote>
<p>I ran the code and found these results</p>
</blockquote>
<blockquote>
<p>Before Augmentation:  [[[ 62  90  50]<br>
[ 70  98  60]<br>
[ 83 108  76]<br>
…<br>
[ 63  88 110]<br>
[ 51  76  98]<br>
[ 49  74  96]]</p>
</blockquote>
<p>[[ 66  94  56]<br>
[ 74 101  66]<br>
[ 88 113  83]<br>
…<br>
[ 63  88 110]<br>
[ 52  77  99]<br>
[ 49  74  96]]</p>
<p>[[ 73 100  65]<br>
[ 83 108  76]<br>
[ 97 122  93]<br>
…<br>
[ 64  89 111]<br>
[ 52  77  99]<br>
[ 50  75  97]]</p>
<p>…</p>
<p>[[108 132  74]<br>
[108 132  74]<br>
[107 131  73]<br>
…<br>
[ 85  63  50]<br>
[ 84  62  49]<br>
[ 82  60  47]]</p>
<p>[[108 133  75]<br>
[108 133  75]<br>
[106 131  73]<br>
…<br>
[ 88  64  54]<br>
[ 87  63  53]<br>
[ 85  61  51]]</p>
<p>[[109 134  76]<br>
[108 133  75]<br>
[107 132  74]<br>
…<br>
[ 91  64  55]<br>
[ 90  63  54]<br>
[ 89  62  53]]] [[[0]<br>
[0]<br>
[0]<br>
…<br>
[0]<br>
[0]<br>
[0]]</p>
<p>[[0]<br>
[0]<br>
[0]<br>
…<br>
[0]<br>
[0]<br>
[0]]</p>
<p>[[0]<br>
[0]<br>
[0]<br>
…<br>
[0]<br>
[0]<br>
[0]]</p>
<p>…</p>
<p>[[0]<br>
[0]<br>
[0]<br>
…<br>
[0]<br>
[0]<br>
[0]]</p>
<p>[[0]<br>
[0]<br>
[0]<br>
…<br>
[0]<br>
[0]<br>
[0]]</p>
<p>[[0]<br>
[0]<br>
[0]<br>
…<br>
[0]<br>
[0]<br>
[0]]]<br>
After Augmentation  [[[ 89  62  53]<br>
[ 91  64  55]<br>
[ 93  70  54]<br>
…<br>
[103 128  70]<br>
[106 131  73]<br>
[108 133  75]]</p>
<p>[[ 84  61  50]<br>
[ 87  64  53]<br>
[ 89  69  51]<br>
…<br>
[104 128  70]<br>
[106 130  72]<br>
[108 132  75]]</p>
<p>[[ 80  60  44]<br>
[ 82  63  47]<br>
[ 84  68  46]<br>
…<br>
[104 128  70]<br>
[106 130  72]<br>
[107 131  73]]</p>
<p>…</p>
<p>[[ 51  76  98]<br>
[ 69  94 116]<br>
[102 127 149]<br>
…<br>
[146 163 147]<br>
[112 134 109]<br>
[ 86 110  80]]</p>
<p>[[ 51  76  97]<br>
[ 68  93 115]<br>
[ 99 123 145]<br>
…<br>
[132 152 133]<br>
[ 98 122  94]<br>
[ 73 100  64]]</p>
<p>[[ 50  75  97]<br>
[ 67  92 114]<br>
[ 97 121 143]<br>
…<br>
[121 142 121]<br>
[ 89 114  84]<br>
[ 66  94  55]]] [[[0]<br>
[0]<br>
[0]<br>
…<br>
[0]<br>
[0]<br>
[0]]</p>
<p>[[0]<br>
[0]<br>
[0]<br>
…<br>
[0]<br>
[0]<br>
[0]]</p>
<p>[[0]<br>
[0]<br>
[0]<br>
…<br>
[0]<br>
[0]<br>
[0]]</p>
<p>…</p>
<p>[[0]<br>
[0]<br>
[0]<br>
…<br>
[0]<br>
[0]<br>
[0]]</p>
<p>[[0]<br>
[0]<br>
[0]<br>
…<br>
[0]<br>
[0]<br>
[0]]</p>
<p>[[0]<br>
[0]<br>
[0]<br>
…<br>
[0]<br>
[0]<br>
[0]]]<br>
After Transpose:  [[[ 89.  91.  93. … 103. 106. 108.]<br>
[ 84.  87.  89. … 104. 106. 108.]<br>
[ 80.  82.  84. … 104. 106. 107.]<br>
…<br>
[ 51.  69. 102. … 146. 112.  86.]<br>
[ 51.  68.  99. … 132.  98.  73.]<br>
[ 50.  67.  97. … 121.  89.  66.]]</p>
<p>[[ 62.  64.  70. … 128. 131. 133.]<br>
[ 61.  64.  69. … 128. 130. 132.]<br>
[ 60.  63.  68. … 128. 130. 131.]<br>
…<br>
[ 76.  94. 127. … 163. 134. 110.]<br>
[ 76.  93. 123. … 152. 122. 100.]<br>
[ 75.  92. 121. … 142. 114.  94.]]</p>
<p>[[ 53.  55.  54. …  70.  73.  75.]<br>
[ 50.  53.  51. …  70.  72.  75.]<br>
[ 44.  47.  46. …  70.  72.  73.]<br>
…<br>
[ 98. 116. 149. … 147. 109.  80.]<br>
[ 97. 115. 145. … 133.  94.  64.]<br>
[ 97. 114. 143. … 121.  84.  55.]]] [[[0. 0. 0. … 0. 0. 0.]<br>
[0. 0. 0. … 0. 0. 0.]<br>
[0. 0. 0. … 0. 0. 0.]<br>
…<br>
[0. 0. 0. … 0. 0. 0.]<br>
[0. 0. 0. … 0. 0. 0.]<br>
[0. 0. 0. … 0. 0. 0.]]]<br>
After Tensor Trasform:  tensor([[[0.3490, 0.3569, 0.3647,  …, 0.4039, 0.4157, 0.4235],<br>
[0.3294, 0.3412, 0.3490,  …, 0.4078, 0.4157, 0.4235],<br>
[0.3137, 0.3216, 0.3294,  …, 0.4078, 0.4157, 0.4196],<br>
…,<br>
[0.2000, 0.2706, 0.4000,  …, 0.5725, 0.4392, 0.3373],<br>
[0.2000, 0.2667, 0.3882,  …, 0.5176, 0.3843, 0.2863],<br>
[0.1961, 0.2627, 0.3804,  …, 0.4745, 0.3490, 0.2588]],</p>
<pre><code>    [[0.2431, 0.2510, 0.2745,  ..., 0.5020, 0.5137, 0.5216],
     [0.2392, 0.2510, 0.2706,  ..., 0.5020, 0.5098, 0.5176],
     [0.2353, 0.2471, 0.2667,  ..., 0.5020, 0.5098, 0.5137],
     ...,
     [0.2980, 0.3686, 0.4980,  ..., 0.6392, 0.5255, 0.4314],
     [0.2980, 0.3647, 0.4824,  ..., 0.5961, 0.4784, 0.3922],
     [0.2941, 0.3608, 0.4745,  ..., 0.5569, 0.4471, 0.3686]],

    [[0.2078, 0.2157, 0.2118,  ..., 0.2745, 0.2863, 0.2941],
     [0.1961, 0.2078, 0.2000,  ..., 0.2745, 0.2824, 0.2941],
     [0.1725, 0.1843, 0.1804,  ..., 0.2745, 0.2824, 0.2863],
     ...,
     [0.3843, 0.4549, 0.5843,  ..., 0.5765, 0.4275, 0.3137],
     [0.3804, 0.4510, 0.5686,  ..., 0.5216, 0.3686, 0.2510],
     [0.3804, 0.4471, 0.5608,  ..., 0.4745, 0.3294, 0.2157]]]) tensor([[[0., 0., 0.,  ..., 0., 0., 0.],
     [0., 0., 0.,  ..., 0., 0., 0.],
     [0., 0., 0.,  ..., 0., 0., 0.],
     ...,
     [0., 0., 0.,  ..., 0., 0., 0.],
     [0., 0., 0.,  ..., 0., 0., 0.],
     [0., 0., 0.,  ..., 0., 0., 0.]]])
</code></pre>

username: rodolpho.neves
cooked: <p>I have found the solution. You have to change on the compose’s get_train/valid_augs() parameters, adding a “is_check_shapes=False”:</p>
<pre><code class="lang-auto">def get_train_augs():
  return A.Compose([
      A.Resize(IMG_SIZE, IMG_SIZE), 
      A.HorizontalFlip(p = 0.5), 
      A.VerticalFlip(p = 0.5), 
      ], is_check_shapes=False)

def get_valid_augs():
  return A.Compose([
      A.Resize(IMG_SIZE, IMG_SIZE),
      ], is_check_shapes=False)
</code></pre>

username: ADONIS_HAMMOUTENE
cooked: <p>But here, it’s supposed that our data is consistent, right ?</p>

username: Yuvraj_kari
cooked: <p>how it works???</p>

username: ADONIS_HAMMOUTENE
cooked: <p>When you do Compose, for instance :</p>
<pre><code class="lang-auto">        tfms = [
            VerticalFlip(p=0.2),
            HorizontalFlip(p=0.2),
            GaussNoise(p=0.2),
            RandomBrightnessContrast(p=0.3),
            RandomShadow(p=0.1),
            ElasticTransform(alpha=2, p=0.3),
            Resize(height, width, interpolation=cv2.INTER_NEAREST, p=1),
        ]

    return A.Compose(tfms)

you can add  is_check_shapes=False in A.Compose ==&gt; A.Compose(tfms, is_check_shapes=False)
</code></pre>