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