Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/moverseai/moai
Browse files Browse the repository at this point in the history
  • Loading branch information
moversekostas committed Jun 18, 2024
2 parents c4be76c + 22a981a commit e21f473
Show file tree
Hide file tree
Showing 20 changed files with 222 additions and 37 deletions.
2 changes: 1 addition & 1 deletion docker/serve/azure/pt220-cu118/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ghcr.io/moverseai/moai/serve:v1.0.0-pt220-cu118
FROM ghcr.io/moverseai/moai/serve:v1.1.0-pt220-cu118

USER root

Expand Down
6 changes: 6 additions & 0 deletions moai/conf/data/train/iterator/window.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# @package data.train.iterator

_target_: moai.data.iterator.Windowed
window_size: ???
stride: 1
internal_stride: 1
3 changes: 3 additions & 0 deletions moai/conf/engine/modules/rerun.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ world_coordinates: RUF
add_floor: true
root: /
memory_limit: 75% #NOTE: can also be absolute, `4GB` for example
log:
handler: moai/logs # null
level: ${hydra:hydra_logging.root.level}
20 changes: 16 additions & 4 deletions moai/conf/examples/smplifyx/flows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,28 @@ _moai_:
_out_: [init_trans]
preprocess:
embedding:
void: [color]
void:
- ${mi:"color[0:1, 1:2, :, :]"}
_out_: [embedding]
translation:
void: [color]
void:
- ${mi:"sq(color, 0)"}
_out_: [translation]
betas:
void: [color]
void:
- ${mi:"flatten(color, 1)"}
_out_: [betas]
_mi_1:
expression:
- ${mi:"embedding + zeros(embedding)"}
- ${mi:"embedding_1 / ones(embedding)"}
- ${mi:"flatten(embedding_2, 1)"}
- ${mi:"sq(embedding_3, 0)"}
- ${mi:"view(embedding_4, 1, 32)"}
_out_:
[embedding_1, embedding_2, embedding_3, embedding_4, embedding_5]
vposer1: # vposer2:
decode: [embedding]
decode: [embedding_5] # [embedding]
_out_: [decoded]
smplx:
pose: [decoded.pose]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @package model.monads._name_
# @package model.monads.mv_weak_perspective_camera

_target_: moai.monads.geometry.MVWeakPerspective
# focal_length: 5000.0
Expand Down
2 changes: 1 addition & 1 deletion moai/conf/model/monads/geometry/projection/project.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @package model.monads._name_
# @package model.monads.project

_target_: moai.monads.geometry.Projection
xyz_at: channel # one of ['channel', 'row]
Expand Down
2 changes: 1 addition & 1 deletion moai/conf/model/monads/geometry/rotation/conversion.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @package model.monads._name_
# @package model.monads.conversion

_target_: moai.monads.geometry.rotations.ConvertRotation
src: ??? # from representation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @package model.monads._name_
# @package model.monads.transform

_target_: moai.monads.geometry.Transformation
xyz_in_at: channel # one of ['channel', 'row]
Expand Down
4 changes: 0 additions & 4 deletions moai/conf/model/monads/human/body/forward_kinematics.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion moai/conf/model/monads/networks/autoencoder.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @package model.monads.autoencoder

_target_: moai.monads.networks.autoencoder.AutoEncoder
_target_: moai.monads.networks._factory.autoencoder.AutoEncoder
checkpoint: ???
2 changes: 1 addition & 1 deletion moai/conf/model/monads/tensor/stack.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @package model.monads._name_
# @package model.monads.stack

_target_: moai.monads.tensor.Stack
dim: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @package model.parameters.groups.retargetting

_target_: moai.parameters.selectors.model.ModelParameterSelector
monads:
- main_flow.pose_params
- main_flow.translation_params
force_grad: true
22 changes: 22 additions & 0 deletions moai/engine/modules/rerun.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import logging
import typing

import colour
import numpy as np
import toolz
from omegaconf.omegaconf import DictConfig

from moai.utils.color.colormap import random_color
from moai.utils.funcs import get

try:
import rerun as rr
Expand All @@ -16,6 +19,15 @@
__all__ = ["Rerun"]


class RerunFilter(logging.Filter):
def __init__(self) -> None:
super().__init__("rerun")

def filter(self, record):
record.msg = record.msg.removeprefix(":moai: ")
return record


class Rerun:
r"""
This logger should support all the different types of rr logging.
Expand All @@ -39,6 +51,7 @@ def __init__(
add_floor: bool = True,
root: str = "/",
memory_limit: str = "75%",
log: DictConfig = {},
) -> None:
# NOTE: https://github.com/iterative/dvc/issues/9731
rr.init(name)
Expand All @@ -62,6 +75,15 @@ def __init__(
if add_floor:
self._create_floor(root)
# NOTE: add more global gizmos (e.g. tr-axis)
if handler_name := get(log, "handler"):
handler = rr.LoggingHandler(handler_name)
handler.addFilter(RerunFilter())
logging.getLogger().addHandler(handler)
if level := get(log, "level"):
if isinstance(level, str):
level = level.upper()
level = logging.getLevelName(level)
logging.getLogger().setLevel(level)

def _create_scalar_plots(self, root: str, plots) -> None:
for plot in plots:
Expand Down
5 changes: 4 additions & 1 deletion moai/engine/progressbar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import rich.progress
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import RichProgressBar
from pytorch_lightning.callbacks.progress.rich_progress import RichProgressBarTheme

Expand All @@ -22,6 +23,8 @@
# NOTE: check https://github.com/Textualize/rich/discussions/482
# NOTE: check https://github.com/facebookresearch/EGG/blob/a139946a73d45553360a7f897626d1ae20759f12/egg/core/callbacks.py#L335
# NOTE: check https://github.com/Textualize/rich/discussions/921


class MoaiProgressBar(RichProgressBar):
def __init__(self) -> None:
super().__init__(
Expand All @@ -39,7 +42,7 @@ def __init__(self) -> None:
# CustomTimeColumn(style=self.theme.time),
# ProcessingSpeedColumn(style=self.theme.processing_speed),
# ]
def configure_columns(self, trainer: "pl.Trainer") -> list:
def configure_columns(self, trainer: Trainer) -> list:
original = super().configure_columns(trainer)
moai_column = rich.progress.TextColumn(":moai:")
spinner_column = rich.progress.SpinnerColumn(spinner_name="dots5")
Expand Down
53 changes: 47 additions & 6 deletions moai/monads/generation/tensor/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import omegaconf.omegaconf
import toolz
import torch

__all__ = [
Expand All @@ -15,6 +16,7 @@
"ZerosLike",
"RandomLike",
"OnesLike",
"TemporalParams",
]


Expand Down Expand Up @@ -166,12 +168,17 @@ def __init__(
init: str = "zeros", # one of [zeros, ones, rand, randn],
):
super(Parameter, self).__init__()
self.register_parameter(
"value",
torch.nn.Parameter(
getattr(torch, init)(tuple(shape))
), # TODO: check omegaconf's convert type annotation
)
if init == "eye":
self.register_parameter(
"value", torch.nn.Parameter(torch.eye(3).repeat(*shape))
)
else:
self.register_parameter(
"value",
torch.nn.Parameter(
getattr(torch, init)(tuple(shape))
), # TODO: check omegaconf's convert type annotation
)

def forward(self, void: torch.Tensor) -> torch.nn.parameter.Parameter:
return self.value
Expand All @@ -193,3 +200,37 @@ def __init__(

def forward(self, void: torch.Tensor) -> torch.nn.parameter.Parameter:
return dict(self.named_parameters())


class TemporalParams(torch.nn.Module):
r"""
Base class for temporal parameter generation.
Args:
parameters (omegaconf.DictConfig): parameters for temporal params
window_size (int): number of frames to generate params for
"""

def __init__(
self,
parameters: omegaconf.DictConfig,
window_size: int,
) -> None:
super().__init__()
window_size = int(window_size) if isinstance(window_size, str) else window_size
for name, param in parameters.items():
# each param should be generated for each frame in the window
for i in range(window_size):
self.register_parameter(
str(name) + str(i),
torch.nn.Parameter(
getattr(torch, toolz.get("init", param, "zeros"))(
tuple(param.shape)
)
), # TODO: check omegaconf's convert type annotation
)

self.window_size = window_size

def forward(self, void: torch.Tensor) -> torch.nn.parameter.Parameter:
return dict(self.named_parameters())
25 changes: 18 additions & 7 deletions moai/monads/human/pose/forward_kinematics.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import typing

import numpy as np
Expand All @@ -6,25 +7,35 @@
__all__ = ["ForwardKinematics"]


log = logging.getLogger(__name__)


class ForwardKinematics(torch.nn.Module):
def __init__(
self, parents
self,
parents: list,
offsets: list = None,
): # TODO: add a col/row major param to adjust offset slicing
super().__init__()
self.parents = parents # TODO: register buffer from list?
self.register_buffer("parents", torch.Tensor(parents).int())
if offsets is not None:
log.warning("offset not set, should be passed in the forward method")
self.register_buffer("offsets", torch.Tensor(offsets))

def forward(
self, # TODO: add parents tensor input?
rotation: torch.Tensor, # [B, (T), J, 3, 3]
position: torch.Tensor, # [B, (T), 3]
offset: torch.Tensor, # [B, (T), J, 3]
offset: typing.Optional[torch.Tensor] = None, # [B, (T), J, 3]
parents: typing.Optional[torch.Tensor] = None, # [B, J]
) -> typing.Dict[str, torch.Tensor]: # { [B, (T), J, 3], [B, (T), J, 3, 3] }
joints = torch.empty(rotation.shape[:-1], device=rotation.device)
joints[..., 0, :] = position.clone() # first joint according to global position
offset = offset[
:, np.newaxis, ..., np.newaxis
] # NOTE: careful, col vs row major order
offset = (
offset[:, np.newaxis, ..., np.newaxis]
if offset is not None
else self.offsets[:, np.newaxis, ..., np.newaxis]
) # NOTE: careful, col vs row major order
# offset = offset[np.newaxis, :, np.newaxis, :] #NOTE: careful, col vs row major order
global_rotation = rotation.clone()
# global_rotation = torch.empty(rotation.shape, device=rotation.device)
Expand All @@ -33,7 +44,7 @@ def forward(
parent_indices = (
parents[0].detach().cpu()
if parents is not None
else self.parents[0].detach().cpu()
else (self.parents[0].detach().cpu())
)
if (
parent_indices.shape[-1] == offset.shape[-3]
Expand Down
9 changes: 4 additions & 5 deletions moai/serve/handlers/azure/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ def __call__(
) -> typing.Any:

if self.json_key not in json:
log.error(f"json key: {self.json_key}, not found in json request")
log.error(f"json key: {self.json_key}, not found in json request")
working_dir = json[self.json_key]


# initialize connection to Azure Blob Storage
connect_str = json[self.connection_string]
try:
Expand Down Expand Up @@ -103,7 +102,7 @@ def __init__(
# self.blob_service_client = BlobServiceClient.from_connection_string(
# connection_string,
# )

self.connection_string = connection_string
self.container_name = container_name
self.blob_paths = blob_paths
Expand All @@ -122,9 +121,9 @@ def __call__(
# NOTE: void is the input json response
# TODO: need to check batched inference
input_json = void[0].get("body") or void[0].get("raw")

if self.json_key not in input_json:
log.error(f"json key: {self.json_key}, not found in json request")
log.error(f"json key: {self.json_key}, not found in json request")
working_dir = input_json[self.json_key]

# initialize connection to Azure Blob Storage
Expand Down
7 changes: 6 additions & 1 deletion moai/validation/metrics/common/distance_accuracy.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import numpy as np
import torch

from moai.validation.metric import MoaiMetric

__all__ = ["DistanceAccuracy"]


class DistanceAccuracy(torch.nn.Module):
class DistanceAccuracy(MoaiMetric):
def __init__(self, threshold: float = 0.05) -> None:
super().__init__()
self.threshold = threshold
Expand All @@ -15,3 +17,6 @@ def forward(
total = np.prod(list(distance.shape)[1:])
acc = (distance.flatten(1) < self.threshold).sum(1) / total
return acc.mean()

def compute(self, accs: np.ndarray) -> np.ndarray:
return accs.mean()
Loading

0 comments on commit e21f473

Please sign in to comment.