Spaces:
Running
on
L40S
Running
on
L40S
File size: 19,143 Bytes
2252f3d |
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 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# -*- coding: utf-8 -*-
# Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V. (MPG) is
# holder of all proprietary rights on this computer program.
# You can only use this computer program if you have closed
# a license agreement with MPG or you get the right to use the computer
# program from someone who is authorized to grant you that right.
# Any use of the computer program without a valid license is prohibited and
# liable to prosecution.
#
# Copyright©2019 Max-Planck-Gesellschaft zur Förderung
# der Wissenschaften e.V. (MPG). acting on behalf of its Max Planck Institute
# for Intelligent Systems. All rights reserved.
#
# Contact: ps-license@tuebingen.mpg.de
#from lib.net.voxelize import Voxelization
from lib.renderer.mesh import compute_normal_batch
from lib.dataset.mesh_util import feat_select, read_smpl_constants, surface_field_deformation
from lib.net.NormalNet import NormalNet
from lib.net.MLP import MLP, DeformationMLP, TransformerEncoderLayer, SDF2Density, SDF2Occ
from lib.net.spatial import SpatialEncoder
from lib.dataset.PointFeat import PointFeat
from lib.dataset.mesh_util import SMPLX
from lib.net.VE import VolumeEncoder
from lib.net.ResBlkPIFuNet import ResnetFilter
from lib.net.UNet import UNet
from lib.net.HGFilters import *
from lib.net.Transformer import ViTVQ
from termcolor import colored
from lib.net.BasePIFuNet import BasePIFuNet
import torch.nn as nn
import torch
import numpy as np
import matplotlib.pyplot as plt
import torch.nn.functional as F
from lib.net.nerf_util import raw2outputs
def normalize(tensor):
min_val = tensor.min()
max_val = tensor.max()
normalized_tensor = (tensor - min_val) / (max_val - min_val)
return normalized_tensor
def visualize_feature_map(feature_map, title, filename):
feature_map=feature_map.permute(0, 2, 3, 1)
# 选择一个样本(如果有多个)
sample_index = 0
sample = feature_map[sample_index]
# 选择一个通道(如果有多个)
channel_index = 0
channel = sample[:, :, channel_index]
channel= normalize(channel)
plt.imshow(channel.cpu().numpy(), cmap='hot')
# plt.title(title)
# plt.colorbar()
plt.axis('off')
plt.savefig(filename, dpi=300,bbox_inches='tight', pad_inches=0) # 保存图片到文件
plt.close() # 关闭图形,释放资源
class HGPIFuNet(BasePIFuNet):
"""
HG PIFu network uses Hourglass stacks as the image filter.
It does the following:
1. Compute image feature stacks and store it in self.im_feat_list
self.im_feat_list[-1] is the last stack (output stack)
2. Calculate calibration
3. If training, it index on every intermediate stacks,
If testing, it index on the last stack.
4. Classification.
5. During training, error is calculated on all stacks.
"""
def __init__(self,
cfg,
projection_mode="orthogonal",
error_term=nn.MSELoss()):
super(HGPIFuNet, self).__init__(projection_mode=projection_mode,
error_term=error_term)
self.l1_loss = nn.SmoothL1Loss()
self.opt = cfg.net
self.root = cfg.root
self.overfit = cfg.overfit
channels_IF = self.opt.mlp_dim
self.use_filter = self.opt.use_filter
self.prior_type = self.opt.prior_type
self.smpl_feats = self.opt.smpl_feats
self.smpl_dim = self.opt.smpl_dim
self.voxel_dim = self.opt.voxel_dim
self.hourglass_dim = self.opt.hourglass_dim
self.in_geo = [item[0] for item in self.opt.in_geo]
self.in_nml = [item[0] for item in self.opt.in_nml]
self.in_geo_dim = sum([item[1] for item in self.opt.in_geo])
self.in_nml_dim = sum([item[1] for item in self.opt.in_nml])
self.in_total = self.in_geo + self.in_nml
self.smpl_feat_dict = None
self.smplx_data = SMPLX()
image_lst = [0, 1, 2]
normal_F_lst = [0, 1, 2] if "image" not in self.in_geo else [3, 4, 5]
normal_B_lst = [3, 4, 5] if "image" not in self.in_geo else [6, 7, 8]
# only ICON or ICON-Keypoint use visibility
if self.prior_type in ["icon", "keypoint"]:
if "image" in self.in_geo:
self.channels_filter = [
image_lst + normal_F_lst,
image_lst + normal_B_lst,
]
else:
self.channels_filter = [normal_F_lst, normal_B_lst]
else:
if "image" in self.in_geo:
self.channels_filter = [
image_lst + normal_F_lst + normal_B_lst
]
else:
self.channels_filter = [normal_F_lst + normal_B_lst]
use_vis = (self.prior_type in ["icon", "keypoint"
]) and ("vis" in self.smpl_feats)
if self.prior_type in ["pamir", "pifu"]:
use_vis = 1
if self.use_filter:
channels_IF[0] = (self.hourglass_dim) * (2 - use_vis)
else:
channels_IF[0] = len(self.channels_filter[0]) * (2 - use_vis)
if self.prior_type in ["icon", "keypoint"]:
channels_IF[0] += self.smpl_dim
elif self.prior_type == "pifu":
channels_IF[0] += 1
else:
print(f"don't support {self.prior_type}!")
self.base_keys = ["smpl_verts", "smpl_faces"]
self.icon_keys = self.base_keys + [
f"smpl_{feat_name}" for feat_name in self.smpl_feats
]
self.keypoint_keys = self.base_keys + [
f"smpl_{feat_name}" for feat_name in self.smpl_feats
]
self.pamir_keys = [
"voxel_verts", "voxel_faces", "pad_v_num", "pad_f_num"
]
self.pifu_keys = []
# channels_IF[0]+=self.hourglass_dim
# self.if_regressor = MLP(
# filter_channels=channels_IF,
# name="if",
# res_layers=self.opt.res_layers,
# norm=self.opt.norm_mlp,
# last_op=nn.Sigmoid() if not cfg.test_mode else None,
# )
self.deform_dim=64
#self.image_filter = ResnetFilter(self.opt, norm_layer=norm_type)
#self.image_filter = UNet(3,128)
# self.xy_plane_filter=ResnetFilter(self.opt, norm_layer=norm_type)
# self.yz_plane_filter=ViTVQ(image_size=512) # ResnetFilter(self.opt, norm_layer=norm_type)
# self.xz_plane_filter=ViTVQ(image_size=512)
self.image_filter=ViTVQ(image_size=512,channels=9)
# self.deformation_mlp=DeformationMLP(input_dim=self.deform_dim,opt=self.opt)
self.mlp=TransformerEncoderLayer(skips=4,multires=6,opt=self.opt)
# self.sdf2density=SDF2Density()
# self.sdf2occ=SDF2Occ()
self.color_loss=nn.L1Loss()
self.sp_encoder = SpatialEncoder()
self.step=0
self.features_costume=None
# network
if self.use_filter:
if self.opt.gtype == "HGPIFuNet":
self.F_filter = HGFilter(self.opt, self.opt.num_stack,
len(self.channels_filter[0]))
# self.refine_filter = FuseHGFilter(self.opt, self.opt.num_stack,
# len(self.channels_filter[0]))
else:
print(
colored(f"Backbone {self.opt.gtype} is unimplemented",
"green"))
summary_log = (f"{self.prior_type.upper()}:\n" +
f"w/ Global Image Encoder: {self.use_filter}\n" +
f"Image Features used by MLP: {self.in_geo}\n")
if self.prior_type == "icon":
summary_log += f"Geometry Features used by MLP: {self.smpl_feats}\n"
summary_log += f"Dim of Image Features (local): {3 if (use_vis and not self.use_filter) else 6}\n"
summary_log += f"Dim of Geometry Features (ICON): {self.smpl_dim}\n"
elif self.prior_type == "keypoint":
summary_log += f"Geometry Features used by MLP: {self.smpl_feats}\n"
summary_log += f"Dim of Image Features (local): {3 if (use_vis and not self.use_filter) else 6}\n"
summary_log += f"Dim of Geometry Features (Keypoint): {self.smpl_dim}\n"
elif self.prior_type == "pamir":
summary_log += f"Dim of Image Features (global): {self.hourglass_dim}\n"
summary_log += f"Dim of Geometry Features (PaMIR): {self.voxel_dim}\n"
else:
summary_log += f"Dim of Image Features (global): {self.hourglass_dim}\n"
summary_log += f"Dim of Geometry Features (PIFu): 1 (z-value)\n"
summary_log += f"Dim of MLP's first layer: {channels_IF[0]}\n"
print(colored(summary_log, "yellow"))
self.normal_filter = NormalNet(cfg)
init_net(self, init_type="normal")
def get_normal(self, in_tensor_dict):
# insert normal features
if (not self.training) and (not self.overfit):
# print(colored("infer normal","blue"))
with torch.no_grad():
feat_lst = []
if "image" in self.in_geo:
feat_lst.append(
in_tensor_dict["image"]) # [1, 3, 512, 512]
if "normal_F" in self.in_geo and "normal_B" in self.in_geo:
if ("normal_F" not in in_tensor_dict.keys()
or "normal_B" not in in_tensor_dict.keys()):
(nmlF, nmlB) = self.normal_filter(in_tensor_dict)
else:
nmlF = in_tensor_dict["normal_F"]
nmlB = in_tensor_dict["normal_B"]
feat_lst.append(nmlF) # [1, 3, 512, 512]
feat_lst.append(nmlB) # [1, 3, 512, 512]
in_filter = torch.cat(feat_lst, dim=1)
else:
in_filter = torch.cat([in_tensor_dict[key] for key in self.in_geo],
dim=1)
return in_filter
def get_mask(self, in_filter, size=128):
mask = (F.interpolate(
in_filter[:, self.channels_filter[0]],
size=(size, size),
mode="bilinear",
align_corners=True,
).abs().sum(dim=1, keepdim=True) != 0.0)
return mask
def filter(self, in_tensor_dict, return_inter=False):
"""
Filter the input images
store all intermediate features.
:param images: [B, C, H, W] input images
"""
in_filter = self.get_normal(in_tensor_dict)
image= in_tensor_dict["image"]
fuse_image=torch.cat([image,in_filter], dim=1)
smpl_normals={
"T_normal_B":in_tensor_dict['normal_B'],
"T_normal_R":in_tensor_dict['T_normal_R'],
"T_normal_L":in_tensor_dict['T_normal_L']
}
features_G = []
# self.smpl_normal=in_tensor_dict['T_normal_L']
if self.prior_type in ["icon", "keypoint"]:
if self.use_filter:
triplane_features = self.image_filter(fuse_image,smpl_normals)
features_F = self.F_filter(in_filter[:,
self.channels_filter[0]]
) # [(B,hg_dim,128,128) * 4]
features_B = self.F_filter(in_filter[:,
self.channels_filter[1]]
) # [(B,hg_dim,128,128) * 4]
else:
assert 0
F_plane_feat,B_plane_feat,R_plane_feat,L_plane_feat=triplane_features
refine_F_plane_feat=F_plane_feat
features_G.append(refine_F_plane_feat)
features_G.append(B_plane_feat)
features_G.append(R_plane_feat)
features_G.append(L_plane_feat)
features_G.append(torch.cat([features_F[-1],features_B[-1]], dim=1))
else:
assert 0
self.smpl_feat_dict = {
k: in_tensor_dict[k] if k in in_tensor_dict.keys() else None
for k in getattr(self, f"{self.prior_type}_keys")
}
if 'animated_smpl_verts' not in in_tensor_dict.keys():
self.point_feat_extractor = PointFeat(self.smpl_feat_dict["smpl_verts"],
self.smpl_feat_dict["smpl_faces"])
else:
assert 0
self.features_G = features_G
# If it is not in training, only produce the last im_feat
if not self.training:
features_out = features_G
else:
features_out = features_G
if return_inter:
return features_out, in_filter
else:
return features_out
def query(self, features, points, calibs, transforms=None,type='shape'):
xyz = self.projection(points, calibs, transforms) # project to image plane
(xy, z) = xyz.split([2, 1], dim=1)
zy=torch.cat([xyz[:,2:3],xyz[:,1:2]],dim=1)
in_cube = (xyz > -1.0) & (xyz < 1.0)
in_cube = in_cube.all(dim=1, keepdim=True).detach().float()
preds_list = []
if self.prior_type in ["icon", "keypoint"]:
densely_smpl=self.smpl_feat_dict['smpl_verts'].permute(0,2,1)
#smpl_origin=self.projection(densely_smpl, torch.inverse(calibs), transforms)
smpl_vis=self.smpl_feat_dict['smpl_vis'].permute(0,2,1)
#verts_ids=self.smpl_feat_dict['smpl_sample_id']
(smpl_xy,smpl_z)=densely_smpl.split([2,1],dim=1)
smpl_zy=torch.cat([densely_smpl[:,2:3],densely_smpl[:,1:2]],dim=1)
point_feat_out = self.point_feat_extractor.query( # this extractor changes if has animated smpl
xyz.permute(0, 2, 1).contiguous(), self.smpl_feat_dict)
vis=point_feat_out['vis'].permute(0,2,1)
#sdf_body=-point_feat_out['sdf'] # this sdf needs to be multiplied by -1
feat_lst = [
point_feat_out[key] for key in self.smpl_feats
if key in point_feat_out.keys()
]
smpl_feat = torch.cat(feat_lst, dim=2).permute(0, 2, 1)
if len(features)==5:
F_plane_feat1,F_plane_feat2=features[0].chunk(2,dim=1)
B_plane_feat1,B_plane_feat2=features[1].chunk(2,dim=1)
R_plane_feat1,R_plane_feat2=features[2].chunk(2,dim=1)
L_plane_feat1,L_plane_feat2=features[3].chunk(2,dim=1)
in_feat=features[4]
F_feat=self.index(F_plane_feat1,xy)
B_feat=self.index(B_plane_feat1,xy)
R_feat=self.index(R_plane_feat1,zy)
L_feat=self.index(L_plane_feat1,zy)
normal_feat=feat_select(self.index(in_feat, xy),vis)
three_plane_feat=(B_feat+R_feat+L_feat)/3
triplane_feat=torch.cat([F_feat,three_plane_feat],dim=1) # 32+32=64
### smpl query ###
smpl_F_feat=self.index(F_plane_feat2,smpl_xy)
smpl_B_feat=self.index(B_plane_feat2,smpl_xy)
smpl_R_feat=self.index(R_plane_feat2,smpl_zy)
smpl_L_feat=self.index(L_plane_feat2,smpl_zy)
smpl_three_plane_feat=(smpl_B_feat+smpl_R_feat+smpl_L_feat)/3
smpl_triplane_feat=torch.cat([smpl_F_feat,smpl_three_plane_feat],dim=1) # 32+32=64
bary_centric_feat=self.point_feat_extractor.query_barycentirc_feats(xyz.permute(0,2,1).contiguous()
,smpl_triplane_feat.permute(0,2,1))
final_feat=torch.cat([triplane_feat,bary_centric_feat.permute(0,2,1),normal_feat],dim=1) # 64+64+6=134
if self.features_costume is not None:
assert 0
if type=='shape':
if 'animated_smpl_verts' in self.smpl_feat_dict.keys():
animated_smpl=self.smpl_feat_dict['animated_smpl_verts']
occ=self.mlp(xyz.permute(0,2,1).contiguous(),animated_smpl,
final_feat,smpl_feat,training=self.training,type=type)
else:
occ=self.mlp(xyz.permute(0,2,1).contiguous(),densely_smpl.permute(0,2,1),
final_feat,smpl_feat,training=self.training,type=type)
occ=occ*in_cube
preds_list.append(occ)
elif type=='color':
if 'animated_smpl_verts' in self.smpl_feat_dict.keys():
animated_smpl=self.smpl_feat_dict['animated_smpl_verts']
color_preds=self.mlp(xyz.permute(0,2,1).contiguous(),animated_smpl,
final_feat,smpl_feat,training=self.training,type=type)
else:
color_preds=self.mlp(xyz.permute(0,2,1).contiguous(),densely_smpl.permute(0,2,1),
final_feat,smpl_feat,training=self.training,type=type)
preds_list.append(color_preds)
return preds_list
def get_error(self, preds_if_list, labels):
"""calculate error
Args:
preds_list (list): list of torch.tensor(B, 3, N)
labels (torch.tensor): (B, N_knn, N)
Returns:
torch.tensor: error
"""
error_if = 0
for pred_id in range(len(preds_if_list)):
pred_if = preds_if_list[pred_id]
error_if += F.binary_cross_entropy(pred_if, labels)
error_if /= len(preds_if_list)
return error_if
def forward(self, in_tensor_dict):
sample_tensor = in_tensor_dict["sample"]
calib_tensor = in_tensor_dict["calib"]
label_tensor = in_tensor_dict["label"]
color_sample=in_tensor_dict["sample_color"]
color_label=in_tensor_dict["color"]
in_feat = self.filter(in_tensor_dict)
preds_if_list = self.query(in_feat,
sample_tensor,
calib_tensor,type='shape')
BCEloss = self.get_error(preds_if_list, label_tensor)
color_preds=self.query(in_feat,
color_sample,
calib_tensor,type='color')
color_loss=self.color_loss(color_preds[0],color_label)
if self.training:
self.color3d_loss= color_loss
error=BCEloss+color_loss
self.grad_loss=torch.tensor(0.).float().to(BCEloss.device)
else:
error=BCEloss
return preds_if_list[-1].detach(), error
|