-
Notifications
You must be signed in to change notification settings - Fork 2
/
play_ground.py
140 lines (109 loc) · 4.39 KB
/
play_ground.py
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
import os
import torch
import numpy as np
from tqdm import tqdm_notebook
import imageio
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
import cv2
import scipy.ndimage
from skimage import img_as_ubyte
# Util function for loading meshes
from pytorch3d.io import load_objs_as_meshes
# io utils
from pytorch3d.io import load_obj
# datastructures
from pytorch3d.structures import Meshes, Textures
# 3D transformations functions
from pytorch3d.transforms import Rotate, Translate
# rendering components
from pytorch3d.renderer import (
OpenGLPerspectiveCameras, look_at_view_transform, look_at_rotation,
RasterizationSettings, MeshRenderer, MeshRasterizer, BlendParams,
SoftSilhouetteShader, HardPhongShader, PointLights,TexturedSoftPhongShader,
Materials
)
from config import *
from flame import FlameLandmarks
from utils.mesh_io import write_obj
ref_img_f = "/home/yam/arabastra/Israel/Tel_aviv/Yehoodit_5/common_ground/data/sentence01/sentence01.000001.26_C.jpg"
thresh = 9
target_img = cv2.imread(ref_img_f)
gray = cv2.cvtColor(target_img,cv2.COLOR_BGR2GRAY)
mask = np.where(gray >= thresh)
bin_img = np.zeros(gray.shape)
bin_img[mask] = 1
ref_silhouette = scipy.ndimage.morphology.binary_fill_holes(bin_img).astype(int)
ref_silhouette = ref_silhouette[:1024,:1024]
# Set the cuda device
device = torch.device("cuda:0")
torch.cuda.set_device(device)
config = get_config_with_default_args()
config.batch_size = 1
config.flame_model_path = './model/male_model.pkl'
# Initialize an OpenGL perspective camera.
cameras = OpenGLPerspectiveCameras(device=device)
flamelayer = FlameLandmarks(config)
flamelayer.cuda()
# To blend the 100 faces we set a few parameters which control the opacity and the sharpness of
# edges. Refer to blending.py for more details.
blend_params = BlendParams(sigma=1e-4, gamma=1e-4)
text_raster_settings = RasterizationSettings(
image_size=1024,
blur_radius=0.0,
faces_per_pixel=1,
)
silhouette_renderer = MeshRenderer(
rasterizer=MeshRasterizer(
cameras=cameras,
raster_settings=text_raster_settings
),
shader=SoftSilhouetteShader(blend_params=blend_params)
)
# show model silhouette using pytorch code
verts,_,_ = flamelayer()
# Initialize each vertex to be white in color.
verts_rgb = torch.ones_like(verts)[None] # (1, V, 3)
textures = Textures(verts_rgb=verts_rgb.to(device))
faces = torch.tensor(np.int32(flamelayer.faces),dtype=torch.long).cuda()
my_mesh = Meshes(
verts=[verts.to(device)],
faces=[faces.to(device)],
textures=textures
)
camera_position = nn.Parameter(
torch.from_numpy(np.array([1, 0.1, 0.1], dtype=np.float32)).to(my_mesh.device))
R = look_at_rotation(camera_position[None, :], device=device) # (1, 3, 3)
T = -torch.bmm(R.transpose(1, 2),camera_position[None, :, None])[:, :, 0] # (1, 3)
silhouete = silhouette_renderer(meshes_world=my_mesh.clone(), R=R, T=T)
silhouete = silhouete.detach().cpu().numpy()[...,3]
print(silhouete.shape)
plt.imshow(silhouete.squeeze())
plt.show()
class Model(nn.Module):
def __init__(self, meshes, renderer, image_ref):
super().__init__()
self.meshes = meshes
self.device = meshes.device
self.renderer = renderer
# Get the silhouette of the reference RGB image by finding all the non zero values.
image_ref = torch.from_numpy((image_ref[..., :3].max(-1) != 0).astype(np.float32))
self.register_buffer('image_ref', image_ref)
# Create an optimizable parameter for the x, y, z position of the camera.
self.camera_position = nn.Parameter(
torch.from_numpy(np.array([3.0, 6.9, +2.5], dtype=np.float32)).to(meshes.device))
def forward(self):
# Render the image using the updated camera position. Based on the new position of the
# camer we calculate the rotation and translation matrices
R = look_at_rotation(self.camera_position[None, :], device=self.device) # (1, 3, 3)
T = -torch.bmm(R.transpose(1, 2), self.camera_position[None, :, None])[:, :, 0] # (1, 3)
image = self.renderer(meshes_world=self.meshes.clone(), R=R, T=T)
# Calculate the silhouette loss
loss = torch.sum((image[..., 3] - self.image_ref) ** 2)
return loss, image
model = Model(meshes=my_mesh, renderer=silhouette_renderer, image_ref=ref_silhouette).to(device)
loss,_ = model()
print('loss', loss)
plt.imshow(silhouete.squeeze())
plt.show()