使用在 GCP 中训练的模型进行推理?
Using model trained in GCP for inferencing?
我是这个话题的新手,所以请多多包涵。
我一直在按照本教程训练我自己的分割模型:ShapeMask on GCP
训练过程成功完成,我得到以下输出:
现在,我正在尝试在 google 提供的 colab notebook 中使用它:Colab
但是我无法向它提供经过训练的模型。我需要在该笔记本中保存一个模型,但是我很幸运将我的输出转换为保存 model.I 在 VM 和 TPU 上使用 TF 版本 1.15.2。
我缺少训练和推理之间的几个步骤。但我不知道它们是什么。非常感谢任何帮助。谢谢!
到目前为止,我已经尝试使用 this. And read thru this 将我的文件转换为保存的模型,但无法理解如何使用它。
所以我能够从检查点保存模型。在 colab 笔记本上使用以下代码片段。我必须在 colab notebook 中启用 TPU(运行时 > 更改运行时类型 > TPU)可能是因为我在 TPU 上尝试过(否则会抛出错误)。
import os
import tensorflow.compat.v1 as tf
from google.protobuf import text_format
from tensorflow import keras
trained_checkpoint_prefix ='<GC storage bucket path>/model.ckpt-1000'
export_dir = '<GC storage bucket path>'
tpu_address = 'grpc://' + os.environ['COLAB_TPU_ADDR']
graph = tf.Graph()
with tf.Session(target=tpu_address,graph=graph) as sess:
# Reste from checkpoint
loader = tf.train.import_meta_graph(trained_checkpoint_prefix + '.meta', clear_devices=True)
loader.restore(sess, trained_checkpoint_prefix)
# Export checkpoint to SavedModel
builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(export_dir)
builder.add_meta_graph_and_variables(sess, [tf.saved_model.TRAINING, tf.saved_model.SERVING], strip_default_attrs=True)
builder.save()
现在我说是因为这个插入 Colab 教程 noteook 的保存模型不起作用。它在单元格 6 中成功读取了模型,但推理部分出现错误。就在这里:
num_detections, detection_boxes, detection_classes, detection_scores, detection_masks, detection_outer_boxes, image_info = session.run(
['NumDetections:0', 'DetectionBoxes:0', 'DetectionClasses:0', 'DetectionScores:0', 'DetectionMasks:0', 'DetectionOuterBoxes:0', 'ImageInfo:0'],
feed_dict={'Placeholder:0': np_image_string})
进程以下列错误结束:
KeyError: "The name 'Placeholder:0' refers to a Tensor which does not exist. The operation, 'Placeholder', does not exist in the graph."
它也找不到所有其他变量名。我不确定是什么原因造成的,一旦我这样做就会更新答案!
编辑 1:
我使用以下方法解决了问题 readme。
首先我使用了 TF 2.2 和 TPU repo 的主要分支而不是 shapemask 分支。然后按照原始教程中的确切步骤进行培训。并使用以下命令导出保存的模型:
python ~/tpu/models/official/detection/export_saved_model.py \
--export_dir="${EXPORT_DIR?}" \
--checkpoint_path="${CHECKPOINT_PATH?}" \
--params_override="${PARAMS_OVERRIDE?}" \
--batch_size=${BATCH_SIZE?} \
--input_type="${INPUT_TYPE?}" \
--input_name="${INPUT_NAME?}" \
此处参数覆盖标志应传递给训练期间创建的 params.yaml 文件。批量大小设置为 1,一次处理一张图像。可以在自述文件中找到更多详细信息。
注意:我必须注释掉以下行才能执行:
import segmentation from serving
它导出模型并能够在 colab notebook 中加载和使用它,只需对 notebook 进行一些细微的调整。
我是这个话题的新手,所以请多多包涵。
我一直在按照本教程训练我自己的分割模型:ShapeMask on GCP 训练过程成功完成,我得到以下输出:
现在,我正在尝试在 google 提供的 colab notebook 中使用它:Colab
但是我无法向它提供经过训练的模型。我需要在该笔记本中保存一个模型,但是我很幸运将我的输出转换为保存 model.I 在 VM 和 TPU 上使用 TF 版本 1.15.2。
我缺少训练和推理之间的几个步骤。但我不知道它们是什么。非常感谢任何帮助。谢谢!
到目前为止,我已经尝试使用 this. And read thru this 将我的文件转换为保存的模型,但无法理解如何使用它。
所以我能够从检查点保存模型。在 colab 笔记本上使用以下代码片段。我必须在 colab notebook 中启用 TPU(运行时 > 更改运行时类型 > TPU)可能是因为我在 TPU 上尝试过(否则会抛出错误)。
import os
import tensorflow.compat.v1 as tf
from google.protobuf import text_format
from tensorflow import keras
trained_checkpoint_prefix ='<GC storage bucket path>/model.ckpt-1000'
export_dir = '<GC storage bucket path>'
tpu_address = 'grpc://' + os.environ['COLAB_TPU_ADDR']
graph = tf.Graph()
with tf.Session(target=tpu_address,graph=graph) as sess:
# Reste from checkpoint
loader = tf.train.import_meta_graph(trained_checkpoint_prefix + '.meta', clear_devices=True)
loader.restore(sess, trained_checkpoint_prefix)
# Export checkpoint to SavedModel
builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(export_dir)
builder.add_meta_graph_and_variables(sess, [tf.saved_model.TRAINING, tf.saved_model.SERVING], strip_default_attrs=True)
builder.save()
现在我说是因为这个插入 Colab 教程 noteook 的保存模型不起作用。它在单元格 6 中成功读取了模型,但推理部分出现错误。就在这里:
num_detections, detection_boxes, detection_classes, detection_scores, detection_masks, detection_outer_boxes, image_info = session.run(
['NumDetections:0', 'DetectionBoxes:0', 'DetectionClasses:0', 'DetectionScores:0', 'DetectionMasks:0', 'DetectionOuterBoxes:0', 'ImageInfo:0'],
feed_dict={'Placeholder:0': np_image_string})
进程以下列错误结束:
KeyError: "The name 'Placeholder:0' refers to a Tensor which does not exist. The operation, 'Placeholder', does not exist in the graph."
它也找不到所有其他变量名。我不确定是什么原因造成的,一旦我这样做就会更新答案!
编辑 1:
我使用以下方法解决了问题 readme。
首先我使用了 TF 2.2 和 TPU repo 的主要分支而不是 shapemask 分支。然后按照原始教程中的确切步骤进行培训。并使用以下命令导出保存的模型:
python ~/tpu/models/official/detection/export_saved_model.py \
--export_dir="${EXPORT_DIR?}" \
--checkpoint_path="${CHECKPOINT_PATH?}" \
--params_override="${PARAMS_OVERRIDE?}" \
--batch_size=${BATCH_SIZE?} \
--input_type="${INPUT_TYPE?}" \
--input_name="${INPUT_NAME?}" \
此处参数覆盖标志应传递给训练期间创建的 params.yaml 文件。批量大小设置为 1,一次处理一张图像。可以在自述文件中找到更多详细信息。
注意:我必须注释掉以下行才能执行:
import segmentation from serving
它导出模型并能够在 colab notebook 中加载和使用它,只需对 notebook 进行一些细微的调整。