Spaces:
Sleeping
Sleeping
File size: 9,799 Bytes
b807ddb |
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# from https://gitlab.tuebingen.mpg.de/mkocabas/projects/-/blob/master/pare/pare/utils/diff_renderer.py
import torch
import numpy as np
import torch.nn as nn
from pytorch3d.renderer import (
PerspectiveCameras,
RasterizationSettings,
DirectionalLights,
BlendParams,
HardFlatShader,
MeshRasterizer,
TexturesVertex,
TexturesAtlas
)
from pytorch3d.structures import Meshes
from .image_utils import get_default_camera
from .smpl_uv import get_tenet_texture
class MeshRendererWithDepth(nn.Module):
"""
A class for rendering a batch of heterogeneous meshes. The class should
be initialized with a rasterizer and shader class which each have a forward
function.
"""
def __init__(self, rasterizer, shader):
super().__init__()
self.rasterizer = rasterizer
self.shader = shader
def forward(self, meshes_world, **kwargs) -> torch.Tensor:
"""
Render a batch of images from a batch of meshes by rasterizing and then
shading.
NOTE: If the blur radius for rasterization is > 0.0, some pixels can
have one or more barycentric coordinates lying outside the range [0, 1].
For a pixel with out of bounds barycentric coordinates with respect to a
face f, clipping is required before interpolating the texture uv
coordinates and z buffer so that the colors and depths are limited to
the range for the corresponding face.
"""
fragments = self.rasterizer(meshes_world, **kwargs)
images = self.shader(fragments, meshes_world, **kwargs)
mask = (fragments.zbuf > -1).float()
zbuf = fragments.zbuf.view(images.shape[0], -1)
# print(images.shape, zbuf.shape)
depth = (zbuf - zbuf.min(-1, keepdims=True).values) / \
(zbuf.max(-1, keepdims=True).values - zbuf.min(-1, keepdims=True).values)
depth = depth.reshape(*images.shape[:3] + (1,))
images = torch.cat([images[:, :, :, :3], mask, depth], dim=-1)
return images
class DifferentiableRenderer(nn.Module):
def __init__(
self,
img_h,
img_w,
focal_length,
device='cuda',
background_color=(0.0, 0.0, 0.0),
texture_mode='smplpix',
vertex_colors=None,
face_textures=None,
smpl_faces=None,
is_train=False,
is_cam_batch=False,
):
super(DifferentiableRenderer, self).__init__()
self.x = 'a'
self.img_h = img_h
self.img_w = img_w
self.device = device
self.focal_length = focal_length
K, R = get_default_camera(focal_length, img_h, img_w, is_cam_batch=is_cam_batch)
K, R = K.to(device), R.to(device)
# T = torch.tensor([[0, 0, 2.5 * self.focal_length / max(self.img_h, self.img_w)]]).to(device)
if is_cam_batch:
T = torch.zeros((K.shape[0], 3)).to(device)
else:
T = torch.tensor([[0.0, 0.0, 0.0]]).to(device)
self.background_color = background_color
self.renderer = None
smpl_faces = smpl_faces
if texture_mode == 'smplpix':
face_colors = get_tenet_texture(mode=texture_mode).to(device).float()
vertex_colors = torch.from_numpy(
np.load(f'data/smpl/{texture_mode}_vertex_colors.npy')[:,:3]
).unsqueeze(0).to(device).float()
if texture_mode == 'partseg':
vertex_colors = vertex_colors[..., :3].unsqueeze(0).to(device)
face_colors = face_textures.to(device)
if texture_mode == 'deco':
vertex_colors = vertex_colors[..., :3].to(device)
face_colors = face_textures.to(device)
self.register_buffer('K', K)
self.register_buffer('R', R)
self.register_buffer('T', T)
self.register_buffer('face_colors', face_colors)
self.register_buffer('vertex_colors', vertex_colors)
self.register_buffer('smpl_faces', smpl_faces)
self.set_requires_grad(is_train)
def set_requires_grad(self, val=False):
self.K.requires_grad_(val)
self.R.requires_grad_(val)
self.T.requires_grad_(val)
self.face_colors.requires_grad_(val)
self.vertex_colors.requires_grad_(val)
# check if smpl_faces is a FloatTensor as requires_grad_ is not defined for LongTensor
if isinstance(self.smpl_faces, torch.FloatTensor):
self.smpl_faces.requires_grad_(val)
def forward(self, vertices, faces=None, R=None, T=None):
raise NotImplementedError
class Pytorch3D(DifferentiableRenderer):
def __init__(
self,
img_h,
img_w,
focal_length,
device='cuda',
background_color=(0.0, 0.0, 0.0),
texture_mode='smplpix',
vertex_colors=None,
face_textures=None,
smpl_faces=None,
model_type='smpl',
is_train=False,
is_cam_batch=False,
):
super(Pytorch3D, self).__init__(
img_h,
img_w,
focal_length,
device=device,
background_color=background_color,
texture_mode=texture_mode,
vertex_colors=vertex_colors,
face_textures=face_textures,
smpl_faces=smpl_faces,
is_train=is_train,
is_cam_batch=is_cam_batch,
)
# this R converts the camera from pyrender NDC to
# OpenGL coordinate frame. It is basicall R(180, X) x R(180, Y)
# I manually defined it here for convenience
self.R = self.R @ torch.tensor(
[[[ -1.0, 0.0, 0.0],
[ 0.0, -1.0, 0.0],
[ 0.0, 0.0, 1.0]]],
dtype=self.R.dtype, device=self.R.device,
)
if is_cam_batch:
focal_length = self.focal_length
else:
focal_length = self.focal_length[None, :]
principal_point = ((self.img_w // 2, self.img_h // 2),)
image_size = ((self.img_h, self.img_w),)
cameras = PerspectiveCameras(
device=self.device,
focal_length=focal_length,
principal_point=principal_point,
R=self.R,
T=self.T,
in_ndc=False,
image_size=image_size,
)
for param in cameras.parameters():
param.requires_grad_(False)
raster_settings = RasterizationSettings(
image_size=(self.img_h, self.img_w),
blur_radius=0.0,
max_faces_per_bin=20000,
faces_per_pixel=1,
)
lights = DirectionalLights(
device=self.device,
ambient_color=((1.0, 1.0, 1.0),),
diffuse_color=((0.0, 0.0, 0.0),),
specular_color=((0.0, 0.0, 0.0),),
direction=((0, 1, 0),),
)
blend_params = BlendParams(background_color=self.background_color)
shader = HardFlatShader(device=self.device,
cameras=cameras,
blend_params=blend_params,
lights=lights)
self.textures = TexturesVertex(verts_features=self.vertex_colors)
self.renderer = MeshRendererWithDepth(
rasterizer=MeshRasterizer(
cameras=cameras,
raster_settings=raster_settings
),
shader=shader,
)
def forward(self, vertices, faces=None, R=None, T=None, face_atlas=None):
batch_size = vertices.shape[0]
if faces is None:
faces = self.smpl_faces.expand(batch_size, -1, -1)
if R is None:
R = self.R.expand(batch_size, -1, -1)
if T is None:
T = self.T.expand(batch_size, -1)
# convert camera translation to pytorch3d coordinate frame
T = torch.bmm(R, T.unsqueeze(-1)).squeeze(-1)
vertex_textures = TexturesVertex(
verts_features=self.vertex_colors.expand(batch_size, -1, -1)
)
# face_textures needed because vertex_texture cause interpolation at boundaries
if face_atlas:
face_textures = TexturesAtlas(atlas=face_atlas)
else:
face_textures = TexturesAtlas(atlas=self.face_colors)
# we may need to rotate the mesh
meshes = Meshes(verts=vertices, faces=faces, textures=face_textures)
images = self.renderer(meshes, R=R, T=T)
images = images.permute(0, 3, 1, 2)
return images
class NeuralMeshRenderer(DifferentiableRenderer):
def __init__(self, *args, **kwargs):
import neural_renderer as nr
super(NeuralMeshRenderer, self).__init__(*args, **kwargs)
self.neural_renderer = nr.Renderer(
dist_coeffs=None,
orig_size=self.img_size,
image_size=self.img_size,
light_intensity_ambient=1,
light_intensity_directional=0,
anti_aliasing=False,
)
def forward(self, vertices, faces=None, R=None, T=None):
batch_size = vertices.shape[0]
if faces is None:
faces = self.smpl_faces.expand(batch_size, -1, -1)
if R is None:
R = self.R.expand(batch_size, -1, -1)
if T is None:
T = self.T.expand(batch_size, -1)
rgb, depth, mask = self.neural_renderer(
vertices,
faces,
textures=self.face_colors.expand(batch_size, -1, -1, -1, -1, -1),
K=self.K.expand(batch_size, -1, -1),
R=R,
t=T.unsqueeze(1),
)
return torch.cat([rgb, depth.unsqueeze(1), mask.unsqueeze(1)], dim=1) |