Picture Credits: Pixabay
In this post, we shall be looking at serving a Tensorflow 2 Keras image classification model with a Flask app.
Training and saving the Model in Google Colab
To train the model, we can run the aforementioned Jupyter Notebook on Google Colab. To train the model successfully, we will need to ensure that Tensorflow 2 beta is installed with the following command:
!pip install tensorflow-gpu==2.0.0-beta1
For convenience, you can open the notebook provided in my repository here.
In Google Colab screen above, click on File > Open Notebook. In the pop-up screen, click on the Github tab. You will see the screen below.
Copy and paste the notebook link here and click on the search button (magnifying glass icon). Once the notebook has loaded, Select ‘Runtime > Change Runtime Type’ from the menu.
At this point, please note that you need to have a Google Account and register yourself to use Google Colab. Otherwise you will note be able to execute the notebook.
You will see the Notebook settings tab. Ensure that ‘Python 3’ and ‘GPU’ options have been selected as shown below and click on Save.
Once done, you can execute the notebook by selecting Runtime > Run all from the menu.
Open the files tab in the left pane. The left pane can be expanded by clicking on the icon highlighted below.
In the files tab below, locate the model file ‘rps.h5’ and click the ‘Download’ button. Note that this same model file that has already been uploaded to the Github repository.
Running the image classifier app
As the original template has been built with Tensorflow 1.x in mind, the code has been updated to work with the Tensorflow 2 beta 1 library.
Clone (or download and unzip) the repository here and ensure that the model has been saved inside the ‘models’ folder. (Model already provided)
$ git clone https://github.com/yoke2/rps_tf2_flask_app.git
Execute the commands below to install dependencies. You will need Python 3+ to be installed.
$ cd rps_tf2_flask_app/ $ pip install -r requirements.txt
Run the app using the command below and open the application in your browser on http://localhost:5000
$ python app.py
You will now see the app below.
Choose a file to classify. Test files have been provided in misc/test_files for you to use.
After clicking on the ‘Predict!’ button, you will see the prediction shown as below.
You can then attempt to deploy this app in the cloud. You can also containerize the application using Docker using the provided Dockerfile in the repository.