InvDN icon indicating copy to clipboard operation
InvDN copied to clipboard

Test on a single image

Open f0enix opened this issue 3 years ago • 11 comments

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

f0enix avatar Jun 15 '21 09:06 f0enix

Just load the image and the pretrained model to test.

Yang-Liu1082 avatar Jul 09 '21 06:07 Yang-Liu1082

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

bui-thanh-lam avatar Feb 14 '22 09:02 bui-thanh-lam

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 avatar Mar 27 '23 06:03 hahazhar

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.

hahazhar avatar Mar 28 '23 06:03 hahazhar

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

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

WangXinping123 avatar Apr 11 '23 02:04 WangXinping123

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

hahazhar avatar Apr 12 '23 01:04 hahazhar

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

WangXinping123 avatar Apr 23 '23 07:04 WangXinping123

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 avatar Aug 24 '23 10:08 LieKKKKK

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 avatar Aug 24 '23 12:08 LieKKKKK

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 avatar Aug 24 '23 12:08 LieKKKKK

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

LieKKKKK avatar Aug 24 '23 13:08 LieKKKKK