Rahaf Almgheed
Rahaf Almgheed

Reputation: 21

Transformers/PIL image contains values outside the range [0, 1]

when I run this code in my image captioning task gives the error:

trainer = Seq2SeqTrainer(
    tokenizer=feature_extractor,
    model=model,
    args=training_args,
    compute_metrics=compute_metrics,
    train_dataset=train_dataset,
    eval_dataset=val_dataset,
    data_collator=default_data_collator,
)
trainer.train()

Error: The image to be converted to a PIL image contains values outside the range [0, 1], got [-0.9764705896377563, 0.9686274528503418] which cannot be converted to uint8.

Although I converted the numpy array into a tensor here:

transforms1 = torchvision.transforms.Compose([torchvision.transforms.Resize(config.IMG_SIZE),
                                              torchvision.transforms.ToTensor(),
                                              torchvision.transforms.Normalize(
                                              mean=0.5,
                                             std=0.5)])
df=  pd.read_csv("/content/drive/MyDrive/captions.txt")
train_df , val_df = train_test_split(df , test_size = 0.2)
df.head()

how to solve this issue?

Upvotes: 1

Views: 1815

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207853

I don't know what you are doing because your code is incomplete, but if your values are in the range -1..+1, and need to be in the range 0..255, you will need to scale them, something like this:

V = ((V + 1.0) * 255/2).astype(np.uint8)

Upvotes: 0

Related Questions