示例#1
0
def calculate_fid_given_paths(paths,
                              batch_size,
                              cuda=True,
                              dims=2048,
                              is_classwise=False):
    """Calculates the FID of two paths"""
    for p in paths:
        if not os.path.exists(p):
            raise RuntimeError('Invalid path: %s' % p)

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]

    model = InceptionV3([block_idx])
    if cuda:
        model.cuda()

    m1, s1 = _compute_statistics_of_path(paths[0], model, batch_size, dims,
                                         cuda, is_classwise)
    m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size, dims,
                                         cuda, is_classwise)
    if is_classwise:
        emos = set(m1.keys()).intersection(m2.keys())
        fid_value = dict()
        for i in emos:
            fid = calculate_frechet_distance(m1[i], s1[i], m2[i], s2[i])
            fid_value[i] = fid
    else:
        fid_value = calculate_frechet_distance(m1, s1, m2, s2)

    return fid_value
示例#2
0
文件: kid_score.py 项目: deJQK/CAT
def calculate_kid_given_paths(paths, batch_size, cuda, dims):
    """Calculates the KID of two paths"""
    pths = []
    for p in paths:
        if not os.path.exists(p):
            raise RuntimeError('Invalid path: %s' % p)
        if os.path.isdir(p):
            pths.append(p)
        elif p.endswith('.npy'):
            np_imgs = np.load(p)
            if np_imgs.shape[0] > 50000:
                np_imgs = np_imgs[np.random.permutation(
                    np.arange(np_imgs.shape[0]))][:50000]
            pths.append(np_imgs)

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]
    model = InceptionV3([block_idx])
    if cuda:
        model.cuda()

    act_true = _compute_activations(pths[0], model, batch_size, dims, cuda)
    pths = pths[1:]
    results = []
    for j, pth in enumerate(pths):
        print(paths[j + 1])
        actj = _compute_activations(pth, model, batch_size, dims, cuda)
        kid_values = polynomial_mmd_averages(act_true,
                                             actj,
                                             n_subsets=100,
                                             subset_size=100)
        results.append(
            (paths[j + 1], kid_values[0].mean(), kid_values[0].std()))
    return results
示例#3
0
文件: fid.py 项目: johndpope/sRender
def calculate_fid_given_paths(paths0, paths1, batch_size, cuda, dims):
    """Calculates the FID of two paths"""
    '''
    for p in paths:
        if not os.path.exists(p):
            raise RuntimeError('Invalid path: %s' % p)
    '''

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]
    # device = 'cuda' if torch.cuda.is_available() else 'cpu'
    model = InceptionV3([block_idx])

    #class
    # model = DenseNet(growthRate=6, depth=10, reduction=0.5, bottleneck=True, nClasses=11)

    if cuda:
        model.cuda()
    '''
    if device == 'cuda':
        model = torch.nn.DataParallel(model)
        cudnn.benchmark = True
    '''
    m1, s1 = _compute_statistics_of_path(paths0, model, batch_size, dims,
                                         cuda)  #[80,2018]
    m2, s2 = _compute_statistics_of_path(paths1, model, batch_size, dims, cuda)
    fid_value = calculate_frechet_distance(m1, s1, m2, s2)

    return fid_value
示例#4
0
def calculate_fid_given_paths_or_tensor(input1, input2, batch_size, cuda, dims,
                                        config):
    """Calculates the FID of two paths.
    Added in 2019 for unet-gan project"""

    if isinstance(input1, str):
        function1 = _compute_statistics_of_path
    else:
        function1 = _compute_statistics_of_tensor

    if isinstance(input2, str):
        function2 = _compute_statistics_of_path
    else:
        function2 = _compute_statistics_of_tensor

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]

    if config["si"]:
        pass
    else:
        pass

    model = InceptionV3([block_idx])

    if cuda:
        model.cuda()

    with torch.no_grad():
        m1, s1 = function1(input1, model, batch_size, dims, cuda)
        m2, s2 = function2(input2, model, batch_size, dims, cuda)

        fid_value = calculate_frechet_distance(m1, s1, m2, s2)

    return fid_value
示例#5
0
def compute_cifar10_statistics(batch_size=50, dims=2048, cuda=True):
    dataset = datasets.CIFAR10(root="~/datasets/data_cifar10",
                               train=False,
                               download=True,
                               transform=transforms.Compose(
                                   [transforms.ToTensor()]))
    data_loader = torch.utils.data.DataLoader(dataset,
                                              batch_size=batch_size,
                                              shuffle=True)

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]
    model = InceptionV3([block_idx])
    if cuda:
        model.cuda()

    model.eval()
    pred_arr = np.empty((len(dataset), dims))
    start = 0

    print("Computing statistics of cifar10...")

    for (x, y) in tqdm(data_loader):
        end = start + x.size(0)
        if cuda:
            x = x.cuda()
        pred = model(x)[0]
        if pred.size(2) != 1 or pred.size(3) != 1:
            pred = adaptive_avg_pool2d(pred, output_size=(1, 1))
        pred_arr[start:end] = pred.cpu().data.numpy().reshape(pred.size(0), -1)
        start = end

    mu = np.mean(pred_arr, axis=0)
    sigma = np.cov(pred_arr, rowvar=False)
    return mu, sigma
示例#6
0
    def load_model(self):
        block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[self.dims]
        self.model = InceptionV3([block_idx])
        if self.gpu is not None:
            self.model.cuda(self.gpu)

        self.model.eval()
def get_inception_score(images,
                        device,
                        splits=10,
                        batch_size=32,
                        verbose=False):
    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM['prob']
    model = InceptionV3([block_idx]).to(device)
    model.eval()

    preds = []
    if verbose:
        iterator = trange(0, len(images), batch_size, dynamic_ncols=True)
    else:
        iterator = range(0, len(images), batch_size)
    for start in iterator:
        end = start + batch_size
        batch_images = images[start:end]
        batch_images = torch.from_numpy(batch_images).type(torch.FloatTensor)
        batch_images = batch_images.to(device)
        pred = model(batch_images)[0]
        preds.append(pred.cpu().numpy())

    preds = np.concatenate(preds, 0)
    scores = []
    for i in range(splits):
        part = preds[(i * preds.shape[0] //
                      splits):((i + 1) * preds.shape[0] // splits), :]
        kl = part * (np.log(part) -
                     np.log(np.expand_dims(np.mean(part, 0), 0)))
        kl = np.mean(np.sum(kl, 1))
        scores.append(np.exp(kl))
    return np.mean(scores), np.std(scores)
示例#8
0
文件: fid_score.py 项目: tzt101/CLADE
def calculate_fid_given_paths(paths, batch_size, cuda, dims, args):
    """Calculates the FID of two paths"""
    for p in paths:
        if not os.path.exists(p):
            raise RuntimeError('Invalid path: %s' % p)

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]

    model = InceptionV3([block_idx])
    if cuda:
        model.cuda()

    if args.load_np_name == '':
        m1, s1 = _compute_statistics_of_path(paths[0], model, batch_size, dims,
                                             cuda, args)
        if args.save_np_name != '':
            root = os.path.join('./datasets/train_mu_si', args.save_np_name)
            if not os.path.exists(root):
                os.mkdir(root)
            np.save(os.path.join(root, 'm.npy'), m1)
            np.save(os.path.join(root, 's.npy'), s1)
    else:
        root = './datasets/train_mu_si'
        m1 = np.load(os.path.join(root, args.load_np_name, 'm.npy'))
        s1 = np.load(os.path.join(root, args.load_np_name, 's.npy'))

    m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size, dims,
                                         cuda, args)
    fid_value = calculate_frechet_distance(m1, s1, m2, s2)

    return fid_value
示例#9
0
文件: FID.py 项目: xq821272536/MiFID
def multi2one(path1, path2, out_path, batch_size=64, dims=2048, cuda=False):
    path_checks(path1, path2, out_path)

    filename1 = os.listdir(path1)
    file1 = list(path1 + '/' + file for file in filename1
                 if os.path.isdir(path1 + '/' + file))
    file1 = sorted(file1)

    FID = xlwt.Workbook(encoding='utf-8')
    sheet1 = FID.add_sheet(u'multi2multi', cell_overwrite_ok=True)  # 创建sheet

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]
    model = InceptionV3([block_idx])
    if cuda:
        model.cuda()

    sheet1.write(0, 1, list(path2.split('/'))[-1])
    mj, sj, _ = _compute_statistics_of_path(path2, model, batch_size, dims,
                                            cuda)
    i = 0
    while i < len(file1):
        f1 = list(file1[i].split('/'))
        mi, si, _ = _compute_statistics_of_path(file1[i], model, batch_size,
                                                dims, cuda)
        if len(mi) == 0:
            i += 1
            continue
        fid_value = calculate_frechet_distance(mi, si, mj, sj)
        print('{}---{}: {}'.format(f1[-2] + '/' + f1[-1], path2, fid_value))
        sheet1.write(i + 1, 0, f1[-1])
        sheet1.write(i + 1, 1, fid_value)
        i += 1
    FID.save(out_path)  # 保存文件
示例#10
0
def calculate_fid_given_paths(paths, batch_size, cuda, dims):
    """Calculates the FID of two paths"""
    for p in paths:
        if not os.path.exists(p):
            raise RuntimeError('Invalid path: %s' % p)

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]

    model = InceptionV3([block_idx])
    if cuda:
        model.cuda()

    imgs1 = load_images(paths[0], requires_grad=True)
    imgs2 = load_images(paths[1], requires_grad=False)

    m1, s1 = calculate_activation_statistics(imgs1, model, batch_size, dims,
                                             cuda)
    m2, s2 = calculate_activation_statistics(imgs2, model, batch_size, dims,
                                             cuda)

    print(m1.size(), s1.size())
    fid_value = calculate_frechet_distance(m1, s1, m2, s2)

    # Backward
    print("Running backward ...")
    fid_value[0][0].backward()
    print("Gradient for images:")
    print(imgs1.grad)

    return fid_value
示例#11
0
def calculate_fid_given_paths(paths, batch_size, cuda, dims, device=0):
    """Calculates the FID of two paths"""
    # for p in paths:
    #     if not os.path.exists(p):
    #         raise RuntimeError('Invalid path: %s' % p)

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]

    model = InceptionV3([block_idx])
    if cuda:
        torch.cuda.set_device(device)
        model.cuda()

    real_paths = paths[0]
    fake_paths_list = paths[1]

    fid_values = []

    m_r, s_r = _compute_statistics_of_path(real_paths, model, batch_size, dims,
                                           cuda)
    for fake_paths in fake_paths_list:
        m_f, s_f = _compute_statistics_of_path(fake_paths, model, batch_size,
                                               dims, cuda)
        fid_values.append(calculate_frechet_distance(m_r, s_r, m_f, s_f))

    return fid_values
示例#12
0
def compute_dataset_statistics(target_set="LSUN", batch_size=50, dims=2048, cuda=True, device='cuda:0'):
    if target_set == "CIFAR10":
        imageSize = 64
        dataset = datasets.CIFAR10(root="~/datasets/data_cifar10", train=False, download=True,
                        transform=transforms.Compose([
                        transforms.Resize(imageSize),
                        transforms.ToTensor(),
                        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                        ]))
    elif target_set == "MNIST":
        imageSize = 64
        dataset = datasets.MNIST(root="~/datasets", train=True, download=True, 
                        transform=transforms.Compose([
                        transforms.Resize(imageSize),
                        transforms.ToTensor(),
                        transforms.Normalize((0.5,), (0.5,)),
                        ]))
    elif target_set == 'LSUN':
        imageSize = 64
        dataset = datasets.LSUN(root="/data1/zhangliangyu/datasets/data_lsun", classes=["church_outdoor_train"],
                        transform=transforms.Compose([
                            transforms.Resize(imageSize),
                            transforms.CenterCrop(imageSize),
                            transforms.ToTensor(),
                            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                        ]))


    data_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True)
    
    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]
    model = InceptionV3([block_idx])
    if cuda:
        model.to(device)

    model.eval()
    pred_arr = np.empty((len(dataset), dims))
    start = 0

    print("Computing statistics of the given dataset...")

    for (x, y) in tqdm(data_loader):
        if target_set == "MNIST":
            tmp = torch.zeros((x.size()[0], 3, x.size()[2], x.size()[3]))
            for i in range(3):
                tmp[:, i, :, :] = x[:, 0, :, :]
            x = tmp
        end = start + x.size(0)
        if cuda:
            x = x.to(device)
        pred = model(x)[0]
        if pred.size(2) != 1 or pred.size(3) != 1:
            pred = adaptive_avg_pool2d(pred, output_size=(1, 1))
        pred_arr[start:end] = pred.cpu().data.numpy().reshape(pred.size(0), -1)
        start = end
        
    
    mu = np.mean(pred_arr, axis=0)
    sigma = np.cov(pred_arr, rowvar=False)
    return mu, sigma
示例#13
0
def fid_score(real_loader,
              fake_loader,
              normalize=False,
              r_cache=None,
              verbose=0):
    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[2048]
    model = InceptionV3([block_idx])

    basename1 = os.path.basename(real_loader.dataset.directory)

    # cache real dataset
    if r_cache and os.path.exists(r_cache):
        m1, s1 = torch.load(r_cache)
        m1 = m1.cuda()
        s1 = s1.cuda()
    elif os.path.exists('{}.cache'.format(basename1)):
        m1, s1 = torch.load('{}.cache'.format(basename1))
        m1 = m1.cuda()
        s1 = s1.cuda()
    else:
        m1, s1 = calculate_activation_statistics(real_loader, model, normalize,
                                                 verbose)
        if r_cache is not None:
            os.makedirs(os.path.dirname(r_cache), exist_ok=True)
            torch.save((m1, s1), r_cache)
        if len(basename1):
            torch.save((m1, s1), '{}.cache'.format(basename1))
    # compute generative image dataset
    m2, s2 = calculate_activation_statistics(fake_loader, model, normalize,
                                             verbose)

    fid_value = calculate_frechet_distance(m1, s1, m2, s2)
    return fid_value
示例#14
0
def calculate_fid_given_paths(paths, batch_size, cuda, dims, save_stats):
    """Calculates the FID of two paths"""
    for p in paths:
        if not os.path.exists(p):
            raise RuntimeError('Invalid path: %s' % p)

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]

    model = InceptionV3([block_idx])
    if cuda:
        model.cuda()

    m1, s1 = _compute_statistics_of_path(paths[0], model, batch_size,
                                         dims, cuda)
    if save_stats and not paths[0].endswith('.npz'):
        save_npz(paths[0], m1, s1)

    m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size,
                                         dims, cuda)
    if save_stats and not paths[1].endswith('.npz'):
        save_npz(paths[1], m2, s2)

    fid_value = calculate_frechet_distance(m1, s1, m2, s2)

    return fid_value
def calculate_fid_given_paths(paths,
                              batch_size,
                              cuda,
                              dims,
                              model=None,
                              m1=None,
                              s1=None):
    """Calculates the FID of two paths"""
    for p in paths:
        if not os.path.exists(p):
            raise RuntimeError('Invalid path: %s' % p)
    if model is None:
        block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]
        model = InceptionV3([block_idx])
        if cuda:
            model.cuda()

    if (m1 is None) and (s1 is None):
        m1, s1 = _compute_statistics_of_path(paths[0], model, batch_size, dims,
                                             cuda)
    m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size, dims,
                                         cuda)
    fid_value = calculate_frechet_distance(m1, s1, m2, s2)

    return fid_value
def load_patched_inception_v3():
    # inception = inception_v3(pretrained=True)
    # inception_feat = Inception3Feature()
    # inception_feat.load_state_dict(inception.state_dict())
    inception_feat = InceptionV3([3], normalize_input=False)

    return inception_feat
示例#17
0
def calculate_fid_given_paths(paths,
                              premodel_path,
                              batch_size,
                              use_gpu,
                              dims,
                              model=None,
                              style = None):
    assert os.path.exists(
        premodel_path
    ), 'pretrain_model path {} is not exists! Please download it first'.format(
        premodel_path)
    for p in paths:
        if not os.path.exists(p):
            raise RuntimeError('Invalid path: %s' % p)

    if model is None and style != 'stargan':
        with fluid.dygraph.guard():
            block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]
            model = InceptionV3([block_idx], class_dim=1008)

    m1, s1 = _compute_statistics_of_path(paths[0], model, batch_size, dims,
                                         use_gpu, premodel_path, style)
    m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size, dims,
                                         use_gpu, premodel_path, style)

    fid_value = _calculate_frechet_distance(m1, s1, m2, s2)
    return fid_value
def getInceptionModel(batch_size, cuda, dims):
    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]

    model = InceptionV3([block_idx])
    if cuda:
        model.cuda()
    return model
示例#19
0
def calculate_fid_given_paths(paths, batch_size, cuda, dims):
    """Calculates the FID of two paths"""
    for p in paths:
        if not os.path.exists(p):
            raise RuntimeError('Invalid path: %s' % p)

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]

    print('Loading model')
    # The weight initialization takes a long time. This goes away in torchvision 0.6.0 But that requires
    # pytorch 1.5.0. This complains about the driers. so no speed up.
    model = InceptionV3([block_idx])
    if cuda:
        model.cuda()

    m1, s1 = _compute_statistics_of_path(paths[0], model, batch_size, dims,
                                         cuda)
    m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size, dims,
                                         cuda)
    # import ipdb; ipdb.set_trace()
    true_img_stats_file = 'ffhq_256X256_fid_stats.npz'
    np.savez(true_img_stats_file, mu=m2, sigma=s2)
    print('True stats saved')

    fid_value = calculate_frechet_distance(m1, s1, m2, s2)

    return fid_value
示例#20
0
def calculate_fid_multiple_gen_dir(paths, batch_size, cuda, dims):
    """Calculates the FID of multiple generated dirs"""
    for p in paths:
        if not os.path.exists(p):
            raise RuntimeError('Invalid path: %s' % p)

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]

    model = InceptionV3([block_idx])
    if cuda:
        model.cuda()

    m1, s1 = _compute_statistics_of_path(paths[0], model, batch_size, dims, cuda)

    gen_dir = os.listdir(args.path[1])
    gen_path = args.path[1]
    fid_list = []
    for i, dir in enumerate(gen_dir):
        args.path[1] = os.path.join(gen_path, dir)
        m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size, dims, cuda)
        fid_value = calculate_frechet_distance(m1, s1, m2, s2)
        print(i+1, len(gen_dir), 'FID: ', fid_value)

        fid_list.append((dir, fid_value))
    log_df = pd.DataFrame(fid_list, columns=['epoch', 'FID'])
    log_df.to_csv("%s/fid.csv" % gen_path)
示例#21
0
def calculate_fid_given_imgs(imgs1,
                             imgs2,
                             batch_size,
                             cuda,
                             dims=2048,
                             model=None):
    """
    Calculates the FID of two sets of images.
    NOTE: imgs1,imgs2 should be uint8s
    """

    if model is None:
        block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]
        model = InceptionV3([block_idx])
        if cuda:
            model.cuda()
    else:
        if cuda:
            model = model.cuda()

    m1, s1 = _compute_statistics_of_imgs(imgs1, model, batch_size, dims, cuda)
    m2, s2 = _compute_statistics_of_imgs(imgs2, model, batch_size, dims, cuda)
    fid_value = calculate_frechet_distance(m1, s1, m2, s2)

    return fid_value
示例#22
0
def calculate_sifid_given_paths(path1, path2, batch_size, cuda, dims, suffix):
    """Calculates the SIFID of two paths"""

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]

    model = InceptionV3([block_idx])
    if cuda:
        model.cuda()

    path1 = pathlib.Path(path1)
    files1 = list(path1.glob('*.%s' % suffix))

    path2 = pathlib.Path(path2)
    files2 = list(path2.glob('*.%s' % suffix))

    fid_values = []
    Im_ind = []
    for i in range(len(files2)):
        m1, s1 = calculate_activation_statistics([files1[i]], model,
                                                 batch_size, dims, cuda)
        m2, s2 = calculate_activation_statistics([files2[i]], model,
                                                 batch_size, dims, cuda)
        fid_values.append(calculate_frechet_distance(m1, s1, m2, s2))
        file_num1 = files1[i].name
        file_num2 = files2[i].name
        print(file_num1)
        #Im_ind.append(int(file_num1[:-4]))
        #Im_ind.append(int(file_num2[:-4]))
    return fid_values
示例#23
0
def calculate_fid_given_paths(paths, batch_size, cuda, dims):
    """Calculates the FID of two paths"""
    for p in paths:
        if not os.path.exists(p):
            raise RuntimeError('Invalid path: %s' % p)

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]

    print("block_idx ", block_idx)

    model = InceptionV3([block_idx])

    if cuda:
        model.cuda()

    with torch.no_grad():
        m1, s1 = _compute_statistics_of_tensor(
            torch.rand(100, 3, 256, 256), model, batch_size, dims, cuda
        )  #_compute_statistics_of_path(paths[0], model, batch_size, dims, cuda)
        m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size, dims,
                                             cuda)

        fid_value = calculate_frechet_distance(m1, s1, m2, s2)

    return fid_value
示例#24
0
def calculate_fid_given_paths(paths, batch_size, cuda, dims):
    """Calculates the FID of two paths"""
    for p in paths:
        if not os.path.exists(p):
            raise RuntimeError('Invalid path: %s' % p)

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]

    model = InceptionV3([block_idx])
    if cuda:
        model.cuda()

    m1, s1 = _compute_statistics_of_path(paths[0], model, batch_size, dims,
                                         cuda)
    m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size, dims,
                                         cuda)
    fid_value = calculate_frechet_distance(m1, s1, m2, s2)

    return fid_value


# if __name__ == '__main__':
#     args = parser.parse_args()
#     os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu

#     fid_value = calculate_fid_given_paths(args.path,
#                                           args.batch_size,
#                                           args.gpu != '',
#                                           args.dims)
#     print('FID: ', fid_value)
示例#25
0
def calculate_given_paths(paths, batch_size, cuda, dims):
    """Calculates the FID of two paths"""
    for p in paths:
        if not os.path.exists(p):
            raise RuntimeError('Invalid path: %s' % p)

    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]

    model = InceptionV3([block_idx],
                        use_fid_inception=True,
                        resize_input=False,
                        normalize_input=True)
    print('Network loaded')
    if cuda:
        str_ids = args.gpu.split(',')
        args.gpu = []
        for str_id in str_ids:
            id = int(str_id)
            if id >= 0:
                args.gpu.append(id)
        if len(args.gpu) > 0:
            torch.cuda.set_device(args.gpu[0])
        model.to(args.gpu[0])
        model = torch.nn.DataParallel(model, args.gpu)
    print('compute statistic A')
    act1, m1, s1 = _compute_statistics_of_path(paths[0], model, batch_size,
                                               dims, cuda)
    print('compute statistic B')
    act2, m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size,
                                               dims, cuda)
    print('compute FID')
    fid_value = calculate_frechet_distance(m1, s1, m2, s2)

    print('FID: ', fid_value)

    ret = polynomial_mmd_averages(act1,
                                  act2,
                                  degree=args.mmd_degree,
                                  gamma=args.mmd_gamma,
                                  coef0=args.mmd_coef0,
                                  ret_var=args.mmd_var,
                                  n_subsets=args.mmd_subsets,
                                  subset_size=args.mmd_subset_size)
    if args.mmd_var:
        mmd2s, vars = ret
    else:
        mmd2s = ret
    print("mean MMD^2 estimate:", mmd2s.mean() * 100)
    print("std MMD^2 estimate:", mmd2s.std())
    print("MMD^2 estimates:", mmd2s, sep='\n')
    print()
    if args.mmd_var:
        print("mean Var[MMD^2] estimate:", vars.mean() * 100)
        print("std Var[MMD^2] estimate:", vars.std())
        print("Var[MMD^2] estimates:", vars, sep='\n')
        print()
示例#26
0
def fid_score_by_folder(path):
    if not os.path.exists(path):
        raise RuntimeError('Invalid path: %s' % p)
    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[2048]

    model = InceptionV3([block_idx])
    if True:
        model.cuda()
    m, s = _compute_statistics_of_path(path, model, 50, 2048, True)
    return m, s
示例#27
0
    def __init__(self, hparams):
        super().__init__()
        self.hparams = hparams

        self.generator = DCGenerator(hparams.z_sz, hparams.gf_sz)
        self.discriminator = DCDiscriminator(hparams.df_sz)

        block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[hparams.fid_dims]
        self.inception = InceptionV3([block_idx]).eval()

        self.constant_z = nn.Parameter(torch.randn(64, hparams.z_sz, 1, 1))
示例#28
0
    def __init__(self):
        self.dims = 2048
        self.batch_size = 64
        self.cuda = True
        self.verbose = False

        block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[self.dims]
        self.model = InceptionV3([block_idx])
        if self.cuda:
            # TODO: put model into specific GPU
            self.model.cuda()
示例#29
0
def calcuate_fid_given_img(imageA, imageB):
    block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims]

    model = InceptionV3([block_idx])
    if cuda:
        model.cuda()
    m1, s1 = calculate_activation_statistics(imageA, model, 10, dims, cuda)
    m1, s1 = _compute_statistics_of_path(paths[0], model, 10, dims, cuda)
    m2, s2 = _compute_statistics_of_path(paths[1], model, 10, dims, cuda)
    fid_value = calculate_frechet_distance(m1, s1, m2, s2)

    return fid_value
示例#30
0
def get_activations(images,
                    model=InceptionV3([InceptionV3.BLOCK_INDEX_BY_DIM[2048]]),
                    batch_size=64,
                    dims=2048,
                    cuda=True,
                    verbose=False):
    """ 全画像に対して、pool3 layerの活性の大きさを計算
    Params:
    -- images      : 3次元のNumpy array
    -- model       : inception モデルのインスタンス
    -- batch_size  : imagesのnumpy arrayをバッチサイズに分割
    -- dims        : inception モデルから帰ってくる特徴量の次元の大きさ
    -- cuda        : GPUを使うか否か
    """

    model = model.cuda()
    model.eval()

    d0 = images.shape[0]
    if batch_size > d0:
        print(('Warning: batch size is bigger than the data size. '
               'Setting batch size to data size'))
        batch_size = d0

    n_batches = d0 // batch_size
    n_used_imgs = n_batches * batch_size

    pred_arr = np.empty((n_used_imgs, dims))
    for i in range(n_batches):
        if verbose:
            print('\rPropagating batch %d/%d' % (i + 1, n_batches),
                  end='',
                  flush=True)
        start = i * batch_size
        end = start + batch_size

        batch = Variable(images[start:end].type(torch.FloatTensor),
                         volatile=True)
        if cuda:
            batch = batch.cuda()

        pred = model(batch)[0]

        if pred.shape[2] != 1 or pred.shape[3] != 1:
            pred = adaptive_avg_pool2d(pred, output_size=(1, 1))

        pred_arr[start:end] = pred.cpu().data.numpy().reshape(batch_size, -1)

    if verbose:
        print(' done')

    return pred_arr