降低来自keras的MobilenetV2的输入形状应用程序不会导致更快的推断

问题描述

我试图找到一种减少MobileNetV2推理时间的方法。我将其输入形状从224x224减小到32x32。 问题是我期望的推理时间比我得到的要短得多。 较小模型的推理时间仅为常规模型的60%。 由于经过减少的像素数量大约占像素数量的1/100,因此我期望性能增益的比率大致相同。 我有什么想念吗?

这是我使用的代码

from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input,decode_predictions
from tensorflow.keras.optimizers import SGD
import numpy as np
from timeit import default_timer as timer
from fake_image import get_fake_image
N = 30
regular_model = MobileNetV2(weights='imagenet')
small_model = MobileNetV2(input_shape = (32,32,3),include_top = False)
small_model.compile(SGD(lr=0.0001,momentum=0.9),loss="categorical_crossentropy")


img_path = 'elephant.jpg'
img = image.load_img(img_path,target_size=(224,224))
x = image.img_to_array(img)
x = np.expand_dims(x,axis=0)

x = preprocess_input(x)

results_m = 0
for i in range(N):
    start = timer()
    preds = regular_model.predict(x)
    end = timer()
    if i>0:
        results_m = results_m + (end-start)
    # decode the results into a list of tuples (class,description,probability)
    # (one such list for each sample in the batch)
    #print('Predicted:',decode_predictions(preds,top=3)[0])
    # Predicted: [(u'n02504013',u'Indian_elephant',0.82658225),(u'n01871265',u'tusker',0.1122357),(u'n02504458',u'African_elephant',0.061040461)]
    print("{:02d}. Regular MobileNetV2 Inference time: {:.4f}sec".format(i,(end-start)))

print("Regular MobileNetV2 Average {:.4f}".format(results_m/(N-1)))


results_s = 0
for i in range(N):
    x = np.expand_dims(get_fake_image(32,1),axis=0)
    x = preprocess_input(x)
    start = timer()
    preds = small_model.predict(x)
    end = timer()
    if i>0:
        results_s = results_s + (end-start)
    print("{:02d}. Small MobileNetV2 Inference time: {:.4f}sec".format(i,(end-start)))
print("Small MobileNetV2 Average {:.4f}".format(results_s/(N-1)))
print("Averages do not include first inference of each model")


print("Ratio Mini of Regular: {:.2f}%".format((results_s/results_m)*100))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...