Tutorial

How to do Custom Image Classification (AutoML) Using Python

This guide explains how to use Eden AI's API in Python for custom image classification. It covers creating a project, uploading data, training a model, running a classification job, and retrieving results, with clear code examples.

How to do Custom Image Classification (AutoML) Using Python
TABLE OF CONTENTS

AutoML image classification allows you to build custom machine learning models for classifying images into categories without deep technical expertise.

With Eden AI, you can use its simple API to create a custom image classifier for your needs. This guide will take you through implementing AutoML image classiffication using Python.

What is Custom Image Classification?

Custom Image Classification with AutoML allows you to automatically create and train a machine learning model to classify images into your own specific categories.

Using an AutoML platform, you upload labeled images, and the tool automatically selects and tunes the best model to recognize and categorize new images based on your custom labels.

This process simplifies machine learning, making it accessible even to those without deep expertise in AI.

Implementing Custom Image Classification in Python

Get Access to Eden AI API

1. Create an Account: Visit Eden AI and sign up for an account. Once registered, navigate to the API section to obtain your API key. This key will give you access to a wide range of AI services including face comparison.

2. Navigate to Image Technologies – After logging in, go to the image section of the platform.

3. Select Custom Image Classification– Choose the Custom Image Classification feature or explore advanced options based on your needs.

Overview of the Process

The process is split into five stages to allow for clear, logical steps that ensure all parts of the project (creation, data uploading, training, prediction, and result retrieval) are managed separately.

This also helps avoid potential issues related to data size or processing time, as the operations are asynchronous.

  1. Creating a Project: This sets up the environment to store your data and model.
  2. Uploading Data: You need to upload and label images for model training.
  3. Training the Model: This is the phase where the model learns from the data.
  4. Launching a Classification Job: Once the model is trained, you can use it to classify new images.
  5. Getting Results: Finally, you fetch the predictions made by the trained model.

Getting Started: Installing Python Requests Module

To interact with Eden AI's API, you’ll need the requests module, which is a simple HTTP library for making API requests in Python. To install it, run:


pip install requests

Once installed, you can use requests to send requests to Eden AI’s endpoints.

Step 1: Creating a Project

The first step in the custom image classification process is to create a project. This will serve as the container for all your data and model settings.


import requests

headers = {"Authorization": "Bearer 🔑 Your_API_Key"}

url = "https://api.edenai.run/v2/image/automl_classification/create_project/"

payload = {
    "providers": ["nyckel"],  # Specify the AI provider
    "name": "Your_project_name"  # Name your project
}

response = requests.post(url, headers=headers, json=payload)
result = response.json()
print(result)

Explanation of the Code:

  • Authorization header: You must include your API key to authenticate the request.
  • providers: Specify the AI provider (in this case, "nyckel") that Eden AI will use for training your model.
  • name: The name you give your project to easily identify it later.

What this does:

This request creates a new project on Eden AI for your image classification. The response will contain a project_id, which will be used in subsequent stages to link your data and model.

Step 2: Uploading Your Data

Now that you’ve created your project, you need to upload your image data for training. Each image needs to be labeled with its category, and you must specify which data type you are uploading (e.g., TEST, TRAIN).


import requests

headers = {"Authorization": "Bearer 🔑 Your_API_Key"}

url = "https://api.edenai.run/v2/image/automl_classification/upload_data_async"

data = {
    "providers": ["nyckel"],  # Specify the AI provider
    "type_of_data": "TEST",  # Indicate whether the data is for training or testing
    "label": "🏷️ Label of your image",  # Label for the image
    "project_id": "🆔 ID of your project"  # ID of the project you created
}

files = {"file": open("🖼️ path/to/your/image.png", "rb")}  # Upload the image file

response = requests.post(url, headers=headers, data=data, files=files)
result = response.json()
print(result)

Explanation of the Code:

  • type_of_data: Specifies whether the data is for training or testing the model. In this case, we’re using TEST for validation.
  • label: The label for the uploaded image (e.g., a category like “cat” or “dog”).
  • file: The image file you want to upload for classification.

What this does:

This sends your labeled image data to Eden AI for use in training or testing your model. You can upload multiple images by repeating this request for each image.

Step 3: Training the Model

Once your data is uploaded, you need to train your model. The next step is to send a request to start the training process using your uploaded data.


import requests

headers = {"Authorization": "Bearer 🔑 Your_API_Key"}

url = "https://api.edenai.run/v2/image/automl_classification/train_async"

payload = {"providers": ["nyckel"], "project_id": "🆔 ID of your project"}

response = requests.post(url, headers=headers, json=payload)
result = response.json()
print(result)

Explanation of the Code:

  • project_id: This links the request to the project you previously created.
  • train_async: This is an asynchronous request, meaning the training will happen in the background.

What this does:

This sends a request to Eden AI to begin training your custom classification model using the data you uploaded. The training may take some time depending on the amount of data.

Step 4: Launching a Classification Job

After training your model, you can now classify new images using the model you trained. The following code sends an image to the trained model for classification.


import requests

headers = {"Authorization": "Bearer 🔑 Your_API_Key"}

url = "https://api.edenai.run/v2/image/automl_classification/predict_async"

data = {
    "providers": ["nyckel"],  # Specify the AI provider
    "project_id": "🆔 ID of your project"  # The project ID of your trained model
}

files = {"file": open("🖼️ path/to/your/image.png", "rb")}  # Image file to classify

response = requests.post(url, headers=headers, data=data, files=files)
result = response.json()
print(result)

Explanation of the Code:

  • predict_async: This endpoint sends the image to the model and classifies it asynchronously.
  • file: The image file you want to classify is sent as part of the request.

What this does:

This sends the image to your model for classification and returns the predicted label.

Step 5: Getting the Results

After launching the classification job, you can retrieve the classification results. The results are usually provided with a public_id, which you can use to check the classification.


import requests

headers = {"Authorization": "Bearer 🔑 Your_API_Key"}

url = "https://api.edenai.run/v2/image/automl_classification/predict_async/<public_id>"

response = requests.get(url, headers=headers)
result = response.json()
print(result)

Explanation of the Code:

  • public_id: This is a unique identifier returned from the classification job. You need to use this to check the result.
  • get request: This retrieves the result of the classification once it's processed.

What this does:

This request retrieves the classification results, showing you the predicted label and confidence score for the image.

Interpreting the Results

Here’s an example of what you might see when you retrieve the results:


{
  "predictions": [
    {
      "label": "Cat",
      "confidence": 0.92
    }
  ]
}

Explanation of Output Fields:

  • label: The predicted category for the image (e.g., "Cat").
  • confidence: The model’s confidence in the prediction (e.g., 92%).

Going Further

For even more customization and better management of your AutoML classification tasks, Eden AI provides additional endpoints that allow you to fine-tune your workflows.

Whether you need to list, launch, or retrieve results for various jobs such as training, prediction, or data uploading, these endpoints offer you greater flexibility and control.

You can explore all available options and detailed instructions in our comprehensive documentation here.

Why Eden AI is the Best Tool for Custom Image Classification

Eden AI offers several advantages for Custom Image Classification.

Access to multiple providers

With Eden AI, you can choose from a variety of providers, giving you great flexibility.

Ease of use

Eden AI’s API is designed to be simple and intuitive, making it easy for developers to integrate many AI services into their applications with minimal effort.

Scalability

Whether you’re working on small projects or large-scale applications, Eden AI is built to scale with your needs, making it suitable for a wide range of use cases.

Conclusion:

Using Eden AI’s API, you can easily create custom image classification models without needing deep machine learning knowledge.

By following the five-stage process (creating a project, uploading data, training, predicting, and retrieving results), you can quickly implement and deploy powerful image classification models for your own applications.

Start Your AI Journey Today

  • Access 100+ AI APIs in a single platform.
  • Compare and deploy AI models effortlessly.
  • Pay-as-you-go with no upfront fees.
Start building FREE

Related Posts

Try Eden AI for free.

You can directly start building now. If you have any questions, feel free to chat with us!

Get startedContact sales
X

Start Your AI Journey Today

Sign up now with free credits to explore 100+ AI APIs.
Get my FREE credits now