Not able to load the exported MATLAB DL model in TensorFlow

8 visualizzazioni (ultimi 30 giorni)
I have use exportNetwork command and got a folder named myModel which contains 4 files init.py, readme.txt, model.py, and weights.h5.
No according to matlab documentation: myModel folder is a package. So we can do
"import myModel
model = myModel.load_model()"
But if i am doing this i am getting error: "ModuleNotFoundError: No module named 'myModel'"
I am running on colab so what i did is: i uploaded the myModel folder in my drive and then i mounted drive. But not able to install that package. How to load such exported model in python please tell us clearly.
I have tried to make the model using 'model.py' code. But then when i am trying to load the weights using kers command:
model = create_model()
weight_path='/content/drive/MyDrive/VGGish_Arka/weights.h5'
model.load_weights(weight_path)
Error is: "ValueError: Layer count mismatch when loading weights from file. Model expected 9 layers, found 0 saved layers."

Risposte (2)

Sanju
Sanju il 30 Apr 2024
It seems like you are trying to load a model that was exported from MATLAB into Python using the “exportNetwork” command. However, you are encountering some errors when trying to import and load the model.
The error message "ModuleNotFoundError: No module named 'myModel'" suggests that Python is unable to find the myModel package. This could be due to the package not being installed or not being in the correct location.
To resolve this issue, you can try the following steps:
  1. Ensure the myModel package is installed in your Python environment. You can use the pip command to install it. For example, in a Jupyter notebook cell, you can “run !pip install myModel”.
  2. Check the location of the myModel package. It should be in a directory that is included in the Python module search path. You can check the module search path by running ““import sys; print(sys.path)”. If the package is not in any of the listed directories, you may need to move it to a directory that is included in the search path.
  3. If you have already installed the myModel package and it is in the correct location, try restarting the Python kernel or the Jupyter notebook runtime. Sometimes, changes to the module search path require a restart to take effect.
Regarding the second issue with loading the weights using Keras, the error message "ValueError: Layer count mismatch when loading weights from file. Model expected 9 layers, found 0 saved layers" suggests that the model architecture defined in “model.py” does not match the architecture of the model that was used to save the weights.
To resolve this issue, you need to make sure that the model architecture defined in “model.py” matches the architecture of the model that was used to save the weights. This includes the number and configuration of layers.
If you are still encountering issues after following these steps, please provide more details about the structure of the myModel package and the code in “model.py”. Additionally, ensure that the weights file (weights.h5) is in the correct location and accessible by the Python script.
You can also refer to the below file exchange link for more information,
Hope this helps!

Don Mathis
Don Mathis il 30 Apr 2024
I was able to do it.
To get a model to export, in MATLAB I ran
>> help exportNetworkToTensorFlow
and then clicked the second example link, "Export Network to TensorFlow and Classify Image". Running that script exports the network to a folder "myModel".
I then created a google colab folder and dragged the myModel folder into it from my desktop.
Then I ran what's in the following screenshot. The key step for me was to CD to the folder containing the myModel folder (step 3).
Hope this helps!

Prodotti


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by