1

I have a tensorflow trained .h5 model (size 27MB), and I found it's very slow in doing prediction (5s to predict whether the image is a dog or cat)

Below are my codes. May I know is it becuase of the model size caused it very slow or other factors?

import tensorflow as tf
import numpy as np
from keras.preprocessing import image


new_model=tf.keras.models.load_model('cat_dog.h5')
img = image.load_img('dog1.jpg', target_size=(150, 150))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])


classes = new_model.predict(images)

if classes[0]>0.8:
    print("dog")
else:
    print("cat")
Liu Xu
  • 21
  • 2
  • Have you instrumented this? That is, is the time really in the `predict` call, or is the time in loading up and translating that 27MB model? – Tim Roberts May 19 '22 at 03:08
  • you should include some runtime functions like mentioned in https://stackoverflow.com/questions/5622976/how-do-you-calculate-program-run-time-in-python. this will give you an idea of which part is taking the most time – Whereismywall May 19 '22 at 03:19
  • It takes about 4s to import tensorflow, which causes the delay – Liu Xu May 19 '22 at 05:53
  • Where are you running this code, please refer to this thread it might help, https://stackoverflow.com/questions/49053434/what-can-cause-the-tensorflow-import-to-be-so-slow – Whereismywall May 19 '22 at 07:50

0 Answers0