Ejemplo n.º 1
0
    def compute_fid_score(self):
        for img_type in self.image_types:
            os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
            self.fid_value[img_type] = calculate_fid_given_paths([self.gt_path, self.model_path], None, low_profile=False)
            print("FID: ", self.fid_value[img_type])
            # self.fid_value[img_type] = calculate_fid_given_paths(paths=[self.gt_path, self.model_path], batch_size=self.batch_size, dims=self.fid_dims, num_workers=self.num_workers, mod_type='_' + img_type)

            device = cuda.get_current_device()
            device.reset()
Ejemplo n.º 2
0
def evaluate_run(run_name, stats_path, inception_path, n_samples=20480, verbose=True):
    with tempfile.TemporaryDirectory() as tmpdir:
        if verbose:
            print("generating images", flush=True)
        generate_images(run_name, tmpdir, n_samples)
        if verbose:
            print("calculating FID", flush=True)
        fid_value = fid.calculate_fid_given_paths([tmpdir, stats_path], inception_path)
        if verbose:
            print("FID:", fid_value)
        return fid_value
Ejemplo n.º 3
0
def fid_wrapper_grid(grid_experiment=None):
    if grid_experiment is not None:
        exp_dir = grid_experiment.exp_dir
    else:
        raise "need argument, either grid_experiment or manual path"

    train_fid_stats = exp_dir / 'FID_stats.npz'

    # Hard coded to use the latest epoch
    test_generated_image_path = Path(
        './results'
    ) / grid_experiment.current_search_name / 'test_latest' / 'images' / 'synthesized_image'

    batch_size = 50
    dims = 2048
    device = torch.device('cuda' if (torch.cuda.is_available()) else 'cpu')
    # ipdb.set_trace()
    fid_value = fid.calculate_fid_given_paths(
        [str(train_fid_stats),
         str(test_generated_image_path)], batch_size, device, dims)
    return fid_value
Ejemplo n.º 4
0
def get_fid_from_array(arrays, convert2image_path, originaldata_path, path2inceptionnet):
    fid_value_list = []
    for arr in arrays:
        i = 0
        # convert array
        if args.dataset == 'norb':
            new_arr = np.zeros((arr.shape[0],arr.shape[1],arr.shape[2],3))
            for j in range(arr.shape[0]):
                new_arr[j] = cv2.cvtColor((arr[j]*255).astype(np.uint8),cv2.COLOR_GRAY2RGB)
            assert new_arr.shape == (arr.shape[0],arr.shape[1],arr.shape[2],3)
            arr = new_arr
        
        # clear converted image folder
        remove_files(convert2image_path)
        for img in arr:
            ims(convert2image_path+str(i)+".jpg",img)
            i+=1
        fid_value = calculate_fid_given_paths([convert2image_path,originaldata_path],path2inceptionnet)
        remove_files(convert2image_path)
        fid_value_list.append(fid_value)
    return fid_value_list
Ejemplo n.º 5
0
    #save

    os.makedirs("test_data/fake_visible", exist_ok = True)
    cv2.imwrite("test_data/fake_visible/" + vis_path , fake_A[0][:,:,::-1] * 255)

    totol_metric_dict_matched = {"mse":0.0,"rmse":0.0,"uqi":0.0,"ssim":0.0,"psnr":0.0,"psnrb":0.0,"vifp":0.0}  #参数指标

true_path = "test_data/visible"
fake_path = "test_data/fake_visiblee"

lenth = len(os.listdir(true_path))

for true_name,fake_name in zip(os.listdir(true_path),os.listdir(fake_path)):
	true = cv2.imread(os.path.join(true_path,true_name))
	fake = cv2.imread(os.path.join(fake_path,fake_name))

	metric_dict_matched = {"mse":mse(fake,true),"rmse":rmse(fake,true),"uqi":uqi(fake,true),"ssim":ssim(fake,true)[0] \
	   				,"psnr":psnr(fake,true),"psnrb":psnrb(fake,true),"vifp":vifp(fake,true)}
	for key,value in metric_dict_matched.items():
		totol_metric_dict_matched[key] = totol_metric_dict_matched[key]+value

for key,value in totol_metric_dict_matched.items():
	totol_metric_dict_matched[key] /= lenth
print(totol_metric_dict_matched)
#path = ["train_data/" + method + "_infrared","train_data/" + method + "_visible"]
path = [true_path,fake_path]
fid_value = fid.calculate_fid_given_paths(path, inception_path = None, low_profile=False)
print("FID: ", fid_value)  
print("done")
def execute(gpu,
            path_to_imgs,
            path_to_stats,
            inception_path,
            model,
            iteration,
            log_dir,
            low_profile=False):
    os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu)

    print('load images in %s...' % path_to_imgs)
    path_imgs = pathlib.Path(path_to_imgs)
    files = list(path_imgs.glob('*.jpg')) + list(path_imgs.glob('*.png'))
    # files = list(path_imgs.glob('*.png'))
    print('found %d files' % len(files))
    imgs_list = [imread(str(fn)).astype(np.float32) for fn in files]
    print('...done. [num=%d]' % len(imgs_list))

    print('calculate inception score...')
    is_mean, is_std = get_inception_score(imgs_list)
    print("IS: mean=%s, std=%s [time=%s, model=%s]" %
          (str(is_mean), str(is_std), str(iteration), model))
    print('...done.')

    print('calculate FID...')
    paths = [np.array(imgs_list), path_to_stats]
    fid_value = calculate_fid_given_paths(paths, inception_path, low_profile)
    print("FID: %s [time=%s, model=%s, path=%s]" %
          (fid_value, str(iteration), model, path_to_imgs))
    print('...done.')

    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    params = Params()
    params.model = model
    params.model_iteration = iteration
    params.model_path = path_to_imgs
    params.stats_path = path_to_stats
    params.fid = float(fid_value)
    params.is_mean = float(is_mean)
    params.is_std = float(is_std)
    params.exec_time = str(datetime.now())

    time = datetime.now().strftime('%Y%m%d_%H%M%S')
    file_name = "log-" + time + "-" + str(iteration) + ".json"
    params.save(os.path.join(log_dir, file_name))

    results_tf_folder = os.path.join(log_dir, "tf")
    if not os.path.exists(results_tf_folder):
        os.makedirs(results_tf_folder)
        print('created results_tf_folder: %s' % results_tf_folder)

    # write TF event file
    fid_sc = tf.constant(params.fid)
    ism_sc = tf.constant(params.is_mean)
    iss_sc = tf.constant(params.is_std)
    tf.summary.scalar(name='FID', tensor=fid_sc)
    tf.summary.scalar(name='IS_mean', tensor=ism_sc)
    tf.summary.scalar(name='IS_std', tensor=iss_sc)
    summary_op = tf.summary.merge_all()
    init = tf.global_variables_initializer()
    # launch the graph in a session
    with tf.Session() as sess:
        writer = tf.summary.FileWriter(results_tf_folder)
        sess.run(init)
        summary = sess.run(summary_op)
        writer.add_summary(summary, iteration)
Ejemplo n.º 7
0
                        float(epoch_iter) / dataset_size, losses)

            if total_iters % opt.save_latest_freq == 0:  # cache our latest model every <save_latest_freq> iterations
                print('saving the latest model (epoch %d, total_iters %d)' %
                      (epoch, total_iters))
                save_suffix = 'iter_%d' % total_iters if opt.save_by_iter else 'latest'
                model.save_networks(save_suffix)

            iter_data_time = time.time()
        if epoch % opt.save_epoch_freq == 0:  # cache our model every <save_epoch_freq> epochs
            print('saving the model at the end of epoch %d, iters %d' %
                  (epoch, total_iters))
            model.save_networks('latest')
            model.save_networks(epoch)

        fids.append(
            fid.calculate_fid_given_paths(path_real, path_fake, 1, 'cuda',
                                          2048))

        fig, ax = plt.subplots(figsize=(16, 9))
        ax.plot(fids)
        ax.set_xlabel('Epoch')
        ax.set_ylabel('FID')
        ax.set_title('Frechet Inception Distance during training')
        fig.savefig('FID.png')
        plt.close(fig)

        print('End of epoch %d / %d \t Time Taken: %d sec' %
              (epoch, opt.n_epochs + opt.n_epochs_decay,
               time.time() - epoch_start_time))