heroku上的Tensorflow_集线器负载内存限制

2024-10-04 01:24:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在tensorflow_hub模型中加载时是否可以减少内存使用?
到目前为止,它达到了heroku的内存配额限制,即512MB。
是否有可能以某种方式分开装载?我尝试过在后台对其进行线程化和加载,但这只解决了请求超时的问题

from flask import Flask, render_template, url_for, make_response,jsonify,request
import tensorflow_hub as hub
import numpy as np
import tensorflow as tf
import threading


app = Flask(__name__,template_folder='templates')




def semantic(search1,search2):
    comparison = model([search1,search2])
    return np.inner(comparison[0],comparison[1])

def task():
    module_url = "https://tfhub.dev/google/universal-sentence-encoder/4" #@param ["https://tfhub.dev/google/universal-sentence-encoder/4", "https://tfhub.dev/google/universal-sentence-encoder-large/5"]
    model = hub.load(module_url)
    tf.keras.backend.clear_session()


@app.route('/')
def menu():
    threading.Thread(target=task).start()
    return render_template("index.html")

@app.route('/<search1>/<search2>',methods=['POST','GET'])
def deploy(search1,search2):
    compare = semantic(search1,search2)
    compare = compare*100
    compare = str(compare)
    compare = compare.strip("")
    response = {
        "Semantic Similarity": compare
    }
    if request.method == 'POST':
        return make_response(jsonify(response),200)
    else:
        return render_template("results.html",compare=compare,)
    

感谢您关注这个帖子,我一直在寻找答案,但唯一的解决方案是要么迁移到另一个平台,要么只是付费


Tags: importappurlreturnresponsetensorflowdefas
1条回答
网友
1楼 · 发布于 2024-10-04 01:24:18

据我所知,不幸的是,在这种情况下,人们无能为力。内存量取决于负责从序列化表示加载权重和图形的逻辑的内部TensorFlow实现。因此,您可以尝试使用TensorFlow提交一个功能请求,看看是否可以改进该逻辑,使其更关注内存

或者,从tfhub.dev加载模型时,库会将内容复制到本地临时目录。如果此目录是内存映射的,则将缓存位置更改为非内存映射位置可能会有所帮助。这可以通过设置TFHUB\U CACHE\U DIR环境变量来实现

最后,如果这两种方法都不起作用,那么尝试使用更小的不同模型也是一种选择

相关问题 更多 >