Covid-19 Detection App with Tensorflow: App & Deploy

Estimated reading time: 3 minutes

Introduction

This is the third part of the series of articles in which we are going to build a production-ready Covid-19 detection system using Tensorflow. In this article, we will incorporate data application development using Streamlit and deploy the app with the model on Google Cloud Run serverless platform.

Streamlit App

The Streamlit is a simple tool to transform data scripts in python into an interactive data app. It supports different visualization libs like plotly, matplotlib and it also includes inbuild visualization.

Writing an app on this platform is very easy, you don’t need to write any fronted end code, just simple python code and you are all set to go. We are following an approach to solving the problem.

  • We load the pre-trained Tensorflow model into the cache, so that for faster processing.
  • The user will submit the image, and our model will run inference to predict if it’s the sample covid-19 positive or not.
  • The code will be containerized for deployment on our production platform.
#app.py 
import streamlit as st
import tensorflow as tf

@st.cache()
def load_model():
    model = tf.keras.models.load_model("streamlit/tl_model_tf.h5")
    return model


if __name__ == '__main__':

    model = load_model()
    st.title('Covid-19 X-Ray Detection: Tensorflow')

    uploaded_file = st.file_uploader('File uploader')

    if not uploaded_file:
        st.warning("Please upload an image before proceeding!")
        st.stop()
    else:
        # Decode Image and Predict Right Class
        image_as_bytes = uploaded_file.read()
        st.image(image_as_bytes, use_column_width=True)
        img = tf.io.decode_image(image_as_bytes, channels=3)
        img = tf.image.resize(img, (256, 256))
        img_array = tf.keras.preprocessing.image.img_to_array(img)
        img_array = tf.expand_dims(img_array, 0)  # Create a batch

        predictions = model.predict(img_array)
        score = tf.nn.sigmoid(predictions[0])

        st.title('Results')

        st.write("This image is %.2f percent Covid and %.2f percent Normal."
                 % (100 * (1 - score), 100 * score)
                 )
  • The above code creates a new streamlit application.
  • We decode the uploaded image and transform it into an array. The prediction return percentage of sample classification.
Tensorflow & Streamlit App for Covid-19
Fig 1: User Interface of our application

Docker Container with Tensorflow Model

FROM python:3.8
WORKDIR /streamlit
COPY requirements.txt ./requirements.txt
RUN pip3 install -r requirements.txt
EXPOSE 8501
COPY . /streamlit
CMD streamlit run streamlit/app.py --server.port $PORT

The Docker file will generate container which we will deploy. We copy our trained model on same container. The better solution will be to deploy model individually and use it as REST api.

Deploy Streamlit App & Tensorflow Model

To deploy model on google cloud run, you need to have GCP account with access to configured terminal with Google SDK. Run Following commands to build our application and deploy on selected region.

gcloud builds submit --tag gcr.io/<Project_ID>/covid-19
ID: 2d1f121f-9a8a-47f6-bb0e-1db9b9b24aee
CREATE_TIME: 2021-06-03T23:19:36+00:00
DURATION: 2M50S
SOURCE: gs://<Project_Id>/source/1622762327.906677-ff271d3c00f645aea034eeab52bf0227.tgz
IMAGES: gcr.io/<Project_Id>/covid-19 (+1 more)
STATUS: SUCCESS
gcloud run deploy gcloud run deploy --image gcr.io/<Project_ID>/covid-19--image gcr.io/<Project_ID>/covid-19
Deploying container to Cloud Run service [covid-19] in project [id] region [europe-central2]
OK Deploying... Done.
  OK Creating Revision...
  OK Routing traffic...
Done.
Service [covid-19] revision [covid-19-00003-fuz] has been deployed and is serving 100 percent of traffic.
Service URL: https://covid-19-mhtggqdhcq-lm.a.run.app

Now you can access the app from given link. Further we can setup CI/CD pipeline for online training and deployment if necessary.

Summary

  • We started this series with an introduction to the problem statement and further extended it to a production-ready prototype for the use case.
  • We tried two different models for training, which includes the custom CNN model and Transfer Learning.
  • You can check out the code at GitHub and the application.
  • Feel free to share your thoughts and suggestion to make this solution more robust.

Related Articles

Federated Learning: An Overview(part-1)

Current machine learning approaches require centralization of training data which invite concerns about privacy in many applications. Federated learning overcomes this without the need of the movement of data to the center node. As it has to deal with high latency and unreliable communication special algorithms and optimization techniques are needed.

Responses

Your email address will not be published. Required fields are marked *