Setting Up an Image Classification System on Linux
This guide provides detailed instructions on how to set up an Image Classification system on Ubuntu using LLaMA CPP. Image classification is a fundamental task in computer vision that involves assigning a label to an input image from a predefined set of categories.
1. Install System Prerequisites
First, ensure your Ubuntu system is updated and that Python and Pip are installed. Open a terminal and run the following commands:
sudo apt update
sudo apt upgrade
sudo apt install python3 python3-pip git cmake build-essential
This installs the necessary system dependencies like Python, Pip, Git, and CMake for building the LLaMA CPP library.
2. Install LLaMA CPP
Clone the LLaMA CPP repository and navigate to the directory:
git clone https://github.com/your-repo/llama.cpp.git
cd llama.cpp
3. Build the LLaMA CPP Library
After navigating to the cloned repository directory, build the LLaMA CPP library using the following command:
make
This command will compile the source code of LLaMA CPP and create the necessary binaries for use in image classification tasks.
4. Install Required Python Libraries
Install additional Python libraries that will be needed for image classification:
pip install torch torchvision numpy opencv-python
These libraries include PyTorch for deep learning, Torchvision for loading datasets, Numpy for numerical operations, and OpenCV for image processing.
5. Create a Python Script for Image Classification
Create a new Python script named image_classification.py
that will use LLaMA CPP along with PyTorch and Torchvision for image classification:
nano image_classification.py
Paste the following code into the file:
import torch
import torchvision.transforms as transforms
from torchvision import models
from PIL import Image
import requests
# Load the pre-trained model (ResNet)
def load_model():
model = models.resnet50(pretrained=True)
model.eval() # Set the model to evaluation mode
return model
# Preprocess the image
def preprocess_image(image_path):
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
image = Image.open(image_path)
image = transform(image).unsqueeze(0) # Add batch dimension
return image
# Classify the image
def classify_image(model, image_tensor):
with torch.no_grad():
output = model(image_tensor)
_, predicted_class = torch.max(output, 1)
return predicted_class.item()
# Load the ImageNet labels
def load_labels():
labels_url = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json"
response = requests.get(labels_url)
labels = response.json()
return labels
# Main function
if __name__ == "__main__":
model = load_model()
labels = load_labels()
image_path = "path/to/your/image.jpg" # Replace with the path to your image
image_tensor = preprocess_image(image_path)
predicted_class = classify_image(model, image_tensor)
print(f"Predicted class: {labels[predicted_class]}")
This script performs the following steps:
- Loads a pre-trained ResNet-50 model from PyTorch's
torchvision
library. - Preprocesses the input image to match the model's input size and normalization requirements.
- Performs image classification using the ResNet-50 model.
- Maps the predicted class index to a human-readable label from ImageNet.
6. Download Pre-Trained Model Weights
The ResNet-50 model is pre-trained on the ImageNet dataset and its weights will be automatically downloaded the first time you run the script. Make sure you have a stable internet connection when executing the script for the first time.
7. Run the Image Classification Script
Once the script is ready, you can run it using the following command:
python3 image_classification.py
Make sure to replace path/to/your/image.jpg
with the actual path to the image you want to classify. The script will print the predicted class label for the input image.
8. Troubleshooting
If you encounter issues, ensure that:
- All required Python libraries (PyTorch, Torchvision, Numpy, OpenCV) are correctly installed.
- The input image path is correct and points to an image file.
- You have internet access for downloading model weights and label files.
9. Conclusion
You have successfully set up an Image Classification system on Ubuntu using LLaMA CPP and a pre-trained ResNet-50 model. This system can now classify images from various categories and can be adapted for other image classification models or datasets.