martinsbruveris / tensorflow-image-models

TensorFlow port of PyTorch Image Models (timm) - image models with pretrained weights.
https://tfimm.readthedocs.io/en/latest/
Apache License 2.0
287 stars 25 forks source link

Cannot load model with `create_model` function (potential conflict with keras) #40

Closed hyenal closed 2 years ago

hyenal commented 2 years ago

Right now I am using my own copy of vit_base_patch32_224_in21k. I am trying to initialize the model using tfimm.create_model. I am trying to load the model using the following:

import tfimm
tfimm.create_model(model_name='vit_base_patch32_224_in21k', model_path ='/my/model/path/vit_base_patch32_224_in21k/', input_shape=(224,224))

This results in

  File "/etc/pyenv/versions/3.8.6/lib/python3.8/site-packages/tfimm/models/factory.py", line 45, in create_model
    loaded_model = tf.keras.models.load_model(model_path)
  File "/etc/pyenv/versions/3.8.6/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/etc/pyenv/versions/3.8.6/lib/python3.8/site-packages/tfimm/models/serialization.py", line 81, in from_config
    _cfg = cfg_class(**_cfg)
TypeError: __init__() got an unexpected keyword argument 'in_chans'

Note that I am using tfimm==0.2.1 and everything worked very well with 0.1.5. In addition the following code work:

import tensorflow as tf
tf.keras.models.load_model('/my/model/path/vit_base_patch32_224_in21k/')
hyenal commented 2 years ago

I have closed this issue as it seems to be a backward compatibility one... Models with version < 0.1.5 are not compatible with models in version >=0.2.

martinsbruveris commented 2 years ago

Yes, that is unfortunately the case. I will try to keep the models backwards compatible with 0.2.x. Basically, one cannot make add/remove/rename fields in the config.