niciBume / Cat_Prey_Analyzer

Cat Prey Image-Classification with deeplearning
MIT License
142 stars 22 forks source link

Cant load the pre trained models #27

Closed MIG1989 closed 4 months ago

MIG1989 commented 4 months ago

Hey,

first of all great work and thank you for the documentation.

Im trying to open the trained tensorflow model with tensorflow 2.16.1 and keras 3.1.1 but i gor the following error:

pi@raspberrypi:~/tensorflow1/models/research $ sh catCam_starter.sh
Executing CatPreyAnalyzer
CatCamPy: /home/pi
Log Dir: /home/pi/CatPreyAnalyzer/log
/home/pi/tensorflow1/models/research
/home/pi/tensorflow1/models/research/object_detection
CNN is ready to go!
Traceback (most recent call last):
  File "/home/pi/CatPreyAnalyzer/cascade.py", line 725, in <module>
    sq_cascade = Sequential_Cascade_Feeder()
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/CatPreyAnalyzer/cascade.py", line 75, in __init__
    self.base_cascade = Cascade()
                        ^^^^^^^^^
  File "/home/pi/CatPreyAnalyzer/cascade.py", line 418, in __init__
    self.pc_stage = PC_Stage()
                    ^^^^^^^^^^
  File "/home/pi/CatPreyAnalyzer/model_stages.py", line 269, in __init__
    self.pc_model = tf.keras.models.load_model(os.path.join(PC_models_dir, self.pc_model_name),1)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/.local/lib/python3.11/site-packages/keras/src/saving/saving_api.py", line 183, in load_model
    return legacy_h5_format.load_model_from_hdf5(filepath)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/.local/lib/python3.11/site-packages/keras/src/legacy/saving/legacy_h5_format.py", line 138, in load_model_from_hdf5
    load_weights_from_hdf5_group(f["model_weights"], model)
  File "/home/pi/.local/lib/python3.11/site-packages/keras/src/legacy/saving/legacy_h5_format.py", line 357, in load_weights_from_hdf5_group
    raise ValueError(
ValueError: Layer count mismatch when loading weights from file. Model expected 1 layers, found 3 saved layers.

can you give me a hint what have i done wrong?

Im sorry but this is my first try to do something with tensorflow.

The Object_detection_picamery file is running really well and is detecting cats, only the prey analyze is missing right now.

niciBume commented 4 months ago

Hey, thanks for the kind words hope you can use it! Seems like your tensorflow packages is too new for the trained models (4 years ago). Here is a list of my package versions:

Package               Version
--------------------- ---------
absl-py               0.9.0
asn1crypto            0.24.0
astor                 0.8.1
attrs                 19.3.0
backcall              0.1.0
bleach                3.1.1
cachetools            4.0.0
certifi               2018.8.24
chardet               3.0.4
colorzero             1.1
cryptography          2.6.1
cycler                0.10.0
Cython                0.29.15
decorator             4.4.1
defusedxml            0.6.0
entrypoints           0.3
future                0.18.2
gast                  0.2.2
google-auth           1.11.2
google-auth-oauthlib  0.4.1
google-pasta          0.1.8
gpiozero              1.5.1
grpcio                1.27.2
h5py                  2.9.0
idna                  2.6
importlib-metadata    1.5.0
ipykernel             5.1.4
ipython               7.12.0
ipython-genutils      0.2.0
ipywidgets            7.5.1
jedi                  0.16.0
Jinja2                2.11.1
jsonschema            3.2.0
jupyter               1.0.0
jupyter-client        6.0.0
jupyter-console       6.1.0
jupyter-core          4.6.3
Keras-Applications    1.0.8
Keras-Preprocessing   1.1.0
keyring               17.1.1
keyrings.alt          3.1.1
kiwisolver            1.1.0
lxml                  4.5.0
Markdown              3.2.1
MarkupSafe            1.1.1
matplotlib            3.1.3
mistune               0.8.4
mock                  4.0.1
nbconvert             5.6.1
nbformat              5.0.4
notebook              6.0.3
numpy                 1.16.2
oauthlib              3.1.0
opencv-contrib-python 4.1.0.25
opencv-python         4.1.1.26
opt-einsum            3.1.0
pandas                1.0.3
pandocfilters         1.4.2
parso                 0.6.2
pexpect               4.8.0
picamera              1.13
pickleshare           0.7.5
Pillow                7.0.0
pip                   20.2.4
prometheus-client     0.7.1
prompt-toolkit        3.0.3
protobuf              3.11.3
ptyprocess            0.6.0
pyasn1                0.4.8
pyasn1-modules        0.2.8
pybind11              2.5.0
pycrypto              2.6.1
Pygments              2.5.2
PyGObject             3.30.4
pyparsing             2.4.6
pyrsistent            0.15.7
python-apt            1.8.4.1
python-dateutil       2.8.1
python-telegram-bot   12.7
pytz                  2019.3
pyxdg                 0.25
pyzmq                 19.0.0
qtconsole             4.6.0
requests              2.21.0
requests-oauthlib     1.3.0
RPi.GPIO              0.7.0
rpimotorlib           3.1
rsa                   4.0
scipy                 1.4.1
SecretStorage         2.3.1
Send2Trash            1.5.0
setuptools            45.2.0
six                   1.14.0
ssh-import-id         5.7
telegram              0.0.1
tensorboard           2.0.2
tensorflow            1.14.0
tensorflow-estimator  1.14.0
termcolor             1.1.0
terminado             0.8.3
testpath              0.4.4
tornado               6.0.3
traitlets             4.3.3
urllib3               1.24.1
wcwidth               0.1.8
webencodings          0.5.1
Werkzeug              1.0.0
wheel                 0.34.2
widgetsnbextension    3.5.1
wrapt                 1.12.0
zipp                  3.0.0

Try to downgrade tensorflow and see if it can be run then.

MIG1989 commented 4 months ago

Thanks for the hint, im not a linux power user, but i tried to downgrade to tensorflow 1.14.

And it seems like this was working:

pi@raspberrypi:~/tensorflow1/models $ pip3 show tensorflow
Name: tensorflow
Version: 1.14.0
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: packages@tensorflow.org
License: Apache 2.0
Location: /home/pi/.pyenv/versions/3.7.17/lib/python3.7/site-packages
Requires: absl-py, astor, gast, google-pasta, grpcio, keras-applications, keras-preprocessing, numpy, protobuf, six, tensorboard, tensorflow-estimator, termcolor, wheel, wrapt
Required-by: 

i also downgraded to python 3.7.17 (due to my rersearch tf 1.14 is ony supported till python3 3.7)

Running the catCam_starter.sh runs still into an error:

pi@raspberrypi:~/tensorflow1/models/research $ sh catCam_starter.sh
Executing CatPreyAnalyzer
Traceback (most recent call last):
  File "/home/pi/.pyenv/versions/3.7.17/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/home/pi/.pyenv/versions/3.7.17/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/home/pi/.pyenv/versions/3.7.17/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/home/pi/.pyenv/versions/3.7.17/lib/python3.7/imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "/home/pi/.pyenv/versions/3.7.17/lib/python3.7/imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: /home/pi/.pyenv/versions/3.7.17/lib/python3.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: invalid ELF header

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "cascade.py", line 15, in <module>
    from CatPreyAnalyzer.model_stages import PC_Stage, FF_Stage, Eye_Stage, Haar_Stage, CC_MobileNet_Stage
  File "/home/pi/CatPreyAnalyzer/model_stages.py", line 1, in <module>
    import tensorflow as tf
  File "/home/pi/.pyenv/versions/3.7.17/lib/python3.7/site-packages/tensorflow/__init__.py", line 28, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "/home/pi/.pyenv/versions/3.7.17/lib/python3.7/site-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/home/pi/.pyenv/versions/3.7.17/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "/home/pi/.pyenv/versions/3.7.17/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/home/pi/.pyenv/versions/3.7.17/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/home/pi/.pyenv/versions/3.7.17/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/home/pi/.pyenv/versions/3.7.17/lib/python3.7/imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "/home/pi/.pyenv/versions/3.7.17/lib/python3.7/imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: /home/pi/.pyenv/versions/3.7.17/lib/python3.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: invalid ELF header

Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

By the way im using a Raspberry Pi5 (more power, i thought this would be better :-))

Is there a chance to fix that or to train in new tensorflow? Sry for taking your time to "debug"

my package versions: Package Version


absl-py 2.1.0 astor 0.8.1 gast 0.5.4 google-pasta 0.2.0 grpcio 1.62.1 h5py 3.8.0 importlib-metadata 6.7.0 Keras-Applications 1.0.8 Keras-Preprocessing 1.1.2 Markdown 3.4.4 MarkupSafe 2.1.5 numpy 1.21.4 opencv-python 3.4.17.63 pip 24.0 protobuf 4.24.4 pytz 2019.3 setuptools 47.1.0 six 1.16.0 tensorboard 1.14.0 tensorflow 1.14.0 tensorflow-estimator 1.14.0 termcolor 2.3.0 typing_extensions 4.7.1 Werkzeug 2.2.3 wheel 0.42.0 wrapt 1.16.0 zipp 3.15.0

niciBume commented 4 months ago

Hmm and if you downgrade all your packages to the version numbers of mine? I assume you installed tf according to this guide: https://github.com/EdjeElectronics/TensorFlow-Object-Detection-on-the-Raspberry-Pi?

Sorry I haven't used tf in a while now, but retraining it with the new tf is not really an option.

MIG1989 commented 4 months ago

i tried to install all the packages you mentioned: Package Version


absl-py 0.9.0 anyio 3.7.1 argon2-cffi 23.1.0 argon2-cffi-bindings 21.2.0 asn1crypto 0.24.0 astor 0.8.1 attrs 19.3.0 backcall 0.1.0 beautifulsoup4 4.12.3 bleach 3.1.1 cachetools 4.0.0 certifi 2018.8.24 cffi 1.15.1 chardet 3.0.4 charset-normalizer 3.3.2 colorzero 1.1 cryptography 42.0.5 cycler 0.10.0 Cython 0.29.15 debugpy 1.7.0 decorator 4.4.1 defusedxml 0.6.0 entrypoints 0.3 exceptiongroup 1.2.0 fastjsonschema 2.19.1 future 0.18.2 gast 0.2.2 google-auth 1.11.2 google-auth-oauthlib 0.4.1 google-pasta 0.1.8 grpcio 1.27.2 h5py 3.8.0 idna 2.8 importlib-metadata 6.7.0 importlib-resources 5.12.0 ipykernel 5.1.4 ipython 7.34.0 ipython-genutils 0.2.0 ipywidgets 7.5.1 jedi 0.16.0 jeepney 0.8.0 Jinja2 3.1.3 jsonschema 3.2.0 jupyter 1.0.0 jupyter-client 6.0.0 jupyter-console 6.1.0 jupyter_core 4.12.0 jupyter-server 1.24.0 jupyterlab-pygments 0.2.2 Keras-Applications 1.0.8 Keras-Preprocessing 1.1.0 keyring 17.1.1 keyrings.alt 3.1.1 kiwisolver 1.1.0 lxml 4.5.0 Markdown 3.2.1 MarkupSafe 2.1.5 matplotlib 3.1.3 matplotlib-inline 0.1.6 mistune 0.8.4 mock 4.0.1 nbclassic 1.0.0 nbclient 0.7.4 nbconvert 5.6.1 nbformat 5.0.4 nest_asyncio 1.6.0 notebook 6.0.3 notebook_shim 0.2.4 numpy 1.16.2 oauthlib 3.1.0 opencv-python 3.4.17.63 opt-einsum 3.1.0 packaging 24.0 pandocfilters 1.4.2 parso 0.6.2 pexpect 4.8.0 pickleshare 0.7.5 pip 24.0 pkgutil_resolve_name 1.3.10 prometheus-client 0.7.1 prompt-toolkit 3.0.3 protobuf 3.11.3 psutil 5.9.8 ptyprocess 0.6.0 pyasn1 0.4.8 pyasn1-modules 0.2.8 pybind11 2.5.0 pycparser 2.21 pycrypto 2.6.1 Pygments 2.5.2 pyparsing 2.4.6 pyrsistent 0.15.7 python-dateutil 2.8.1 pytz 2019.3 pyxdg 0.25 pyzmq 19.0.0 qtconsole 4.6.0 QtPy 2.4.1 requests 2.21.0 requests-oauthlib 1.3.0 rpimotorlib 3.1 rsa 4.0 SecretStorage 2.3.1 Send2Trash 1.5.0 setuptools 45.2.0 six 1.14.0 sniffio 1.3.1 soupsieve 2.4.1 ssh-import-id 5.7 tensorboard 2.0.2 tensorflow 1.14.0 tensorflow-estimator 1.14.0 termcolor 1.1.0 terminado 0.8.3 testpath 0.4.4 tinycss2 1.2.1 tornado 6.0.3 traitlets 4.3.3 typing_extensions 4.7.1 urllib3 1.24.1 wcwidth 0.1.8 webencodings 0.5.1 websocket-client 1.6.1 Werkzeug 1.0.0 wheel 0.34.2 widgetsnbextension 3.5.1 wrapt 1.12.0 zipp 3.0.0

not all where possible, but still the error "ImportError: /home/pi/.pyenv/versions/3.7.17/lib/python3.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: invalid ELF header "

it ssems like that TF1.14 is not compatible with Pi5 (aarch64).

I will buy a Pi4B. Which OS do you use?

niciBume commented 4 months ago

That seems unfortunate. Alternatively you could perhaps try getting it to run in a docker container. I used the default raspian OS on my RPI4.

MIG1989 commented 4 months ago

With a Pi4B and Buster as OS it is working :-)

Where do you open your cat flap in the code? I have a cat flap with an API and i want to open it only when there is no Prey :-)

niciBume commented 4 months ago

That is great to hear! Check out this issue here for the actuation: https://github.com/niciBume/Cat_Prey_Analyzer/issues/5