Brain Tumor Detector part 7. Save model and Production | by Nelson Punch | Software-Dev-Explore | Nov, 2023


Photo by Elijah Hiett on Unsplash

Finally I can save the model for production use. Here I am going to save the model as a file and then deploy it on a web app for user to interact with.

Streamlit is a tool for creating a machine learning web app quickly.

I saved the model as keras and h5 format. And I will use h5 later for web app. The difference between keras and h5.

After they are saved then I can download the model I want from Kaggel’s notebook into local machine. In addition download id_to_class.txt file as well.

Environment

It is a good idea to create an ioslated environment for my app at local machine. I can use either

for an isolated environment and install necessary libraries. Libraries which will be used to create streamlit app.

Note

The library for opencv used opencv-python-headless instead of opencv-python because in this case I had encountered an error by using opencv-python after web app is deployed.

Model wrapper

A wrapper to wrap around the model. There are 2 reasons to create this model wapper.

  1. Load model and talk to the model within this wapper
  2. Provide interface for my application

The purpose of this wrapper class is to make prediction from an image. Moreover it preprocessing the input image before making prediction. The preprocessing method Line 72 ~ 101 is as same as the code was written in Kaggle’s notebook.

Lines 21 ~ 30 is where actual prediction happend and it return the following

  • cls : class name
  • prob : probability
  • alert : whether there is a tumor or not

Lines 36 ~ 38 is a support method for prediction. it take an image data in a form of pillow image. And use method line 65 ~ 70 to turn an image into tensor data. Finally it invoke prediction method and return the result.

The model’s output is in integer where each unique integer correspond to a certain class name. Lines 48 ~ 55 is where it load the file which contain the reference of index to class name.

Localization

I could not find an existing library to handle localization for Streamlit. Therefore to support multiple languages for the web app, I need to create a my library to handle localization.

This class simply load any encountered localization file in json format within a direcotry. In addition it support changing language on the fly.

Here is examples of the file structure and localization file.

App

For the web app. I put everything together.

Put everything under the same folder and then use git to do version control, track all files and push to Github.

Create a repository on Github. Follow streamlit instuction on how to delploy app with Github.

requirement.txt is a necessary file that need to be created in order to deploy app to streamlit cloud server. To automatically generate requirement.txt, pipreqs library can be used for the purpose.

The github would look similar to this.

Now I have a live app here.

This is end of brain tumor detector journey from an idea to finished working product. Despite of some prediction errors for identifying the type of tumor, the model can generalize well on unseen real life sample.

It would be greate to have an auxiliary feature that model can predict the tumor location and mark it on image.



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*