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 generate local noise feature using RGB input? #26

Open
ShiinaMitsuki opened this issue Jul 8, 2020 · 1 comment
Open

How to generate local noise feature using RGB input? #26

ShiinaMitsuki opened this issue Jul 8, 2020 · 1 comment

Comments

@ShiinaMitsuki
Copy link

ShiinaMitsuki commented Jul 8, 2020

Sorry but I can't find the implementation code of the noise input using SRM filter?

I write one using pytorch but the output seem different from paper, here's the code.

import torch
from torch.nn.functional import conv2d
from torch.nn import Conv2d
import numpy as np


def srm_filter(x):
    q = [4.0, 12.0, 2.0]
    filter1 = [[0, 0, 0, 0, 0],
               [0, -1, 2, -1, 0],
               [0, 2, -4, 2, 0],
               [0, -1, 2, -1, 0],
               [0, 0, 0, 0, 0]]
    filter2 = [[-1, 2, -2, 2, -1],
               [2, -6, 8, -6, 2],
               [-2, 8, -12, 8, -2],
               [2, -6, 8, -6, 2],
               [-1, 2, -2, 2, -1]]
    filter3 = [[0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0],
               [0, 1, -2, 1, 0],
               [0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0]]
    filter1 = np.asarray(filter1, dtype=np.float32) / q[0]
    filter2 = np.asarray(filter2, dtype=np.float32) / q[1]
    filter3 = np.asarray(filter3, dtype=np.float32) / q[2]
    filters = np.asarray(
        [
            [filter1, filter2, filter3],    # R
            [filter1, filter2, filter3],    # G
            [filter1, filter2, filter3],    # B
        ], dtype=np.float32)

    output = conv2d(torch.from_numpy(x.astype(np.float32)), torch.from_numpy(filters), stride=1, padding=2)

    return output


if __name__ == '__main__':
    from PIL import Image

    raw_img = Image.open(
        r'/project/scene_classify/test_images/截屏2020-07-07 下午9.52.22.png').convert('RGB')

    noisemap = srm_filter((np.asarray(raw_img) / 255.0).transpose(2, 0, 1)[np.newaxis, ...])

    noisemap = Image.fromarray((noisemap.squeeze(dim=0).numpy() * 255.0).transpose(1, 2, 0).astype(np.uint8))
    noisemap.save('noise_map.png')
    raw_img.save('raw_map.png')

and here's the output:
raw

noise

the output from paper:

noise_from_paper

@jzousz
Copy link

jzousz commented Mar 11, 2024

import torch
from torch.nn.functional import conv2d
from torch.nn import Conv2d
import numpy as np

def srm_filter(x):
q = [4.0, 12.0, 2.0]
filter1 = [[0, 0, 0, 0, 0],
[0, -1, 2, -1, 0],
[0, 2, -4, 2, 0],
[0, -1, 2, -1, 0],
[0, 0, 0, 0, 0]]
filter2 = [[-1, 2, -2, 2, -1],
[2, -6, 8, -6, 2],
[-2, 8, -12, 8, -2],
[2, -6, 8, -6, 2],
[-1, 2, -2, 2, -1]]
filter3 = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, -2, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]
filter1 = np.asarray(filter1, dtype=np.float32) / q[0]
filter2 = np.asarray(filter2, dtype=np.float32) / q[1]
filter3 = np.asarray(filter3, dtype=np.float32) / q[2]
filters = np.asarray(
[
[filter3, filter3, filter3], # R
[filter2, filter2, filter2], # G
[filter1, filter1, filter1], # B
], dtype=np.float32)

output = conv2d(torch.from_numpy(x.astype(np.float32)), torch.from_numpy(filters), stride=1, padding=2)

return output

if name == 'main':
from PIL import Image

raw_img = Image.open(
    r'./2.png').convert('RGB')

noisemap = srm_filter((np.asarray(raw_img) / 255.0).transpose(2, 0, 1)[np.newaxis, ...])

noisemap = Image.fromarray((noisemap.squeeze(dim=0).numpy() * 255.0).transpose(1, 2, 0).astype(np.uint8))
noisemap.save('noise_map.png')
raw_img.save('raw_map.png')

Uploading 2.png…

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

2 participants