# 'ViTImageProcessor' object has no attribute 'pad'

**URL:** <https://discuss.huggingface.co/t/vitimageprocessor-object-has-no-attribute-pad/32511>\
**Category:** Beginners\
**Created:** [February 25, 2023, 4:43pm UTC](https://discuss.huggingface.co/t/vitimageprocessor-object-has-no-attribute-pad/32511 "2023-02-25T16:43:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yoyo1a1](https://avatars.discourse-cdn.com/v4/letter/y/f475e1/32.png) [@yoyo1a1](https://discuss.huggingface.co/u/yoyo1a1)\
**Post date:** [February 25, 2023, 4:43pm UTC](https://discuss.huggingface.co/t/vitimageprocessor-object-has-no-attribute-pad/32511/1 "2023-02-25T16:43:01Z")

</div>

Hi

I’m a student, new to this topic in general and that’s my first time using Hugging Face.  
I want to fine-tune ViT with my own dataset, In order to do so, I’m following [this tutorial](https://huggingface.co/blog/fine-tune-vit), but since I don’t import my data set from datasets, some things are different.

This is my custom data set class:

import os  
import torch  
import numpy as np  
from transformers import ViTImageProcessor  
from torchvision import transforms  
from PIL import Image

model\_name\_or\_path = ‘google/vit-base-patch16-224-in21k’  
feature\_extractor = ViTImageProcessor.from\_pretrained(model\_name\_or\_path)

```auto
class ViTCustomDataSet():
    def __init__ (self, image_dir, device):
        # super(ImageDataset, self). __init__ ()
        self.image_dir = image_dir

        self.transform = transforms.Compose([
            transforms.ToPILImage(),
            self.extract_features
        ])

        self.image_dir = image_dir
        self.device = device

    def extract_features(self, image):
        return feature_extractor(image, return_tensors='pt')
        return feature_extractor(image, return_tensors='pt')['pixel_values']

    def __len__ (self):
        return len(os.listdir(self.image_dir))

    def __getitem__ (self, idx):
        image_name = os.listdir(self.image_dir)[idx]
        image_path = os.path.join(self.image_dir, image_name)
        image = np.array(Image.open(image_path).convert('RGB'))
        image = self.transform(image)
        label = self.get_label_from_filename(image_name)
        return image.to(self.device), label

    def get_label_from_filename(self, image_name):
        return 1 if image_name.split('_')[-2] == 'M' else 0

    def get_train_and_test_data(self):
        train_size = int(0.8 * self. __len__ ())
        test_size = self. __len__ () - train_size

        train_dataset, test_dataset = torch.utils.data.random_split(
            self, [train_size, test_size])

        return torch.utils.data.DataLoader(
            train_dataset, batch_size=32, shuffle=True).dataset, torch.utils.data.DataLoader(
            test_dataset, batch_size=32, shuffle=True).dataset

```

And that’s my main:

```auto
training_args = TrainingArguments(
    output_dir="./vit",
    # per_device_train_batch_size=16,
    evaluation_strategy="steps",
    num_train_epochs=4,
    # fp16=True,
    save_steps=100,
    eval_steps=100,
    logging_steps=10,
    learning_rate=2e-4,
    save_total_limit=2,
    remove_unused_columns=False,
    push_to_hub=False,
    report_to='tensorboard',
    load_best_model_at_end=True,
)

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

    vitData = ViTCustomDataSet(
        'D:/data/NIST302/images/auxiliary/flat/M/500/plain/png/equal', device=device)

    train_dataset, test_dataset = vitData.get_train_and_test_data()

    model = ViTForImageClassification.from_pretrained(
        model_name_or_path,
        num_labels=num_classes)

    feature_extractor = ViTImageProcessor.from_pretrained(
        model_name_or_path, padding=False)

    trainer = Trainer(
        model=model,
        args=training_args,
        compute_metrics=compute_metrics,
        train_dataset=train_dataset,
        eval_dataset=test_dataset,
        data_collator=None,
        tokenizer=feature_extractor,
    )

    train_results = trainer.train()
    trainer.save_model()
    trainer.log_metrics("train", train_results.metrics)
    trainer.save_metrics("train", train_results.metrics)
    trainer.save_state()

    metrics = trainer.evaluate(test_dataset)
    trainer.log_metrics("eval", metrics)
    trainer.save_metrics("eval", metrics)

```

(I’m sorry for the mess, I’m transforming an old code of mine and I want to make sure things work before I refactor)

Anyway, I’m getting the following error:

> ViTImageProcessor does not have a pad attribute

Which I don’t see how to solve.

Also - since I’m new, any other tip will be welcomed!  
Thank you

---

<div class="post-metadata">

**Author:** ![iwaitu](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/iwaitu/32/29935_2.png) [@iwaitu](https://discuss.huggingface.co/u/iwaitu)\
**Post date:** [September 27, 2023, 4:55pm UTC](https://discuss.huggingface.co/t/vitimageprocessor-object-has-no-attribute-pad/32511/2 "2023-09-27T16:55:52Z")

</div>

I have the same problem.

---

<div class="post-metadata">

**Author:** ![Sandy1857](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Sandy1857](https://discuss.huggingface.co/u/Sandy1857)\
**Post date:** [October 6, 2023, 12:53pm UTC](https://discuss.huggingface.co/t/vitimageprocessor-object-has-no-attribute-pad/32511/3 "2023-10-06T12:53:30Z")

</div>

Just remove the padding parameter from the below:

```auto
feature_extractor = ViTImageProcessor.from_pretrained(
        model_name_or_path, padding=False)

```

It should be just:

```auto
feature_extractor = ViTImageProcessor.from_pretrained(
        model_name_or_path)

```

---

<div class="post-metadata">

**Author:** ![minhocas](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@minhocas](https://discuss.huggingface.co/u/minhocas)\
**Post date:** [December 1, 2023, 5:16pm UTC](https://discuss.huggingface.co/t/vitimageprocessor-object-has-no-attribute-pad/32511/4 "2023-12-01T17:16:06Z")

</div>

Did you resolve this issue? I’m having the same problem.

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [March 27, 2024, 8:08am UTC](https://discuss.huggingface.co/t/vitimageprocessor-object-has-no-attribute-pad/32511/5 "2024-03-27T08:08:21Z")

</div>

Hi,

This can be bypassed by passing the default data collator to the Trainer:

```auto
from transformers import default_data_collator

trainer = Trainer(...
            tokenizer=image_processor,
            data_collator=default_data_collator
)

```

However, we’re going to fix this issue by adding an `image_processor` argument to the Trainer so that users no longer need to do this. You can follow the progress here: [[Trainer] Allow passing image processor by NielsRogge · Pull Request #29896 · huggingface/transformers · GitHub](https://github.com/huggingface/transformers/pull/29896)
