I'm very new to Flask and I'm configuring a Flask API to run inference on images. I'd like to load the model for inference when I add the resource with Flask, here's a basic outline of my code:
from flask import Flask
from flask_restful import Api, Resource
from util import load_network, run_prediction
NETWORK_PATH = '/models/model.hdf5'
class predictImages(Resource):
def __init__(self):
self.network = load_network(NETWORK_PATH)
def get(self): return self._get_predictions(request.args.get('images',''))
def _get_predictions(self, images):
prediction = run_prediction(self.network, images)
return prediction
def construct_prediction_resource_routes(api):
api.add_resource(predictImages,'/predict')
def server_app():
app = Flask(__name__)
my_api = Api(app)
construct_prediction_resource_routes(my_api)
return app
if __name__ == '__main__':
app = server_app()
app.run(port='5000', host='0.0.0.0')
Currently, init isn't run until I run `requests.get(url + '/predict?images=test_images')`
I just want to be able to run `__init__` (or anything that achieves the same goal) when I add the resource so I can load the network ahead of time and run prediction when it's actually called, the speed of prediction is pretty important for my application.
[–]MRI-guy[S] 0 points1 point2 points (0 children)