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

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!

16 Likes

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?

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…

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

@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!

I am using the fastai/courses/dl1/lesson.ipynb and I save the weights like this :

arch=resnet34
data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz))
learn = ConvLearner.pretrained(arch, data, precompute=True)
learn.save('modelweights')

After that, I copied the file to /static/_model/modelweights.h5

but when I ran, python server.py it is giving the following error

### start server  2018-07-19 12:01:39.808413

### image upload folder: /home/ubuntu/flask_fastai_CNN/static/_uploads/unknown/

### data folder: /home/ubuntu/flask_fastai_CNN/static/data/redux
Using gpu device 0: Tesla K80 (CNMeM is disabled, cuDNN Mixed dnn version. The header is from one version, but we link with a different version (5103, 7005))
Using Theano backend.

### initializing model: 
VVV/home/ubuntu/flask_fastai_CNN/static/_model/modelweights.h5
Traceback (most recent call last):
  File "server.py", line 52, in <module>
    vgg = Vgg16()
  File "/home/ubuntu/flask_fastai_CNN/utils/vgg16.py", line 32, in __init__
    self.create()
  File "/home/ubuntu/flask_fastai_CNN/utils/vgg16.py", line 84, in create
    model.load_weights(trained_model_path)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 2494, in load_weights
    f = h5py.File(filepath, mode='r')
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/h5py/_hl/files.py", line 269, in __init__
    fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/h5py/_hl/files.py", line 99, in make_fid
    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
IOError: Unable to open file (file signature not found)

Please tell me where I am wrong

Were you able to find a solution ?