Data Science | Machine Learning with Python for Researchers
32.5K subscribers
2.99K photos
104 videos
22 files
3.21K links
ads: @HusseinSheikho

The Data Science and Python channel is for researchers and advanced programmers

Buy ads: https://telega.io/c/dataScienceT
Download Telegram
💡 ViT for Fashion MNIST Classification

This lesson demonstrates how to use a pre-trained Vision Transformer (ViT) to classify an image from the Fashion MNIST dataset. ViT treats an image as a sequence of patches, similar to how language models treat sentences, making it a powerful architecture for computer vision tasks. We will use a model from the Hugging Face Hub that is already fine-tuned for this specific dataset.

from transformers import ViTImageProcessor, ViTForImageClassification
from datasets import load_dataset
import torch

# 1. Load a model fine-tuned on Fashion MNIST and its processor
model_name = "abhishek/autotrain-fashion-mnist-283834433"
processor = ViTImageProcessor.from_pretrained(model_name)
model = ViTForImageClassification.from_pretrained(model_name)

# 2. Load the dataset and get a sample image
dataset = load_dataset("fashion_mnist", split="test")
image = dataset[100]['image'] # Get the 100th image

# 3. Preprocess the image and prepare it for the model
inputs = processor(images=image, return_tensors="pt")

# 4. Perform inference to get the classification logits
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits

# 5. Get the predicted class and its label
predicted_class_idx = logits.argmax(-1).item()
predicted_class = model.config.id2label[predicted_class_idx]

print(f"Image is a: {dataset[100]['label']}")
print(f"Model predicted: {predicted_class}")


Code explanation: This script uses the transformers library to load a ViT model specifically fine-tuned for Fashion MNIST classification. It then loads the dataset, selects a single sample image, and uses the model's processor to convert it into the correct input format. The model performs inference, and the script identifies the most likely class from the output logits, printing the final human-readable prediction.

#Python #MachineLearning #ViT #ComputerVision #HuggingFace

━━━━━━━━━━━━━━━
By: @DataScienceT
💡 ViT for Fashion MNIST Classification

This lesson demonstrates how to use a pre-trained Vision Transformer (ViT) to classify an image from the Fashion MNIST dataset. ViT treats an image as a sequence of patches, similar to how language models treat sentences, making it a powerful architecture for computer vision tasks. We will use a model from the Hugging Face Hub that is already fine-tuned for this specific dataset.

from transformers import ViTImageProcessor, ViTForImageClassification
from datasets import load_dataset
import torch

# 1. Load a model fine-tuned on Fashion MNIST and its processor
model_name = "abhishek/autotrain-fashion-mnist-283834433"
processor = ViTImageProcessor.from_pretrained(model_name)
model = ViTForImageClassification.from_pretrained(model_name)

# 2. Load the dataset and get a sample image
dataset = load_dataset("fashion_mnist", split="test")
image = dataset[100]['image'] # Get the 100th image

# 3. Preprocess the image and prepare it for the model
inputs = processor(images=image, return_tensors="pt")

# 4. Perform inference to get the classification logits
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits

# 5. Get the predicted class and its label
predicted_class_idx = logits.argmax(-1).item()
predicted_class = model.config.id2label[predicted_class_idx]

print(f"Image is a: {dataset[100]['label']}")
print(f"Model predicted: {predicted_class}")


Code explanation: This script uses the transformers library to load a ViT model specifically fine-tuned for Fashion MNIST classification. It then loads the dataset, selects a single sample image, and uses the model's processor to convert it into the correct input format. The model performs inference, and the script identifies the most likely class from the output logits, printing the final human-readable prediction.

#Python #MachineLearning #ViT #ComputerVision #HuggingFace

━━━━━━━━━━━━━━━
By: @DataScienceT