Food Image Classification with Hugging Face
A popular social media platform dedicated to food enthusiasts wants to improve user engagement by adding advanced image recognition features. As a machine learning engineer, you are tasked with developing a food image classification system using Hugging Face's state-of-the-art models. This system will automatically identify and categorize food items in user-uploaded photos, allowing for better content organization and personalized food content recommendations.
Your responsibility is to develop a robust food category image classification system using pre-trained models from Hugging Face.
By leveraging Hugging Face's powerful models, the goal is to enhance user interaction by providing accurate food classification, enabling users to easily find and engage with content related to their favorite foods, and improving the overall experience on the platform.
In this dynamic project, we leverage the power of PyTorch and transformers, utilizing an open-source model from Hugging Face as the backbone of our solution. Your objectives are:
- Creating an uploader widget for an image file, storing the file, and displaying it.
- Using Hugging Face's most trending Image Classification model for food categories, employing the pipeline() function to classify the food category of the uploaded image and print the food category with the highest likelihood.
This project offers an opportunity to harness the potential of machine learning to revolutionize user engagement on a social media platform. Let's delve into the interplay between technology and user experience, unlocking new possibilities for enhancing content interaction and personalization.
# Install required libraries
!pip install matplotlib
!pip install pillow
!pip install torch
!pip install transformers
!pip install tf-keras# Import required libraries
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import torch
from transformers import pipeline
from transformers.utils import logging
logging.set_verbosity_error()
import warnings
warnings.filterwarnings("ignore")# Read the image
image = Image.open('Food Pictures/food_6.png').convert("RGB") # Ensure the image is in RGB format
# Display the image
plt.imshow(image)
plt.axis('off')
plt.show()# Define the model ID for the pre-trained food category classification model
modelId = "Kaludi/food-category-classification-v2.0"
# Load the image classification pipeline using the specified model ID
model = pipeline("image-classification", model=modelId)
# Save the pre-trained model to a local directory
model.save_pretrained(save_directory=f"models/{modelId}")# Get the probabilities across different food categories
class_scores=model(image)
print(class_scores)
# Get the class with the highest score
highest_probability_class = max(class_scores, key=lambda x: x['score'])
# Print the label of the highest probability class for the uploaded image
print(f"Predicted food category: {highest_probability_class['label']}")