# How to normalize a ConcatDataset?

**URL:** <https://discuss.pytorch.org/t/how-to-normalize-a-concatdataset/27403>\
**Category:** Uncategorized\
**Created:** [October 16, 2018, 9:17pm UTC](https://discuss.pytorch.org/t/how-to-normalize-a-concatdataset/27403 "2018-10-16T21:17:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gewa24](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@gewa24](https://discuss.pytorch.org/u/gewa24)\
**Post date:** [October 16, 2018, 9:17pm UTC](https://discuss.pytorch.org/t/how-to-normalize-a-concatdataset/27403/1 "2018-10-16T21:17:51Z")

</div>

Hi all,  
I have a dataset where each sample has 7 different channels. Currently I build the datasets for each of my 4 classes separately and then use a concatdataset to put them together. I need to perform a z-score normalization on the whole training set, separately for each channel - it looks like I want to use transforms.Normalize to do this, but I’m having trouble figuring out how.

Would the best practice be to subclass the concatenated dataset in someway to add this normalization? Also, is there an efficient way to get the mean/stddev for each channel once I have built the whole training set? Lastly, if I need to build my own Dataset (i.e., just subclass the generic Dataset), is there a way I can draw examples from the three dataset’s I’ve already made?  
Thanks so much for any help!

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 17, 2018, 9:36pm UTC](https://discuss.pytorch.org/t/how-to-normalize-a-concatdataset/27403/2 "2018-10-17T21:36:16Z")

</div>

I’ve created a small code sample to calculate the mean and std of your dataset of the fly in case all images do not fit into your memory [here](https://discuss.pytorch.org/t/about-normalization-using-pre-trained-vgg16-networks/23560/6?u=ptrblck).

After you’ve calculated the mean and std you can create a `Dataset` and use `transform.Normalize` to normalize the images:

```python

transform = transforms.Normalize(mean=mean, std=std)

class MyDataset(Dataset):
    def __init__ (self, data, transform=None):
        self.data = data
        self.transform = transform
        
    def __getitem__ (self, index):
        x = self.data[index]
        if self.transform:
            x = self.transform(x)
            
        return x
    
    def __len__ (self):
        return len(self.data)

dataset = MyDataset(data, transform=transform)

```

Let me know, if that works for you!

---

<div class="post-metadata">

**Author:** ![gewa24](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@gewa24](https://discuss.pytorch.org/u/gewa24)\
**Post date:** [October 18, 2018, 12:45am UTC](https://discuss.pytorch.org/t/how-to-normalize-a-concatdataset/27403/3 "2018-10-18T00:45:05Z")

</div>

Thanks so much for the help! One follow up - I’m not sure how I can modify your code to calculate the mean and stddev seperately for each of my channels (i.e. every one of my batches will be shape [batchsz, 7, 20] and I’d like to normalize each of the 7 channels seperately). Any thoughts on this? Thanks again

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 18, 2018, 11:04am UTC](https://discuss.pytorch.org/t/how-to-normalize-a-concatdataset/27403/4 "2018-10-18T11:04:08Z")

</div>

In the linked code snippet the mean and std are calculated for each channel, such that both estimates will contain 7 values.  
If you want to calculate it separately for each channel, you could split the data in each channel and run the code.  
Is there a reason you don’t want to calculate the mean and std for every channel in a single run?

---

<div class="post-metadata">

**Author:** ![gewa24](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@gewa24](https://discuss.pytorch.org/u/gewa24)\
**Post date:** [October 18, 2018, 3:21pm UTC](https://discuss.pytorch.org/t/how-to-normalize-a-concatdataset/27403/5 "2018-10-18T15:21:25Z")

</div>

No sorry, what you said should work then! I thought they were being calculated as single scalar values, not as vectors with a value for each channel - I must have changed something without realizing. Thanks so much for all the help!

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 18, 2018, 3:23pm UTC](https://discuss.pytorch.org/t/how-to-normalize-a-concatdataset/27403/6 "2018-10-18T15:23:27Z")

</div>

No worries! Let me know, if you get stuck somewhere or my code doesn’t produce the right results.
