open-mmlab / mmpretrain

OpenMMLab Pre-training Toolbox and Benchmark
https://mmpretrain.readthedocs.io/en/latest/
Apache License 2.0
3.49k stars 1.08k forks source link

[Bug] Problems using collate with batch size > 1 on inference model #1074

Closed mmeendez8 closed 2 years ago

mmeendez8 commented 2 years ago

Describe the bug

I am trying to customize inference_model function so it can work with batches.

It seems the collate is not working as expected. This would serve as a simple example:

from mmcls.datasets.pipelines import Compose
import numpy as np
from mmcv.parallel import collate

img_norm_cfg = dict(mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
pipeline = [
    dict(type='Resize', size=(50, 50)),
    dict(type='Normalize', **img_norm_cfg),
    dict(type='ImageToTensor', keys=['img']),
    dict(type='Collect', keys=['img'])
]

transform = Compose(pipeline)

img = np.random.rand(10, 10, 3).astype(dtype=np.uint8)
image_dict = dict(img=img, ori_shape=img.shape)

images = [transform(image_dict)] * 4

data = collate(images, samples_per_gpu=2)

print(data["img"].shape)  # torch.Size([4, 3, 50, 50])
print(len(data["img_metas"]))  # 2

I think that data["img"] should be a list of 2 element with sizes [2, 3, 50, 50].

After checking collate implementation I saw that bare tensors are just being stacked. So probably we should not be using collate directly after a pipeline transformation? Is there another way for doing this?

I am willing to collaborate if needed.

Thanks :)

Environment

Previous command is not working for me

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mmendez/.cache/pypoetry/virtualenvs/ai-homography-estimation-MIZJYHGs-py3.8/lib/python3.8/site-packages/mmcls/utils/collect_env.py", line 10, in collect_env
    env_info = collect_base_env()
  File "/home/mmendez/.cache/pypoetry/virtualenvs/ai-homography-estimation-MIZJYHGs-py3.8/lib/python3.8/site-packages/mmcv/utils/env.py", line 112, in collect_env
    from mmcv.ops import get_compiler_version, get_compiling_cuda_version
  File "/home/mmendez/.cache/pypoetry/virtualenvs/ai-homography-estimation-MIZJYHGs-py3.8/lib/python3.8/site-packages/mmcv/ops/__init__.py", line 2, in <module>
    from .active_rotated_filter import active_rotated_filter
  File "/home/mmendez/.cache/pypoetry/virtualenvs/ai-homography-estimation-MIZJYHGs-py3.8/lib/python3.8/site-packages/mmcv/ops/active_rotated_filter.py", line 10, in <module>
    ext_module = ext_loader.load_ext(
  File "/home/mmendez/.cache/pypoetry/virtualenvs/ai-homography-estimation-MIZJYHGs-py3.8/lib/python3.8/site-packages/mmcv/utils/ext_loader.py", line 13, in load_ext
    ext = importlib.import_module('mmcv.' + name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: /home/mmendez/.cache/pypoetry/virtualenvs/ai-homography-estimation-MIZJYHGs-py3.8/lib/python3.8/site-packages/
mmcv/_ext.cpython-38-x86_64-linux-gnu.so: undefined symbol: _ZN2at5sliceERKNS_6TensorElN3c108optionalIlEES5_l

I have run pip list instead:

absl-py                           1.2.0
addict                            2.4.0
ai-aws-client                     0.0.1+fc23ca8
aiobotocore                       2.4.0
aiohttp                           3.8.1
aioitertools                      0.10.0
aiosignal                         1.2.0
alabaster                         0.7.12
aniso8601                         9.0.1
ansi2html                         1.7.0
anyio                             3.6.1
argon2-cffi                       21.3.0
argon2-cffi-bindings              21.2.0
arrow                             1.2.2
asgiref                           3.5.1
asttokens                         2.0.5
astunparse                        1.6.3
async-timeout                     4.0.2
attrs                             22.1.0
auth0-python                      3.23.1
Babel                             2.10.3
backcall                          0.2.0
beautifulsoup4                    4.11.1
bleach                            5.0.1
boto3                             1.24.59
boto3-stubs                       1.20.54
botocore                          1.27.59
botocore-stubs                    1.27.5
cachetools                        4.2.4
captum                            0.5.0
catalogue                         2.0.7
certifi                           2022.6.15.2
cffi                              1.15.1
cfgv                              3.3.1
charset-normalizer                2.1.1
click                             8.1.3
cloudpickle                       2.0.0
coverage                          6.4.4
cryptography                      38.0.1
cycler                            0.11.0
debugpy                           1.6.3
decorator                         4.4.2
defusedxml                        0.7.1
Deprecated                        1.2.13
distlib                           0.3.6
docstring-parser                  0.14.1
docutils                          0.16
entrypoints                       0.4
enum-compat                       0.0.3
executing                         0.8.3
fastapi                           0.78.0
fastapi-health                    0.4.0
fastjsonschema                    2.16.1
ffmpeg-python                     0.2.0
filelock                          3.8.0
fire                              0.4.0
Flask                             2.1.1
Flask-RESTful                     0.3.9
flatbuffers                       1.12
fonttools                         4.37.1
frozenlist                        1.3.1
fsspec                            2022.8.2
future                            0.18.2
gast                              0.4.0
google-api-core                   2.7.3
google-api-python-client          1.12.11
google-auth                       1.35.0
google-auth-httplib2              0.1.0
google-auth-oauthlib              0.4.6
google-cloud-core                 2.3.0
google-cloud-storage              1.44.0
google-crc32c                     1.3.0
google-pasta                      0.2.0
google-resumable-media            2.3.2
googleapis-common-protos          1.56.0
grpcio                            1.48.1
h11                               0.13.0
h5py                              3.7.0
httplib2                          0.20.4
identify                          2.5.5
idna                              3.4
imageio                           2.21.3
imageio-ffmpeg                    0.4.7
imagesize                         1.4.1
importlib-metadata                4.12.0
importlib-resources               5.9.0
iniconfig                         1.1.1
install                           1.3.5
ipdb                              0.13.9
ipykernel                         6.15.3
ipython                           7.34.0
ipython-genutils                  0.2.0
ipywidgets                        8.0.2
itsdangerous                      2.1.2
jedi                              0.18.1
Jinja2                            3.1.2
jmespath                          1.0.1
joblib                            1.1.0
jsonschema                        3.2.0
jupyter                           1.0.0
jupyter_client                    7.3.5
jupyter-console                   6.4.4
jupyter-core                      4.11.1
jupyterlab-pygments               0.2.2
jupyterlab-widgets                3.0.3
keras                             2.9.0
Keras-Preprocessing               1.1.2
kfp                               1.8.13
kfp-pipeline-spec                 0.1.16
kfp-server-api                    1.8.4
kiwisolver                        1.4.4
kubernetes                        18.20.0
libclang                          14.0.6
loguru                            0.6.0
lxml                              4.9.1
m2r2                              0.3.2
Markdown                          3.4.1
MarkupSafe                        2.1.1
matplotlib                        3.5.3
matplotlib-inline                 0.1.6
mistune                           0.8.4
mmcls                             0.23.1
mmcv                              1.5.3
mmcv-full                         1.4.6
moto                              2.3.2
moviepy                           1.0.3
multidict                         6.0.2
mypy                              0.910
mypy-boto3-s3                     1.20.49
mypy-extensions                   0.4.3
nbclient                          0.6.8
nbconvert                         6.5.3
nbformat                          5.5.0
nest-asyncio                      1.5.5
nodeenv                           1.7.0
notebook                          6.4.12
numpy                             1.22.0
nvgpu                             0.9.0
oauthlib                          3.2.1
opencv-python                     4.5.5.64
opt-einsum                        3.3.0
orjson                            3.7.2
packaging                         21.3
pandas                            1.4.4
pandocfilters                     1.5.0
parso                             0.8.3
pastel                            0.2.1
pexpect                           4.8.0
pickleshare                       0.7.5
Pillow                            9.2.0
pip                               21.3.1
pkgutil_resolve_name              1.3.10
platformdirs                      2.5.2
pluggy                            1.0.0
poethepoet                        0.13.1
pre-commit                        2.20.0
proglog                           0.1.10
prometheus-client                 0.14.1
prometheus-fastapi-instrumentator 5.8.1
prompt-toolkit                    3.0.31
protobuf                          3.19.5
psutil                            5.9.2
ptyprocess                        0.7.0
pure-eval                         0.2.2
py                                1.11.0
pyasn1                            0.4.8
pyasn1-modules                    0.2.8
pyathena                          2.14.0
pycparser                         2.21
pydantic                          1.8.2
Pygments                          2.13.0
PyJWT                             2.4.0
pynvml                            11.4.1
pyparsing                         3.0.9
pyrsistent                        0.18.1
pytest                            6.2.5
pytest-env                        0.6.2
python-dateutil                   2.8.2
python-decouple                   3.6
python-dotenv                     0.19.2
pytz                              2022.2.1
PyYAML                            5.4.1
pyzmq                             23.2.1
qtconsole                         5.3.2
QtPy                              2.2.0
requests                          2.28.1
requests-oauthlib                 1.3.1
requests-toolbelt                 0.9.1
responses                         0.21.0
rope                              0.16.0
rsa                               4.9
s3fs                              2022.8.2
s3transfer                        0.6.0
scikit-learn                      1.1.2
scipy                             1.9.1
semver                            2.13.0
Send2Trash                        1.8.0
setuptools                        59.5.0
setuptools-scm                    6.4.2
six                               1.16.0
sniffio                           1.2.0
snowballstemmer                   2.2.0
soupsieve                         2.3.2.post1
Sphinx                            4.5.0
sphinx-rtd-theme                  0.5.2
sphinxcontrib-applehelp           1.0.2
sphinxcontrib-devhelp             1.0.2
sphinxcontrib-htmlhelp            2.0.0
sphinxcontrib-jsmath              1.0.1
sphinxcontrib-qthelp              1.0.3
sphinxcontrib-serializinghtml     1.1.5
stack-data                        0.2.0
starlette                         0.19.1
strip-hints                       0.1.10
tabulate                          0.8.9
tenacity                          8.0.1
tensorboard                       2.10.0
tensorboard-data-server           0.6.1
tensorboard-plugin-wit            1.8.1
tensorflow                        2.9.1
tensorflow-estimator              2.9.0
tensorflow-io                     0.26.0
tensorflow-io-gcs-filesystem      0.26.0
termcolor                         1.1.0
terminado                         0.15.0
testpath                          0.6.0
threadpoolctl                     3.1.0
tinycss2                          1.1.1
toml                              0.10.2
tomli                             2.0.1
tomlkit                           0.7.2
torch                             1.11.0+cu113
torch-model-archiver              0.5.3
torchserve                        0.5.3
torchvision                       0.12.0+cu113
tornado                           6.2
tqdm                              4.64.1
traitlets                         5.4.0
typer                             0.6.1
types-orjson                      3.6.2
types-PyYAML                      6.0.7
types-requests                    2.28.10
types-setuptools                  57.4.18
types-urllib3                     1.26.24
typing_extensions                 4.3.0
uritemplate                       3.0.1
urllib3                           1.26.12
uvicorn                           0.15.0
video-client-python               1.3.0
video-common-python               0.1.2
virtualenv                        20.16.5
wcwidth                           0.2.5
webencodings                      0.5.1
websocket-client                  1.3.2
Werkzeug                          2.2.2
wheel                             0.37.0
widgetsnbextension                4.0.3
wrapt                             1.14.1
xmltodict                         0.13.0
yapf                              0.32.0
yarl                              1.8.1
zipp                              3.8.1

Other information

No response

mzr1996 commented 2 years ago

It looks like a mmcv installation problem, please try to reinstall mmcv-full according to the MMCV documentation.