MCPcopy
hub / github.com/SizheAn/PanoHead / synthesis

Method synthesis

training/triplane.py:82–139  ·  view source on GitHub ↗
(self, ws, c, neural_rendering_resolution=None, update_emas=False, 
            ws_bcg=None, cache_backbone=False, use_cached_backbone=False, **synthesis_kwargs)

Source from the content-addressed store, hash-verified

80 return c_new, delta_c
81
82 def synthesis(self, ws, c, neural_rendering_resolution=None, update_emas=False,
83 ws_bcg=None, cache_backbone=False, use_cached_backbone=False, **synthesis_kwargs):
84 cam2world_matrix = c[:, :16].view(-1, 4, 4)
85 intrinsics = c[:, 16:25].view(-1, 3, 3)
86
87 if neural_rendering_resolution is None:
88 neural_rendering_resolution = self.neural_rendering_resolution
89 else:
90 self.neural_rendering_resolution = neural_rendering_resolution
91
92 # Create a batch of rays for volume rendering
93 ray_origins, ray_directions = self.ray_sampler(cam2world_matrix, intrinsics, neural_rendering_resolution)
94
95 # Create triplanes by running StyleGAN backbone
96 N, M, _ = ray_origins.shape
97 if use_cached_backbone and self._last_planes is not None:
98 planes = self._last_planes
99 else:
100 planes = self.backbone.synthesis(ws, update_emas=update_emas, **synthesis_kwargs)
101 if cache_backbone:
102 self._last_planes = planes
103
104 # Reshape output into three D*32-channel planes, where D=self.rendering_kwargs['triplane_depth'], defines the depth of the tri-grid
105 planes = planes.view(len(planes), 3, 32 * self.rendering_kwargs['triplane_depth'], planes.shape[-2], planes.shape[-1])
106
107 # Perform volume rendering
108 feature_samples, depth_samples, weights_samples = self.renderer(planes, self.decoder, ray_origins, ray_directions, self.rendering_kwargs) # channels last
109
110 # Reshape into 'raw' neural-rendered image
111 H = W = self.neural_rendering_resolution
112 feature_image = feature_samples.permute(0, 2, 1).reshape(N, feature_samples.shape[-1], H, W).contiguous()
113 depth_image = depth_samples.permute(0, 2, 1).reshape(N, 1, H, W)
114 weights_samples = weights_samples.permute(0, 2, 1).reshape(N, 1, H, W)
115
116 # Run superresolution to get final image
117 if self.decoder.activation == "sigmoid":
118 feature_image = feature_image * 2 - 1 # Scale to (-1, 1), taken from ray marcher
119 # Generate Background
120 if self.bcg_synthesis:
121 ws_bcg = ws[:,:self.bcg_synthesis.num_ws] if ws_bcg is None else ws_bcg[:,:self.bcg_synthesis.num_ws]
122 if ws_bcg.size(1) < self.bcg_synthesis.num_ws:
123 ws_bcg = torch.cat([ws_bcg, ws_bcg[:,-1:].repeat(1,self.bcg_synthesis.num_ws-ws_bcg.size(1),1)], 1)
124 bcg_image = self.bcg_synthesis(ws_bcg, update_emas=update_emas, **synthesis_kwargs)
125 bcg_image = torch.nn.functional.interpolate(bcg_image, size=feature_image.shape[2:],
126 mode='bilinear', align_corners=False, antialias=self.rendering_kwargs['sr_antialias'])
127 feature_image = feature_image + (1-weights_samples) * bcg_image
128
129 # Generate Raw image
130 if self.torgb:
131 rgb_image = self.torgb(feature_image, ws[:,-1], fused_modconv=False)
132 rgb_image = rgb_image.to(dtype=torch.float32, memory_format=torch.contiguous_format)
133 else:
134 rgb_image = feature_image[:, :3]
135 # Run superresolution to get final image
136 sr_image = self.superresolution(rgb_image, feature_image, ws, noise_mode=self.rendering_kwargs['superresolution_noise_mode'], **{k:synthesis_kwargs[k] for k in synthesis_kwargs.keys() if k != 'noise_mode'})
137
138 mask_image = weights_samples*(1 + 2*0.001) - 0.001
139 return {'image': sr_image, 'image_raw': rgb_image, 'image_depth': depth_image, "image_mask": mask_image}

Callers 15

forwardMethod · 0.95
gen_interp_videoFunction · 0.80
generate_imagesFunction · 0.80
gen_interp_videoFunction · 0.80
generate_imagesFunction · 0.80
gen_interp_videoFunction · 0.80
projectFunction · 0.80
project_ptiFunction · 0.80
run_projectionFunction · 0.80
gen_interp_videoFunction · 0.80
projectFunction · 0.80
project_ptiFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected