# Convert a model in .h5 format

**URL:** <https://discuss.huggingface.co/t/convert-a-model-in-h5-format/20957>\
**Category:** Amazon SageMaker\
**Created:** [July 28, 2022, 3:47pm UTC](https://discuss.huggingface.co/t/convert-a-model-in-h5-format/20957 "2022-07-28T15:47:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgrnd4](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@dgrnd4](https://discuss.huggingface.co/u/dgrnd4)\
**Post date:** [July 28, 2022, 3:47pm UTC](https://discuss.huggingface.co/t/convert-a-model-in-h5-format/20957/1 "2022-07-28T15:47:49Z")

</div>

Hi @loubnabnl I read your [article](https://www.philschmid.de/image-classification-huggingface-transformers-keras) and after that I trained the model in keras this error pop up:  
`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.`

So it doesn’t let me save the model into a “.h5” file or tensorflowLight.

Can you please help me with it? How can I do the conversation in one of these two file?

I’m using a VisualTransformer.

---

<div class="post-metadata">

**Author:** ![philschmid](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/philschmid/32/38022_2.png) [@philschmid](https://discuss.huggingface.co/u/philschmid)\
**Post date:** [July 28, 2022, 4:17pm UTC](https://discuss.huggingface.co/t/convert-a-model-in-h5-format/20957/2 "2022-07-28T16:17:30Z")

</div>

How are you trying to save the model?

---

<div class="post-metadata">

**Author:** ![dgrnd4](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@dgrnd4](https://discuss.huggingface.co/u/dgrnd4)\
**Post date:** [August 1, 2022, 6:53pm UTC](https://discuss.huggingface.co/t/convert-a-model-in-h5-format/20957/3 "2022-08-01T18:53:23Z")

</div>

@philschmid in these 3 ways:

> model\_directory = “drive/MyDrive/Tirocinio/1ris\_vit-stanford-dog-dataset”
> 
> tokenizer = ViTFeatureExtractor.from\_pretrained(model\_directory)  
> model = ViTForImageClassification.from\_pretrained(model\_directory)
> 
> pytorch\_path = “drive/MyDrive/Tirocinio/ViT\_model\_pytorch”  
> model\_pytorch = ViTForImageClassification.from\_pretrained(pytorch\_path)
> 
> h5\_path = “drive/MyDrive/Tirocinio/ViT\_model\_tf”  
> model\_h5 = ViTForImageClassification.from\_pretrained(h5\_path, from\_tf=True)

The point is that I want to save the model into an unique “.h5” file.
