TypeError: Object of type ViTConfig is not JSON serializable when pushing a custom ViT model to Hugging Face Hub

246 Views Asked by At

I'm new to hugging face and hugsvision. I'm trying to push a custom Vision Transformer (ViT) model to the Hugging Face Hub. I've defined a custom configuration using ViTConfig, but I get this error when trying to push the feature extractor to the Hub. I want this model to be pushed to hub so I will load it later to fine tune on another dataset.

Here's my code:

from transformers import ViTConfig, ViTFeatureExtractor, ViTForImageClassification

config = ViTConfig (
   _name_or_path= "myViT",
  architectures= [
    "ViTForImageClassification"
  ],
  id2label= {
    "0": "Benign",
    "1": "Malignant"
  },
  ...
)

model1 = ViTForImageClassification(
    config
)

feature_extractor1 = ViTFeatureExtractor(
    config
)

training_args1 = TrainingArguments(
        ...
)

trainer1 = Trainer(
      ...
)

trainer1.train()

model1.push_to_hub("my-username/my-custom-vit-model")
feature_extractor1.push_to_hub("my-username/my-custom-vit-model")

The error message I receive for the last line is:

TypeError: Object of type ViTConfig is not JSON serializable

This works fine when I use pretrained models.

Any help or suggestions on how to resolve this issue would be greatly appreciated.

I could not find much documentation regarding pushing a custom ViT model to hub. Also, I tried to dump the model.config into json but I did not work.

0

There are 0 best solutions below