Yang-Liu1082 / InvDN

Implementation for the paper: Invertible Denoising Network: A Light Solution for Real Noise Removal (CVPR2021).
Apache License 2.0
188 stars 35 forks source link

Test on a single image #8

Open f0enix opened 3 years ago

f0enix commented 3 years ago

How do i try it on a single rgb image without any dataset?

Yang-Liu1082 commented 3 years ago

Just load the image and the pretrained model to test.

bui-thanh-lam commented 2 years ago

Could you please provide the code to test with an image in normal format? (.jpg or .png, use cv2.imread for examples)

hahazhar commented 1 year ago
def my_test(model, opt):
    dataset_dir = opt['name']
    out_dir = os.path.join( "..", "experiments", dataset_dir)

    if not os.path.exists(out_dir):
        os.makedirs(out_dir)

    out_dir = os.path.join(out_dir, 'my_test')
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)

    # load info
    filePath = os.path.join(opt['datasets']['test_1']['dataroot_Noisy'], 'NOISY_SRGB_010.png')
    Nimg = cv2.imread(filePath)[:256,:256,:]
    print(Nimg.shape)
    DenoisedBlocksSrgb = np.empty_like(Nimg)

    # process data
    Nimg = torch.from_numpy(np.transpose(Nimg, (2, 0, 1))).type(torch.FloatTensor)
    data = Nimg.unsqueeze(dim=0)
    model.feed_test_data(data)
    if opt['self_ensemble']:
        model.test(opt['self_ensemble'])
    elif opt['mc_ensemble']:
        model.MC_test()
    else:
        model.test()    

    img = model.fake_H.detach().float().cpu()
    Idenoised_crop = util.tensor2img_Real(img)  # uint8
    Idenoised_crop = np.transpose(Idenoised_crop, (1, 2, 0))
    DenoisedBlocksSrgb = Idenoised_crop

    save_file = os.path.join(out_dir, 'my_test.PNG')
    cv2.imwrite(save_file, cv2.cvtColor(Idenoised_crop, cv2.COLOR_RGB2BGR))
    print('Executed sucessfully is done\n')  

Added this function to test_Real.py to test for a single image But when trying out a single image, I am unable to retrieve the correct denoised image. I have included both my input image and output image. Could anyone tell me what might be causing this ?

my_test_img

my_test

hahazhar commented 1 year ago

Essentially, the image that is passed in, should be normalised. Pixel values should fall between 0 & 1 Afterwhich, it works well. One thing to note would be the size of the input image, it cannot be too large depending on your computer specifications.

WangXinping123 commented 1 year ago
def my_test(model, opt):
    dataset_dir = opt['name']
    out_dir = os.path.join( "..", "experiments", dataset_dir)

    if not os.path.exists(out_dir):
        os.makedirs(out_dir)

    out_dir = os.path.join(out_dir, 'my_test')
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)

    # load info
    filePath = os.path.join(opt['datasets']['test_1']['dataroot_Noisy'], 'NOISY_SRGB_010.png')
    Nimg = cv2.imread(filePath)[:256,:256,:]
    print(Nimg.shape)
    DenoisedBlocksSrgb = np.empty_like(Nimg)

    # process data
    Nimg = torch.from_numpy(np.transpose(Nimg, (2, 0, 1))).type(torch.FloatTensor)
    data = Nimg.unsqueeze(dim=0)
    model.feed_test_data(data)
    if opt['self_ensemble']:
        model.test(opt['self_ensemble'])
    elif opt['mc_ensemble']:
        model.MC_test()
    else:
        model.test()    

    img = model.fake_H.detach().float().cpu()
    Idenoised_crop = util.tensor2img_Real(img)  # uint8
    Idenoised_crop = np.transpose(Idenoised_crop, (1, 2, 0))
    DenoisedBlocksSrgb = Idenoised_crop

    save_file = os.path.join(out_dir, 'my_test.PNG')
    cv2.imwrite(save_file, cv2.cvtColor(Idenoised_crop, cv2.COLOR_RGB2BGR))
    print('Executed sucessfully is done\n')  

Added this function to test_Real.py to test for a single image But when trying out a single image, I am unable to retrieve the correct denoised image. I have included both my input image and output image. Could anyone tell me what might be causing this ?

my_test_img

my_test

您好,我和您出现了一样的问题,请问您是如何解决的呢?

hahazhar commented 1 year ago

Have to add this line

# process data
Nimg = Nimg / 255.0 # Normalisation
Nimg = torch.from_numpy(np.transpose(Nimg, (2, 0, 1))).type(torch.FloatTensor)

Hope it works for you

WangXinping123 commented 1 year ago

The problem has been resolved. Thank you for your help.

LieKKKKK commented 1 year ago

The problem has been resolved. Thank you for your help.

Can you help me how to use the my_test fun above to test on a single img

LieKKKKK commented 1 year ago
def my_test(model, opt):
    dataset_dir = opt['name']
    out_dir = os.path.join( "..", "experiments", dataset_dir)

    if not os.path.exists(out_dir):
        os.makedirs(out_dir)

    out_dir = os.path.join(out_dir, 'my_test')
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)

    # load info
    filePath = os.path.join(opt['datasets']['test_1']['dataroot_Noisy'], 'NOISY_SRGB_010.png')
    Nimg = cv2.imread(filePath)[:256,:256,:]
    print(Nimg.shape)
    DenoisedBlocksSrgb = np.empty_like(Nimg)

    # process data
    Nimg = torch.from_numpy(np.transpose(Nimg, (2, 0, 1))).type(torch.FloatTensor)
    data = Nimg.unsqueeze(dim=0)
    model.feed_test_data(data)
    if opt['self_ensemble']:
        model.test(opt['self_ensemble'])
    elif opt['mc_ensemble']:
        model.MC_test()
    else:
        model.test()    

    img = model.fake_H.detach().float().cpu()
    Idenoised_crop = util.tensor2img_Real(img)  # uint8
    Idenoised_crop = np.transpose(Idenoised_crop, (1, 2, 0))
    DenoisedBlocksSrgb = Idenoised_crop

    save_file = os.path.join(out_dir, 'my_test.PNG')
    cv2.imwrite(save_file, cv2.cvtColor(Idenoised_crop, cv2.COLOR_RGB2BGR))
    print('Executed sucessfully is done\n')  

Added this function to test_Real.py to test for a single image But when trying out a single image, I am unable to retrieve the correct denoised image. I have included both my input image and output image. Could anyone tell me what might be causing this ?

my_test_img

my_test

can you tell me how to run your codes to test single image, whats you env?

LieKKKKK commented 1 year ago
def my_test(model, opt):
    dataset_dir = opt['name']
    out_dir = os.path.join( "..", "experiments", dataset_dir)

    if not os.path.exists(out_dir):
        os.makedirs(out_dir)

    out_dir = os.path.join(out_dir, 'my_test')
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)

    # load info
    filePath = os.path.join(opt['datasets']['test_1']['dataroot_Noisy'], 'NOISY_SRGB_010.png')
    Nimg = cv2.imread(filePath)[:256,:256,:]
    print(Nimg.shape)
    DenoisedBlocksSrgb = np.empty_like(Nimg)

    # process data
    Nimg = torch.from_numpy(np.transpose(Nimg, (2, 0, 1))).type(torch.FloatTensor)
    data = Nimg.unsqueeze(dim=0)
    model.feed_test_data(data)
    if opt['self_ensemble']:
        model.test(opt['self_ensemble'])
    elif opt['mc_ensemble']:
        model.MC_test()
    else:
        model.test()    

    img = model.fake_H.detach().float().cpu()
    Idenoised_crop = util.tensor2img_Real(img)  # uint8
    Idenoised_crop = np.transpose(Idenoised_crop, (1, 2, 0))
    DenoisedBlocksSrgb = Idenoised_crop

    save_file = os.path.join(out_dir, 'my_test.PNG')
    cv2.imwrite(save_file, cv2.cvtColor(Idenoised_crop, cv2.COLOR_RGB2BGR))
    print('Executed sucessfully is done\n')  

Added this function to test_Real.py to test for a single image But when trying out a single image, I am unable to retrieve the correct denoised image. I have included both my input image and output image. Could anyone tell me what might be causing this ?

my_test_img

my_test

can you tell me how to run your codes to test single image, whats you env?

LieKKKKK commented 1 year ago

Have to add this line

# process data
Nimg = Nimg / 255.0 # Normalisation
Nimg = torch.from_numpy(np.transpose(Nimg, (2, 0, 1))).type(torch.FloatTensor)

Hope it works for you

everything is done thank you for your nice code