So, I'm following this tutorial: https://www.youtube.com/watch?v=t6NI0u_lgNo&t=1826s and right after the tensorflow serving part I had been testing my fastapi API code which looks like this:
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import numpy as np
from io import BytesIO
from PIL import Image
import tensorflow as tf
import os
import requests
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
app = FastAPI()
endpoint = "http://localhost:8501/v1/models/plant_model:predict"
CLASS_NAMES = ['Potato___Early_blight',
'Potato___Late_blight',
'Potato___healthy',
'Tomato_Early_blight',
'Tomato_Late_blight',
'Tomato_healthy']
@app.get("/ping")
async def ping():
return "Hello, I am alive"
def read_file_as_image(data) -> np.ndarray:
image = np.array(Image.open(BytesIO(data)))
return image
@app.post("/predict")
async def predict(
file: UploadFile = File(...)
):
image = read_file_as_image(await file.read())
img_batch = np.expand_dims(image, 0)
json_data = {
"instances": img_batch.tolist()
}
response = requests.post(endpoint, json=json_data)
prediction = np.array(response.json()["predictions"][0])
predicted_class = CLASS_NAMES[np.argmax(prediction[0])]
confidence = np.max(prediction[0])
return {
'class': predicted_class,
'confidence': float(confidence)
}
if __name__ == "__main__":
uvicorn.run(app, host='localhost', port=8000)
By the way I'm using Ubuntu Ubuntu 20.04.
and I'm passing the image of a 255x255 leaf to it. (my model is made to classify different kinds of diseases for different kinds of vegetable leaves)
But, for some reason it always gives me this same false output:
"class": "Potato___Early_blight",
"confidence": 0.374938548
}
I also tried it with another leaf image but it's still the same just with a different confidence:
"class": "Potato___Early_blight",
"confidence": 1.21042137e-06
I can't post images here because my rank is too low
and here is the link to the AI google colab notebook I made for the AI:https://colab.research.google.com/drive/1i2v_RbZ8lI-e0joE-qBxym6_6xF5rR0g?usp=sharing
So, what am I doing wrong? I have checked other answers but they go into the specifics of the code instead of a general answer.