Save T5 model as H5 and convert it to model.json to be used in TensorflowJS
Author: zirlman
Pull request: no
Labels: name: wontfix

# ❓ Questions & Help

How can I save a T5 model as HDF5 file?
In the end, I want to load it in the browser via `tensorflow-converter `and `tensorflowjs`

Code:

```
model_str = "t5-small"
tokenizer = T5Tokenizer.from_pretrained(model_str)
model = TFT5ForConditionalGeneration.from_pretrained(model_str)

tokenizer_path = "/content/t5_tokenizer"
model_path = "/content/t5_base_model"
tfjs_path = "/content/t5_base_model_js"

model.save(model_path,save_format="h5")
```

When I run that code I receive the following error:

```
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-53-8343a8512f91> in <module>()
----> 1 model.save(model_path,save_format="h5")

1 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/saving/save.py in save_model(model, filepath, overwrite, include_optimizer, save_format, signatures, options)
    103         not isinstance(model, sequential.Sequential)):
    104       raise NotImplementedError(
--> 105           'Saving the model to HDF5 format requires the model to be a '
    106           'Functional model or a Sequential model. It does not work for '
    107           'subclassed models, because such models are defined via the body of '

NotImplementedError: Saving the model to HDF5 format requires the model to be a Functional model or a Sequential model. It does not work for subclassed models, because such models are defined via the body of a Python method, which isn't safely serializable. Consider saving to the Tensorflow SavedModel format (by setting save_format="tf") or using `save_weights`.
```

The error says that the model needs to be a Functional/Sequential model, but as far as I know, TF_Transformers are based on TF 2.0 so this shouldn't be a problem. Also when I run this code `isinstance(model, tf.keras.models.Model)` it returns True


Comments:
Hi, our API has its own saving function: `model.save_pretrained("directory")`, which saves the model alongside its configuration file. Can you let me know if this method fits your use case?

Hi, using that method I've managed to save the model. Unfortunately, when I convert the model into `model.json` load it later in my JS file I receive the following error: 
```
TypeError: Cannot read property 'model_config' of null
    at models.ts:287
    at common.ts:14
    at Object.next (common.ts:14)
    at a (common.ts:14)
```
Bellow you can steps I've followed (to try) to load the model into the JS file

Model saving:
```
model_path = "/content/t5_base_model"
tfjs_path = "/content/t5_base_model_js"

model.save_pretrained(model_path)
model_path += "/tf_model.h5"
```
Conversion:
```
!tensorflowjs_converter --input_format keras \
                       $model_path \
                       $tfjs_path 
```

JS file:
```
var model;
const model_path = "../libraries/models/t5_base_model_js/model.json";

document.querySelector("#msg").innerText = "[LOADING TF MODEL]";
tf.loadLayersModel(model_path)
.then((loadedModel) => {
        model = loadedModel;
        document.querySelector("#msg").innerText = "[MODEL LOADED]";
}).catch(err => console.log(err));
```

I assume the problem occurs because `model.save_pretrained(dir)` saves the config in a separate file. I've tried to figure out if maybe `tf-converter` has a parameter for additional files to bundle into model.json, but with no success. @LysandreJik any idea what I could do to fix this error?

P.S. I've tried to load mobilenet from TF-Hub to make sure my script works fine and it successfully loaded it.



This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.


Any updates on this? Stuck on saving mobilebert as a tf layers model