Deploying Computer Vision Models for Health Diagnostics Imaging.
Tags: mlops, computervision, ai
Published: 2023-06-28 19:20:44

The capabilities of Computer Vision (CV) models are progressing by leaps and bounds fostering more and more adoption across a number of industries. One of these industries is the healthcare sector where computer vision is being used as a powerful diagnostic imaging tool to provide potential life saving results for patients. These CV models can help diagnose diseases, monitor treatments, and improve outcomes for patients. Analyzing images from various sources, such as X-rays, MRI scans, ultrasound, or optical cameras, they can detect anomalies, measure features, segment regions, and classify conditions. 

In this post, we will demonstrate putting a computer vision model into production that detects mitochondria from high resolution images.

For this example we will be using a high resolution 1536 x 2048 image that is broken down into “patches” of 256x256 images that can be quickly analyzed.

Mitochondria are known as the “powerhouse” of the cell, and having a healthy amount of mitochondria indicates that a patient has enough energy to live a healthy life, or may have underlying issues that a doctor can check for.

Scanning high resolution images of patient cells can be used to count how many mitochondria a patient has, but the process is laborious. The following ML Model is trained to examine an image of cells, then detect which structures are mitochondria. This is used to speed up the process of testing patients and determining next steps.

Before moving on, you can step through this computer vision deployment exercise by downloading the [Wallaroo Community Edition](https://portal.wallaroo.community/) and the [Health Diagnostic Imaging Tutorial](https://docs.wallaroo.ai/wallaroo-tutorials/wallaroo-tutorials-model-cookbooks/wallaroo-cv-mitochondria/). 

After loading the necessary dependencies and libraries the next step is to establish a connection to the Wallaroo Client. The Python library is included in the Wallaroo install and available through the Jupyter Hub interface provided with your Wallaroo environment.

We will create a workspace to manage our pipeline and models. The following variables will set the name of our sample workspace then set it as the current workspace.

![Python code for Workspace, Pipeline creation, and Model file name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6k7elchsip03vpgfzcd4.png)

![Python code for setting to the current workspace and pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scpqvnhh8z4ofwzntjeg.png)

![Python code for setting to the current workspace and pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ohzkt06d7c8abaig6uk.png)

In the next step we will:

1. Upload our model.
2. Apply it as a step in our pipeline.
3. Create a pipeline deployment with enough memory to perform the inferences.
4. Deploy the pipeline

![Deploying the ML model to production](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2q52t2qnyxe1zr1xvnys.png)

It’s important to point out that our model is deployed in less than 45 seconds which can take days and weeks normally with other ML tools. 

The next step is to process the image into a numpy array that the model is trained to detect from.

We start by retrieving all the patch images from a recorded time series tiff recorded on one of our microscopes.

![Python code for retrieving all the patch images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fcvnmxgwo6ipafbha59f.png)

Randomly we will retrieve a 256x256 patch image and use it to do our semantic segmentation prediction.

We’ll then convert it into a numpy array and insert into a DataFrame for a single inference.

The following helper function `loadImageAndConvertTiff` is used to convert the image into a numpy, then insert that into the DataFrame. This allows a later command to take the randomly grabbed image and perform the process on other images.

![Python code for helper function `loadImageAndConvertTiff` used to convert the image into a numpy and insertion into the DataFrame](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cavypczhhcifya5xyb4r.png)

![Python commands to take the randomly grabbed image and perform the process on other images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkxkplqcdk1gmilv9cor.png)

We will now perform our inferences and display the results. This results in a predicted mask showing us where the mitochondria cells are located.

1. The first image is the input image.
2. The 2nd image is the ground truth. The mask was created by a human who identified the mitochondria cells in the input image
3. The 3rd image is the predicted mask after running inference on the Wallaroo pipeline.

We’ll perform this 10 times to show how quickly the inferences can be submitted.

![Python code for running the inferences ten times](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7457r3kygvzvdhofz46.png)

![ML model Mitochondria image output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rzo7uizjtt5kq2svtq81.png)

![ML model Mitochondria image output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9k43uitmbtbcc1z1j20.png)

Finally we will undeploy our pipeline and give the resources back to the instance.

![Python code for undeploying the ML pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zw7ezqggyyqgdupjis5c.png)


This is just one example of how to use Machine Learning to overcome difficulties to deploy and serve CV models without complex technical infrastructure bottlenecks or requiring data engineering expertise to imaging the data, and to use computer vision models for advanced object detection, object classification for health diagnostic imaging in areas of radiology, pathology, cell-imaging. 

As mentioned above you can try this yourself by downloading the [Wallaroo Community Edition](https://portal.wallaroo.community/) and the [Health Diagnostic Imaging Tutorial](https://docs.wallaroo.ai/wallaroo-tutorials/wallaroo-tutorials-model-cookbooks/wallaroo-cv-mitochondria/).