MD.ai

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

  1. In Vertex AI, export your model in the Container format. This saves a saved_model.pb file to a Cloud Storage bucket. Download it.
  2. Download the MD.ai Vertex AI template and replace its saved_model.pb with yours.
  3. Optionally, set device_type: gpu in model/.mdai/config.yaml to run on a GPU. For binary and multiclass models, that's all you need to change.
  4. For multilabel or other custom outputs, edit model/.mdai/mdai_deploy.py.
  5. Deploy the model. When building the version, set Input File Type to Thumbnail.

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

On this page