ArrowTypeError: Could not convert <PIL.PngImagePlugin.PngImageFile image mode=RGB size=32×32 at 0x7F2223B6ED10>

Question:

When I execute vit model in google colab, I got some error.

import numpy as np
from datasets import Features, ClassLabel, Array3D

def preprocess_images(examples):
  images = examples['img']  
  images = [np.array(image, dtype=np.uint8) for image in images] 
  images = [np.moveaxis(image, source=-1, destination=0) for image in images] 
  inputs = feature_extractor(images=images) 
  examples['pixel_values'] = inputs['pixel_values'] 

  return examples 

features = Features({ 
  'label': ClassLabel(
      names=['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']), 
  'img': Array3D(dtype="int64", shape=(3,32,32)), 
  'pixel_values': Array3D(dtype="float32", shape=(3, 224, 224)), 
}) 

preprocessed_train_ds = train_ds.map(preprocess_images, batched=True, features=features) 
preprocessed_val_ds = val_ds.map(preprocess_images, batched=True, features=features) 
preprocessed_test_ds = test_ds.map(preprocess_images, batched=True, features=features)

ArrowTypeError: Could not convert <PIL.PngImagePlugin.PngImageFile image mode=RGB size=32×32 at 0x7F2223B6ED10> with type PngImageFile: was not a sequence or recognized null for conversion to list type

The error occurs at preprocessed_train_ds this line, I guess it might be a feature extraction problem.

use the:

  1. huggingface/transformers
  2. ViTFeatureExtractor: google/vit-base-patch16-224-in21k
  3. cifar10, split=[‘train[:5000]’, ‘test[:2000]’])
    (Consistent with most example settings)

Can someone help me? This problem has been bothering me for a long time. 🙁
Thank you very much.

Asked By: Jyun-Xin

||

Answers:

If the "img" column is a list of PIL images, you need to change the features of "img" from array to image, replace features with the following:

features = Features({
    'label': ClassLabel(names=['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']),
    'img': Image(decode=True, id=None),
    'pixel_values': Array3D(dtype="float32", shape=(3, 224, 224)), })
Answered By: Eduardo Bocarruido