from typing import Dict, List, Any from fastai.learner import load_learner from PIL import Image import os import json import numpy as np class PreTrainedPipeline: def __init__(self, path=""): # IMPLEMENT_THIS # Preload all the elements you are going to need at inference. # For instance your model, processors, tokenizer that might be needed. # This function is only called once, so do all the heavy processing I/O here""" self.model = load_learner(os.path.join(path, "export.pkl")) with open(os.path.join(path, "config.json")) as config: config = json.load(config) self.id2label = config["id2label"] def __call__(self, inputs: "Image.Image") -> List[Dict[str, Any]]: """ Args: inputs (:obj:`PIL.Image`): The raw image representation as PIL. No transformation made whatsoever from the input. Make all necessary transformations here. Return: A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82} It is preferred if the returned list is in decreasing `score` order """ # IMPLEMENT_THIS # FastAI expects a np array, not a PIL Image. _, _, preds = self.model.predict(np.array(inputs)) preds = preds.tolist() labels = [ {"label": str(self.id2label["0"]), "score": preds[0]}, {"label": str(self.id2label["1"]), "score": preds[1]}, {"label": str(self.id2label["2"]), "score": preds[2]}, {"label": str(self.id2label["3"]), "score": preds[3]}, {"label": str(self.id2label["4"]), "score": preds[4]}, {"label": str(self.id2label["5"]), "score": preds[5]}, {"label": str(self.id2label["6"]), "score": preds[6]}, {"label": str(self.id2label["7"]), "score": preds[7]}, {"label": str(self.id2label["8"]), "score": preds[8]}, {"label": str(self.id2label["9"]), "score": preds[9]}, {"label": str(self.id2label["10"]), "score": preds[10]}, {"label": str(self.id2label["11"]), "score": preds[11]}, {"label": str(self.id2label["12"]), "score": preds[12]}, {"label": str(self.id2label["13"]), "score": preds[13]}, {"label": str(self.id2label["14"]), "score": preds[14]}, {"label": str(self.id2label["15"]), "score": preds[15]}, {"label": str(self.id2label["16"]), "score": preds[16]}, {"label": str(self.id2label["17"]), "score": preds[17]}, {"label": str(self.id2label["18"]), "score": preds[18]}, {"label": str(self.id2label["19"]), "score": preds[19]}, {"label": str(self.id2label["20"]), "score": preds[20]}, {"label": str(self.id2label["21"]), "score": preds[21]}, {"label": str(self.id2label["22"]), "score": preds[22]}, {"label": str(self.id2label["23"]), "score": preds[23]}, {"label": str(self.id2label["24"]), "score": preds[24]}, {"label": str(self.id2label["25"]), "score": preds[25]}, {"label": str(self.id2label["26"]), "score": preds[26]}, {"label": str(self.id2label["27"]), "score": preds[27]}, {"label": str(self.id2label["28"]), "score": preds[28]}, {"label": str(self.id2label["29"]), "score": preds[29]}, {"label": str(self.id2label["30"]), "score": preds[30]}, {"label": str(self.id2label["31"]), "score": preds[31]}, {"label": str(self.id2label["32"]), "score": preds[32]}, {"label": str(self.id2label["33"]), "score": preds[33]}, {"label": str(self.id2label["34"]), "score": preds[34]}, {"label": str(self.id2label["35"]), "score": preds[35]}, {"label": str(self.id2label["36"]), "score": preds[36]}, ] return labels