espejelomar commited on
Commit
923f66c
1 Parent(s): cdd2963

adding backend folder

Browse files
backend/__init__.py ADDED
File without changes
backend/config.json ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "id2label": {
3
+ "0": "Abyssinian",
4
+ "1": "Bengal",
5
+ "2": "Birman",
6
+ "3": "Bombay",
7
+ "4": "British_Shorthair",
8
+ "5": "Egyptian_Mau",
9
+ "6": "Maine_Coon",
10
+ "7": "Persian",
11
+ "8": "Ragdoll",
12
+ "9": "Russian_Blue",
13
+ "10": "Siamese",
14
+ "11": "Sphynx",
15
+ "12": "american_bulldog",
16
+ "13": "american_pit_bull_terrier",
17
+ "14": "basset_hound",
18
+ "15": "beagle",
19
+ "16": "boxer",
20
+ "17": "chihuahua",
21
+ "18": "english_cocker_spaniel",
22
+ "19": "english_setter",
23
+ "20": "german_shorthaired",
24
+ "21": "great_pyrenees",
25
+ "22": "havanese",
26
+ "23": "japanese_chin",
27
+ "24": "keeshond",
28
+ "25": "leonberger",
29
+ "26": "miniature_pinscher",
30
+ "27": "newfoundland",
31
+ "28": "pomeranian",
32
+ "29": "pug",
33
+ "30": "saint_bernard",
34
+ "31": "samoyed",
35
+ "32": "scottish_terrier",
36
+ "33": "shiba_inu",
37
+ "34": "staffordshire_bull_terrier",
38
+ "35": "wheaten_terrier",
39
+ "36": "yorkshire_terrier"
40
+ }
41
+ }
backend/pipeline.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from PIL import Image
3
+ import os
4
+ import json
5
+ import numpy as np
6
+ from fastai.learner import load_learner
7
+
8
+
9
+ class PreTrainedPipeline:
10
+ def __init__(self, path=""):
11
+ # IMPLEMENT_THIS
12
+ # Preload all the elements you are going to need at inference.
13
+ # For instance your model, processors, tokenizer that might be needed.
14
+ # This function is only called once, so do all the heavy processing I/O here"""
15
+ self.model = load_learner(os.path.join(path, "export.pkl"))
16
+ with open(os.path.join(path, "config.json")) as config:
17
+ config = json.load(config)
18
+ self.id2label = config["id2label"]
19
+
20
+ def __call__(self, inputs: "Image.Image") -> List[Dict[str, Any]]:
21
+ """
22
+ Args:
23
+ inputs (:obj:`PIL.Image`):
24
+ The raw image representation as PIL.
25
+ No transformation made whatsoever from the input. Make all necessary transformations here.
26
+ Return:
27
+ A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82}
28
+ It is preferred if the returned list is in decreasing `score` order
29
+ """
30
+ # IMPLEMENT_THIS
31
+ # FastAI expects a np array, not a PIL Image.
32
+ _, _, preds = self.model.predict(np.array(inputs))
33
+ preds = preds.tolist()
34
+ labels = [
35
+ {"label": str(self.id2label["0"]), "score": preds[0]},
36
+ {"label": str(self.id2label["1"]), "score": preds[1]},
37
+ {"label": str(self.id2label["2"]), "score": preds[2]},
38
+ {"label": str(self.id2label["3"]), "score": preds[3]},
39
+ {"label": str(self.id2label["4"]), "score": preds[4]},
40
+ {"label": str(self.id2label["5"]), "score": preds[5]},
41
+ {"label": str(self.id2label["6"]), "score": preds[6]},
42
+ {"label": str(self.id2label["7"]), "score": preds[7]},
43
+ {"label": str(self.id2label["8"]), "score": preds[8]},
44
+ {"label": str(self.id2label["9"]), "score": preds[9]},
45
+ {"label": str(self.id2label["10"]), "score": preds[10]},
46
+ {"label": str(self.id2label["11"]), "score": preds[11]},
47
+ {"label": str(self.id2label["12"]), "score": preds[12]},
48
+ {"label": str(self.id2label["13"]), "score": preds[13]},
49
+ {"label": str(self.id2label["14"]), "score": preds[14]},
50
+ {"label": str(self.id2label["15"]), "score": preds[15]},
51
+ {"label": str(self.id2label["16"]), "score": preds[16]},
52
+ {"label": str(self.id2label["17"]), "score": preds[17]},
53
+ {"label": str(self.id2label["18"]), "score": preds[18]},
54
+ {"label": str(self.id2label["19"]), "score": preds[19]},
55
+ {"label": str(self.id2label["20"]), "score": preds[20]},
56
+ {"label": str(self.id2label["21"]), "score": preds[21]},
57
+ {"label": str(self.id2label["22"]), "score": preds[22]},
58
+ {"label": str(self.id2label["23"]), "score": preds[23]},
59
+ {"label": str(self.id2label["24"]), "score": preds[24]},
60
+ {"label": str(self.id2label["25"]), "score": preds[25]},
61
+ {"label": str(self.id2label["26"]), "score": preds[26]},
62
+ {"label": str(self.id2label["27"]), "score": preds[27]},
63
+ {"label": str(self.id2label["28"]), "score": preds[28]},
64
+ {"label": str(self.id2label["29"]), "score": preds[29]},
65
+ {"label": str(self.id2label["30"]), "score": preds[30]},
66
+ {"label": str(self.id2label["31"]), "score": preds[31]},
67
+ {"label": str(self.id2label["32"]), "score": preds[32]},
68
+ {"label": str(self.id2label["33"]), "score": preds[33]},
69
+ {"label": str(self.id2label["34"]), "score": preds[34]},
70
+ {"label": str(self.id2label["35"]), "score": preds[35]},
71
+ {"label": str(self.id2label["36"]), "score": preds[36]},
72
+ ]
73
+ return labels
backend/util.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from backend.pipeline import PreTrainedPipeline
4
+ import pandas as pd
5
+ import io
6
+ import matplotlib.pyplot as plt
7
+ import numpy as np
8
+
9
+
10
+ def import_fig():
11
+ image = st.file_uploader("Upload your picture.", type=["png", "jpg", "jpeg"])
12
+ if image:
13
+ bytes_image = image.getvalue()
14
+ image = Image.open(io.BytesIO(bytes_image))
15
+ st.image(image, caption=["We are classifying this image..."])
16
+ return image
17
+
18
+
19
+ def plot(data=None):
20
+
21
+ fig = plt.figure()
22
+ ax = fig.add_axes([0, 0, 1, 1])
23
+ breeds = data.head(3)["label"].tolist()
24
+ labels = data.head(3)["score"].tolist()
25
+ ax.bar(breeds, labels)
26
+ ax.set_ylabel("Probability that your pet is breed X")
27
+ ax.grid("on")
28
+
29
+ st.pyplot(fig)
30
+
31
+
32
+ @st.cache(allow_output_mutation=True)
33
+ def fastai_model(image):
34
+ if image:
35
+ model = PreTrainedPipeline(path="backend")
36
+ outputs = model(image)
37
+
38
+ outputs_df = pd.DataFrame(outputs)
39
+
40
+ return outputs_df.sort_values(by=["score"], ascending=False)