def __init__(self, config, device='cuda'): self.batch_size = config.batch_size self.image_size = config.image_size self.config = config self.device = device # self.flame = FLAME(self.config).to(self.device) self.flametex = FLAMETex(self.config).to(self.device) if config.model_name == 'flame': shape_prior = np.zeros((1, self.config.shape_params)) else: shape_prior = np.load(config.shape_factors_path) self.shape_prior = torch.Tensor(shape_prior.mean(0, keepdims=True)) eye = (2.0, 16.0) mouth = (2.0, 16.0) w68 = [(1.0, 1.0)] * 68 for i in range(36, 48): w68[i] = eye for i in range(48, 68): w68[i] = mouth self.weights68 = torch.tensor(w68, requires_grad=False).to(self.device) w51 = [(1.0, 1.0)] * 51 for i in range(36 - 17, 48 - 17): w51[i] = eye for i in range(48 - 17, 68 - 17): w51[i] = mouth self.weights51 = torch.tensor(w51, requires_grad=False).to(self.device) self._setup_renderer()
def __init__(self, config, device='cuda'): self.batch_size = config.batch_size self.image_size = config.image_size self.config = config self.device = device self.flame = FLAME(self.config).to(self.device) self.flametex = FLAMETex(self.config).to(self.device) self._setup_renderer()
def face_recon_FLAME(params_path, crop_size=256, img_size=224, device='cuda'): global config config = util.dict2obj(config) flame = FLAME(config).to(device) flametex = FLAMETex(config).to(device) mesh_file = '/data/yunfan.liu/Data_Preparation_Face_Swapping_Reenactment/3DMM_models/FLAME_Fitting/data/head_template_mesh.obj' render = Renderer(img_size, obj_filename=mesh_file).to(device) params = np.load(params_path, allow_pickle=True) shape = torch.from_numpy(params.item()['shape']).float().to(device) exp = torch.from_numpy(params.item()['exp']).float().to(device) pose = torch.from_numpy(params.item()['pose']).float().to(device) tex = torch.from_numpy(params.item()['tex']).float().to(device) light = torch.from_numpy(params.item()['lit']).float().to(device) verts = torch.from_numpy(params.item()['verts']).float().to(device) # trans_vertices vertices, _, _ = flame(shape_params=shape, expression_params=exp, pose_params=pose) albedos = flametex(tex) / 255. img_render = render(vertices, verts, albedos, light)['images'] return img_render
# dfr = DFRParamRegressor(config).to(device) # flame = FLAME(config).to(device) # flametex = FLAMETex(config).to(device) # mesh_file = './data/head_template_mesh.obj' # render = Renderer(config.image_size, obj_filename=mesh_file).to(device) dfr = DFRParamRegressor(config) if args.ckpt is not None: # g_ema.load_state_dict(torch.load(checkpoint_sg2)['g_ema'], strict=False) # g_ema = g_ema.to(device) # g_ema.eval(); dfr.load_state_dict(torch.load(args.ckpt)['dfr'], strict=False) dfr.eval() flame = FLAME(config) flametex = FLAMETex(config) mesh_file = './data/head_template_mesh.obj' render = Renderer(config.image_size, obj_filename=mesh_file) # # self._setup_renderer() # # mesh_file = './data/head_template_mesh.obj' # mesh_file = f'{path_photo_optim_repo}/data/head_template_mesh.obj' # render = Renderer(image_size, obj_filename=mesh_file).to(device) # if args.distributed: # dfr = nn.parallel.DistributedDataParallel( # dfr, # device_ids=[args.local_rank], # output_device=args.local_rank, # # broadcast_buffers=False # )