Cats vs Dogs "as a service" (python Flask wrapper for CNN)


(alex) #1

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 :blush:

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!


Using fastai model in external api
How do I use ML as a service?
(segovia) #2

Hi Alex, thanks for this interesting project. I am a little confused - do I need to create my own modelweights.h5 file and save it in the “/static/_model/” path? Or you have provided it somewhere?


(alex) #3

Hey Segovia,

Yes that’s correct - you need to create your own model weights file and save it to:

/static/_model/modelweights.h5

My model training process is in a separate notebook but it’s essentially the same as lesson2.ipynb where you call something like:

model.save_weights(‘modelweights.h5’)

Hope that helps…


(amine) #4

Hi @markovbling . I want to do the same thing as you did with fastai flask version but since the framework behind the scenes changed from keras to pytorch I want to ask you if you succeeded to update your api and use the new fastai library and if you still working with keras could you provid me with the old version of lesson2.ipynb that inspired you to train your modal


#5

@markovbling Could you please provide a little more context about the .ipynb we can use to save the weights? It would be useful to refer to the current github version of the lessons.

I personally use the fastai/courses/dl1/lesson1-vgg.ipynb and I save the weights like this:

learn.save(‘modelweights’) # these are save as .h5

However, the

python server.py

gives an error like:

fid = h5f.open(name, flags, fapl=fapl)
File “h5py/_objects.pyx”, line 54, in h5py._objects.with_phil.wrapper
File “h5py/_objects.pyx”, line 55, in h5py._objects.with_phil.wrapper
File “h5py/h5f.pyx”, line 78, in h5py.h5f.open
OSError: Unable to open file (File signature not found)

Thanks!