jinnh / GSAD

[NeurIPS 2023] Global Structure-Aware Diffusion Process for Low-Light Image Enhancement
114 stars 9 forks source link

About inference time #26

Open finnn00 opened 3 weeks ago

finnn00 commented 3 weeks ago

Regarding the inference time in test.py , could you please confirm if the following code is used for calculation? Looking forward to ur reply! ················································································································································································································································· def main():



    avg_psnr = 0.0
    avg_ssim = 0.0
    idx = 0
    lpipss = []

    **total_time = 0**.0  

    for val_data in val_loader:

        idx += 1

        **start_time = time.time()**  #Start timing and record the time before the data is fed into the model.

        diffusion.feed_data(val_data)  
        diffusion.test(continous=False)  
        visuals = diffusion.get_current_visuals()  

        normal_img = Metrics.tensor2img(visuals['HQ'])
        if normal_img.shape[0] != normal_img.shape[1]: # lolv1 and lolv2-real
            normal_img = normal_img[8:408, 4:604,:]
        gt_img = Metrics.tensor2img(visuals['GT'])
        ll_img = Metrics.tensor2img(visuals['LQ'])

        img_mode = 'single'
        if img_mode == 'single':
            util.save_img(
                gt_img, '{}/{}_gt.png'.format(result_path_gt, idx))
            util.save_img(
                ll_img, '{}/{}_lq.png'.format(result_path_input, idx))
        else:
            util.save_img(
                gt_img, '{}/{}_gt.png'.format(result_path, idx))
            util.save_img(
                normal_img, '{}/{}_{}_normal_process.png'.format(result_path, idx))

        gt_img = gt_img / 255.
        normal_img = normal_img / 255.
        mean_gray_out = cv2.cvtColor(normal_img.astype(np.float32), cv2.COLOR_BGR2GRAY).mean()
        mean_gray_gt = cv2.cvtColor(gt_img.astype(np.float32), cv2.COLOR_BGR2GRAY).mean()
        normal_img_adjust = np.clip(normal_img * (mean_gray_gt / mean_gray_out), 0, 1)

        normal_img = (normal_img_adjust * 255).astype(np.uint8)
        gt_img = (gt_img * 255).astype(np.uint8)

        psnr = util.calculate_psnr(normal_img, gt_img)
        ssim = util.calculate_ssim(normal_img, gt_img)

        util.save_img(normal_img, '{}/{}_normal.png'.format(result_path_out, idx))  

        **end_time = time.time()**  # Stop the timer and record the time after the image is saved.
        **process_time = end_time - start_time**  
        **total_time += process_time**  

        # lpips
        img_hq = np.transpose(normal_img/255, (2, 0, 1))
        img_hq = transform(torch.from_numpy(img_hq).unsqueeze(0))
        img_gt = np.transpose(gt_img/255, (2, 0, 1))
        img_gt = transform(torch.from_numpy(img_gt).unsqueeze(0))
        lpips_ = loss_fn_vgg(img_hq.to(torch.float32), img_gt.to(torch.float32))

        lpipss.append(lpips_.detach().numpy())

        logger_val.info('### {} cPSNR: {:.4e} cSSIM: {:.4e} cLPIPS: {:.4e} Time(s/image): {:.4f}'.format(
            idx, psnr, ssim, lpips_.detach().numpy()[0][0][0][0], elapsed_time))
        avg_ssim += ssim
        avg_psnr += psnr

    avg_psnr = avg_psnr / idx
    avg_ssim = avg_ssim / idx
    avg_time_ = total_time / idx  

    # log
    logger_val.info('# Validation # avgPSNR: {:.4e} avgSSIM: {:.4e} avgLPIPS: {:.4e} avgTime(s/image): {:.4f}'.format(
        avg_psnr, avg_ssim, np.mean(lpipss), avg_time_per_image))
·······························································································································································································································································
jinnh commented 3 weeks ago

Hi, @finnn00. To calculate the model's inference time, you can set the start and end times around the code, i.e., diffusion.test(continous=False). If you want to measure the time until the output image is saved, you should consider moving the end_time calculation before the PSNR and SSIM calculations, since they are time-consuming.