2017-02-10 47 views
1

我使用芹菜和redis的瓶服务器。调用.apply_async()时发生错误。 numpy数组是可视化keral神经网络模型输出的一部分。我知道有一种方法可以将keras模型转换为json。我的主要问题在于,我不知道芹菜何时或如何进行转化,我无法控制它。芹菜错误:numpy数组不是JSON可序列化

这里是我的代码:

@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: cannot represent an object: keras.engine.training.Model object at 0x10fdf26d0>

泡菜:

EncodeError: Can't pickle type 'module': attribute lookup builtin.module failed

msgpack:

EncodeError: can't serialize array([[[[-103.93900299, -107.77899933, -123.68000031],... , dtype=float32) (numpy array)

JSON:

EncodeError: array([[[[-103.93900299, -107.77899933, -123.68000031],... , dtype=float32) (numpy array) is not JSON serializable

任何意见或建议是极大的赞赏。谢谢。

+0

'json'是一个与'javascript'兼容的字符串格式。它编码字典,列表和字符串。其他的'python'类必须将它们自己“序列化”成其中一个结构。 'numpy'阵列不会自动做到这一点,尽管有些工具可以提供帮助。做一些关于'keras'和'json'的搜索。 – hpaulj

+0

感谢您的评论。我知道有一种方法可以将keras模型转换为json。我的主要问题在于,我不知道芹菜何时或如何进行转化,我无法控制它。 – matchifang

回答

-1

https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model

# save as JSON 
json_string = model.to_json() 

虽然可以节省只是architecture不是重量等

在您需要探索通过keras提供的方法的任何情况。

+0

感谢您的评论。问题是转换是由芹菜完成的,所以我无法控制模型如何转换为json。 – matchifang

+0

你能告诉我我应该把那行代码放在哪里吗?我不知道芹菜在哪里连载它。谢谢。 – matchifang

1

My main problem lies in the fact that I do not know when or how celery performs the conversion, and I do not have control over it.

有剂量存在控制转化的方法。 你可以注册可以转储numpy数组的自定义json串行器。

请参阅本文档Serializers

而且有一个很好的example