Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Tensor shape error, what is the problem? #1264

Closed
2 tasks done
hahamini opened this issue Jun 25, 2024 · 6 comments
Closed
2 tasks done

[BUG]: Tensor shape error, what is the problem? #1264

hahamini opened this issue Jun 25, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@hahamini
Copy link

Before You Report a Bug, Please Confirm You Have Done The Following...

  • I have updated to the latest version of the packages.
  • I have searched for both existing issues and closed issues and found none that matched my issue.

DeepFace's version

0.0.92

Python version

3.8.10

Operating System

Ubuntu 20.04

Dependencies

absl-py==2.1.0
addict==2.4.0
asttokens==2.4.1
astunparse==1.6.3
attrs==23.2.0
backcall==0.2.0
beautifulsoup4==4.12.3
blinker==1.8.2
cachetools==5.3.3
certifi==2024.6.2
cffi==1.16.0
charset-normalizer==3.3.2
click==8.1.7
comm==0.2.2
ConfigArgParse==1.7
contourpy==1.1.1
cycler==0.12.1
dash==2.17.1
dash-core-components==2.0.0
dash-html-components==2.0.0
dash-table==5.0.0
dataclasses-json==0.5.14
decorator==5.1.1
-e git+ssh:https://[email protected]/serengil/deepface.git@c2a82e6153ee0dcf40bc8494b30a5fafa382f32d#egg=deepface
dlib==19.24.4
executing==2.0.1
facenet-pytorch==2.6.0
fastjsonschema==2.20.0
filelock==3.15.4
fire==0.6.0
Flask==3.0.3
Flask-Cors==4.0.1
flatbuffers==24.3.25
fonttools==4.53.0
fsspec==2024.6.0
gast==0.4.0
gdown==5.2.0
google-auth==2.30.0
google-auth-oauthlib==1.0.0
google-pasta==0.2.0
grpcio==1.64.1
gunicorn==22.0.0
h5py==3.11.0
idna==3.7
importlib_metadata==7.2.1
importlib_resources==6.4.0
ipython==8.12.3
ipywidgets==8.1.3
itsdangerous==2.2.0
jax==0.4.13
jaxlib==0.4.13
jedi==0.19.1
Jinja2==3.1.4
joblib==1.4.2
jsonschema==4.22.0
jsonschema-specifications==2023.12.1
jupyter_core==5.7.2
jupyterlab_widgets==3.0.11
keras==2.13.1
kiwisolver==1.4.5
libclang==18.1.1
Markdown==3.6
MarkupSafe==2.1.5
marshmallow==3.21.3
matplotlib==3.7.5
matplotlib-inline==0.1.7
mediapipe==0.10.11
ml-dtypes==0.2.0
mpmath==1.3.0
mtcnn==0.1.1
mypy-extensions==1.0.0
nbformat==5.7.0
nest-asyncio==1.6.0
networkx==3.1
numpy==1.24.3
nvidia-cublas-cu12==12.1.3.1
nvidia-cuda-cupti-cu12==12.1.105
nvidia-cuda-nvrtc-cu12==12.1.105
nvidia-cuda-runtime-cu12==12.1.105
nvidia-cudnn-cu12==8.9.2.26
nvidia-cufft-cu12==11.0.2.54
nvidia-curand-cu12==10.3.2.106
nvidia-cusolver-cu12==11.4.5.107
nvidia-cusparse-cu12==12.1.0.106
nvidia-nccl-cu12==2.19.3
nvidia-nvjitlink-cu12==12.5.40
nvidia-nvtx-cu12==12.1.105
oauthlib==3.2.2
open3d==0.17.0
opencv-contrib-python==4.10.0.84
opencv-python==4.10.0.84
opt-einsum==3.3.0
packaging==24.1
pandas==2.0.3
parso==0.8.4
pexpect==4.9.0
pickleshare==0.7.5
pillow==10.2.0
pkg_resources==0.0.0
pkgutil_resolve_name==1.3.10
platformdirs==4.2.2
plotly==5.22.0
prompt_toolkit==3.0.47
protobuf==3.20.3
psutil==6.0.0
ptyprocess==0.7.0
pure-eval==0.2.2
py-cpuinfo==9.0.0
pyasn1==0.6.0
pyasn1_modules==0.4.0
pycparser==2.22
Pygments==2.18.0
pyk4a==1.5.0
pyparsing==3.1.2
pypng==0.0.21
pypylon==2.3.0
pyquaternion==0.9.9
pyrealsense2==2.55.1.6486
PySocks==1.7.1
python-dateutil==2.9.0.post0
pytz==2024.1
PyYAML==6.0.1
referencing==0.35.1
requests==2.32.3
requests-oauthlib==2.0.0
retina-face==0.0.17
retrying==1.3.4
rpds-py==0.18.1
rsa==4.9
scikit-learn==1.3.2
scipy==1.10.1
seaborn==0.13.2
six==1.16.0
sounddevice==0.4.7
soupsieve==2.5
stack-data==0.6.3
sympy==1.12.1
tenacity==8.4.2
tensorboard==2.13.0
tensorboard-data-server==0.7.2
tensorflow==2.13.1
tensorflow-estimator==2.13.0
tensorflow-io-gcs-filesystem==0.34.0
termcolor==2.4.0
threadpoolctl==3.5.0
torch==2.2.2
torchvision==0.17.2
tqdm==4.66.4
traitlets==5.14.3
triton==2.2.0
typing-inspect==0.9.0
typing_extensions==4.12.2
tzdata==2024.1
ultralytics==8.2.42
ultralytics-thop==2.0.0
ur-rtde==1.5.8
urllib3==2.2.2
wcwidth==0.2.13
Werkzeug==3.0.3
widgetsnbextension==4.0.11
wrapt==1.16.0
zipp==3.19.2

Reproducible example

import cv2
from deepface import DeepFace

cam = cv2.VideoCapture(0)
backends = [
    "opencv",
    "ssd",
    "dlib",
    "mtcnn",
    "fastmtcnn",
    "retinaface",
    "mediapipe",
    "yolov8",
    "yunet",
    "centerface",
]

alignment_modes = [True, False]

try:
    while True:
        rval, rgb = cam.read()

        if not rval:
            print("No image!")
            continue

        cv2.imshow("frame", rgb)
        key = cv2.waitKey(1)
        if key & 0xFF == ord("q"):
            break

        elif key == ord("s"):
            result = DeepFace.extract_faces(
                # img_path="test.jpg",
                # rgb = "tests/dataset/img59.jpg",
                img_path=rgb,
                detector_backend=backends[4],
                align=alignment_modes[0],
            )
            print(result)

except Exception as e:
    print(e)
    cv2.destroyAllWindows()

Relevant Log Output

2024-06-25 16:36:12.329829: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-06-25 16:36:12.940596: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
The shape of the mask [10510] at index 0 does not match the shape of the indexed tensor [10551, 4] at index 0

Expected Result

No response

What happened instead?

No response

Additional Info

No response

@hahamini hahamini added the bug Something isn't working label Jun 25, 2024
@serengil
Copy link
Owner

with this explanation, i cannot reproduce that error. try to make it more modular.

e.g. what is rgb? share it in the bug description. also, instead of sharing this snippet just share the problematic line with clear args.

@hahamini
Copy link
Author

with this explanation, i cannot reproduce that error. try to make it more modular.

e.g. what is rgb? share it in the bug description. also, instead of sharing this snippet just share the problematic line with clear args.

This sample code reads images from a webcam and calls the extract_faces function.

rgb is the numpy array taken from the webcam by the opencv library and is of (480,640,3) shape.

I read the image from the webcam and run the extract_faces function from the DeepFace library, but I get an error of this type: The shape of the mask [44415] at index 0 does not match the shape of the indexed tensor [45395, 4] at index 0.

Also, if I run the function with the provided image tests/dataset/img59.jpg rather than the webcam image as input, the same error occurs.

@serengil
Copy link
Owner

independent from webcam capture, the following code snippet throws same exception?

result = DeepFace.extract_faces(
                img_path="tests/dataset/img59.jpg",
                detector_backend="fastmtcnn",
                align=True,
            )

@hahamini
Copy link
Author

hahamini commented Jun 25, 2024

Yes, here is the error message.

from deepface import DeepFace
result = DeepFace.extract_faces(
    # img_path="test.jpg",
    # img_path=rgb,
    # detector_backend=backends[4],
    # align=alignment_modes[0],
    img_path="tests/dataset/img59.jpg",
    detector_backend="fastmtcnn",
    align=True,
)
print(result)
Traceback (most recent call last):
  File "test.py", line 31, in <module>
    result = DeepFace.extract_faces(
  File "/home/mini/workspace/deepface/deepface/DeepFace.py", line 531, in extract_faces
    return detection.extract_faces(
  File "/home/mini/workspace/deepface/deepface/modules/detection.py", line 86, in extract_faces
    face_objs = DetectorWrapper.detect_faces(
  File "/home/mini/workspace/deepface/deepface/detectors/DetectorWrapper.py", line 116, in detect_faces
    facial_areas = face_detector.detect_faces(img)
  File "/home/mini/workspace/deepface/deepface/detectors/FastMtCnn.py", line 27, in detect_faces
    detections = self.model.detect(
  File "/home/mini/workspace/deepface/.venv/lib/python3.8/site-packages/facenet_pytorch/models/mtcnn.py", line 313, in detect
    batch_boxes, batch_points = detect_face(
  File "/home/mini/workspace/deepface/.venv/lib/python3.8/site-packages/facenet_pytorch/models/utils/detect_face.py", line 123, in detect_face
    boxes = torch.cat((boxes[ipass, :4], score[ipass].unsqueeze(1)), dim=1)
IndexError: The shape of the mask [7888] at index 0 does not match the shape of the indexed tensor [15355, 4] at index 0

@serengil
Copy link
Owner

your pytorch installation seems not okay, can you try to change detector to opencv?

@hahamini
Copy link
Author

You are right. I changed the detector and it worked fine. I don't know the cause, but it seems like a problem with my system.
I'm trying to solve this problem using Docker.
Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants