mirror of
https://github.com/facebookresearch/pytorch3d.git
synced 2025-08-02 20:02:49 +08:00
Remove _read_image from densepose example
Summary: As noted in #601, the example notebook was using an internal function _read_image from PyTorch3D, which has changed signature recently. It is not meant to be used externally. Switch to using PIL directly. Other changes: (1) removed unused skimage import. (2) some small tidyups. We now don't have places where cells modify values set by other cells. (3) removed bad calls to `plt.grid` which have no effect. Reviewed By: theschnitz, nikhilaravi Differential Revision: D27080372 fbshipit-source-id: 2fce651b3e5d7a4619f0a2b298c5db18c8fa1e2c
This commit is contained in:
parent
8e1bcd5568
commit
6c4151a820
@ -92,12 +92,11 @@
|
|||||||
"import os\n",
|
"import os\n",
|
||||||
"import torch\n",
|
"import torch\n",
|
||||||
"import matplotlib.pyplot as plt\n",
|
"import matplotlib.pyplot as plt\n",
|
||||||
"from skimage.io import imread\n",
|
|
||||||
"import numpy as np\n",
|
"import numpy as np\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# libraries for reading data from files\n",
|
"# libraries for reading data from files\n",
|
||||||
"from scipy.io import loadmat\n",
|
"from scipy.io import loadmat\n",
|
||||||
"from pytorch3d.io.utils import _read_image\n",
|
"from PIL import Image\n",
|
||||||
"import pickle\n",
|
"import pickle\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Data structures and functions for rendering\n",
|
"# Data structures and functions for rendering\n",
|
||||||
@ -184,13 +183,15 @@
|
|||||||
" data = pickle.load(f, encoding='latin1') \n",
|
" data = pickle.load(f, encoding='latin1') \n",
|
||||||
" v_template = torch.Tensor(data['v_template']).to(device) # (6890, 3)\n",
|
" v_template = torch.Tensor(data['v_template']).to(device) # (6890, 3)\n",
|
||||||
"ALP_UV = loadmat(data_filename)\n",
|
"ALP_UV = loadmat(data_filename)\n",
|
||||||
"tex = torch.from_numpy(_read_image(file_name=tex_filename, format='RGB') / 255. ).unsqueeze(0).to(device)\n",
|
"with Image.open(tex_filename) as image:\n",
|
||||||
|
" np_image = np.asarray(image.convert(\"RGB\")).astype(np.float32)\n",
|
||||||
|
"tex = torch.from_numpy(np_image / 255.)[None].to(device)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"verts = torch.from_numpy((ALP_UV[\"All_vertices\"]).astype(int)).squeeze().to(device) # (7829, 1)\n",
|
"verts = torch.from_numpy((ALP_UV[\"All_vertices\"]).astype(int)).squeeze().to(device) # (7829,)\n",
|
||||||
"U = torch.Tensor(ALP_UV['All_U_norm']).to(device) # (7829, 1)\n",
|
"U = torch.Tensor(ALP_UV['All_U_norm']).to(device) # (7829, 1)\n",
|
||||||
"V = torch.Tensor(ALP_UV['All_V_norm']).to(device) # (7829, 1)\n",
|
"V = torch.Tensor(ALP_UV['All_V_norm']).to(device) # (7829, 1)\n",
|
||||||
"faces = torch.from_numpy((ALP_UV['All_Faces'] - 1).astype(int)).to(device) # (13774, 3)\n",
|
"faces = torch.from_numpy((ALP_UV['All_Faces'] - 1).astype(int)).to(device) # (13774, 3)\n",
|
||||||
"face_indices = torch.Tensor(ALP_UV['All_FaceIndices']).squeeze()"
|
"face_indices = torch.Tensor(ALP_UV['All_FaceIndices']).squeeze() # (13774,)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -202,7 +203,6 @@
|
|||||||
"# Display the texture image\n",
|
"# Display the texture image\n",
|
||||||
"plt.figure(figsize=(10, 10))\n",
|
"plt.figure(figsize=(10, 10))\n",
|
||||||
"plt.imshow(tex.squeeze(0).cpu())\n",
|
"plt.imshow(tex.squeeze(0).cpu())\n",
|
||||||
"plt.grid(\"off\");\n",
|
|
||||||
"plt.axis(\"off\");"
|
"plt.axis(\"off\");"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -228,6 +228,9 @@
|
|||||||
" part = rows * i + j + 1 # parts are 1-indexed in face_indices\n",
|
" part = rows * i + j + 1 # parts are 1-indexed in face_indices\n",
|
||||||
" offset_per_part[part] = (u, v)\n",
|
" offset_per_part[part] = (u, v)\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"U_norm = U.clone()\n",
|
||||||
|
"V_norm = V.clone()\n",
|
||||||
|
"\n",
|
||||||
"# iterate over faces and offset the corresponding vertex u and v values\n",
|
"# iterate over faces and offset the corresponding vertex u and v values\n",
|
||||||
"for i in range(len(faces)):\n",
|
"for i in range(len(faces)):\n",
|
||||||
" face_vert_idxs = faces[i]\n",
|
" face_vert_idxs = faces[i]\n",
|
||||||
@ -238,15 +241,15 @@
|
|||||||
" # vertices are reused, but we don't want to offset multiple times\n",
|
" # vertices are reused, but we don't want to offset multiple times\n",
|
||||||
" if vert_idx.item() not in already_offset:\n",
|
" if vert_idx.item() not in already_offset:\n",
|
||||||
" # offset u value\n",
|
" # offset u value\n",
|
||||||
" U[vert_idx] = U[vert_idx] / cols + offset_u\n",
|
" U_norm[vert_idx] = U[vert_idx] / cols + offset_u\n",
|
||||||
" # offset v value\n",
|
" # offset v value\n",
|
||||||
" # this also flips each part locally, as each part is upside down\n",
|
" # this also flips each part locally, as each part is upside down\n",
|
||||||
" V[vert_idx] = (1 - V[vert_idx]) / rows + offset_v\n",
|
" V_norm[vert_idx] = (1 - V[vert_idx]) / rows + offset_v\n",
|
||||||
" # add vertex to our set tracking offsetted vertices\n",
|
" # add vertex to our set tracking offsetted vertices\n",
|
||||||
" already_offset.add(vert_idx.item())\n",
|
" already_offset.add(vert_idx.item())\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# invert V values\n",
|
"# invert V values\n",
|
||||||
"U_norm, V_norm = U, 1 - V"
|
"V_norm = 1 - V_norm"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -263,10 +266,7 @@
|
|||||||
"# Therefore when initializing the Meshes class,\n",
|
"# Therefore when initializing the Meshes class,\n",
|
||||||
"# we need to map each of the vertices referenced by the DensePose faces (in verts, which is the \"All_vertices\" field)\n",
|
"# we need to map each of the vertices referenced by the DensePose faces (in verts, which is the \"All_vertices\" field)\n",
|
||||||
"# to the correct xyz coordinate in the SMPL template mesh.\n",
|
"# to the correct xyz coordinate in the SMPL template mesh.\n",
|
||||||
"v_template_extended = torch.stack(list(map(lambda vert: v_template[vert-1], verts))).unsqueeze(0).to(device) # (1, 7829, 3)\n",
|
"v_template_extended = v_template[verts-1][None] # (1, 7829, 3)"
|
||||||
"\n",
|
|
||||||
"# add a batch dimension to faces\n",
|
|
||||||
"faces = faces.unsqueeze(0)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -286,8 +286,8 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"texture = TexturesUV(maps=tex, faces_uvs=faces, verts_uvs=verts_uv)\n",
|
"texture = TexturesUV(maps=tex, faces_uvs=faces[None], verts_uvs=verts_uv)\n",
|
||||||
"mesh = Meshes(v_template_extended, faces, texture)"
|
"mesh = Meshes(v_template_extended, faces[None], texture)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -352,7 +352,6 @@
|
|||||||
"images = renderer(mesh)\n",
|
"images = renderer(mesh)\n",
|
||||||
"plt.figure(figsize=(10, 10))\n",
|
"plt.figure(figsize=(10, 10))\n",
|
||||||
"plt.imshow(images[0, ..., :3].cpu().numpy())\n",
|
"plt.imshow(images[0, ..., :3].cpu().numpy())\n",
|
||||||
"plt.grid(\"off\");\n",
|
|
||||||
"plt.axis(\"off\");"
|
"plt.axis(\"off\");"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -393,7 +392,6 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"plt.figure(figsize=(10, 10))\n",
|
"plt.figure(figsize=(10, 10))\n",
|
||||||
"plt.imshow(images[0, ..., :3].cpu().numpy())\n",
|
"plt.imshow(images[0, ..., :3].cpu().numpy())\n",
|
||||||
"plt.grid(\"off\");\n",
|
|
||||||
"plt.axis(\"off\");"
|
"plt.axis(\"off\");"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user