When using tensorflow in django, I would like to make it possible to use the model that I built and loaded all the time while starting the application.

Asked 2 years ago, Updated 2 years ago, 124 views

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

python django tensorflow

2022-09-30 20:13

1 Answers

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?


2022-09-30 20:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.