Create pipeline.py
Browse files- pipeline.py +43 -0
pipeline.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
import PIL
|
| 3 |
+
import torch
|
| 4 |
+
import base64
|
| 5 |
+
import os
|
| 6 |
+
import io
|
| 7 |
+
from transformers import ViTImageProcessor, VisionEncoderDecoderModel
|
| 8 |
+
|
| 9 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class PreTrainedPipeline():
|
| 13 |
+
def __init__(self, path=""):
|
| 14 |
+
self.model = VisionEncoderDecoderModel.from_pretrained(
|
| 15 |
+
pretrained_model_name_or_path=path,
|
| 16 |
+
config=os.path.join(path, 'config.json')
|
| 17 |
+
)
|
| 18 |
+
self.model.eval()
|
| 19 |
+
self.model = self.model.to(device)
|
| 20 |
+
|
| 21 |
+
self.processor = ViTImageProcessor.from_pretrained(
|
| 22 |
+
pretrained_model_name_or_path=os.path.join(
|
| 23 |
+
path, 'preprocessor_config.json')
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
def __call__(self, data: Any) -> Dict[str, List[float]]:
|
| 27 |
+
"""
|
| 28 |
+
Args:
|
| 29 |
+
data (:str:):
|
| 30 |
+
Inputs should be an image encoded in base 64.
|
| 31 |
+
Return:
|
| 32 |
+
A :obj:`dict`:. The object returned should be a dict like
|
| 33 |
+
{"feature_vector": [0.6331314444541931,...,-0.7866355180740356,]} containing :
|
| 34 |
+
- "feature_vector": A list of floats corresponding to the image embedding.
|
| 35 |
+
"""
|
| 36 |
+
# decode base64 image to PIL
|
| 37 |
+
image = PIL.Image.open(io.BytesIO(base64.b64decode(data)))
|
| 38 |
+
|
| 39 |
+
inputs = self.processor(images=image, return_tensors="pt")
|
| 40 |
+
outputs = self.model.encoder(**inputs)
|
| 41 |
+
feature_vector = outputs.last_hidden_state[0, 0, :].tolist()
|
| 42 |
+
# postprocess the prediction
|
| 43 |
+
return {"feature_vector": feature_vector}
|