tuvovan / NHDRRNet

Keras Implementation of the paper Deep HDR Imaging via A Non-Local Network - TIP 2020
MIT License
51 stars 6 forks source link

can not reproduce the result in the paper #8

Closed h4nwei closed 3 years ago

h4nwei commented 3 years ago

Hi tuvovan@tuvovan,

Thanks much for your excellent work.

When I apply the provided pre-trained parameters to compute the PSNR value after tone-mapping (log(1+mu*H)/log(1+mu), mu=5000), the average PSNR is 30.9612, which is far away behind the shown result (PSNR_mu = 42.4143). Here is the test code:

import os
import sys
import glob
import time
import math
import argparse
import numpy as np
import tensorflow as tf
import generate_HDR_dataset

from HDR import *
from val import run
from PIL import Image
from tensorflow.keras import Model, Input
from tensorflow.keras.utils import multi_gpu_model
from tensorflow.keras.layers import Concatenate, Conv2D, Input
import cv2

def get_test_data_real(images_path):
    imgs_np = np.zeros([1, 3, 768, 1024, 6])
    file1 = open(os.path.join(images_path, 'exposure.txt'), 'r')
    Lines = file1.readlines()
    t = [float(i) for i in Lines]
    hdr_file = glob.glob(os.path.join(images_path, 'HDRImg.hdr'))
    hdr_gt = cv2.imread(hdr_file[0], -1).astype(np.float32)
    hdr_gt = cv2.resize(hdr_gt, (1024, 768))
    #hdr_gt = cv2.cvtColor(hdr_gt, cv2.COLOR_BGR2RGB)

    for j, f in enumerate(sorted(glob.glob(os.path.join(images_path, '*.tif')))):
        ldr = (cv2.imread(f) / 255.0).astype(np.float32)
        ldr = cv2.resize(ldr, (1024, 768))
        #ldr = cv2.cvtColor(ldr, cv2.COLOR_BGR2RGB)
        hdr = ldr ** 2.2 / (2 ** t[j])
        X = np.concatenate([ldr, hdr], axis=-1)
        imgs_np[0, j, :, :, :] = X

    return imgs_np, hdr_gt

def compute_psnr_(img1, img2):
    mse = np.mean( (img1 - img2) ** 2 )
    if mse == 0:
        return 100
    PIXEL_MAX = 1.0 # input -1~1
    return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))

def save_hdr(opt, prediction, i):
    save_dir = opt.save_path
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    image_dir = os.path.join(save_dir, i + '_predict_hdr.hdr')

    cv2.imwrite(image_dir, prediction)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    # Input Parameters
    parser.add_argument('--test_path', type=str,
                        default="/media/kedeleshier/f1f792db-19f4-429e-8701-a57f0a6f4187/hanwei"
                                "/dataSet/img_seq_2048/6/SIGGRAPH17_HDR_Testset/Test/test/")
    parser.add_argument('--save_path', type=str, default='./resultHDR/')
    parser.add_argument('--gpu', type=int, default=1)
    parser.add_argument('--weight_test_path', type=str, default="./weights/NNHDRNet.h5")
    parser.add_argument('--filter', type=int, default=32)
    parser.add_argument('--kernel', type=int, default=3)
    parser.add_argument('--encoder_kernel', type=int, default=3)
    parser.add_argument('--decoder_kernel', type=int, default=4)
    parser.add_argument('--triple_pass_filter', type=int, default=256)

    config = parser.parse_args()

    #   if not os.path.exists(config.checkpoints_folder):
    #       os.mkdir(config.checkpoints_folder)

    os.environ['CUDA_VISIBLE_DEVICES'] = str(config.gpu)

    model_x = NHDRRNet(config)
    x = Input(shape=(3, 768, 1024, 6))
    out = model_x.main_model(x)
    model = Model(inputs=x, outputs=out)
    model.load_weights(config.weight_test_path)
    # model.summary()
    psnr = []
    for j, f in enumerate(sorted(os.listdir(config.test_path))):
        SDR, GT = get_test_data_real(os.path.join(config.test_path, f))

        rs = model.predict(SDR)
        out = rs[0]

        # cal psnr_hdr
        MU = 5000.0
        gt_ldr = tf.math.log(1 + MU * GT) / tf.math.log(1 + MU)
        out_ldr = tf.math.log(1 + MU * out) / tf.math.log(1 + MU)

        psnr.append(compute_psnr_(out_ldr, gt_ldr))
        print(compute_psnr_(out_ldr, gt_ldr))
        save_hdr(config, out, f)
    print("avg psnr:", sum(psnr)/len(psnr))

    #run(f, model)

It is really appreciated any help you can provide. Thank you.

tuvovan commented 3 years ago

Hi, for your problem, I would say that you could load the weight and train the model with more epoch until you get the desired results.

My initial intention was not to provide the exact weight that match the one provided by the author but just to know how the algorithm work so I didn't try to reach the number provided in the paper. :D

Anyways, thank you for stopping by and your concern!

Goodluck!