Categories:

Social Share

Sample Image Classification model deployment for reference

The following files can be used as a reference for Image Classification inference - Pneumonia Prediction - Cluzters Vault and Google Docs - Cluzter Model Guidelines


Guidelines

Inference.zip Folder structure:

requirements.txt

Trained Model file

inference.py

Other files and folders used


Inference.py file format:

Import Statements

Onetime executable operations

{Ex: Loading the Model, label encoding  etc.}

def predict(Input arguments as per the use-case)

{

Data Preprocessing

Inference

Return output based on the use-case

}

*Do not change the naming convention for the entities marked inblue
  • inference.py and requirements.txt are mandatory files
  • inference.py should contain a function predict which takes one argument - a string (image path). The return value should be a string(prediction text displayed to the user).


Sample inference.py
  import numpy as np from tensorflow.keras.preprocessing.image import load_img from tensorflow.keras.applications.vgg16 import preprocess_input from tensorflow.keras.preprocessing.image import array_to_img, img_to_array from tensorflow.keras.models import load_model classes = ["person", "car", "truck"] model = load_model("model.h5") def predict(img_path): #mandatory: function name should be predict and it accepts a string which is image location image = load_img(img_path, target_size=(224, 224)) image = img_to_array(image) image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) image = preprocess_input(image) yhat = model.predict(image) yhat = np.array(yhat) indices = np.argmax(yhat, axis=1) scores = yhat[np.arange(len(yhat)), indices] predicted_categories = [classes[i] for i in indices] output = predicted_categories[0] return output #mandatory: the return should be a string​​​

Sample requirements.txt
tensorflow==2.4 numpy pillow​ ​​