用服务进行张量流图像再训练
tensorflow image retraining with serving
我正在尝试使用 tensorflow 服务来提供重新训练的初始图。对于再培训,我正在使用 example. However I need to make changes to this graph to get it working with serving export code.
由于在 tensorflow 服务中,您将收到序列化图像作为输入,图形输入应以此开头:
serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
feature_configs = {
'image/encoded': tf.FixedLenFeature(shape=[], dtype=tf.string),
}
tf_example = tf.parse_example(serialized_tf_example, feature_configs)
jpegs = tf_example['image/encoded']
images = tf.map_fn(preprocess_image, jpegs, dtype=tf.float32)
此图像张量应输入到重新训练的初始图。但是我不知道是否可以在张量流中将一张图添加到另一张图之前,就像您可以使用 placeholder_with_input 轻松附加(已在再训练代码中完成)一样。
graph, bottleneck_tensor, jpeg_data_tensor, resized_image_tensor = (
create_inception_graph())
理想情况下,在图像再训练代码中,我收到一个占位符张量 jpeg_input_data
。我需要将张量 images
附加到此占位符张量 jpeg_data_tensor
并使用导出器将其导出为单个图,以便可以使用 tensorflow 服务提供服务。但是我没有执行它的任何张量流指令。除了这种方法还有其他方法吗?
一种解决方法是:
model_path = 'trained/export.pb'
with tf.Graph().as_default():
with tf.Session() as sess:
with gfile.FastGFile(model_path, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
# Your prepending ops here
images_placeholder = tf.placeholder(tf.string, name='tf_example')
...
images = tf.map_fn(preprocess_image, jpegs, dtype=tf.float32)
tf.import_graph_def(graph_def, name='inception', input_map={'ResizeBilinear:0': images})
请特别注意 input_map
参数。 ResizeBilinear:0
可能不是您需要的操作的正确名称 - 您可以通过以下方式列出操作:
[n.name for n in tf.get_default_graph().as_graph_def().node]
我知道这不是一个完整的答案,也许不是最有效的,但希望它可以帮助您入门。就一个heads-up,还有this blogpost。
因此,由于您已经重新训练了模型,我假设该模型是 Protobuf,但您可以将其加载到 Python 对象中并使用自定义函数从该对象中获取服务处理批处理或原子操作。
关于你的图形问题,据我所知,当你加载一个 tf.Graph() 对象时,你只能使用该对象而不能使用其他图形......也就是说如果您有另一个图是现有 Inception-V3 图的扩展,那么您可以很容易地将它添加到自定义图的计算图中。
我正在尝试使用 tensorflow 服务来提供重新训练的初始图。对于再培训,我正在使用 example. However I need to make changes to this graph to get it working with serving export code.
由于在 tensorflow 服务中,您将收到序列化图像作为输入,图形输入应以此开头:
serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
feature_configs = {
'image/encoded': tf.FixedLenFeature(shape=[], dtype=tf.string),
}
tf_example = tf.parse_example(serialized_tf_example, feature_configs)
jpegs = tf_example['image/encoded']
images = tf.map_fn(preprocess_image, jpegs, dtype=tf.float32)
此图像张量应输入到重新训练的初始图。但是我不知道是否可以在张量流中将一张图添加到另一张图之前,就像您可以使用 placeholder_with_input 轻松附加(已在再训练代码中完成)一样。
graph, bottleneck_tensor, jpeg_data_tensor, resized_image_tensor = (
create_inception_graph())
理想情况下,在图像再训练代码中,我收到一个占位符张量 jpeg_input_data
。我需要将张量 images
附加到此占位符张量 jpeg_data_tensor
并使用导出器将其导出为单个图,以便可以使用 tensorflow 服务提供服务。但是我没有执行它的任何张量流指令。除了这种方法还有其他方法吗?
一种解决方法是:
model_path = 'trained/export.pb'
with tf.Graph().as_default():
with tf.Session() as sess:
with gfile.FastGFile(model_path, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
# Your prepending ops here
images_placeholder = tf.placeholder(tf.string, name='tf_example')
...
images = tf.map_fn(preprocess_image, jpegs, dtype=tf.float32)
tf.import_graph_def(graph_def, name='inception', input_map={'ResizeBilinear:0': images})
请特别注意 input_map
参数。 ResizeBilinear:0
可能不是您需要的操作的正确名称 - 您可以通过以下方式列出操作:
[n.name for n in tf.get_default_graph().as_graph_def().node]
我知道这不是一个完整的答案,也许不是最有效的,但希望它可以帮助您入门。就一个heads-up,还有this blogpost。
因此,由于您已经重新训练了模型,我假设该模型是 Protobuf,但您可以将其加载到 Python 对象中并使用自定义函数从该对象中获取服务处理批处理或原子操作。
关于你的图形问题,据我所知,当你加载一个 tf.Graph() 对象时,你只能使用该对象而不能使用其他图形......也就是说如果您有另一个图是现有 Inception-V3 图的扩展,那么您可以很容易地将它添加到自定义图的计算图中。