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

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

当我在 google colab 中执行 vit 模型时,出现了一些错误。

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: 无法转换 类型为 PngImageFile: 不是序列或识别为 null 以转换为列表类型

错误出现在preprocessed_train_ds这一行,估计是特征提取问题

使用:

  1. huggingface/transformers
  2. ViTFeatureExtractor:google/vit-base-patch16-224-in21k
  3. cifar10,拆分=['train[:5000]','test[:2000]']) (与大多数示例设置一致)

有人可以帮助我吗?这个问题困扰了我很久。 :( 非常感谢。

如果“img”列是PIL图片列表,需要将“img”的特征从数组改为图片,将features替换为:

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)), })