facebookresearch / facebookresearch/detectron2

DensePoseResultsMplContourVisualizer output shape mismatch

Open
#3,772 1 comment 0 reactions 0 assignees View on GitHub
densepose
Dominant language
Python
Stars
34.7k
Forks
7.9k
PR merge metrics
No merged PRs in 30d

Description

1. Full runnable code or full changes you made:
None
2. What exact command you run:
```
python apply_net.py show \
configs/densepose_rcnn_R_101_FPN_DL_s1x.yaml \
https://dl.fbaipublicfiles.com/densepose/densepose_rcnn_R_101_FPN_DL_s1x/165712116/model_final_844d15.pkl \
image \
dp_contour,dp_v,dp_u,dp_segm \
-v
```
3. __Full logs__ or other relevant observations:
There is a known issue in matplotlib with rounding of floating point figure size: [#1](https://github.com/matplotlib/matplotlib/issues/15363), [#2](https://github.com/matplotlib/matplotlib/issues/21879).
This error leads to unexpected behaviour: while infering Densepose with input image of size (1708, 2560, 3), output image size becomes (1707, 2560,3)
The bug appears In DensePoseResultsMplContourVisualizer at [lines](https://github.com/facebookresearch/detectron2/blob/08f617cc2c9276a48e7e7dc96ae946a5df23af3f/projects/DensePose/densepose/vis/densepose_results.py#L99-L118) where
```python
w, h = map(int, fig.get_size_inches() * fig.get_dpi())
```
is executed as
```python
w, h = map(int, [1707.9999999999998, 2560.0]) #1707.99...8 was obtained just because of a rounding error in matplotlib
#w, h = 1707, 2560
```
**Possible workaround:**
Add additional check if int(h,w)==round(h,w) to [context_to_image_bgr function](https://github.com/facebookresearch/detectron2/blob/cbbc1ce26473cb2a5cc8f58e8ada9ae14cb41052/projects/DensePose/densepose/vis/densepose_results.py#L112-L120). If it's not, resize resulting image to correct shape. Possible implementation:
```python
def context_to_image_bgr(self, context):
fig = context["fig"]
w, h = map(int, fig.get_size_inches() * fig.get_dpi())
true_w, true_h = np.round(fig.get_size_inches() * fig.get_dpi()).astype(int)
canvas = context["canvas"]
canvas.draw()
image_1d = np.fromstring(canvas.tostring_rgb(), dtype="uint8")
image_rgb = image_1d.reshape(h, w, 3)
image_bgr = image_rgb[:, :, ::-1].copy()
if (true_w, true_h) == (w,h):
return image_bgr
else:
return cv2.resize(image_bgr, (true_w, true_h))
```

### Environment:
```
sys.platform linux
Python 3.7.10 | packaged by conda-forge | (default, Feb 19 2021, 16:07:37) [GCC 9.3.0]
numpy 1.19.2
detectron2 0.5
Compiler GCC 7.3
CUDA compiler CUDA 11.0
detectron2 arch flags 3.7, 5.0, 5.2, 6.0, 6.1, 7.0, 7.5, 8.0
DETECTRON2_ENV_MODULE
PyTorch 1.7.1
PyTorch debug build False
GPU available Yes
GPU 0 Tesla T4 (arch=7.5)
CUDA_HOME /usr/local/cuda-11.1
Pillow 8.1.0
torchvision 0.8.2
torchvision arch flags 3.5, 5.0, 6.0, 7.0, 7.5, 8.0
fvcore 0.1.5.post20211023
iopath 0.1.8
cv2 4.5.1
---------------------- ---------------------------------------------------------------------------------------------
PyTorch built with:
- GCC 7.3
- C++ Version: 201402
- Intel(R) Math Kernel Library Version 2020.0.2 Product Build 20200624 for Intel(R) 64 architecture applications
- Intel(R) MKL-DNN v1.6.0 (Git Hash 5ef631a030a6f73131c77892041042805a06064f)
- OpenMP 201511 (a.k.a. OpenMP 4.5)
- NNPACK is enabled
- CPU capability usage: AVX2
- CUDA Runtime 11.0
- NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_80,code=sm_80;-gencode;arch=compute_37,code=compute_37
- CuDNN 8.0.5
- Magma 2.5.2
- Build settings: BLAS=MKL, BUILD_TYPE=Release, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -fopenmp -DNDEBUG -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DUSE_VULKAN_WRAPPER -O2 -fPIC -Wno-narrowing -Wall -Wextra -Werror=return-type -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-unused-local-typedefs -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-psabi -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -Wno-stringop-overflow, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, USE_CUDA=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON,
```

Contributor guide

Open the contributing guide

Research direction

Start in projects/DensePose/densepose/vis/densepose_results.py at DensePoseResultsMplContourVisualizer.context_to_image_bgr, focusing on the figure-size conversion and canvas image reshape. Run the reported apply_net.py show command with the provided image dimensions, then verify that the visualizer output preserves the input shape when Matplotlib returns a fractional pixel size.

Written by the indexing model from the issue text.

Assessment

Tech stack
matplotlib, numpy, opencv, python
Domain
computer-vision
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.