Example: Vertex AI Models
Deploy AutoML Edge image classification models from Google Cloud Vertex AI
Image classification models trained with Vertex AI AutoML can be exported and deployed on MD.ai with a ready-made template.
Deploy a Vertex AI model
- In Vertex AI, export your model in the
Containerformat. This saves asaved_model.pbfile to a Cloud Storage bucket. Download it. - Download the MD.ai Vertex AI template and replace its
saved_model.pbwith yours. - Optionally, set
device_type: gpuinmodel/.mdai/config.yamlto run on a GPU. For binary and multiclass models, that's all you need to change. - For multilabel or other custom outputs, edit
model/.mdai/mdai_deploy.py. - Deploy the model. When building the version, set
Input File TypetoThumbnail.
The template is also on GitHub.
Inputs
AutoML Edge models take JPEG or PNG images, not DICOM. With Input File Type set to Thumbnail, MD.ai sends every image as PNG or JPEG, along with its DICOM tags. The template reads the image UIDs from those tags to return outputs:
import os
from io import BytesIO
import numpy as np
import tensorflow as tf
class MDAIModel:
def __init__(self):
modelpath = os.path.dirname(os.path.dirname(__file__))
model = tf.saved_model.load(modelpath)
self.model = model.signatures["serving_default"]
def predict(self, data):
outputs = []
for file in data["files"]:
if file["content_type"] not in ["image/jpeg", "image/png"]:
continue
tags = file["dicom_tags"]
image = BytesIO(file["content"]).getvalue()
prediction = self.model(
image_bytes=tf.convert_to_tensor([image]),
key=tf.convert_to_tensor([tags["SOPInstanceUID"]]),
)
probabilities = prediction["scores"].numpy()[0]
# Binary and multiclass: the most likely class.
# For multilabel models, return an output per class instead.
class_index = int(np.argmax(probabilities))
outputs.append(
{
"type": "ANNOTATION",
"study_uid": tags["StudyInstanceUID"],
"series_uid": tags["SeriesInstanceUID"],
"instance_uid": tags["SOPInstanceUID"],
"class_index": class_index,
"probability": [
{"class_index": i, "probability": float(p)}
for i, p in enumerate(probabilities)
],
}
)
return outputs