gpt4 book ai didi

python - 如何加载经过训练的 TensorFlow 模型以使用不同的批量大小进行预测?

转载 作者:行者123 更新时间:2023-12-01 09:32:09 25 4
gpt4 key购买 nike

这是一个非常简单的问题,但确实吸引了我。

我的目的是加载经过训练的 TF 模型,然后创建一个 session 来运行预测/推理,并评估适合我的设备环境的批量大小。

我准备了两种类型的模型:卡住模型和保存模型。

对于卡住模型,我通过.ParseFromString()成功加载并获取了GraphDef,并通过TensorRT优化了GraphDef,但输入节点的batch_size固定为1(1*299*299*3)。似乎在导出然后卡住模型时无法配置batch_size,并且之后无法更改,因为它是仅附加的。

对于保存的模型,输入节点的维度是? (?*299*299*3)。似乎它应该能够接受任何大小的批处理。但是当我加载保存的模型后,然后输入299 * 299 * 3图像,并得到以下错误:

ValueError: Cannot feed value of shape (299, 299, 3) for Tensor u'input:0', which has shape '(?, 299, 299, 3)'

我尝试将 axis=0 扩展到输入图像,但仍然出现错误:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'import/input' with dtype float and shape [?,299,299,3]

[[Node: import/input = Placeholder[dtype=DT_FLOAT, shape=[?,299,299,3], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]

总的来说,这应该是一个非常简单的问题,但我不知道如何解决它。

欢迎任何想法。

谢谢

<小时/>

卡住模型的脚本,占位符固定为(1*299*299*3),因此只接受“args.batch_size”为1。

    g = ops.Graph()
with g.as_default():
inp, out = importer.import_graph_def(graph_def=f32_graph, return_elements=['Placeholder','InceptionV3/Logits/SpatialSqueeze'])
inp = inp.outputs[0]
out = out.outputs[0]
print tf.shape(inp)
if args.image:
import numpy as np
func = TestKit.preprocess_func['tensorflow'][args.network]
img = func(args.image)
img = np.expand_dims(img, axis = 0)
batch_input = img
for i in range(int(args.batch_size)-1):
batch_input = np.concatenate((batch_input, img), axis=0)
print len(batch_input)
gpu_options = cpb2.GPUOptions(per_process_gpu_memory_fraction=0.625)
with csess.Session(config=cpb2.ConfigProto(gpu_options=gpu_options), graph=g) as sess:
t0 = time.time()
for _ in range(2000):
val = sess.run(out, {inp: batch_input})
predict = np.squeeze(val)
top_indices = predict.argsort()[-5:][::-1]
result = [(i, predict[i]) for i in top_indices]
t1 = time.time()
print result
print 'Duration:', str(t1-t0)
<小时/>

保存模型的脚本,“input:0”节点是(?*299*299*3),但不能接受上面提到的任何形状的输入图像。

    with tf.Graph().as_default():                                                                                                                       
with tf.Session(graph=tf.Graph()) as sess:
tf.saved_model.loader.load(sess, ['serve'], './')
for op in sess.graph.get_operations():
print str(op.name)

if args.image:
import numpy as np
func = TestKit.preprocess_func['tensorflow'][args.network]
img = func(args.image)
img = np.expand_dims(img, axis = 0)
inp, out = importer.import_graph_def(graph_def=sess.graph.as_graph_def(), return_elements=['input','InceptionV3/Logits/SpatialSqueeze'])
t0 = time.time()
for _ in range(1000):
val = sess.run(out, {'input:0':img})
predict = np.squeeze(val)
top_indices = predict.argsort()[-5:][::-1]
result = [(i, predict[i]) for i in top_indices]
t1 = time.time()
print result
print 'Duration:', str(t1-t0)

最佳答案

我终于解决了这个问题。要点是卡住模型不会更改批量大小,因为图形是仅附加的,因此一旦使用输入占位符(1 * 299 * 299 * 3)生成原始图形,它就永远不会改变。

最后,我尝试使用输入占位符(None * 299 * 299 * 3)重新生成原始二进制图(二进制.pbtxt)。然后将此二值图转换为卡住模型,卡住模型可以具有维度为 (? * 299 * 299 * 3) 的输入占位符。现已支持批量输入。

谢谢

关于python - 如何加载经过训练的 TensorFlow 模型以使用不同的批量大小进行预测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49872844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com