maikherbig / AIDeveloper

GUI-based software for training, evaluating and applying deep neural nets for image classification
BSD 2-Clause "Simplified" License
110 stars 20 forks source link

Loading a JSON model #54

Closed DankMemeGuy closed 5 months ago

DankMemeGuy commented 1 year ago

Apologies for my ignorance in advance.

I have tried googling but can't seem how to do this. I have a model that was created in Keras (based on Inception model). The files I have are:

group1-shard1of6
group1-shard2of6
group1-shard3of6
group1-shard4of6
group1-shard5of6
group1-shard6of6
model.json

Is there a way to load this model into the software? Or a way to convert this into an H5 model or something your software can accept? All the searching I am doing is teaching how to convert H5 model into a TensorFlow model (i think that's what I have), but none show the process reversed. I just want to start retraining that model instead of starting fresh

maikherbig commented 1 year ago

This is hard to answer. I dont know the filetype these group-shard and have no idea how to load that. But there is also a .json, which is the filetype how keras saves the model architecture. AIDeveloper also saves such files and they are called '.arch' (stands for architecture). You can try to only load the architecture (without weights) by renaming the .json into .arch and then use the function 'Load and restart' in AIDeveloper. Chances are low that this will work since different versions of keras could be incompatible.

Option 2: Open the .json in your notepad to find out which version of keras was used to generate that file. Install a Python enviroment with the exact same version of keras. Now you should be able to load the json file via:

from tensorflow.keras.models import model_from_json model_keras = model_from_json(model_config)

Now, save the model again: model_keras.save('coolmodel.model',save_format='h5')

It should be possible to load that .model file (hdf5) in AIDevleoper.

Still, I have no idea how to add the weights...

DankMemeGuy commented 1 year ago

I will try that out. Just to confirm, does the software accept .h5 files or only .model files?

I tried loading an H5 file and got an error. If ".model" file is H5, how come the software doesn't load ".h5" files?

DankMemeGuy commented 1 year ago

just following up in case my comment slipped your inbox :)

maikherbig commented 1 year ago

Sorry, I missed it. AIDeveloper really wants that the model filename ends with .model. You simply need to rename the model file like that. The reason behind that is that the filetype of the data is actually also HDF5 and I wanted to prevent confusion.

DankMemeGuy commented 1 year ago

Sorry, I missed it. AIDeveloper really wants that the model filename ends with .model. You simply need to rename the model file like that. The reason behind that is that the filetype of the data is actually also HDF5 and I wanted to prevent confusion.

When I renamed an H5 file to .model the software had some errors. I'll try with other H5 files though. thank you!

But I think this actually causes more confusion since ".model" is not a filetype that's standard. If you google for ".model" help or models etc you won't get any results, but if you search "H5" you will get many results. I think this will cause more problem for people's understanding of how the files are connected/the same. May want to put a disclaimer or info message just to let people know.

DankMemeGuy commented 1 year ago

https://i.imgur.com/S9vPQHq.png thats the error i keep getting

maikherbig commented 1 year ago

Looks like you are trying to load a model architecture file (.json or .arch) that was created using a different version of keras. As I wrote above, I would guess that this should fail. There is still another way (Option 2 - see above) which would require to load the file in pure Python using

DankMemeGuy commented 1 year ago

Looks like you are trying to load a model architecture file (.json or .arch) that was created using a different version of keras. As I wrote above, I would guess that this should fail. There is still another way (Option 2 - see above) which would require to load the file in pure Python using

I will try that in Python, but just wanted to say the model is using "2.2.4" of Keras.

DankMemeGuy commented 1 year ago

Which version of Keras does AIdevloper use? Or work with?

maikherbig commented 1 year ago

AIDeveloper uses the keras that comes with tensorflow 2.7.1. This tensorflow was installed via pip install tensorflow-gpu==2.7.1

Interestingly, the keras version inside that version of tensorflow has the version number 2.6.0.

One can find such information by using the Python-Tab inside AIDeveloper (find 'Python' at the top). There, one can use Python code to find out the version numbers:

import tensorflow as tf
print(tf.__version__)
print(tf.keras.__version__)

2.7.1 2.6.0

DankMemeGuy commented 1 year ago

Option 2: Open the .json in your notepad to find out which version of keras was used to generate that file. Install a Python enviroment with the exact same version of keras. Now you should be able to load the json file via:

from tensorflow.keras.models import model_from_json model_keras = model_from_json(model_config)

Now, save the model again: model_keras.save('coolmodel.model',save_format='h5')

It should be possible to load that .model file (hdf5) in AIDevleoper.

Thank you!

Once again, please forgive my ignorance/stupidity, but based on this:

Option 2: Open the .json in your notepad to find out which version of keras was used to generate that file. Install a Python enviroment with the exact same version of keras. Now you should be able to load the json file via:

from tensorflow.keras.models import model_from_json
model_keras = model_from_json(model_config)

Now, save the model again:
model_keras.save('coolmodel.model',save_format='h5')

It should be possible to load that .model file (hdf5) in AIDevleoper.

How would it change anything? If the model was built using say Keras 2.2.4, and then I install that version of Keras and then create a .model/h5 file, why would that change anything than just getting the .h5 file that was already built using 2.2.4 and renaming it to .model? They're both going to be 2.2.4, and both still wouldn't work with AIDeveloper? Am I missing some logical step?

Wouldn't the only way this to work is converting the 2.2.4 version model to 2.7.1?

maikherbig commented 1 year ago

in my experience, the h5 files of full models worked between versions of keras