Skip to content

Commit

Permalink
[Fix] Fix potential circular import in core, data and utils, disconne…
Browse files Browse the repository at this point in the history
…ct unnecessary pytorch3d dependency (#190)

- Move mmhuman3d.core.visualization.renderer to mmhuman3d.core.renderer
- Split mmhuman3d.core.renderer.torch3d_renderer.builder into several seperated builders.
- Make __init__.py in mmhuman3d.core.renderer, mmhuman3d.data, mmhuman3d.utils empty.
- Allow pytorch3d import failure in mmhuman3d.utils.transforms, because the transforms.py is widely imported by many modules that do not need pytorch3d at all (transforms.py is introduced by demo_utils.py).
- Fix failed tests caused by the changes above.
- Accept mmcv 1.5.2.

Co-authored-by: gaoyang3 <gaoyang3@sensetime.com>
  • Loading branch information
LazyBusyYang and LazyBusyYang committed Jun 20, 2022
1 parent 4164f6a commit 21a8ce7
Show file tree
Hide file tree
Showing 48 changed files with 207 additions and 403 deletions.
4 changes: 2 additions & 2 deletions demo/estimate_smpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
inference_video_based_model,
init_model,
)
from mmhuman3d.core.visualization import visualize_smpl_hmr
from mmhuman3d.core.visualization.visualize_smpl import visualize_smpl_hmr
from mmhuman3d.data.data_structures.human_data import HumanData
from mmhuman3d.utils import array_to_images
from mmhuman3d.utils.demo_utils import (
extract_feature_sequence,
get_speed_up_interval,
Expand All @@ -28,6 +27,7 @@
speed_up_interpolate,
speed_up_process,
)
from mmhuman3d.utils.ffmpeg_utils import array_to_images
from mmhuman3d.utils.transforms import rotmat_to_aa

try:
Expand Down
5 changes: 2 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ def get_version():
]

autodoc_mock_imports = [
'mmhuman3d.version',
'mmhuman3d.core.visualization.renderer.torch3d_renderer', 'mmcv.ops',
'pytorch3d'
'mmhuman3d.version', 'mmhuman3d.core.renderer.torch3d_renderer',
'mmcv.ops', 'pytorch3d'
]

# Ignore >>> when copying code
Expand Down
10 changes: 5 additions & 5 deletions docs/render.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ renderer = MeshRenderer(
shader=SoftPhongShader(device=device, cameras=cameras, lights=lights))

### initialized by mmhuman3d
from mmhuman3d.core.visualization.renderer import MeshRenderer
from mmhuman3d.core.renderer.torch3d_renderer.builder import MeshRenderer
# rasterizer could be passed by nn.Module or dict
rasterizer = dict(
image_size=128,
Expand All @@ -60,7 +60,7 @@ shader = dict(type='SoftPhongShader')
These two methods are equal.
```python
import torch.nn as nn
from mmhuman3d.core.visualization.renderer import MeshRenderer, build_renderer
from mmhuman3d.core.renderer.torch3d_renderer.builder import MeshRenderer, build_renderer

renderer = MeshRenderer(shader=shader, device=device, rasterizer=rasterizer, resolution=resolution)
renderer = build_renderer(dict(type='mesh', device=device, shader=shader, rasterizer=rasterizer, resolution=resolution))
Expand All @@ -77,7 +77,7 @@ We provide `tensor2rgba` function for visualization, the returned tensor will be
The operation is simple:
```python
import torch
from mmhuman3d.core.visualization.renderer import build_renderer
from mmhuman3d.core.renderer.torch3d_renderer.builder import build_renderer

renderer = build_renderer(dict(type='mesh', device=device, resolution=resolution))
rendered_tensor = renderer(meshes=meshes, cameras=cameras, lights=lights)
Expand Down Expand Up @@ -107,7 +107,7 @@ You could pass your data by `render_runner` to render a series batch of render.

```python
import torch
from mmhuman3d.core.visualization.renderer import render_runner
from mmhuman3d.core.renderer.torch3d_renderer import render_runner

render_data = dict(cameras=cameras, lights=lights, meshes=meshes, backgrounds=backgrounds)
# no_grad=True for non-differentiable render
Expand All @@ -130,7 +130,7 @@ Warp a gray texture image to smpl_mesh.
import torch
from mmhuman3d.models.body_models.builder import build_body_model
from pytorch3d.structures import meshes
from mmhuman3d.core.visualization.renderer import build_renderer
from mmhuman3d.core.renderer.torch3d_renderer.builder import build_renderer
body_model = build_body_model(dict(type='smpl', model_path=model_path)).to(device)
pose_dict = body_model.tensor2dict(torch.zeros(1, 72))
verts = body_model(**pose_dict)['vertices']
Expand Down
10 changes: 5 additions & 5 deletions docs/visualize_smpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

You have smpl pose tensor or array shape of which is (frame, 72)
```python
from mmhuman3d.core.visualization import visualize_smpl_pose
from mmhuman3d.core.visualization.visualize_smpl import visualize_smpl_pose
body_model_config = dict(
type='smpl', model_path=model_path)
visualize_smpl_pose(
Expand All @@ -29,7 +29,7 @@
If you want to visualize a T-pose smpl or your poses do not have global_orient, you can do:
```python
import torch
from mmhuman3d.core.visualization import visualize_T_pose
from mmhuman3d.core.visualization.visualize_smpl import visualize_T_pose
body_model_config = dict(
type='smpl', model_path=model_path)
visualize_T_pose(
Expand All @@ -45,7 +45,7 @@
E.g., we use vibe sample_video.mp4 as an example.
```python
import pickle
from mmhuman3d.core.visualization import visualize_smpl_vibe
from mmhuman3d.core.visualization.visualize_smpl import visualize_smpl_vibe
with open('vibe_output.pkl', 'rb') as f:
d = pickle.load(f, encoding='latin1')
poses = d[1]['pose']
Expand Down Expand Up @@ -86,7 +86,7 @@
E.g., we use vibe sample_video.mp4 as an example.
```python
import pickle
from mmhuman3d.core.visualization import visualize_smpl_hmr
from mmhuman3d.core.visualization.visualize_smpl import visualize_smpl_hmr
gender = 'female'
focal_length = 5000
det_width = 224
Expand Down Expand Up @@ -142,7 +142,7 @@
- **visualize smpl with opencv camera:**
You should pass the opencv defined intrinsic matrix K and extrinsic matrix R, T.
```python
from mmhuman3d.core.visualization import visualize_smpl_calibration
from mmhuman3d.core.visualization.visualize_smpl import visualize_smpl_calibration
body_model_config = dict(
type='smpl', model_path=model_path, gender=gender)
visualize_smpl_calibration(
Expand Down
2 changes: 1 addition & 1 deletion mmhuman3d/apis/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from mmhuman3d.core.evaluation import DistEvalHook, EvalHook
from mmhuman3d.core.optimizer import build_optimizers
from mmhuman3d.data.datasets import build_dataloader, build_dataset
from mmhuman3d.utils import get_root_logger
from mmhuman3d.utils.logger import get_root_logger


def set_random_seed(seed, deterministic=False):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from mmhuman3d.core.conventions.cameras.convert_convention import \
enc_camera_convention # prevent yapf isort conflict
from mmhuman3d.utils import get_different_colors
from mmhuman3d.utils.demo_utils import get_different_colors
from mmhuman3d.utils.ffmpeg_utils import images_to_video
from mmhuman3d.utils.path_utils import check_path_suffix

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
from mmhuman3d.core.cameras import MMCamerasBase
from mmhuman3d.utils.ffmpeg_utils import images_to_gif, images_to_video
from mmhuman3d.utils.path_utils import check_path_suffix
from .builder import RENDERER, build_lights, build_shader
from .lights import build_lights
from .shader import build_shader
from .utils import normalize, rgb2bgr, tensor2array


@RENDERER.register_module(
name=['base', 'Base', 'base_renderer', 'BaseRenderer'])
class BaseRenderer(nn.Module):

def __init__(self,
Expand Down
48 changes: 48 additions & 0 deletions mmhuman3d/core/renderer/torch3d_renderer/builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from mmcv.utils import Registry

from .base_renderer import BaseRenderer
from .depth_renderer import DepthRenderer
from .mesh_renderer import MeshRenderer
from .normal_renderer import NormalRenderer
from .pointcloud_renderer import PointCloudRenderer
from .segmentation_renderer import SegmentationRenderer
from .silhouette_renderer import SilhouetteRenderer
from .uv_renderer import UVRenderer

RENDERER = Registry('renderer')
RENDERER.register_module(
name=['base', 'Base', 'base_renderer', 'BaseRenderer'],
module=BaseRenderer)
RENDERER.register_module(
name=['Depth', 'depth', 'depth_renderer', 'DepthRenderer'],
module=DepthRenderer)
RENDERER.register_module(
name=['Mesh', 'mesh', 'mesh_renderer', 'MeshRenderer'],
module=MeshRenderer)
RENDERER.register_module(
name=['Normal', 'normal', 'normal_renderer', 'NormalRenderer'],
module=NormalRenderer)
RENDERER.register_module(
name=[
'PointCloud', 'pointcloud', 'point_cloud', 'pointcloud_renderer',
'PointCloudRenderer'
],
module=PointCloudRenderer)
RENDERER.register_module(
name=[
'segmentation', 'segmentation_renderer', 'Segmentation',
'SegmentationRenderer'
],
module=SegmentationRenderer)
RENDERER.register_module(
name=[
'silhouette', 'silhouette_renderer', 'Silhouette', 'SilhouetteRenderer'
],
module=SilhouetteRenderer)
RENDERER.register_module(
name=['uv_renderer', 'uv', 'UV', 'UVRenderer'], module=UVRenderer)


def build_renderer(cfg):
"""Build renderers."""
return RENDERER.build(cfg)
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

from mmhuman3d.core.cameras import MMCamerasBase
from .base_renderer import BaseRenderer
from .builder import RENDERER, build_shader
from .shader import build_shader
from .utils import normalize


@RENDERER.register_module(
name=['Depth', 'depth', 'depth_renderer', 'DepthRenderer'])
class DepthRenderer(BaseRenderer):
"""Render depth map with the help of camera system."""
shader_type = 'DepthShader'
Expand Down
10 changes: 10 additions & 0 deletions mmhuman3d/core/renderer/torch3d_renderer/lights/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# yapf: disable
from .builder import ( # noqa: F401
AmbientLights,
DirectionalLights,
PointLights,
build_lights,
)
from .lights import MMLights # noqa: F401

# yapf: enable
17 changes: 17 additions & 0 deletions mmhuman3d/core/renderer/torch3d_renderer/lights/builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from mmcv.utils import Registry

from .lights import AmbientLights, DirectionalLights, PointLights # noqa:E401

LIGHTS = Registry('lights')
LIGHTS.register_module(
name=['directional', 'directional_lights', 'DirectionalLights'],
module=DirectionalLights)
LIGHTS.register_module(
name=['point', 'point_lights', 'PointLights'], module=PointLights)
LIGHTS.register_module(
name=['ambient', 'ambient_lights', 'AmbientLights'], module=AmbientLights)


def build_lights(cfg):
"""Build lights."""
return LIGHTS.build(cfg)
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
from pytorch3d.structures import Meshes

from mmhuman3d.core.cameras import MMCamerasBase
from mmhuman3d.core.visualization.renderer.torch3d_renderer.lights import \
MMLights # noqa: E501
from .base_renderer import BaseRenderer
from .builder import RENDERER
from .lights import MMLights


@RENDERER.register_module(
name=['Mesh', 'mesh', 'mesh_renderer', 'MeshRenderer'])
class MeshRenderer(BaseRenderer):
"""Render RGBA image with the help of camera system."""
shader_type = 'SoftPhongShader'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from mmhuman3d.utils.mesh_utils import \
join_meshes_as_batch as _join_meshes_as_batch
from .builder import build_renderer
from .textures import TexturesNearest
from .textures.textures import TexturesNearest
from .utils import align_input_to_padded


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@

from mmhuman3d.core.cameras import MMCamerasBase
from .base_renderer import BaseRenderer
from .builder import RENDERER
from .utils import normalize


@RENDERER.register_module(
name=['Normal', 'normal', 'normal_renderer', 'NormalRenderer'])
class NormalRenderer(BaseRenderer):
"""Render normal map with the help of camera system."""
shader_type = 'NormalShader'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@
from mmhuman3d.core.cameras import MMCamerasBase
from mmhuman3d.utils.mesh_utils import mesh_to_pointcloud_vc
from .base_renderer import BaseRenderer
from .builder import RENDERER


@RENDERER.register_module(name=[
'PointCloud', 'pointcloud', 'point_cloud', 'pointcloud_renderer',
'PointCloudRenderer'
])
class PointCloudRenderer(BaseRenderer):

def __init__(self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from mmhuman3d.core.cameras import MMCamerasBase
from mmhuman3d.core.cameras.builder import build_cameras
from .base_renderer import BaseRenderer
from .builder import build_lights, build_renderer
from .lights import AmbientLights, MMLights
from .builder import build_renderer
from .lights import AmbientLights, MMLights, build_lights

osj = os.path.join

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@
from pytorch3d.structures import Meshes

from mmhuman3d.core.cameras import MMCamerasBase
from mmhuman3d.utils import get_different_colors
from mmhuman3d.utils.demo_utils import get_different_colors
from .base_renderer import BaseRenderer
from .builder import RENDERER
from .utils import normalize


@RENDERER.register_module(name=[
'segmentation', 'segmentation_renderer', 'Segmentation',
'SegmentationRenderer'
])
class SegmentationRenderer(BaseRenderer):
"""Render segmentation map into a segmentation index tensor."""
shader_type = 'SegmentationShader'
Expand Down
16 changes: 16 additions & 0 deletions mmhuman3d/core/renderer/torch3d_renderer/shader/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# yapf: disable
from .builder import ( # noqa: F401
DepthShader,
HardFlatShader,
HardGouraudShader,
HardPhongShader,
NoLightShader,
NormalShader,
SegmentationShader,
SilhouetteShader,
SoftGouraudShader,
SoftPhongShader,
build_shader,
)

# yapf: enable

0 comments on commit 21a8ce7

Please sign in to comment.