Skip to content

dshkliarenko/color-detection

Repository files navigation

Color detection API

Description

REST API which serves two models based on different approaches: one developed with Scikit-Learn and one with PyTorch. These models have been trained to classify color of samples based on input vector. API will take one or multiple samples of input and produce an output with the probabilities for each of the colors.

API contains following endpoints:

  • /sklearn, which uses sklearn model
  • /pytorch, which uses pytorch model
  • /universal, which allows to specify model

Service listens to port 3000

Requests

Example of a POST request to sklearn or pytorch endpoint:

{
  "sampleData": [[0.92, 0.12, 0.31, 0.09]]
}
{
  "sampleData": [
    [0.92, 0.12, 0.31, 0.09],
    [0.31, 0.54, 0.78, 0.17],
    [0.38, 0.04, 0.21, 0.98]
    ]
}

Example of a POST request to the universal endpoint:

{
  "sampleData": [[0.92, 0.12, 0.31, 0.09]],
  "model": "pytorch"
}

The model field can take two valid values: "pytorch" or "sklearn".

Responses

The labels are the following:

["blue", "green", "yellow"]

These correspond to the following outputs in the models:

[0, 1, 2]
  • Example for single predictions:
{
  "prediction": [
    "blue"
  ],
  "scores": [
    {
      "blue": 0.9799801195910143,
      "green": 0.02001979984409025,
      "yellow": 8.056489543610177e-8
    }
  ]
}
  • Example for batch predictions
{
  "prediction": [
    "blue",
    "blue",
    "green"
  ],
  "scores": [
    {
      "blue": 0.9927769331813387,
      "green": 0.007223053394387628,
      "yellow": 1.3424273700413213e-8
    },
    {
      "blue": 0.9924667517303258,
      "green": 0.007533175370560812,
      "yellow": 7.28991131694263e-8
    },
    {
      "blue": 0.007533175370560812,
      "green": 0.9954911544544908,
      "yellow": 1.0907653719137907e-7
    }
  ]
}

Possible response status codes

  • 200 Success
  • 422 The data is corrupt (either missing or extra dimensions, other type of values, etc)
  • 400 The chosen model in universal endpoint is not "sklearn" or "pytorch"
  • 500 Prediction fails

User instructions

Run application locally

  1. Create and activate virtual environment (optional)
  2. Install dependencies
pip install -r requirements.txt
  1. Start server
uvicorn api:app --host localhost --port 3000

Run application in container

docker build -f Dockerfile -t classifier-app .
docker run classifier-app

Run application in local cluster (minikube)

  1. Start minikube and configure local Docker client
minikube start
eval $(minikube -p minikube docker-env)
  1. Deploy application with load balancer using minikube tunnel:
make deploy
minikube tunnel
  1. Clean-up (optional):
make clean-up

Send request to the application

  • /sklearn
curl -X POST http://127.0.0.1:3000/sklearn -H "Content-Type: application/json" -d '{"sampleData": [[0.1, 0.2, 0.3, 0.1]]}'
  • /pytorch
curl -X POST http://127.0.0.1:3000/pytorch -H "Content-Type: application/json" -d '{"sampleData": [[0.1, 0.2, 0.3, 0.1]]}'
  • /universal
curl -X POST http://127.0.0.1:3000/universal -H "Content-Type: application/json" -d '{"sampleData": [[0.1, 0.2, 0.3, 0.1]], "model": "pytorch"}'

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published