huggingface / transformers

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
https://huggingface.co/transformers
Apache License 2.0
131.79k stars 26.24k forks source link

transformers/configuration_utils.py: TypeError: Object of type ResNetConfig is not JSON serializable ( AutoModelForObjectDetection.from_pretrained("microsoft/table-transformer-detection"..)) #28979

Closed dokondr closed 6 months ago

dokondr commented 6 months ago

System Info

When loading model: AutoModelForObjectDetection.from_pretrained("microsoft/table-transformer-detection", revision="no_timm")

transformers/configuration_utils.py returns a TypeError: Object of type ResNetConfig is not JSON serializable

This error happens when I run in virtual environment (Win10): torch==2.2.0 torchvision==0.17.0 transformers==4.31.0

And also with latest versions of these libraries.

Yet it does not happen in Conda default environment that was created long ago: torch==2.2.0 torchvision==0.17.0 transformers==4.31.0.dev0

Today when I try: pip install transformers==4.31.0.dev0

I get: ERROR: No matching distribution found for transformers==4.31.0.dev0

So, it looks like 'transformers 4.31.0.dev0' no longer exist. How to solve then TypeError: Object of type ResNetConfig is not JSON serializable ?

Detailed environment info where this error happens

Who can help?

@amyeroberts

Information

Tasks

Reproduction

import torch from torchvision import transforms from transformers import AutoModelForObjectDetection

model = AutoModelForObjectDetection.from_pretrained("microsoft/table-transformer-detection", revision="no_timm")

Expected behavior

"microsoft/table-transformer-detection" model should load without errors

amyeroberts commented 6 months ago

Hi @dokondr, thanks for raising an issue!

I'm unable to replicate this issue locally. Could you try installing with pip and using the most recent version of transformers?

pip install -U transformers
dokondr commented 6 months ago

Hi @dokondr, thanks for raising an issue!

I'm unable to replicate this issue locally. Could you try installing with pip and using the most recent version of transformers?

pip install -U transformers

Hi amyeroberts!

It solved the problem, many thanks!

sizhky commented 2 months ago

I was still getting the same error with,

# Load model directly
from transformers import AutoImageProcessor, AutoModelForObjectDetection

processor = AutoImageProcessor.from_pretrained("microsoft/table-transformer-structure-recognition-v1.1-all")
model = AutoModelForObjectDetection.from_pretrained("microsoft/table-transformer-structure-recognition-v1.1-all")

even after updating transformers to the latest version.

For now the dirty way, is to patch a custom json serialising method - https://stackoverflow.com/a/38764817/4296772

from transformers.models.resnet.configuration_resnet import ResNetConfig
from fastcore import patch_to
from json import JSONEncoder

def _default(self, obj):
    return getattr(obj.__class__, "__json__", _default.default)(obj)
_default.default = JSONEncoder().default
JSONEncoder.default = _default

@patch_to(ResNetConfig)
def __json__(self):
    return self.to_dict()
amyeroberts commented 2 months ago

@sizhky Could you share your running environment: run transformers-cli env in the terminal and copy-paste the output?

I'm unable to replicate with the shared snippet running on the development branch

sizhky commented 1 month ago

Sorry I have moved on to a different project. You can share more details here if the issue is still relevant