Simple debug version
Browse files- pipeline.py +89 -10
pipeline.py
CHANGED
@@ -1,9 +1,13 @@
|
|
1 |
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
import streamlit as st
|
5 |
-
import tensorflow as tf
|
6 |
-
from tensorflow.keras.models import load_model
|
7 |
|
8 |
# most of this code has been obtained from Datature's prediction script
|
9 |
# https://github.com/datature/resources/blob/main/scripts/bounding_box/prediction.py
|
@@ -11,20 +15,95 @@ from tensorflow.keras.models import load_model
|
|
11 |
class PreTrainedPipeline():
|
12 |
def __init__(self, path: str):
|
13 |
# load the model
|
14 |
-
self.model =
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
22 |
|
23 |
labels = []
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
return labels
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
# # -----------------
|
30 |
# def load_model():
|
|
|
1 |
import cv2
|
2 |
+
import json
|
3 |
+
from typing import Any, Dict, List
|
4 |
+
import tensorflow as tf
|
5 |
+
import base64
|
6 |
+
import io
|
7 |
+
import os
|
8 |
import numpy as np
|
9 |
from PIL import Image
|
10 |
import streamlit as st
|
|
|
|
|
11 |
|
12 |
# most of this code has been obtained from Datature's prediction script
|
13 |
# https://github.com/datature/resources/blob/main/scripts/bounding_box/prediction.py
|
|
|
15 |
class PreTrainedPipeline():
|
16 |
def __init__(self, path: str):
|
17 |
# load the model
|
18 |
+
self.model = keras.models.load_model(os.path.join(path, "tf_model.h5"))
|
19 |
+
|
20 |
+
def __call__(self, inputs: "Image.Image")-> List[Dict[str, Any]]:
|
21 |
+
|
22 |
+
# convert img to numpy array, resize and normalize to make the prediction
|
23 |
+
img = np.array(inputs)
|
24 |
|
25 |
+
im = tf.image.resize(img, (128, 128))
|
26 |
+
im = tf.cast(im, tf.float32) / 255.0
|
27 |
+
pred_mask = self.model.predict(im[tf.newaxis, ...])
|
28 |
+
|
29 |
+
# take the best performing class for each pixel
|
30 |
+
# the output of argmax looks like this [[1, 2, 0], ...]
|
31 |
+
pred_mask_arg = tf.argmax(pred_mask, axis=-1)
|
32 |
|
33 |
labels = []
|
34 |
+
|
35 |
+
# convert the prediction mask into binary masks for each class
|
36 |
+
binary_masks = {}
|
37 |
+
mask_codes = {}
|
38 |
+
|
39 |
+
# when we take tf.argmax() over pred_mask, it becomes a tensor object
|
40 |
+
# the shape becomes TensorShape object, looking like this TensorShape([128])
|
41 |
+
# we need to take get shape, convert to list and take the best one
|
42 |
+
|
43 |
+
rows = pred_mask_arg[0][1].get_shape().as_list()[0]
|
44 |
+
cols = pred_mask_arg[0][2].get_shape().as_list()[0]
|
45 |
+
|
46 |
+
for cls in range(pred_mask.shape[-1]):
|
47 |
+
|
48 |
+
binary_masks[f"mask_{cls}"] = np.zeros(shape = (pred_mask.shape[1], pred_mask.shape[2])) #create masks for each class
|
49 |
+
|
50 |
+
for row in range(rows):
|
51 |
+
|
52 |
+
for col in range(cols):
|
53 |
+
|
54 |
+
if pred_mask_arg[0][row][col] == cls:
|
55 |
+
|
56 |
+
binary_masks[f"mask_{cls}"][row][col] = 1
|
57 |
+
else:
|
58 |
+
binary_masks[f"mask_{cls}"][row][col] = 0
|
59 |
+
|
60 |
+
mask = binary_masks[f"mask_{cls}"]
|
61 |
+
mask *= 255
|
62 |
+
img = Image.fromarray(mask.astype(np.int8), mode="L")
|
63 |
+
|
64 |
+
# we need to make it readable for the widget
|
65 |
+
with io.BytesIO() as out:
|
66 |
+
img.save(out, format="PNG")
|
67 |
+
png_string = out.getvalue()
|
68 |
+
mask = base64.b64encode(png_string).decode("utf-8")
|
69 |
+
|
70 |
+
mask_codes[f"mask_{cls}"] = mask
|
71 |
+
|
72 |
+
|
73 |
+
# widget needs the below format, for each class we return label and mask string
|
74 |
+
labels.append({
|
75 |
+
"label": f"LABEL_{cls}",
|
76 |
+
"mask": mask_codes[f"mask_{cls}"],
|
77 |
+
"score": 1.0,
|
78 |
+
})
|
79 |
+
|
80 |
+
labels = [{"score":0.9509243965148926,"label":"car","box":{"xmin":142,"ymin":106,"xmax":376,"ymax":229}},
|
81 |
+
{"score":0.9981777667999268,"label":"car","box":{"xmin":405,"ymin":146,"xmax":640,"ymax":297}},
|
82 |
+
{"score":0.9963648915290833,"label":"car","box":{"xmin":0,"ymin":115,"xmax":61,"ymax":167}},
|
83 |
+
{"score":0.974663257598877,"label":"car","box":{"xmin":155,"ymin":104,"xmax":290,"ymax":141}},
|
84 |
+
{"score":0.9986898303031921,"label":"car","box":{"xmin":39,"ymin":117,"xmax":169,"ymax":188}},
|
85 |
+
{"score":0.9998276233673096,"label":"person","box":{"xmin":172,"ymin":60,"xmax":482,"ymax":396}},
|
86 |
+
{"score":0.9996274709701538,"label":"skateboard","box":{"xmin":265,"ymin":348,"xmax":440,"ymax":413}}]
|
87 |
|
88 |
return labels
|
89 |
|
90 |
+
# class PreTrainedPipeline():
|
91 |
+
# def __init__(self, path: str):
|
92 |
+
# # load the model
|
93 |
+
# self.model = tf.saved_model.load('./saved_model')
|
94 |
+
|
95 |
+
# def __call__(self, inputs: "Image.Image")-> List[Dict[str, Any]]:
|
96 |
+
# image = np.array(inputs)
|
97 |
+
# image = tf.cast(image, tf.float32)
|
98 |
+
# image = tf.image.resize(image, [150, 150])
|
99 |
+
# image = np.expand_dims(image, axis = 0)
|
100 |
+
# predictions = self.model.predict(image)
|
101 |
+
|
102 |
+
# labels = []
|
103 |
+
# labels = [{"score":0.9509243965148926,"label":"car","box":{"xmin":142,"ymin":106,"xmax":376,"ymax":229}},{"score":0.9981777667999268,"label":"car","box":{"xmin":405,"ymin":146,"xmax":640,"ymax":297}},{"score":0.9963648915290833,"label":"car","box":{"xmin":0,"ymin":115,"xmax":61,"ymax":167}},{"score":0.974663257598877,"label":"car","box":{"xmin":155,"ymin":104,"xmax":290,"ymax":141}},{"score":0.9986898303031921,"label":"car","box":{"xmin":39,"ymin":117,"xmax":169,"ymax":188}},{"score":0.9998276233673096,"label":"person","box":{"xmin":172,"ymin":60,"xmax":482,"ymax":396}},{"score":0.9996274709701538,"label":"skateboard","box":{"xmin":265,"ymin":348,"xmax":440,"ymax":413}}]
|
104 |
+
|
105 |
+
# return labels
|
106 |
+
|
107 |
|
108 |
# # -----------------
|
109 |
# def load_model():
|