Spaces:
Runtime error
Runtime error
Create chart_output_hflip.py
Browse files
3rdparty/densepose/converters/chart_output_hflip.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates.
|
| 2 |
+
from dataclasses import fields
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
from densepose.structures import DensePoseChartPredictorOutput, DensePoseTransformData
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def densepose_chart_predictor_output_hflip(
|
| 9 |
+
densepose_predictor_output: DensePoseChartPredictorOutput,
|
| 10 |
+
transform_data: DensePoseTransformData,
|
| 11 |
+
) -> DensePoseChartPredictorOutput:
|
| 12 |
+
"""
|
| 13 |
+
Change to take into account a Horizontal flip.
|
| 14 |
+
"""
|
| 15 |
+
if len(densepose_predictor_output) > 0:
|
| 16 |
+
|
| 17 |
+
PredictorOutput = type(densepose_predictor_output)
|
| 18 |
+
output_dict = {}
|
| 19 |
+
|
| 20 |
+
for field in fields(densepose_predictor_output):
|
| 21 |
+
field_value = getattr(densepose_predictor_output, field.name)
|
| 22 |
+
# flip tensors
|
| 23 |
+
if isinstance(field_value, torch.Tensor):
|
| 24 |
+
setattr(densepose_predictor_output, field.name, torch.flip(field_value, [3]))
|
| 25 |
+
|
| 26 |
+
densepose_predictor_output = _flip_iuv_semantics_tensor(
|
| 27 |
+
densepose_predictor_output, transform_data
|
| 28 |
+
)
|
| 29 |
+
densepose_predictor_output = _flip_segm_semantics_tensor(
|
| 30 |
+
densepose_predictor_output, transform_data
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
for field in fields(densepose_predictor_output):
|
| 34 |
+
output_dict[field.name] = getattr(densepose_predictor_output, field.name)
|
| 35 |
+
|
| 36 |
+
return PredictorOutput(**output_dict)
|
| 37 |
+
else:
|
| 38 |
+
return densepose_predictor_output
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def _flip_iuv_semantics_tensor(
|
| 42 |
+
densepose_predictor_output: DensePoseChartPredictorOutput,
|
| 43 |
+
dp_transform_data: DensePoseTransformData,
|
| 44 |
+
) -> DensePoseChartPredictorOutput:
|
| 45 |
+
point_label_symmetries = dp_transform_data.point_label_symmetries
|
| 46 |
+
uv_symmetries = dp_transform_data.uv_symmetries
|
| 47 |
+
|
| 48 |
+
N, C, H, W = densepose_predictor_output.u.shape
|
| 49 |
+
u_loc = (densepose_predictor_output.u[:, 1:, :, :].clamp(0, 1) * 255).long()
|
| 50 |
+
v_loc = (densepose_predictor_output.v[:, 1:, :, :].clamp(0, 1) * 255).long()
|
| 51 |
+
Iindex = torch.arange(C - 1, device=densepose_predictor_output.u.device)[
|
| 52 |
+
None, :, None, None
|
| 53 |
+
].expand(N, C - 1, H, W)
|
| 54 |
+
densepose_predictor_output.u[:, 1:, :, :] = uv_symmetries["U_transforms"][Iindex, v_loc, u_loc]
|
| 55 |
+
densepose_predictor_output.v[:, 1:, :, :] = uv_symmetries["V_transforms"][Iindex, v_loc, u_loc]
|
| 56 |
+
|
| 57 |
+
for el in ["fine_segm", "u", "v"]:
|
| 58 |
+
densepose_predictor_output.__dict__[el] = densepose_predictor_output.__dict__[el][
|
| 59 |
+
:, point_label_symmetries, :, :
|
| 60 |
+
]
|
| 61 |
+
return densepose_predictor_output
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def _flip_segm_semantics_tensor(
|
| 65 |
+
densepose_predictor_output: DensePoseChartPredictorOutput, dp_transform_data
|
| 66 |
+
):
|
| 67 |
+
if densepose_predictor_output.coarse_segm.shape[1] > 2:
|
| 68 |
+
densepose_predictor_output.coarse_segm = densepose_predictor_output.coarse_segm[
|
| 69 |
+
:, dp_transform_data.mask_label_symmetries, :, :
|
| 70 |
+
]
|
| 71 |
+
return densepose_predictor_output
|