File size: 1,107 Bytes
9a6dac6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import torch
import numpy as np


def calculate_isc(featuresdict, feat_layer_name, rng_seed, samples_shuffle, splits):
    # print("Computing Inception Score")
    
    features = featuresdict[feat_layer_name]

    assert torch.is_tensor(features) and features.dim() == 2
    N, C = features.shape
    if samples_shuffle:
        rng = np.random.RandomState(rng_seed)
        features = features[rng.permutation(N), :]
    features = features.double()

    p = features.softmax(dim=1)
    log_p = features.log_softmax(dim=1)

    scores = []
    for i in range(splits):
        p_chunk = p[(i * N // splits) : ((i + 1) * N // splits), :]  # δΈ€ιƒ¨εˆ†ηš„ι’„ζ΅‹ζ¦‚ηŽ‡
        log_p_chunk = log_p[(i * N // splits) : ((i + 1) * N // splits), :]  # log
        q_chunk = p_chunk.mean(dim=0, keepdim=True)  # ζ¦‚ηŽ‡ηš„ε‡ε€Ό
        kl = p_chunk * (log_p_chunk - q_chunk.log())  #
        kl = kl.sum(dim=1).mean().exp().item()
        scores.append(kl)
    # print("scores",scores)
    return {
        "inception_score_mean": float(np.mean(scores)),
        "inception_score_std": float(np.std(scores)),
    }