【发布时间】:2017-02-10 16:56:37
【问题描述】:
我正在使用带有 celery 和 redis 的烧瓶服务器。调用 .apply_async() 时发生错误。 numpy 数组是可视化 keral 神经网络模型输出的一部分。我知道有一种方法可以将 keras 模型转换为 json。我的主要问题在于我不知道 celery 何时或如何执行转换,并且我无法控制它。
这是我的代码:
@celery.task(bind=True)
def celery_createDirectoryAndSaveNNOutput(self, pInput, ID, filename, layersToShow, model):
layer_outputs = [layer.output for layer in model.layers[1:]]
viz_model = Model(input=model.input, output=layer_outputs)
features = viz_model.predict(pInput)
layerOutputs = {}
folderName = "static/"+ID+"_"+filename
if not os.path.exists(folderName):
os.makedirs(folderName)
for layerIndex in layersToShow:
images = getFeatureMapImages(features[int(layerIndex)])
layerOutputs[layerIndex] = []
for i in range(0, len(images)):
path = folderName+"/layer"+str(int(layerIndex))+"_"+str(i)+".jpg"
cv2.imwrite(path, images[i])
layerOutputs[layerIndex].append(path)
self.update_state(state='PROGRESS', meta={'current': 0, 'total': 10,"status":filename})
return {'current': i, 'total': len(layersToShow),'status': "temp"}
@app.route("/nnvisualisation_uploadMultipleImages", methods=["POST"])
def nnvisualisation_uploadMultipleImages():
uploaded_files = request.files.getlist("file[]")
weight = request.form.get("weight")
ID = request.form.get("ID")
layersToShow = [5]
modelName = "VGG16"
preds = {}
path = os.path.join(STATIC_PATH, uploaded_files[0].filename)
uploaded_files[0].save(os.path.join(STATIC_PATH, uploaded_files[0].filename))
pInput, result = preTrainedModel[modelName](path)
#ERROR HERE:
task = celery_createDirectoryAndSaveNNOutput.s( pInput=pInput, ID=ID, filename=uploaded_files[0].filename, layersToShow=layersToShow, model=getModel(modelName)).apply_async(serializer='json')
...
return jsonify({}), 202, {'Location': url_for('taskstatus',task_id=task.id)}
我已经尝试了所有可用的序列化程序 yaml:
EncodeError:无法表示对象:keras.engine.training.Model 对象位于 0x10fdf26d0>
泡菜:
EncodeError: Can't pickle type 'module': 属性查找 内置.module失败
消息包:
EncodeError: can't serialize array([[[[-103.93900299, -107.77899933, -123.68000031],... , dtype=float32) (numpy 数组)
json:
EncodeError: array([[[[-103.93900299, -107.77899933, -123.68000031],... , dtype=float32) (numpy 数组) 不是 JSON 可序列化的
非常感谢任何评论或建议。谢谢。
【问题讨论】:
-
json是与javascript兼容的字符串格式。它对字典、列表和字符串进行编码。其他python类必须将自己“序列化”为其中一种结构。numpy数组不会自动执行此操作,尽管有一些工具可以提供帮助。搜索一下keras和json。 -
感谢您的评论。我知道有一种方法可以将 keras 模型转换为 json。我的主要问题在于我不知道 celery 何时或如何执行转换,并且我无法控制它。
标签: python json numpy serialization celery