eval_funcs.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:tf-exercise-gan 作者: sanghoon 项目源码 文件源码
def eval_images_naive(it, gen, data, tag='', sampler=None):
    metrics = OrderedDict()

    if sampler is not None:
        z = sampler(128)
        samples = gen(z)        # Feed z
    else:
        samples = gen(128)      # Generate n images

    true_samples = data.validation.images
    true_labels = data.validation.labels if 'labels' in dir(data.validation) else None


    # Compute dist.
    dist_func = lambda a, b: np.linalg.norm((a - b).reshape((-1)), ord=2)

    # Distance: (generated samples) x (true samples)
    dist = np.array([[dist_func(x, x_true) for x_true in true_samples] for x in samples])

    best_matching_i_true = np.argmin(dist, axis=1)
    metrics['n_modes'] = len(np.unique(best_matching_i_true))
    metrics['ave_dist'] = np.average(np.min(dist, axis=1))


    # Check the labels (if exist)
    if true_labels is not None:
        label_cnts = np.sum(true_labels[best_matching_i_true], axis=0)
        metrics['n_labels'] = np.sum(label_cnts > 0)


    # Compute SSIM among top-k candidates (XXX: No supporting evidence for this approx.)
    k = 10
    top_k_matching_samples = np.argpartition(dist, k, axis=1)[:, :k]

    # Please refer to https://en.wikipedia.org/wiki/Structural_similarity
    # compare_ssim assumes (W, H, C) ordering
    sim_func = lambda a, b: ssim(a, b, multichannel=True, data_range=2.0)

    # Similarity: (generated samples) x (top-k candidates)
    sim = [[sim_func(samples[i], true_samples[i_true]) for i_true in i_topk] \
                                for i, i_topk in enumerate(top_k_matching_samples)]
    sim = np.array(sim)

    metrics['ave_sim'] = np.average(np.max(sim, axis=1))


    # TODO: Impl. IvOM

    # TODO: Impl. better metrics

    print "Eval({}) ".format(it), ', '.join(['{}={:.2f}'.format(k, v) for k, v in metrics.iteritems()])

    return metrics
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号