Beispiel #1
0
def main(args, config):
    cuda = cnn_utils.check_cuda(config)
    model = cnn_utils.load_model_and_weights(args, config)
    if cuda:
        model = model.cuda()

    file_path = os.path.join(config['PATH']['hdf5_dir'],
                             config['PATH']['hdf5_name'])
    with h5py.File(file_path, mode='r', libver='latest') as hdf5_file:
        overall_psnr_accum = (0, 0, 0)
        overall_ssim_accum = (0, 0, 0)

        for sample_num in range(args.nSamples):
            p1, s1 = do_one_demo(args, config, hdf5_file, model, sample_num,
                                 cuda)
            overall_psnr_accum = welford.update(overall_psnr_accum, p1)
            overall_ssim_accum = welford.update(overall_ssim_accum, s1)

        if args.nSamples > 1:
            psnr_mean, psnr_var, _ = welford.finalize(overall_psnr_accum)
            ssim_mean, ssim_var, _ = welford.finalize(overall_ssim_accum)
            print("\nOverall cnn psnr average {:5f}, stddev {:5f}".format(
                psnr_mean, math.sqrt(psnr_var)))
            print("Overall cnn ssim average {:5f}, stddev {:5f}".format(
                ssim_mean, math.sqrt(ssim_var)))
        #Ground truth possible
        """
def main(args, config, sample_index):
    cuda = cnn_utils.check_cuda(config)
    model = cnn_utils.load_model_and_weights(args, config)
    if cuda:
        model = model.cuda()

    model.eval()

    # Create output directory
    base_dir = os.path.join(config['PATH']['output_dir'], 'warped')
    if not os.path.isdir(base_dir):
        pathlib.Path(base_dir).mkdir(parents=True, exist_ok=True)
    save_dir = get_sub_dir_for_saving(base_dir)

    start_time = time.time()
    file_path = os.path.join(config['PATH']['hdf5_dir'],
                             config['PATH']['hdf5_name'])
    with h5py.File(file_path, mode='r', libver='latest') as hdf5_file:
        depth_grp = hdf5_file['val']['disparity']
        SNUM = sample_index
        depth_images = torch.squeeze(
            torch.tensor(depth_grp['images'][SNUM], dtype=torch.float32))

        colour_grp = hdf5_file['val']['colour']
        colour_images = torch.tensor(colour_grp['images'][SNUM],
                                     dtype=torch.float32)

        sample = {
            'depth': depth_images,
            'colour': colour_images,
            'grid_size': depth_images.shape[0]
        }

        warped = data_transform.center_normalise(sample)
        im_input = warped['inputs'].unsqueeze_(0)

        if cuda:
            im_input = im_input.cuda()

        output = model(im_input)

        time_taken = time.time() - start_time
        print("Time taken was {:4f}s".format(time_taken))
        grid_size = 64

        psnr_accumulator = (0, 0, 0)
        ssim_accumulator = (0, 0, 0)

        print("Saving output to", save_dir)

        output = torch.squeeze(denormalise_lf(output))
        cpu_output = np.around(output.cpu().detach().numpy()).astype(np.uint8)

        if (not args.no_eval) or args.get_diff:
            ground_truth = np.around(
                denormalise_lf(colour_images).numpy()).astype(np.uint8)

        grid_len = int(math.sqrt(grid_size))
        for i in range(grid_size):
            row, col = i // grid_len, i % grid_len

            file_name = 'Colour{}{}.png'.format(row, col)
            save_location = os.path.join(save_dir, file_name)
            if i == 0:
                print("Saving images of size ", cpu_output[i].shape)
            image_warping.save_array_as_image(cpu_output[i], save_location)

            if args.get_diff:
                colour = ground_truth[i]
                diff = image_warping.get_diff_image(colour, cpu_output[i])
                #diff = get_diff_image_floatint(res, colour)
                file_name = 'Diff{}{}.png'.format(row, col)
                save_location = os.path.join(save_dir, file_name)
                image_warping.save_array_as_image(diff, save_location)

            if not args.no_eval:
                img = ground_truth[i]
                file_name = 'GT_Colour{}{}.png'.format(row, col)
                save_location = os.path.join(save_dir, file_name)
                image_warping.save_array_as_image(img, save_location)
                psnr = evaluate.my_psnr(cpu_output[i], img)
                ssim = evaluate.ssim(cpu_output[i], img)
                psnr_accumulator = welford.update(psnr_accumulator, psnr)
                ssim_accumulator = welford.update(ssim_accumulator, ssim)

        psnr_mean, psnr_var, _ = welford.finalize(psnr_accumulator)
        ssim_mean, ssim_var, _ = welford.finalize(ssim_accumulator)
        print("For cnn, psnr average {:5f}, stddev {:5f}".format(
            psnr_mean, math.sqrt(psnr_var)))
        print("For cnn, ssim average {:5f}, stddev {:5f}".format(
            ssim_mean, math.sqrt(ssim_var)))