How to normalize the custom dataset

Xiaoyu_Song: <p><strong>Context</strong>: I am doing image segmentation using Pytorch, before feed the training data to the network, I need to do the normalisation<br>
My image size is <em>256x256x3</em>, and my mask size is <em>256x256x3</em><br>
I have a <strong>TrainDataset</strong> class, and my sample is a <em>dict</em> type<br>
for my image, I should use: <code>sample['image']</code> for my image and <code>sample['mask']</code> for the mask<br>
<strong>The Question is:</strong> How can I do the normalization for a <em>dict</em> type dataset<br>
I put my <strong>TrainDataset</strong> class below for information</p>
<pre><code class="lang-auto">class TrainDataset(Dataset):
    """Training dataset with mask image on gray scale/RGB"""
    def __init__(self, train_dir, semantic_dir, transform=None):
        """
        Args:
            train_dir (string): Directory with training images
            transform (callable): Optional transform to be applied on a sample
            semantic_dir (string): Directory with semantic segmentation training image
        """
        self.train_dir = train_dir
        self.transform = transform
        self.semantic_dir = semantic_dir
        
    def __len__(self):
        return len(os.listdir(self.train_dir))
    
    def __getitem__(self, idx):
        img_name = os.path.join(self.train_dir, os.listdir(self.train_dir)[idx])
        semantic_name = os.path.join(self.semantic_dir, os.listdir(self.semantic_dir)[idx])
        image = io.imread(img_name)
        semantic = io.imread(semantic_name)
        sample = {'image':image, 'semantic':semantic}
        
        if self.transform:
            sample = self.transform(sample)
        return sample
</code></pre>
<p><code>composed = transforms.Compose([RandomCrop(256), ToTensor()])</code></p>
<pre><code class="lang-auto">transformed_dataset = TrainDataset(train_dir=train, semantic_dir =train_mask, transform = composed)
</code></pre>
<pre><code class="lang-auto">dataloader = DataLoader(transformed_dataset, batch_size=4, shuffle = False, num_workers = 4)
</code></pre>
<pre><code class="lang-auto">for i_batch, (inputs, target) in enumerate(train_loader):
    print(i_batch, sample_batched['image'].size(), sample_batched['semantic'].size())
    print(sample_batched['image'].dtype)
    if i_batch == 3:   
        break
</code></pre>
<p>Here is the result for the data size:</p>
<pre><code class="lang-auto">0 torch.Size([4, 3, 256, 256]) torch.Size([4, 3, 256, 256])
torch.uint8
1 torch.Size([4, 3, 256, 256]) torch.Size([4, 3, 256, 256])
torch.uint8
2 torch.Size([4, 3, 256, 256]) torch.Size([4, 3, 256, 256])
torch.uint8
3 torch.Size([4, 3, 256, 256]) torch.Size([4, 3, 256, 256])
torch.uint8
</code></pre>
<p>Thank you in advance.</p>

ptrblck: <p><code>torchvision.transforms</code> won’t take a <code>dict</code>, so you should call the transformations on your data and target directly or you could write an own <code>transform</code> method in your <code>Dataset</code>, which takes the specified <code>dict</code> as its input.</p>
<p>Besides that, note that the default tensor image shape in PyTorch is <code>[batch_size, channels, height, width]</code>. Currently it looks like the channel dimension is in a wrong position.<br>
Also, could you give us some information on the segmentation mask? Based on the given shape it looks like the segmentation mask is a color image. If you would like to use a standard classification criterion like <code>nn.CrossEntropyLoss</code>, you would have to transform the colors to a class index.<br>
If I’m correct in my assumption about the segmentation mask (different colors correspond to different classes), you should also be careful with the normalization, since this might distort your indices.</p>

Mona_Jalal: <aside class="quote no-group" data-username="Xiaoyu_Song" data-post="1" data-topic="33733">
<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/xiaoyu_song/48/33419_2.png" class="avatar"> Xiaoyu_Song:</div>
<blockquote>
<p><code>shuffle = False</code></p>
</blockquote>
</aside>
<p>any special reason you set the shuffle to False?</p>
<pre><code class="lang-auto">shuffle = False
</code></pre>

Xiaoyu_Song: <p>Hi, this is not a must, you could make the<code> shuffle = True</code></p>