Update pipeline.py
Browse files- pipeline.py +41 -51
pipeline.py
CHANGED
|
@@ -3,67 +3,57 @@ from typing import Any, Dict, List
|
|
| 3 |
|
| 4 |
import tensorflow as tf
|
| 5 |
from tensorflow import keras
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
import base64
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
-
MODEL_FILENAME = "saved_model.pb"
|
| 11 |
-
CONFIG_FILENAME = "config.json"
|
| 12 |
-
|
| 13 |
|
| 14 |
-
class PreTrainedPipeline(
|
| 15 |
def __init__(self, model_id: str):
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
# Reload Keras SavedModel
|
| 19 |
-
self.model = keras.models.load_model('./model.h5')
|
| 20 |
-
|
| 21 |
-
# Number of labels
|
| 22 |
-
self.num_labels = self.model.output_shape[1]
|
| 23 |
-
|
| 24 |
-
self.id2label = self.id2label = {"0": "pet", "1":"no_pet"}
|
| 25 |
-
|
| 26 |
-
def __call__(self, inputs: "Image.Image") -> List[Dict[str, Any]]:
|
| 27 |
-
"""
|
| 28 |
-
Args:
|
| 29 |
-
inputs (:obj:`PIL.Image`):
|
| 30 |
-
The raw image representation as PIL.
|
| 31 |
-
No transformation made whatsoever from the input. Make all necessary transformations here.
|
| 32 |
-
Return:
|
| 33 |
-
A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX" (str), mask: "base64 encoding of the mask" (str), "score": float}
|
| 34 |
-
It is preferred if the returned list is in decreasing `score` order
|
| 35 |
-
"""
|
| 36 |
-
# Resize image to expected size
|
| 37 |
-
|
| 38 |
-
expected_input_size = self.model.input_shape
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
with Image.open(inputs) as im:
|
| 42 |
-
inputs = np.array(im)
|
| 43 |
-
if expected_input_size[-1] == 1:
|
| 44 |
-
inputs = inputs.convert("L")
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
target_size = (expected_input_size[1], expected_input_size[2])
|
| 49 |
-
img = tf.image.resize(inputs, target_size)
|
| 50 |
-
|
| 51 |
-
img_array = tf.keras.preprocessing.image.img_to_array(img)
|
| 52 |
-
img_array = img_array[tf.newaxis, ...]
|
| 53 |
-
|
| 54 |
-
predictions = self.model.predict(img_array)
|
| 55 |
-
|
| 56 |
-
self.single_output_unit = (
|
| 57 |
-
self.model.output_shape[1] == 1
|
| 58 |
-
) # if there are two classes
|
| 59 |
-
|
| 60 |
labels = []
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
labels.append({
|
| 64 |
-
"label":
|
| 65 |
-
"mask":
|
| 66 |
"score": 1.0,
|
| 67 |
})
|
| 68 |
-
|
| 69 |
-
return sorted(labels)
|
|
|
|
| 3 |
|
| 4 |
import tensorflow as tf
|
| 5 |
from tensorflow import keras
|
| 6 |
+
from huggingface_hub import from_pretrained_keras, hf_hub_download
|
| 7 |
from PIL import Image
|
| 8 |
import base64
|
| 9 |
+
import numpy as np
|
| 10 |
+
from PIL import Image
|
| 11 |
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
class PreTrainedPipeline():
|
| 15 |
def __init__(self, model_id: str):
|
| 16 |
|
| 17 |
+
self.model = keras.models.load_model(model_id)
|
| 18 |
+
|
| 19 |
+
def __call__(self, inputs: "Image.Image")-> List[Dict[str, Any]]:
|
| 20 |
+
|
| 21 |
+
with Image.open(inputs) as img:
|
| 22 |
+
img = np.array(img)
|
| 23 |
+
|
| 24 |
+
im = tf.image.resize(img, (128, 128))
|
| 25 |
+
im = tf.cast(im, tf.float32) / 255.0
|
| 26 |
+
pred_mask = model.predict(im[tf.newaxis, ...])
|
| 27 |
+
pred_mask_arg = tf.argmax(pred_mask, axis=-1)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
labels = []
|
| 30 |
+
|
| 31 |
+
binary_masks = {}
|
| 32 |
+
mask_codes = {}
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
for cls in range(pred_mask.shape[-1]):
|
| 36 |
+
|
| 37 |
+
binary_masks[f"mask_{cls}"] = np.zeros(shape = (pred_mask.shape[1], pred_mask.shape[2]))
|
| 38 |
+
for row in range(pred_mask_arg[0][1].get_shape().as_list()[0]):
|
| 39 |
+
|
| 40 |
+
for col in range(pred_mask_arg[0][2].get_shape().as_list()[0]):
|
| 41 |
+
|
| 42 |
+
if pred_mask_arg[0][row][col] == cls:
|
| 43 |
+
|
| 44 |
+
binary_masks[f"mask_{cls}"][row][col] = 1
|
| 45 |
+
else:
|
| 46 |
+
binary_masks[f"mask_{cls}"][row][col] = 0
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
mask_codes[f"mask_{cls}"] = base64.b64encode(binary_masks[f"mask_{cls}"])
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
for i in range(pred_mask.shape[-1]): #for every class
|
| 53 |
|
| 54 |
labels.append({
|
| 55 |
+
"label": f"LABEL_{i}",
|
| 56 |
+
"mask": mask_codes[f"mask_{i}"],
|
| 57 |
"score": 1.0,
|
| 58 |
})
|
| 59 |
+
return labels
|
|
|