How to Create a Big Cat Classifier with Vision Transformers

Category :

Ever wanted to distinguish between majestic big cats like tigers, lions, and leopards? With modern machine learning techniques, specifically using Vision Transformers (ViTs), you can build an image classifier that identifies these fascinating species from images. In this guide, we will take you through the setup, usage, and troubleshooting processes of creating your very own Big Cat Classifier.

Overview of the Big Cat Classifier

The Big Cat Classifier is a remarkable image classification model designed to categorize various species of big cats, including:

  • Cheetah
  • Jaguar
  • Leopard
  • Lion
  • Tiger

Here’s the accuracy of the model:

Accuracy: 0.9107

Big Cat Classifier

Training Process

To harness the power of this model, you will first need to train it. The training process involves working with datasets that contain images of big cats. You can conveniently use Google Colab to streamline this process. Click the following link to open the training notebook:

Open In Colab

Inference Process

Once the model is trained, you can test its performance on new images by running an inference. Use the following link to access the inference notebook:

Open In Colab

Usage Guidelines

To classify an image using the Big Cat Classifier, you can follow these steps:

python
from PIL import Image
import matplotlib.pyplot as plt
from transformers import ViTFeatureExtractor, ViTForImageClassification

def identify_big_cat(img_path: str) -> str:
    img = Image.open(img_path)
    model_panthera = ViTForImageClassification.from_pretrained("smaranjitghose/big-cat-classifier")
    feature_extractor = ViTFeatureExtractor.from_pretrained("smaranjitghose/big-cat-classifier")
    inputs = feature_extractor(images=img, return_tensors="pt")
    outputs = model_panthera(**inputs)
    logits = outputs.logits
    predicted_class_idx = logits.argmax(-1).item()
    return model_panthera.config.id2label[predicted_class_idx]

path_of_the_image = "path/to/your/big_cat_image.jpg"
our_big_cat = identify_big_cat(path_of_the_image)
print(f"Predicted species: {our_big_cat}")

Understanding the Code with an Analogy

Consider the identify_big_cat function as a well-trained zookeeper at a wildlife sanctuary. Here’s how they operate:

  • Opening the Cage: When the zookeeper accesses an image, it’s like opening the cage when a visitor wants to see a big cat.
  • Getting the Model Ready: The zookeeper has a guidebook, which represents our pre-trained model, ready to recognize the various cats.
  • Viewing the Cat: The feature extractor prepares the image for analysis just as if the zookeeper cleans and prepares the environment for a closer look.
  • Identifying the Species: After examining the cat, our proficient zookeeper can identify the species using their guidebook by matching observations to descriptions.

Running the Application

You can run the application either without Docker or using Docker. Follow the steps accordingly:

Without Using Docker

  • Ensure you have the latest version of Python 3 installed.
  • Install the Python dependencies:
  • pip install -r requirements.txt
  • Start the Streamlit app on a local server:
  • streamlit run app.py

Using Docker

  • Ensure you have Docker installed.
  • Build the Docker image:
  • docker build -t smaranjitghose/big-cat-classifier:latest .
  • Run the Docker container:
  • docker run -t -i -p 8080:8080 --name big-cat-classifier smaranjitghose/big-cat-classifier
  • Visit the app on your browser at localhost:8080.
Streamlit App

Hosting on Heroku

If you’re interested in hosting your application on Heroku, here’s a quick guide:

  • Remove any lines exposing a specific port in the Docker container.
  • Ensure the startup command is exposed with a variable port number:
  • ENTRYPOINT [streamlit, run, app.py, --server.port=$PORT]
  • Log in to Heroku:
  • heroku login -i
  • Create a new Heroku app:
  • heroku create
  • Log in to the Container Registry:
  • heroku container:login
  • Build and push the Docker image:
  • heroku container:push web
  • Release the app:
  • heroku container:release web
  • Check the hosted version:
  • heroku open

Troubleshooting

If you encounter any issues while using the Big Cat Classifier, consider the following troubleshooting suggestions:

  • Ensure that your image path is correct.
  • Double-check that all necessary dependencies are installed.
  • If using Docker, verify that the Docker daemon is running properly.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

References

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox

Latest Insights

© 2024 All Rights Reserved

×