1

I tried to execute the ViT model from Image Classification with Hugging Face Transformers and Keras, I got an error, particularly in this instruction:

processed_dataset = ds.map(augmentation, batched=True)

the error :

ValueError: Exception encountered when calling layer "resizing_8" (type Resizing).

Attempt to convert a value (<PIL.BmpImagePlugin.BmpImageFile image mode=L size=190x100 at 0x7F35C52AD210>) with an unsupported type (<class 'PIL.BmpImagePlugin.BmpImageFile'>) to a Tensor.

Call arguments received: • inputs=<PIL.BmpImagePlugin.BmpImageFile image mode=L size=190x100 at 0x7F35C52AD210>

I tried the answer in this link ArrowTypeError: Could not convert <PIL.PngImagePlugin.PngImageFile image mode=RGB size=32x32 at 0x7F2223B6ED10>, where I added 'img': Image(decode=True, id=None) to my features in create_image_folder_dataset() and I still have the same problem except for a small change in this part

ValueError: Exception encountered when calling layer "resizing_13" (type Resizing).

What I should do to solve this problem?

create_image_folder_dataset function:

def create_image_folder_dataset(root_path):
  """creates `Dataset` from image folder structure"""
  
  # get class names by folders names
  _CLASS_NAMES= os.listdir(root_path)
  # defines `datasets` features`
  features=datasets.Features({
                      "img": datasets.Image(decode=True, id=None),
                      #"img": datasets.Image(),
                      "label": datasets.features.ClassLabel(names=_CLASS_NAMES),
                      
                  })
  #print(_CLASS_NAMES)
  # temp list holding datapoints for creation
  img_data_files=[]
  label_data_files=[]
  # load images into list for creation
  for img_class in os.listdir(root_path):
    for img in os.listdir(os.path.join(root_path,img_class)):
      path_=os.path.join(root_path,img_class,img)
      img_data_files.append(path_)
      label_data_files.append(img_class)
  # create dataset
  ds = datasets.Dataset.from_dict({"img":img_data_files,"label":label_data_files},features=features)
  return ds
ds = create_image_folder_dataset("/content/drive/MyDrive/FINAL_DATASET")
ds[0] """ return: 
{'img': <PIL.BmpImagePlugin.BmpImageFile image mode=L size=190x100 at 0x7F35C54ECC10>,
 'label': 0}"""

my Augmentation function :

from transformers import ViTFeatureExtractor
from tensorflow import keras 
from tensorflow.keras import layers


model_id = "google/vit-base-patch16-224-in21k"
#google/vit-base-patch32-384
feature_extractor = ViTFeatureExtractor.from_pretrained(model_id)

# learn more about data augmentation here: https://www.tensorflow.org/tutorials/images/data_augmentation
data_augmentation = keras.Sequential(
    [
        layers.Resizing(feature_extractor.size, feature_extractor.size),
        layers.Rescaling(1./255),
        layers.RandomFlip("horizontal"),
        layers.RandomRotation(factor=0.02),
        layers.RandomZoom(
            height_factor=0.2, width_factor=0.2
        ),
    ],
    name="data_augmentation",
)
# use keras image data augementation processing
def augmentation(examples):
    print(examples["img"])
    examples["pixel_values"] = [data_augmentation(image) for image in examples["img"]]
    return examples


# basic processing (only resizing)
def process(examples):
    examples.update(feature_extractor(examples['img'], ))
    return examples


# we are also renaming our label col to labels to use `.to_tf_dataset` later
#ds = ds.rename_column("label", "labels")
Ime TIM
  • 21
  • 4

1 Answers1

0

Now it's working, I convert my dataset from "L" to "RGB".

Ime TIM
  • 21
  • 4