我正在使用Keras预测图像类.它适用于Google Cloud ML(GCML),但为了提高效率,需要将其更改为传递base64字符串而不是json数组. 相关文档
我可以轻松运行python代码将base64字符串解码为json数组,但是当使用GCML时,我没有机会运行预处理步骤(除非在Keras中使用Lambda层,但我认为不是正确的方法).
另一个答案建议添加tf.placeholder类型tf.string,这是有道理的,但如何将其纳入Keras模型?
以下是培训模型和保存GCML导出模型的完整代码...
import os
import numpy as np
import tensorflow as tf
import keras
from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.preprocessing import image
from tensorflow.python.platform import gfile
IMAGE_HEIGHT = 138
IMAGE_WIDTH = 106
NUM_CLASSES = 329
def preprocess(filename):
# decode the image file starting from the filename
# end up with pixel values that …Run Code Online (Sandbox Code Playgroud) 我正在尝试将keras模型转换为用于Google Cloud的ml-engine预测的模型。我有一个预先训练的分类器,将numpy数组作为输入。我发送给model.predict的正常工作数据称为input_data。
我将其转换为base 64,并json使用以下几行将其转储到文件中:
data = {}
data['image_bytes'] = [{'b64':base64.b64encode(input_data.tostring())}]
with open('weights/keras/example.json', 'w') as outfile:
json.dump(data, outfile)
Run Code Online (Sandbox Code Playgroud)
现在,我尝试从现有模型创建TF模型:
from keras.models import model_from_json
import tensorflow as tf
from keras import backend as K
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import utils
from tensorflow.python.saved_model import tag_constants, signature_constants
from tensorflow.python.saved_model.signature_def_utils_impl import build_signature_def, predict_signature_def
init = tf.global_variables_initializer()
with tf.Session() as sess:
K.set_session(sess)
sess.run(init)
print("Keras model & weights loading...")
K.set_learning_phase(0)
with open(json_file_path, 'r') as file_handle:
model = …Run Code Online (Sandbox Code Playgroud)