Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to understand the MeanShift layer? #94

Closed
Alxemade opened this issue Dec 6, 2018 · 3 comments
Closed

How to understand the MeanShift layer? #94

Alxemade opened this issue Dec 6, 2018 · 3 comments

Comments

@Alxemade
Copy link

Alxemade commented Dec 6, 2018

Hi, @thstkdgus35
Thanks for sharing your code. I read your code, but I feel confused about the MeanShift layerEDSR-PyTorch/src/model/common.py. The MeanShift layer defined as follows:

class MeanShift(nn.Conv2d):
    def __init__(
        self, rgb_range,
        rgb_mean=(0.4488, 0.4371, 0.4040), rgb_std=(1.0, 1.0, 1.0), sign=-1):

        super(MeanShift, self).__init__(3, 3, kernel_size=1)
        std = torch.Tensor(rgb_std)
        self.weight.data = torch.eye(3).view(3, 3, 1, 1) / std.view(3, 1, 1, 1)
        self.bias.data = sign * rgb_range * torch.Tensor(rgb_mean) / std
        self.requires_grad = False

In the edsy.py, you set sign=-1 in the first MeanShift layer to sub the mean, and you set sign=1 in the last layer to add the mean. Is that correct? Can you share me the details this layer such as the meaning of self.weight.data and self.bias.data.
Thank you!

@sanghyun-son
Copy link
Owner

Hello.

The meanshift layer is a module that adds/subtracts channel-wise mean from the input/output images.

It is equivalent to:

x[:, 0, :, :] -= red_channel_mean
x[:, 1, :, :] -= green_channel_mean
x[:, 2, :, :] -= blue_channel_mean

Since I do not prefer such notations, I inherited the convolutional layer to implement it.

If you check the code carefully, you will notice that only bias matters!

Thank you.

@Alxemade
Copy link
Author

Alxemade commented Dec 6, 2018

That's cool! Thank you for your explanation.

@ninesun127
Copy link

I have also meet this problem when training on my own dataset,what is the meaning of the std in code?
https://github.com/yulunzhang/RCAN/issues/60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants