Medical Image Classification using Azure Functions and Cognitive Services

Parag Ghorpade
Sep 4 · 10 min read
Image for post
Image for post

#ServerlessSeptember

Introduction

Prerequisites

Section I: Training the model

Dataset

Microsoft Custom Vision

Image for post
Image for post
Successfully deployed custom vision service
Image for post
Image for post
Image for post
Image for post

Section II: Azure Functions model serving

git clone https://github.com/Parag0506/Azure-Functions-and-Cognitive-Services.git
git clone https://github.com/Azure-Samples/functions-python-tensorflow-tutorial.git
BASH:-cd startpython -m venv .venvsource .venv/bin/activateIf Python didn’t install the venv package on your Linux distribution, run the following command:sudo apt-get install python3-venvCMD:- cd startpy -3.6 -m venv .venv.venv\scripts\activate
func init — worker-runtime python
func new --name classify --template “HTTP trigger”

Testing App Locally

func start

Import and serve the Tensorflow Model

BASH:-cp ../resources/predict.py classify
CMD
:-
copy ..\resources\predict.py classify
tensorflow==1.14
Pillow
requests
pip install --no-cache-dir -r requirements.txt

Configure Function to run predictions

import logging
import azure.functions as func
import json
# Import helper script
from .predict import predict_image_from_url
def main(req: func.HttpRequest) -> func.HttpResponse:
image_url = req.params.get('img')
logging.info('Image URL received: ' + image_url)
results = predict_image_from_url(image_url) headers = {
"Content-type": "application/json",
"Access-Control-Allow-Origin": "*"
}
return func.HttpResponse(json.dumps(results), headers = headers)
func start
http://localhost:7071/api/classify?img=<URL>eg:
http://localhost:7071/api/classify?img=https://raw.githubusercontent.com/Parag0506/Azure-Functions-and-Cognitive-Services/master/test_images/PNEUMONIA/person1946_bacteria_4875.jpeg
Sample Images:
https://github.com/Parag0506/Azure-Functions-and-Cognitive-Services/tree/master/test_images
RESULT (you should be able to see similar response in the browser):-
{"created": "2020-09-03T16:16:01.225972", "predictedTagName": "Pneumonia", "prediction": [{"tagName": "Pneumonia", "probability": 1.0}]}

Run basic front end locally

https://github.com/Parag0506/Azure-Functions-and-Cognitive-Services/tree/master/frontend
BASH:- python -m http.serverCMD:- py -m http.server
localhost:8000
Image for post
Image for post
https://github.com/Parag0506/Azure-Functions-and-Cognitive-Services/tree/master/test_images

Inference

Image for post
Image for post

Hosting the function app

az login
az group create --name classify-image --location eastus
az storage account create --name <STORAGE_NAME> --location eastus --resource-group classify-image --sku Standard_LRS
az functionapp create --resource-group classify-image --os-type Linux --consumption-plan-location eastus --runtime python --runtime-version 3.6 --functions-version 2 --name <APP_NAME> --storage-account <STORAGE_NAME>
func azure functionapp publish <APP_NAME>
Image for post
Image for post
https://xraydetection.azurewebsites.net/api/classify?code=WSQa5/aMpB51FEMUNd4dJk00/4GsB00golB5Kd/FjhwTf9ZaxQU2uw==&img=https://raw.githubusercontent.com/Parag0506/Azure-Functions-and-Cognitive-Services/master/test_images/PNEUMONIA/person1946_bacteria_4875.jpeg

References:

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store