我在使用Resnet50预训练模型预测的时候,用cpu花了1.6s,用gpu花了6.4s,机子比较老用的tensorflow1.12。代码用的是官方提供的,甚至不知道哪里出问题。
import time
# from tensorflow.keras.applications.resnet50 import ResNet50,preprocess_input, decode_predictions
# from tensorflow.keras.preprocessing import image
from keras.applications.resnet50 import ResNet50,preprocess_input, decode_predictions
from keras.preprocessing import image
import numpy as np
ss = time.time()
model = ResNet50(weights=None, include_top=True)
model.load_weights(r'E:\pycode\fight\resnet18_imagenet_1000.h5', by_name=True)
ee = time.time()
img_path = r'E:\pycode\fight\hashiqi.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)
s = time.time()
preds = model.predict(x)
e = time.time()
# 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])
print(e-s)
print(ee-ss)
cpu
gpu