text_plain_output_1.png
from datasets import load_dataset

from sklearn.metrics import accuracy_score

from torch.utils.data import DataLoader

from transformers import TrainingArguments, Trainer

from transformers import ViTForImageClassification

from transformers import ViTImageProcessor

import numpy as np

import torch

import torch

from datasets import load_dataset
train_ds = load_dataset('miladfa7/5-Flower-Types-Classification-Dataset')

train_ds = train_ds['train'].train_test_split(test_size=0.15)
train_data = train_ds['train']
test_data = train_ds['test']

label = list(set(train_data['label']))
id2label = {id: label for id, label in enumerate(label)}
label2id = {label: id for id, label in id2label.items()}

from transformers import ViTImageProcessor
processor = ViTImageProcessor.from_pretrained('google/vit-base-patch16-224-in21k')

from torchvision.transforms import CenterCrop, Compose, Normalize, RandomHorizontalFlip, RandomResizedCrop, Resize, ToTensor
image_mean, image_std = (processor.image_mean, processor.image_std)
size = processor.size['height']
normalize = Normalize(mean=image_mean, std=image_std)
_train_transforms = Compose([Resize((size, size)), RandomHorizontalFlip(), ToTensor(), normalize])
_val_transforms = Compose([Resize((size, size)), ToTensor(), normalize])
def train_transforms(examples):
    examples['pixel_values'] = [_train_transforms(image.convert('RGB')) for image in examples['image']]
    return examples
def val_transforms(examples):
    examples['pixel_values'] = [_val_transforms(image.convert('RGB')) for image in examples['image']]
    return examples

train_data.set_transform(train_transforms)
test_data.set_transform(val_transforms)

from torch.utils.data import DataLoader
import torch
def collate_fn(examples):
    pixel_values = torch.stack([example['pixel_values'] for example in examples])
    labels = torch.tensor([label2id[example['label']] for example in examples])
    return {'pixel_values': pixel_values, 'labels': labels}
train_dataloader = DataLoader(train_data, collate_fn=collate_fn, batch_size=4)
test_dataloader = DataLoader(test_data, collate_fn=collate_fn, batch_size=4)

from transformers import ViTForImageClassification
model = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224-in21k', id2label=id2label, label2id=label2id)

from transformers import TrainingArguments, Trainer
metric_name = 'accuracy'
args = TrainingArguments('5-Flower-Types-Classification', save_strategy='epoch', evaluation_strategy='epoch', learning_rate=2e-05, per_device_train_batch_size=32, per_device_eval_batch_size=4, num_train_epochs=5, weight_decay=0.01, load_best_model_at_end=True, metric_for_best_model=metric_name, logging_dir='logs', remove_unused_columns=False)

from sklearn.metrics import accuracy_score
import numpy as np
def compute_metrics(eval_pred):
    predictions, labels = eval_pred
    predictions = np.argmax(predictions, axis=1)
    return dict(accuracy=accuracy_score(predictions, labels))

import torch
trainer = Trainer(model, args, train_dataset=train_data, eval_dataset=test_data, data_collator=collate_fn, compute_metrics=compute_metrics, tokenizer=processor)

trainer.train()

outputs = trainer.predict(test_data)