Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

eval_metrics.py 1.9 KB

You have to be logged in to leave a comment. Sign In
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
  1. import os
  2. import shutil
  3. import torch
  4. import math
  5. from torch_fidelity import calculate_metrics
  6. from torchvision.utils import save_image
  7. from tqdm import tqdm
  8. import copy
  9. import argparse
  10. import shutil
  11. import curriculums
  12. if __name__ == '__main__':
  13. parser = argparse.ArgumentParser()
  14. parser.add_argument('generator_file', type=str)
  15. parser.add_argument('--real_image_dir', type=str, required=True)
  16. parser.add_argument('--output_dir', type=str, default='temp')
  17. parser.add_argument('--num_images', type=int, default=2048)
  18. parser.add_argument('--max_batch_size', type=int, default=94800000)
  19. parser.add_argument('--curriculum', type=str, default='CELEBA')
  20. opt = parser.parse_args()
  21. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  22. if os.path.exists(opt.output_dir) and os.path.isdir(opt.output_dir):
  23. shutil.rmtree(opt.output_dir)
  24. os.makedirs(opt.output_dir, exist_ok=False)
  25. generator = torch.load(opt.generator_file, map_location=device)
  26. generator.set_device(device)
  27. ema_file = opt.generator_file.split('generator')[0] + 'ema.pth'
  28. ema = torch.load(ema_file)
  29. ema.copy_to(generator.parameters())
  30. generator.eval()
  31. curriculum = curriculums.extract_metadata(getattr(curriculums, opt.curriculum), generator.step)
  32. curriculum['img_size'] = 128
  33. curriculum['psi'] = 1
  34. curriculum['last_back'] = curriculum.get('eval_last_back', False)
  35. curriculum['nerf_noise'] = 0
  36. for img_counter in tqdm(range(opt.num_images)):
  37. z = torch.randn(1, 256, device=device)
  38. with torch.no_grad():
  39. img = generator.staged_forward(z, max_batch_size=opt.max_batch_size, **curriculum)[0].to(device)
  40. save_image(img, os.path.join(opt.output_dir, f'{img_counter:0>5}.jpg'), normalize=True, range=(-1, 1))
  41. metrics_dict = calculate_metrics(opt.output_dir, opt.real_image_dir, cuda=True, isc=True, fid=True, kid=True, verbose=False)
  42. print(metrics_dict)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...