I am creating a web application that uses tensorflow in django, but when the learned model performs forward processing on the data entered, it takes time to build the model and load the learned parameters every time.If possible, I would like to build the model and load the learned parameters when the application is up and not build the model every time I perform the forward process.Below is my simple code.Incidentally, I import functions that I define on views.py in django.
views.py
from main import main#it is the function that deal the image data
classExample (Formview):
def form_valid(self, form):
image=form.data
output=main(image)
return JsonResponse (output)
main.py
defmain(input):
with tf.Graph().as_default():
with tf.Session() asess:
# load model and parameters
net.load_model(model)
# deal the image data
output = net(input)
return output
If you only want to do it once during Django startup, you can override the AppConfig.ready()
method (version 1.7 and later).For more information on how to work with earlier versions, please visit the following page:
Execute code when Django starts ONCE only?
© 2024 OneMinuteCode. All rights reserved.