Spaces:
Running
on
Zero
Running
on
Zero
File size: 16,862 Bytes
458efe2 |
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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
import sys
import os
import os.path
import random
import scipy
import scipy.sparse.linalg as sla
# ^^^ we NEED to import scipy before torch, or it crashes :(
# (observed on Ubuntu 20.04 w/ torch 1.6.0 and scipy 1.5.2 installed via conda)
import numpy as np
import torch
import torch.nn as nn
def to_basis(values, basis, massvec):
"""
Transform data in to an orthonormal basis (where orthonormal is wrt to massvec)
Inputs:
- values: (B,V,D)
- basis: (B,V,K)
- massvec: (B,V)
Outputs:
- (B,K,D) transformed values
"""
basisT = basis.transpose(-2, -1)
return torch.matmul(basisT, values * massvec.unsqueeze(-1))
def from_basis(values, basis):
"""
Transform data out of an orthonormal basis
Inputs:
- values: (K,D)
- basis: (V,K)
Outputs:
- (V,D) reconstructed values
"""
if values.is_complex() or basis.is_complex():
return utils.cmatmul(utils.ensure_complex(basis), utils.ensure_complex(values))
else:
return torch.matmul(basis, values)
class LearnedTimeDiffusion(nn.Module):
"""
Applies diffusion with learned per-channel t.
In the spectral domain this becomes
f_out = e ^ (lambda_i t) f_in
Inputs:
- values: (V,C) in the spectral domain
- L: (V,V) sparse laplacian
- evals: (K) eigenvalues
- mass: (V) mass matrix diagonal
(note: L/evals may be omitted as None depending on method)
Outputs:
- (V,C) diffused values
"""
def __init__(self, C_inout, method='spectral'):
super(LearnedTimeDiffusion, self).__init__()
self.C_inout = C_inout
self.diffusion_time = nn.Parameter(torch.Tensor(C_inout)) # (C)
self.method = method # one of ['spectral', 'implicit_dense']
nn.init.constant_(self.diffusion_time, 0.0)
def forward(self, x, L, mass, evals, evecs):
# project times to the positive halfspace
# (and away from 0 in the incredibly rare chance that they get stuck)
with torch.no_grad():
self.diffusion_time.data = torch.clamp(self.diffusion_time, min=1e-8)
if x.shape[-1] != self.C_inout:
raise ValueError("Tensor has wrong shape = {}. Last dim shape should have number of channels = {}".format(
x.shape, self.C_inout))
if self.method == 'spectral':
# Transform to spectral
x_spec = to_basis(x, evecs, mass)
# Diffuse
time = self.diffusion_time
diffusion_coefs = torch.exp(-evals.unsqueeze(-1) * time.unsqueeze(0))
x_diffuse_spec = diffusion_coefs * x_spec
# Transform back to per-vertex
x_diffuse = from_basis(x_diffuse_spec, evecs)
elif self.method == 'implicit_dense':
V = x.shape[-2]
# Form the dense matrices (M + tL) with dims (B,C,V,V)
mat_dense = L.to_dense().unsqueeze(1).expand(-1, self.C_inout, V, V).clone()
mat_dense *= self.diffusion_time.unsqueeze(0).unsqueeze(-1).unsqueeze(-1)
mat_dense += torch.diag_embed(mass).unsqueeze(1)
# Factor the system
cholesky_factors = torch.linalg.cholesky(mat_dense)
# Solve the system
rhs = x * mass.unsqueeze(-1)
rhsT = torch.transpose(rhs, 1, 2).unsqueeze(-1)
sols = torch.cholesky_solve(rhsT, cholesky_factors)
x_diffuse = torch.transpose(sols.squeeze(-1), 1, 2)
else:
raise ValueError("unrecognized method")
return x_diffuse
class SpatialGradientFeatures(nn.Module):
"""
Compute dot-products between input vectors. Uses a learned complex-linear layer to keep dimension down.
Input:
- vectors: (V,C,2)
Output:
- dots: (V,C) dots
"""
def __init__(self, C_inout, with_gradient_rotations=True):
super(SpatialGradientFeatures, self).__init__()
self.C_inout = C_inout
self.with_gradient_rotations = with_gradient_rotations
if (self.with_gradient_rotations):
self.A_re = nn.Linear(self.C_inout, self.C_inout, bias=False)
self.A_im = nn.Linear(self.C_inout, self.C_inout, bias=False)
else:
self.A = nn.Linear(self.C_inout, self.C_inout, bias=False)
# self.norm = nn.InstanceNorm1d(C_inout)
def forward(self, vectors):
vectorsA = vectors # (V,C)
if self.with_gradient_rotations:
vectorsBreal = self.A_re(vectors[..., 0]) - self.A_im(vectors[..., 1])
vectorsBimag = self.A_re(vectors[..., 1]) + self.A_im(vectors[..., 0])
else:
vectorsBreal = self.A(vectors[..., 0])
vectorsBimag = self.A(vectors[..., 1])
dots = vectorsA[..., 0] * vectorsBreal + vectorsA[..., 1] * vectorsBimag
return torch.tanh(dots)
class MiniMLP(nn.Sequential):
'''
A simple MLP with configurable hidden layer sizes.
'''
def __init__(self, layer_sizes, dropout=False, activation=nn.ReLU, name="miniMLP"):
super(MiniMLP, self).__init__()
for i in range(len(layer_sizes) - 1):
is_last = (i + 2 == len(layer_sizes))
if dropout and i > 0:
self.add_module(name + "_mlp_layer_dropout_{:03d}".format(i), nn.Dropout(p=.5))
# Affine map
self.add_module(
name + "_mlp_layer_{:03d}".format(i),
nn.Linear(
layer_sizes[i],
layer_sizes[i + 1],
),
)
# Nonlinearity
# (but not on the last layer)
if not is_last:
self.add_module(name + "_mlp_act_{:03d}".format(i), activation())
class DiffusionNetBlock(nn.Module):
"""
Inputs and outputs are defined at vertices
"""
def __init__(self,
C_width,
mlp_hidden_dims,
dropout=True,
diffusion_method='spectral',
with_gradient_features=True,
with_gradient_rotations=True):
super(DiffusionNetBlock, self).__init__()
# Specified dimensions
self.C_width = C_width
self.mlp_hidden_dims = mlp_hidden_dims
self.dropout = dropout
self.with_gradient_features = with_gradient_features
self.with_gradient_rotations = with_gradient_rotations
# Diffusion block
self.diffusion = LearnedTimeDiffusion(self.C_width, method=diffusion_method)
self.MLP_C = 2 * self.C_width
if self.with_gradient_features:
self.gradient_features = SpatialGradientFeatures(self.C_width, with_gradient_rotations=self.with_gradient_rotations)
self.MLP_C += self.C_width
# MLPs
self.mlp = MiniMLP([self.MLP_C] + self.mlp_hidden_dims + [self.C_width], dropout=self.dropout)
def forward(self, x_in, mass, L, evals, evecs, gradX, gradY):
# Manage dimensions
B = x_in.shape[0] # batch dimension
if x_in.shape[-1] != self.C_width:
raise ValueError("Tensor has wrong shape = {}. Last dim shape should have number of channels = {}".format(
x_in.shape, self.C_width))
# Diffusion block
x_diffuse = self.diffusion(x_in, L, mass, evals, evecs)
# Compute gradient features, if using
if self.with_gradient_features:
# Compute gradients
x_grads = [
] # Manually loop over the batch (if there is a batch dimension) since torch.mm() doesn't support batching
for b in range(B):
# gradient after diffusion
x_gradX = torch.mm(gradX[b, ...], x_diffuse[b, ...])
x_gradY = torch.mm(gradY[b, ...], x_diffuse[b, ...])
x_grads.append(torch.stack((x_gradX, x_gradY), dim=-1))
x_grad = torch.stack(x_grads, dim=0)
# Evaluate gradient features
x_grad_features = self.gradient_features(x_grad)
# Stack inputs to mlp
feature_combined = torch.cat((x_in, x_diffuse, x_grad_features), dim=-1)
else:
# Stack inputs to mlp
feature_combined = torch.cat((x_in, x_diffuse), dim=-1)
# Apply the mlp
x0_out = self.mlp(feature_combined)
# Skip connection
x0_out = x0_out + x_in
return x0_out
class DiffusionNet(nn.Module):
def __init__(self,
C_in,
C_out,
C_width=128,
N_block=4,
last_activation=None,
outputs_at='vertices',
mlp_hidden_dims=None,
dropout=True,
with_gradient_features=True,
with_gradient_rotations=True,
diffusion_method='spectral',
num_eigenbasis=128):
"""
Construct a DiffusionNet.
Parameters:
C_in (int): input dimension
C_out (int): output dimension
last_activation (func) a function to apply to the final outputs of the network, such as torch.nn.functional.log_softmax (default: None)
outputs_at (string) produce outputs at various mesh elements by averaging from vertices. One of ['vertices', 'edges', 'faces', 'global_mean']. (default 'vertices', aka points for a point cloud)
C_width (int): dimension of internal DiffusionNet blocks (default: 128)
N_block (int): number of DiffusionNet blocks (default: 4)
mlp_hidden_dims (list of int): a list of hidden layer sizes for MLPs (default: [C_width, C_width])
dropout (bool): if True, internal MLPs use dropout (default: True)
diffusion_method (string): how to evaluate diffusion, one of ['spectral', 'implicit_dense']. If implicit_dense is used, can set k_eig=0, saving precompute.
with_gradient_features (bool): if True, use gradient features (default: True)
with_gradient_rotations (bool): if True, use gradient also learn a rotation of each gradient. Set to True if your surface has consistently oriented normals, and False otherwise (default: True)
num_eigenbasis (int): for trunking the eigenvalues eigenvectors
"""
super(DiffusionNet, self).__init__()
## Store parameters
# Basic parameters
self.C_in = C_in
self.C_out = C_out
self.C_width = C_width
self.N_block = N_block
# Outputs
self.last_activation = last_activation
self.outputs_at = outputs_at
if outputs_at not in ['vertices', 'edges', 'faces', 'global_mean']:
raise ValueError("invalid setting for outputs_at")
# MLP options
if mlp_hidden_dims == None:
mlp_hidden_dims = [C_width, C_width]
self.mlp_hidden_dims = mlp_hidden_dims
self.dropout = dropout
# Diffusion
self.diffusion_method = diffusion_method
if diffusion_method not in ['spectral', 'implicit_dense']:
raise ValueError("invalid setting for diffusion_method")
self.num_eigenbasis = num_eigenbasis
# Gradient features
self.with_gradient_features = with_gradient_features
self.with_gradient_rotations = with_gradient_rotations
## Set up the network
# First and last affine layers
self.first_lin = nn.Linear(C_in, C_width)
self.last_lin = nn.Linear(C_width, C_out)
# DiffusionNet blocks
self.blocks = []
for i_block in range(self.N_block):
block = DiffusionNetBlock(C_width=C_width,
mlp_hidden_dims=mlp_hidden_dims,
dropout=dropout,
diffusion_method=diffusion_method,
with_gradient_features=with_gradient_features,
with_gradient_rotations=with_gradient_rotations)
self.blocks.append(block)
self.add_module("block_" + str(i_block), self.blocks[-1])
def forward(self, x_in, mass, L=None, evals=None, evecs=None, gradX=None, gradY=None, edges=None, faces=None):
"""
A forward pass on the DiffusionNet.
In the notation below, dimension are:
- C is the input channel dimension (C_in on construction)
- C_OUT is the output channel dimension (C_out on construction)
- N is the number of vertices/points, which CAN be different for each forward pass
- B is an OPTIONAL batch dimension
- K_EIG is the number of eigenvalues used for spectral acceleration
Generally, our data layout it is [N,C] or [B,N,C].
Call get_operators() to generate geometric quantities mass/L/evals/evecs/gradX/gradY. Note that depending on the options for the DiffusionNet, not all are strictly necessary.
Parameters:
x_in (tensor): Input features, dimension [N,C] or [B,N,C]
mass (tensor): Mass vector, dimension [N] or [B,N]
L (tensor): Laplace matrix, sparse tensor with dimension [N,N] or [B,N,N]
evals (tensor): Eigenvalues of Laplace matrix, dimension [K_EIG] or [B,K_EIG]
evecs (tensor): Eigenvectors of Laplace matrix, dimension [N,K_EIG] or [B,N,K_EIG]
gradX (tensor): Half of gradient matrix, sparse real tensor with dimension [N,N] or [B,N,N]
gradY (tensor): Half of gradient matrix, sparse real tensor with dimension [N,N] or [B,N,N]
Returns:
x_out (tensor): Output with dimension [N,C_out] or [B,N,C_out]
"""
## Check dimensions, and append batch dimension if not given
if x_in.shape[-1] != self.C_in:
raise ValueError("DiffusionNet was constructed with C_in={}, but x_in has last dim={}".format(
self.C_in, x_in.shape[-1]))
N = x_in.shape[-2]
if len(x_in.shape) == 2:
appended_batch_dim = True
# add a batch dim to all inputs
x_in = x_in.unsqueeze(0)
mass = mass.unsqueeze(0)
if L != None:
L = L.unsqueeze(0)
if evals != None:
evals = evals.unsqueeze(0)
if evecs != None:
evecs = evecs.unsqueeze(0)
if gradX != None:
gradX = gradX.unsqueeze(0)
if gradY != None:
gradY = gradY.unsqueeze(0)
if edges != None:
edges = edges.unsqueeze(0)
if faces != None:
faces = faces.unsqueeze(0)
elif len(x_in.shape) == 3:
appended_batch_dim = False
else:
raise ValueError("x_in should be tensor with shape [N,C] or [B,N,C]")
evals = evals[..., :self.num_eigenbasis]
evecs = evecs[..., :self.num_eigenbasis]
# Apply the first linear layer
x = self.first_lin(x_in)
# Apply each of the blocks
for b in self.blocks:
x = b(x, mass, L, evals, evecs, gradX, gradY)
# Apply the last linear layer
x = self.last_lin(x)
# Remap output to faces/edges if requested
if self.outputs_at == 'vertices':
x_out = x
elif self.outputs_at == 'edges':
# Remap to edges
x_gather = x.unsqueeze(-1).expand(-1, -1, -1, 2)
edges_gather = edges.unsqueeze(2).expand(-1, -1, x.shape[-1], -1)
xe = torch.gather(x_gather, 1, edges_gather)
x_out = torch.mean(xe, dim=-1)
elif self.outputs_at == 'faces':
# Remap to faces
x_gather = x.unsqueeze(-1).expand(-1, -1, -1, 3)
faces_gather = faces.unsqueeze(2).expand(-1, -1, x.shape[-1], -1)
xf = torch.gather(x_gather, 1, faces_gather)
x_out = torch.mean(xf, dim=-1)
elif self.outputs_at == 'global_mean':
# Produce a single global mean ouput.
# Using a weighted mean according to the point mass/area is discretization-invariant.
# (A naive mean is not discretization-invariant; it could be affected by sampling a region more densely)
x_out = torch.sum(x * mass.unsqueeze(-1), dim=-2) / torch.sum(mass, dim=-1, keepdim=True)
# Apply last nonlinearity if specified
if self.last_activation != None:
x_out = self.last_activation(x_out)
# Remove batch dim if we added it
if appended_batch_dim:
x_out = x_out.squeeze(0)
return x_out
|