python - 如何减少预训练模型的加载时间?
问题描述
使用 ResNet50 加载“Imagenet”的权重时,每次加载权重几乎需要 10-11 秒。有什么办法可以减少加载时间?
代码:
from flask import Flask, render_template, request
from werkzeug import secure_filename
from flask import request,Flask
import json
import os
import time
from keras.preprocessing import image as image_util
from keras.applications.imagenet_utils import preprocess_input
from keras.applications.imagenet_utils import decode_predictions
# from keras.applications import ResNet50
from keras.applications.inception_v3 import InceptionV3
import numpy as np
app = Flask(__name__)
@app.route('/object_rec', methods=['POST'])
def object_rec():
f = request.files['file']
file_path = ("./upload/"+secure_filename(f.filename))
f.save(file_path)
image = image_util.load_img(file_path,target_size=(299,299))
image = image_util.img_to_array(image)
image = np.expand_dims(image,axis=0) #(224,224,3) --> (1,224,224,3)
image = preprocess_input(image)
start_time = time.time()
model = InceptionV3(weights="imagenet")
pred = model.predict(image)
p = decode_predictions(pred)
ans = p[0][0]
acc = ans[2]
acc = str(acc)
if ans[1] == "Granny_Smith":
ans = ans[1]
ans = 'Apple'
else:
ans = ans[1]
print("THE PREDICTED IMAGE IS: "+ans)
print("THE ACCURACY IS: "+acc)
print("--- %s seconds ---" % (time.time() - start_time))
result = {
"status": True,
"object": ans,
"score":acc
}
result = json.dumps(result)
return result
if __name__ == '__main__':
app.run(host='0.0.0.0',port=6000,debug=True)
所用时间会在 8-11 秒之间有所不同。如果它在 3-4 秒内加载模型并进行分类,我会很好。
提前致谢
解决方案
您可以这样做的方法是在特定会话中加载模型,然后每次您想要使用模型时只需设置该特定会话,然后只需在需要它的位置调用 predict :
app = Flask(__name__)
sess = tf.Session(config=tf_config)
graph = tf.get_default_graph()
# IMPORTANT: models have to be loaded AFTER SETTING THE SESSION for keras!
# Otherwise, their weights will be unavailable in the threads after the
session there has been set
set_session(sess)
model = InceptionV3(weights="imagenet")
@app.route('/object_rec', methods=['POST'])
def object_rec():
global sess
global graph
with graph.as_default():
set_session(sess)
model.predict(...)
if __name__ == '__main__':
app.run(host='0.0.0.0',port=6000,debug=True)
推荐阅读
- javascript - Firebase 云功能在所有功能运行之前完成
- autohotkey - 即使用户已经登录,通过自动热键运行 URL 也会重定向到空白登录页面
- r - 按百分比拆分
- c# - 如何使用更新在我的游戏中正确使用鼠标
- c - 这些有什么区别?
- c# - EF Core - What is the correct configuration?
- laravel - laravel blade syntax for loop how to call iteration index inside laravel helper functions
- javascript - Cannot assign retrieved json to object in ReactJS using setState and then retrieve it from state
- linux - 如何使用 `gcc` 编译和运行 C 程序?
- service - Anylogic - 根据源在延迟块中设置多个延迟时间