【问题标题】:how do i reduce the loading time of a pre-trained model?如何减少预训练模型的加载时间?
【发布时间】:2020-05-27 22:08:48
【问题描述】:

使用 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 秒内加载模型并进行分类,我会很好。

提前致谢

【问题讨论】:

  • 你应该在你的函数之外实例化模型,以便它只加载一次。对于像 ResNet 这样的大型模型来说,大约 10 秒的时间并不令人惊讶。
  • 是的,但是在函数外调用模型是行不通的。我实际上是使用flask将代码转换为API,所以当我在函数外部实例化模型加载时会给我张量错误。
  • 我根据发现的一个 tf+Flask 错误更新了答案here

标签: python keras deep-learning resnet transfer-learning


【解决方案1】:

您可以做到这一点的方法是在特定会话中加载模型,然后每次您想使用模型时只需设置该特定会话,然后只需在需要它的位置调用预测:

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)

【讨论】:

  • 这样做会给我一个错误:ValueError: Tensor Tensor("fc1000/Softmax:0", shape=(?, 1000), dtype=float32) 不是该图的元素。当我在 object_rec() 中调用它时,这个 get_model() 每次都会重新加载。
  • 您不会将其加载到 object_req 中,而是将其加载到外部,就像我写的那样。要解决此问题,请尝试 app.run(..., threaded= False)
  • 我得到了同样的错误:ValueError: Tensor Tensor("fc1000/Softmax:0", shape=(?, 1000), dtype=float32) is not an element of this graph.
  • 还是同样的错误:ValueError: Tensor Tensor("fc1000/Softmax:0", shape=(?, 1000), dtype=float32) is not an element of this graph.
  • 您在开发、部署吗?这取决于你使用的是什么配置,看this
猜你喜欢
  • 2021-02-19
  • 2021-07-16
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-27
相关资源
最近更新 更多