gpt4 book ai didi

python - TFJS 将模型保存到带有 header 的 http

转载 作者:行者123 更新时间:2023-12-01 00:12:18 27 4
gpt4 key购买 nike

我正在尝试使用 https://www.tensorflow.org/js/guide/save_load 上的指南保存并上传带有附加 header (用于类名)的 tfjs 模型。有后端复制自https://gist.github.com/dsmilkov/1b6046fd6132d7408d5257b0976f7864 。但遵循指南并不能按照指南中的预期和说明进行操作。我哪里出错了?谢谢

我的浏览器代码是:

const saveResult = await model.save(tf.io.http('http://localhost:5000/upload', {method: 'POST', headers: {'class': 'Dog'}}));

服务器的代码是:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import io

from flask import Flask, Response, request
from flask_cors import CORS, cross_origin
import tensorflow as tf
import tensorflowjs as tfjs
import werkzeug.formparser

class ModelReceiver(object):

def __init__(self):
self._model = None
self._model_json_bytes = None
self._model_json_writer = None
self._weight_bytes = None
self._weight_writer = None

@property
def model(self):
self._model_json_writer.flush()
self._weight_writer.flush()
self._model_json_writer.seek(0)
self._weight_writer.seek(0)

json_content = self._model_json_bytes.read()
weights_content = self._weight_bytes.read()
return tfjs.converters.deserialize_keras_model(
json_content,
weight_data=[weights_content],
use_unique_name_scope=True)

def stream_factory(self,
total_content_length,
content_type,
filename,
content_length=None):
# Note: this example code isnot* thread-safe.
if filename == 'model.json':
self._model_json_bytes = io.BytesIO()
self._model_json_writer = io.BufferedWriter(self._model_json_bytes)
return self._model_json_writer
elif filename == 'model.weights.bin':
self._weight_bytes = io.BytesIO()
self._weight_writer = io.BufferedWriter(self._weight_bytes)
return self._weight_writer


def main():
app = Flask('model-server')
CORS(app)
app.config['CORS_HEADER'] = 'Content-Type'

model_receiver = ModelReceiver()

@app.route('/upload', methods=['POST'])
@cross_origin()
def upload():
print('headers are:')
print(request.headers)
print('Handling request...')
werkzeug.formparser.parse_form_data(
request.environ, stream_factory=model_receiver.stream_factory)
print('Received model:')
with tf.Graph().as_default(), tf.Session():
model = model_receiver.model
model.summary()
# You can perform `model.predict()`, `model.fit()`,
# `model.evaluate()` etc. here.
return Response(status=200)

app.run('localhost', 5000)


if __name__ == '__main__':
main()

最佳答案

如果您的目标是使用模型存储一些辅助信息(例如类标签),那么 TensorFlow.js 中的 tf.LayersModel 的一个相对鲜为人知的功能将使您的生活变得更加轻松更轻松。它比使用 header 更简单。

它是 setUserDefinedMetadata()getUserDefinedMetadata() 方法。

在 JavaScript 方面,执行以下操作:

// The argument to setUserDefinedMetadata() can be any serializable JSON
// object of a reasonable size.
myModel.setUserDefinedMetadata({outputClassLabels: ['Cat', 'Dog', 'Turtle']});

// The user metadata is stored with the model itself. No need to specify
// additional headers.
await model.save('http://localhost:5000/upload');

接收模型工件的服务器可以简单地检查请求中 JSON 负载的“userDefinedMetadata”字段。

关于python - TFJS 将模型保存到带有 header 的 http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59556710/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com