Llama 3.2 Image Classification
Using Llama 3.2 Vision for Image Classification
In our previous tutorial on Llama 3.2 Vision, we used function calling capabilities to identify the dog breed from an image and use it to call the API Ninjas Dogs API to extract additional information about the breed. In this tutorial we'll take it a step further to identify more features about the images and create a robust image classification tool for those same dog pictures.
To do so we'll use Llama 3.2 Vision in conjunction with Groq API's JSON mode to extract specific information from the provided image, such as the breed of dog or its characteristics. By doing so, we'll show how to create a pipeline for image classification and data extraction, converting unstructured images into structured data that can be easily searched, filtered, and analyzed.
Setup
To complete this tutorial, you'll need a Groq API key. If you don't already have one, you can easily create a free account on GroqCloud by clicking here and generating your API key. Once you have your API key, you'll need to set it as an environment variable called GROQ_API_KEY for the tutorial to work. We'll be working with the llama-3.2-11b-vision-dev model throughout this tutorial.
Basic LLaVA usage
For this tutorial, we'll be using the same set of dog images from Britannica that we utilized in the other tutorials. You can access these images in the Groq API cookbook repository, specifically in the images folder. We'll be encoding a local image to base64 to integrate it with Llama 3.2 Vision. This is one of two methods to use an image with the vision model - the other approach involves providing the actual URL of the image.
To use locally saved images with Llama 3.2 Vision, we'll need to encode them to base64 first:
Analyzing an image with Llama 3.2 Vision using Groq API functions the same as with LLaVA. We'll make a request to the https://api.groq.com/openai/v1/chat/completions API endpoint, passing the image and prompt in a JSON structure that includes the image path:
The image shows a side view of a black and tan dachshund. The dog's dark brown coat is emphasized more on the outer body, with light brown splotches running on its throat, chest, and paws. Its tail is plaited/braided and held over the dog's back, and its ears, which are floppy, are tucked over its head. The dog is standing on a paved surface, indicating that the photo may have been taken in a purpose-built dog training area, dog park, or outside an animal clinic. The background is out of focus and appears to be a white wall made of tiles with a line of darker brown trees. Overall, the image is well-composed, making the dog the clear focus of the shot.
However, Llama 3.2 Vision offers more functionality beyond just image recognition. It offers most of the functionality of any other chat model, including tool use and JSON mode (the only thing it can't do is process a system prompt). By leveraging JSON mode, we can utilize Llama 3.2 Vision to identify the breed of the dog in the image and return the result in a structured, easily digestible format. Note that to use JSON mode in the Groq API you must include "JSON" in your system or user prompt, and specify the desired output (see this cookbook tutorial for more information):
{'dog_breed': 'Dachshund'} Image Classification
With JSON mode enabled, we can leverage Llama 3.2 Vision for robust image classification at scale, making it easy to analyze and categorize large datasets. By iterating through each image in the folder, we can utilize Llama 3.2 Vision to classify them based on various features, such as breed, color, number of dogs, dog activity, and more. This functionality could be particularly valuable for a pet adoption agency, which could use it to streamline the adoption process by uploading an image of a new adoptable pet and instantly making it searchable and filterable by certain features on their website.
First, let's expand our JSON mode prompt to include an array of additional features, and test it on our Dachshund image:
{'dog_breed': 'Dachshund',
, 'dog_color': 'Black and Tan',
, 'number_of_dogs': 1,
, 'puppies_present': False,
, 'dog_size': 'Small',
, 'is_pet': True,
, 'dog_activity': 'Standing',
, 'background_environment': 'Outdoors',
, 'dog_accessories': 'None'} Looks good! Now, we can apply it to each dog image in our file folder and append the JSON outputs to a list:
Finally, we'll convert the results to a pandas dataframe. Now we have a structured table of features from all 18 dog images that can be processed, analyzed, or stored in a database:
Conclusion
One of the most valuable things LLMs can do is convert unstructured data to structured data, and as we've seen in this tutorial, you can use Llama 3.2 Vision to do just that with image data. This ability to extract meaningful features from images opens up a wide range of possibilities for applications such as image classification, object detection, and more, and we're excited to see the innovative solutions that developers will create using Llama 3.2 Vision on Groq API!