Hey guys,
I’ve seen a few threads with people asking how to turn a model into an API. I did this recently for the “cats vs dogs” fine-tuned VGG CNN from lesson 2 and thought it might help some people if I share my code
I can’t upload a zip so I put the code on my github here:
Essentially it’s just a simple Flask server that allows you to upload an image and then that image gets fed through the predict function and the results are returned to another page as JSON. To handle concurrent requests, images are stored in /static/_uploads/ while they’re being processed then moved to /static/_uploads_done/ once they’re done since the fit function runs inference on all images in the uploads folder and you don’t want that to happen for all old images too because it gets slow.
I unfortunately can’t share a link to a live version of this due to the nature of the project (I fine-tuned my network on something other than cats vs dogs for a consulting project) but the code pretty much runs as-is on a t2.medium EC2 instance on AWS. I found this tutorial very useful for getting the flask app running on AWS and getting nginx and uwsgi running (nginx + uwsgi handle the requests that get fed to the flask app which wraps the CNN predictions).
If you’re running anaconda, it should work out-the-box, just unzip the app and cd to the directory then run:
python server.py
The only thing that’s missing is the saved model weights. I trained the model on 20k images of each of 2 classes on the P2 GPU instance we set up as part of the course then saved the weights. I hacked the vgg16.py
script to read the trained model weights from the following path - just place your saved model here with this file name and everything should work: /static/_model/modelweights.h5
It’s setup to work with 2 classes but it’s straight-forward to generalize to arbitrarily many classes and return the set of class probabilities in the result page - the hard part for me was getting the upload and file paths working…
Please let me know if anything breaks or needs clarifying
Enjoy!