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

render_video.py 3.5 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
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
  1. """Script to render a video using a trained pi-GAN model."""
  2. import argparse
  3. import math
  4. import os
  5. from torchvision.utils import save_image
  6. import torch
  7. import numpy as np
  8. from PIL import Image
  9. from tqdm import tqdm
  10. import numpy as np
  11. import skvideo.io
  12. import curriculums
  13. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  14. parser = argparse.ArgumentParser()
  15. parser.add_argument('path', type=str)
  16. parser.add_argument('--seeds', nargs='+', default=[0, 1, 2])
  17. parser.add_argument('--output_dir', type=str, default='vids')
  18. parser.add_argument('--batch_size', type=int, default=1)
  19. parser.add_argument('--max_batch_size', type=int, default=2400000)
  20. parser.add_argument('--depth_map', action='store_true')
  21. parser.add_argument('--lock_view_dependence', action='store_true')
  22. parser.add_argument('--image_size', type=int, default=256)
  23. parser.add_argument('--ray_step_multiplier', type=int, default=2)
  24. parser.add_argument('--num_frames', type=int, default=36)
  25. parser.add_argument('--curriculum', type=str, default='CelebA')
  26. parser.add_argument('--trajectory', type=str, default='front')
  27. opt = parser.parse_args()
  28. os.makedirs(opt.output_dir, exist_ok=True)
  29. curriculum = getattr(curriculums, opt.curriculum)
  30. curriculum['num_steps'] = curriculum[0]['num_steps'] * opt.ray_step_multiplier
  31. curriculum['img_size'] = opt.image_size
  32. curriculum['psi'] = 0.7
  33. curriculum['v_stddev'] = 0
  34. curriculum['h_stddev'] = 0
  35. curriculum['lock_view_dependence'] = opt.lock_view_dependence
  36. curriculum['last_back'] = curriculum.get('eval_last_back', False)
  37. curriculum['num_frames'] = opt.num_frames
  38. curriculum['nerf_noise'] = 0
  39. curriculum = {key: value for key, value in curriculum.items() if type(key) is str}
  40. def tensor_to_PIL(img):
  41. img = img.squeeze() * 0.5 + 0.5
  42. return Image.fromarray(img.mul(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).to('cpu', torch.uint8).numpy())
  43. generator = torch.load(opt.path, map_location=device)
  44. ema_file = opt.path.split('generator')[0] + 'ema.pth'
  45. ema = torch.load(ema_file, map_location=device)
  46. ema.copy_to(generator.parameters())
  47. generator.set_device(device)
  48. generator.eval()
  49. if opt.trajectory == 'front':
  50. trajectory = []
  51. for t in np.linspace(0, 1, curriculum['num_frames']):
  52. pitch = 0.2 * np.cos(t * 2 * math.pi) + math.pi/2
  53. yaw = 0.4 * np.sin(t * 2 * math.pi) + math.pi/2
  54. fov = 12
  55. fov = 12 + 5 + np.sin(t * 2 * math.pi) * 5
  56. trajectory.append((pitch, yaw, fov))
  57. elif opt.trajectory == 'orbit':
  58. trajectory = []
  59. for t in np.linspace(0, 1, curriculum['num_frames']):
  60. pitch = math.pi/4
  61. yaw = t * 2 * math.pi
  62. fov = curriculum['fov']
  63. trajectory.append((pitch, yaw, fov))
  64. for seed in opt.seeds:
  65. frames = []
  66. depths = []
  67. output_name = f'{seed}.mp4'
  68. writer = skvideo.io.FFmpegWriter(os.path.join(opt.output_dir, output_name), outputdict={'-pix_fmt': 'yuv420p', '-crf': '21'})
  69. torch.manual_seed(seed)
  70. z = torch.randn(1, 256, device=device)
  71. with torch.no_grad():
  72. for pitch, yaw, fov in tqdm(trajectory):
  73. curriculum['h_mean'] = yaw
  74. curriculum['v_mean'] = pitch
  75. curriculum['fov'] = fov
  76. curriculum['h_stddev'] = 0
  77. curriculum['v_stddev'] = 0
  78. frame, depth_map = generator.staged_forward(z, max_batch_size=opt.max_batch_size, depth_map=opt.depth_map, **curriculum)
  79. frames.append(tensor_to_PIL(frame))
  80. for frame in frames:
  81. writer.writeFrame(np.array(frame))
  82. writer.close()
Tip!

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

Comments

Loading...