thomas-rooty / lego-detector

Using AI to detect Legos on your picture !
https://github.com/thomas-rooty/what-the-brick
MIT License
13 stars 2 forks source link

app.py doesn't run, TF throws an exception #1

Open yotamhc opened 6 months ago

yotamhc commented 6 months ago

Hi, thanks a lot for this project! I tried to test it and I can't run app.py.

I get the following error:

lego-detector % python3 app.py
Traceback (most recent call last):
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/ops/operation.py", line 208, in from_config
    return cls(**config)
           ^^^^^^^^^^^^^
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/layers/convolutional/conv2d.py", line 107, in __init__
    super().__init__(
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/layers/convolutional/base_conv.py", line 107, in __init__
    super().__init__(activity_regularizer=activity_regularizer, **kwargs)
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/layers/layer.py", line 247, in __init__
    Operation.__init__(self, dtype=dtype, name=name)
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/ops/operation.py", line 21, in __init__
    raise ValueError(
ValueError: Argument `name` must be a string and cannot contain character `/`. Received: name=conv1/conv (of type <class 'str'>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/user/code/lego-detector/app.py", line 9, in <module>
    model = tf.keras.models.load_model("model/lego_predicter.h5")
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/saving/saving_api.py", line 183, in load_model
    return legacy_h5_format.load_model_from_hdf5(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/legacy/saving/legacy_h5_format.py", line 133, in load_model_from_hdf5
    model = saving_utils.model_from_config(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/legacy/saving/saving_utils.py", line 85, in model_from_config
    return serialization.deserialize_keras_object(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/legacy/saving/serialization.py", line 495, in deserialize_keras_object
    deserialized_obj = cls.from_config(
                       ^^^^^^^^^^^^^^^^
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/models/model.py", line 517, in from_config
    return functional_from_config(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/models/functional.py", line 517, in functional_from_config
    process_layer(layer_data)
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/models/functional.py", line 497, in process_layer
    layer = saving_utils.model_from_config(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/legacy/saving/saving_utils.py", line 85, in model_from_config
    return serialization.deserialize_keras_object(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/legacy/saving/serialization.py", line 504, in deserialize_keras_object
    deserialized_obj = cls.from_config(cls_config)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/code/lego-detector/.venv/lib/python3.12/site-packages/keras/src/ops/operation.py", line 210, in from_config
    raise TypeError(
TypeError: Error when deserializing class 'Conv2D' using config={'name': 'conv1/conv', 'trainable': False, 'dtype': 'float32', 'filters': 64, 'kernel_size': [7, 7], 'strides': [2, 2], 'padding': 'valid', 'data_format': 'channels_last', 'dilation_rate': [1, 1], 'groups': 1, 'activation': 'linear', 'use_bias': False, 'kernel_initializer': {'class_name': 'GlorotUniform', 'config': {'seed': None}}, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}.

Exception encountered: Argument `name` must be a string and cannot contain character `/`. Received: name=conv1/conv (of type <class 'str'>)

I installed all required packages. Any idea what is going wrong?

EasySouls commented 2 months ago

I encountered the exact same issue here, has there been any solutions?

gad-zooks commented 2 months ago

Me 3...

gad-zooks commented 2 months ago

I got it! Install Python 3.11 in your .venv, and update the requirements.txt like so to install the contemporary version of the needed files:

matplotlib==3.8.0 opencv-python-headless==4.8.0.76 numpy==1.26.0 Flask==3.0.0 tensorflow=-2.14.0

If you get a flask error about Flask, then reinstall flask: python -m pip install flask

For this error, if it still happens, be sure you're in the right directory when running py.app, for example I changed to ~/Documents/projects/lego-detector (master)

File "/Users/user/code/lego-detector/app.py", line 9, in model = tf.keras.models.load_model("model/lego_predicter.h5") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

To post image files, etc. read the readme.md carefully. My flask server is working and accepts image uploads, but I'm getting a server error with the /guess_lego part now that I'm trying to figure out.

EasySouls commented 2 months ago

Weird, I had an error right up on starting the app with the keras model. Do you just run the app.py or is there a framework you should run instead? Im not familiar with python, but for example when you develop a web app with js you don't just run the index.js, but you start the server with node.js.

EasySouls commented 2 months ago

Oh and @gad-zooks you started the app from the root directory, right? I mean not from inside the .venv directory, but from the root of the whole repo.