forked from microsoft/unilm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_fid_fidelity.py
More file actions
133 lines (123 loc) · 4.68 KB
/
evaluate_fid_fidelity.py
File metadata and controls
133 lines (123 loc) · 4.68 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import argparse
import json
import os
import numpy as np
import torch
from torchvision import transforms
from torchvision.datasets import ImageFolder
import torch_fidelity
from utils import center_crop_arr, safe_blob_write
def parse_args():
parser = argparse.ArgumentParser(description="Simple example of a training script.")
parser.add_argument(
"--seed",
type=int,
default=0,
help="A seed to use for the random number generator. Can be negative to not set a seed.",
)
parser.add_argument(
"--model",
type=str,
default="Transformer-L",
help="The config of the UNet model to train, leave as None to use standard DDPM configuration.",
)
parser.add_argument(
"--vae",
type=str,
default=None,
)
parser.add_argument("--train_data_dir", type=str, default="/tmp/ILSVRC/Data/CLS-LOC/train", help="A folder containing the training data.")
parser.add_argument(
"--ref_stat_path",
type=str,
default="/mnt/unilm/hangbo/beit3/t2i/assets/fid_stats/imagenet_256_val.npz",
)
parser.add_argument(
"--image_size",
type=int,
default=256,
help=(
"The image_size for input images, all the images in the train/validation dataset will be resized to this"
" image_size"
),
)
parser.add_argument(
"--batch_size", type=int, default=32, help="Batch size (per device) for the training dataloader."
)
parser.add_argument(
"--steps_per_class", type=int, default=50, help="Number of steps per class."
)
parser.add_argument("--use_ema", action="store_true", help="Whether to use Exponential Moving Average for the final model weights.")
parser.add_argument("--ddpm_num_steps", type=int, default=1000)
parser.add_argument("--ddpm_num_inference_steps", type=int, default=250)
parser.add_argument("--ddpm_beta_schedule", type=str, default="cosine", help="The beta schedule to use for DDPM.")
parser.add_argument("--prediction_type", type=str, default="epsilon", help="Whether the model should predict the 'epsilon'/noise error or directly the reconstructed image 'x0'.")
parser.add_argument("--cfg-scale", type=float, default=4.0)
parser.add_argument(
"--checkpoint",
type=str,
default=None,
help=(
"Whether training should be resumed from a previous checkpoint. Use a path saved by"
' `--checkpointing_steps`, or `"latest"` to automatically select the last available checkpoint.'
),
)
args = parser.parse_args()
return args
class ImageDataset(torch.utils.data.Dataset):
def __init__(self, images):
self.images = images
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
return self.images[idx]
class RefImageDataset(torch.utils.data.Dataset):
def __init__(self, dataset):
self.dataset = dataset
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
item = self.dataset[idx]
item = np.array(item[0])
item = torch.from_numpy(item).permute(2, 0, 1)
return item
@torch.no_grad()
def main(args):
prefix = "ema" if args.use_ema else "standard"
exp_name = f"{prefix}_{args.steps_per_class}_{args.cfg_scale}_{args.ddpm_beta_schedule}_{args.ddpm_num_inference_steps}"
print(f"Exp_name {exp_name}")
image_path = os.path.join(args.checkpoint, f"images_{exp_name}.npz")
print(f"Computing fidelity metrics from {image_path}...")
images = np.load(image_path)["arr_0"]
images = torch.from_numpy(images).permute(0, 3, 1, 2)
print(images.shape)
dataset = ImageDataset(images)
ref_dataset = ImageFolder(args.train_data_dir, transform=transforms.Lambda(lambda pil_image: center_crop_arr(pil_image, args.image_size)))
ref_dataset = RefImageDataset(ref_dataset)
metrics_dict = torch_fidelity.calculate_metrics(
input1=dataset,
input2=ref_dataset,
batch_size=args.batch_size,
cuda=True,
isc=True,
fid=True,
kid=False,
prc=False,
save_cpu_ram=True,
verbose=True,
)
print(metrics_dict)
# metrics_dict = torch_fidelity.calculate_metrics(
# input1=dataset,
# input2=ref_dataset,
# batch_size=args.batch_size,
# cuda=True,
# prc=True,
# prc_batch_size=args.batch_size,
# save_cpu_ram=True,
# verbose=True,
# )
# print(metrics_dict)
if __name__ == "__main__":
args = parse_args()
main(args)