Trying to use this transformer model by executing this code
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image
import requests
# Load image from the IAM database
url = 'https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg'
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
# Load model
processor = TrOCRProcessor.from_pretrained('fhswf/TrOCR_german_handwritten')
model = VisionEncoderDecoderModel.from_pretrained('fhswf/TrOCR_german_handwritten')
#print(model.generation_config)
# Process image
pixel_values = processor(images=image, return_tensors="pt").pixel_values
generated_ids = model.generate(pixel_values)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(generated_text)
produces this error of which I would like to get rid:
C:\Users\user\Projects\Project\venv\lib\site-packages\transformers\generation\utils.py:1376: UserWarning: You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use and modify the model generation configuration (see https://huggingface.co/docs/transformers/generation_strategies#default-text-generation-configuration )
When I check the generation config by running
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
processor = TrOCRProcessor.from_pretrained('fhswf/TrOCR_german_handwritten')
model = VisionEncoderDecoderModel.from_pretrained('fhswf/TrOCR_german_handwritten')
print(model.generation_config)
I get
GenerationConfig {
"bos_token_id": 0,
"decoder_start_token_id": 2,
"eos_token_id": 2,
"pad_token_id": 1,
"use_cache": false
}
From this manual I know that these config settings are the ones that differ from the default generation configuration. In the answer to this post it says
If you want to control generation, you can pass various arguments to the
generate
function, likemax_length
,temperature
,top_k
, etc. However, you shouldn’t directly modify the model’s configuration for these parameters
So, my idea is to set the model.generation_config back to default, so that the warning from above will no longer appear. But since I do not want to alter the performance of the model, I intend to add the config settings, which differ from default, into the generate function, as described above. This would leave me with
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image
import requests
# Load image from the IAM database
url = 'https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg'
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
# Load model
processor = TrOCRProcessor.from_pretrained('fhswf/TrOCR_german_handwritten')
model = VisionEncoderDecoderModel.from_pretrained('fhswf/TrOCR_german_handwritten')
print(model.generation_config)
# Process image
pixel_values = processor(images=image, return_tensors="pt").pixel_values
generated_ids = model.generate(pixel_values,
bos_token_id=0,
decoder_start_token_id=2,
eos_token_id=2,
pad_token_id=1,
use_cache=False)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(generated_text)
The problem is that I do not know how to set the model configuration back to default. Which file would I have to manipulate where?
And if I do, will my strategy work, such that the warning will not appear anymore, but I will still be able to use the model with the config settings intended by its authors?