Prediction is slower when model is loaded than if it is fited during the process












1














I have a strange issue, the DNN.predict method is quite slower when I load my model's weight than when I train with the fit method. I've also noted that when I run a prediction over a batch of images, it's getting faster and faster to predict.



Here is my code



class Reseau(object):

def init(self, img_size, lr=-1, activation=" "):
tf.logging.set_verbosity(tf.logging.ERROR)
self.lr = lr
self.activation = activation
self.img_size = img_size
self.alreadySaved = 0

def setting(self, X, Y, test_x, test_y, nbEpoch):
tflearn.init_graph(num_cores=32, gpu_memory_fraction=1)
with tf.device("/device:GPU:0"):
convnet = input_data(shape=[None, self.img_size, self.img_size, 3], name='input')

convnet = conv_2d(convnet, 32, 5, activation=self.activation)
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 64, 5, activation=self.activation)
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 128, 5, activation=self.activation)
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 64, 5, activation=self.activation)
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 32, 5, activation=self.activation)
convnet = max_pool_2d(convnet, 5)

convnet = flatten(convnet)

convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=self.lr, loss='categorical_crossentropy', name='targets')

self.model = tflearn.DNN(convnet, tensorboard_dir='log')

if self.alreadySaved == 0:
self.model.fit({'input': X}, {'targets': Y}, n_epoch=nbEpoch, validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=500, show_metric=True, run_id="model")
self.model.save("./model")
else:
self.model.load("./model", weights_only=True)
return self.model

def predire(self, img, label):
image = array(img).reshape(1, self.img_size,self.img_size,3)
model_out = self.model.predict(image)
rep = 0

if np.argmax(model_out) == np.argmax(label): rep = 1
else: rep = 0

return rep


Here is a part of my main



reseau.setting(X, Y, test_x, test_y, NB_EPOCH)

X = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,3)
Y = [i[1] for i in test]
cpt = 0

vrai = 0
start_time = time.time()

for i in range(20):
cpt = 0

vrai = 0
start_time = time.time()
for img in tqdm(X):
prediction = reseau.predire(img, Y[cpt])
cpt += 1
if prediction == 1:
vrai += 1


As you can see, I predict the same batch of images 20 times. The first time is always slower than the other ones (without fitting, I predict 82 images the first and then 340 a second, with fitting, it's 255 images the first time and 340 a seconde then).



I'm really out of idea to fix this.










share|improve this question



























    1














    I have a strange issue, the DNN.predict method is quite slower when I load my model's weight than when I train with the fit method. I've also noted that when I run a prediction over a batch of images, it's getting faster and faster to predict.



    Here is my code



    class Reseau(object):

    def init(self, img_size, lr=-1, activation=" "):
    tf.logging.set_verbosity(tf.logging.ERROR)
    self.lr = lr
    self.activation = activation
    self.img_size = img_size
    self.alreadySaved = 0

    def setting(self, X, Y, test_x, test_y, nbEpoch):
    tflearn.init_graph(num_cores=32, gpu_memory_fraction=1)
    with tf.device("/device:GPU:0"):
    convnet = input_data(shape=[None, self.img_size, self.img_size, 3], name='input')

    convnet = conv_2d(convnet, 32, 5, activation=self.activation)
    convnet = max_pool_2d(convnet, 5)

    convnet = conv_2d(convnet, 64, 5, activation=self.activation)
    convnet = max_pool_2d(convnet, 5)

    convnet = conv_2d(convnet, 128, 5, activation=self.activation)
    convnet = max_pool_2d(convnet, 5)

    convnet = conv_2d(convnet, 64, 5, activation=self.activation)
    convnet = max_pool_2d(convnet, 5)

    convnet = conv_2d(convnet, 32, 5, activation=self.activation)
    convnet = max_pool_2d(convnet, 5)

    convnet = flatten(convnet)

    convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
    convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
    convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
    convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
    convnet = dropout(convnet, 0.8)

    convnet = fully_connected(convnet, 2, activation='softmax')
    convnet = regression(convnet, optimizer='adam', learning_rate=self.lr, loss='categorical_crossentropy', name='targets')

    self.model = tflearn.DNN(convnet, tensorboard_dir='log')

    if self.alreadySaved == 0:
    self.model.fit({'input': X}, {'targets': Y}, n_epoch=nbEpoch, validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=500, show_metric=True, run_id="model")
    self.model.save("./model")
    else:
    self.model.load("./model", weights_only=True)
    return self.model

    def predire(self, img, label):
    image = array(img).reshape(1, self.img_size,self.img_size,3)
    model_out = self.model.predict(image)
    rep = 0

    if np.argmax(model_out) == np.argmax(label): rep = 1
    else: rep = 0

    return rep


    Here is a part of my main



    reseau.setting(X, Y, test_x, test_y, NB_EPOCH)

    X = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,3)
    Y = [i[1] for i in test]
    cpt = 0

    vrai = 0
    start_time = time.time()

    for i in range(20):
    cpt = 0

    vrai = 0
    start_time = time.time()
    for img in tqdm(X):
    prediction = reseau.predire(img, Y[cpt])
    cpt += 1
    if prediction == 1:
    vrai += 1


    As you can see, I predict the same batch of images 20 times. The first time is always slower than the other ones (without fitting, I predict 82 images the first and then 340 a second, with fitting, it's 255 images the first time and 340 a seconde then).



    I'm really out of idea to fix this.










    share|improve this question

























      1












      1








      1







      I have a strange issue, the DNN.predict method is quite slower when I load my model's weight than when I train with the fit method. I've also noted that when I run a prediction over a batch of images, it's getting faster and faster to predict.



      Here is my code



      class Reseau(object):

      def init(self, img_size, lr=-1, activation=" "):
      tf.logging.set_verbosity(tf.logging.ERROR)
      self.lr = lr
      self.activation = activation
      self.img_size = img_size
      self.alreadySaved = 0

      def setting(self, X, Y, test_x, test_y, nbEpoch):
      tflearn.init_graph(num_cores=32, gpu_memory_fraction=1)
      with tf.device("/device:GPU:0"):
      convnet = input_data(shape=[None, self.img_size, self.img_size, 3], name='input')

      convnet = conv_2d(convnet, 32, 5, activation=self.activation)
      convnet = max_pool_2d(convnet, 5)

      convnet = conv_2d(convnet, 64, 5, activation=self.activation)
      convnet = max_pool_2d(convnet, 5)

      convnet = conv_2d(convnet, 128, 5, activation=self.activation)
      convnet = max_pool_2d(convnet, 5)

      convnet = conv_2d(convnet, 64, 5, activation=self.activation)
      convnet = max_pool_2d(convnet, 5)

      convnet = conv_2d(convnet, 32, 5, activation=self.activation)
      convnet = max_pool_2d(convnet, 5)

      convnet = flatten(convnet)

      convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
      convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
      convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
      convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
      convnet = dropout(convnet, 0.8)

      convnet = fully_connected(convnet, 2, activation='softmax')
      convnet = regression(convnet, optimizer='adam', learning_rate=self.lr, loss='categorical_crossentropy', name='targets')

      self.model = tflearn.DNN(convnet, tensorboard_dir='log')

      if self.alreadySaved == 0:
      self.model.fit({'input': X}, {'targets': Y}, n_epoch=nbEpoch, validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=500, show_metric=True, run_id="model")
      self.model.save("./model")
      else:
      self.model.load("./model", weights_only=True)
      return self.model

      def predire(self, img, label):
      image = array(img).reshape(1, self.img_size,self.img_size,3)
      model_out = self.model.predict(image)
      rep = 0

      if np.argmax(model_out) == np.argmax(label): rep = 1
      else: rep = 0

      return rep


      Here is a part of my main



      reseau.setting(X, Y, test_x, test_y, NB_EPOCH)

      X = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,3)
      Y = [i[1] for i in test]
      cpt = 0

      vrai = 0
      start_time = time.time()

      for i in range(20):
      cpt = 0

      vrai = 0
      start_time = time.time()
      for img in tqdm(X):
      prediction = reseau.predire(img, Y[cpt])
      cpt += 1
      if prediction == 1:
      vrai += 1


      As you can see, I predict the same batch of images 20 times. The first time is always slower than the other ones (without fitting, I predict 82 images the first and then 340 a second, with fitting, it's 255 images the first time and 340 a seconde then).



      I'm really out of idea to fix this.










      share|improve this question













      I have a strange issue, the DNN.predict method is quite slower when I load my model's weight than when I train with the fit method. I've also noted that when I run a prediction over a batch of images, it's getting faster and faster to predict.



      Here is my code



      class Reseau(object):

      def init(self, img_size, lr=-1, activation=" "):
      tf.logging.set_verbosity(tf.logging.ERROR)
      self.lr = lr
      self.activation = activation
      self.img_size = img_size
      self.alreadySaved = 0

      def setting(self, X, Y, test_x, test_y, nbEpoch):
      tflearn.init_graph(num_cores=32, gpu_memory_fraction=1)
      with tf.device("/device:GPU:0"):
      convnet = input_data(shape=[None, self.img_size, self.img_size, 3], name='input')

      convnet = conv_2d(convnet, 32, 5, activation=self.activation)
      convnet = max_pool_2d(convnet, 5)

      convnet = conv_2d(convnet, 64, 5, activation=self.activation)
      convnet = max_pool_2d(convnet, 5)

      convnet = conv_2d(convnet, 128, 5, activation=self.activation)
      convnet = max_pool_2d(convnet, 5)

      convnet = conv_2d(convnet, 64, 5, activation=self.activation)
      convnet = max_pool_2d(convnet, 5)

      convnet = conv_2d(convnet, 32, 5, activation=self.activation)
      convnet = max_pool_2d(convnet, 5)

      convnet = flatten(convnet)

      convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
      convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
      convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
      convnet = fully_connected(convnet, 1024, activation=self.activation, name='last')
      convnet = dropout(convnet, 0.8)

      convnet = fully_connected(convnet, 2, activation='softmax')
      convnet = regression(convnet, optimizer='adam', learning_rate=self.lr, loss='categorical_crossentropy', name='targets')

      self.model = tflearn.DNN(convnet, tensorboard_dir='log')

      if self.alreadySaved == 0:
      self.model.fit({'input': X}, {'targets': Y}, n_epoch=nbEpoch, validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=500, show_metric=True, run_id="model")
      self.model.save("./model")
      else:
      self.model.load("./model", weights_only=True)
      return self.model

      def predire(self, img, label):
      image = array(img).reshape(1, self.img_size,self.img_size,3)
      model_out = self.model.predict(image)
      rep = 0

      if np.argmax(model_out) == np.argmax(label): rep = 1
      else: rep = 0

      return rep


      Here is a part of my main



      reseau.setting(X, Y, test_x, test_y, NB_EPOCH)

      X = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,3)
      Y = [i[1] for i in test]
      cpt = 0

      vrai = 0
      start_time = time.time()

      for i in range(20):
      cpt = 0

      vrai = 0
      start_time = time.time()
      for img in tqdm(X):
      prediction = reseau.predire(img, Y[cpt])
      cpt += 1
      if prediction == 1:
      vrai += 1


      As you can see, I predict the same batch of images 20 times. The first time is always slower than the other ones (without fitting, I predict 82 images the first and then 340 a second, with fitting, it's 255 images the first time and 340 a seconde then).



      I'm really out of idea to fix this.







      python tensorflow tflearn






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 8:04









      Arlhal

      62




      62





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257986%2fprediction-is-slower-when-model-is-loaded-than-if-it-is-fited-during-the-process%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257986%2fprediction-is-slower-when-model-is-loaded-than-if-it-is-fited-during-the-process%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma