在 TensorFlow 中运行具有不同批量大小的保存模型的最佳方法是什么? [英] What is the best way to run saved model with different batch size in TensorFlow?

查看:36
本文介绍了在 TensorFlow 中运行具有不同批量大小的保存模型的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我训练了 来自 TensorFlow 存储库的 Cifar10 示例模型batch_size 为 128,效果很好.然后我冻结图并设法运行它使用 C++ 就像他们在 C++ 标签图像示例.

I trained Cifar10 example model from TensorFlow's repository with batch_size 128 and it worked fine. Then I froze graph and managed to run it with C++ just like they do it in their C++ label image example.

唯一的问题是我必须人工生成形状 [128, image_height, image_width, channels] 的张量来使用 C++ 对单个图像进行分类,因为保存的模型需要批量输入 128 个样本,因为这是到来的样本数来自队列.

The only problem was that I had to artificially generate tensor of shape [128, image_height, image_width, channels] to classify single image with C++ because saved model expects input of 128 samples in a batch since that is number of samples that comes from queue.

我尝试使用 batch_size = 1 训练 Cifar10 示例,然后当我使用 C++ 运行模型时,我设法对示例进行了一个一个分类,但这似乎不是一个很好的解决方案.我还尝试在保存的图形文件中手动更改张量形状,但没有奏效.

I tried training Cifar10 example with batch_size = 1 and then I managed to classify examples one by one when I run model with C++, but that doesn't seem like a great solution. I also tried manually changing tensor shapes in saved graph file but it didn't work.

我的问题是用固定批量大小(如 32、64、128 等)训练模型然后保存模型以便它可以用于任意长度的批量大小的最佳方法是什么?如果做不到,那么如何保存模型才能对样本进行一一分类.

My question is what is the best way to train model with fixed batch size (like 32, 64, 128 etc.) and then save model so that it can be used with batch size of arbitrary length? If that's not possible, then how to save model to be able to classify samples one by one.

推荐答案

听起来问题在于 TensorFlow 正在将批量大小烘焙"到图中的其他张量(例如,如果图中包含 tf.shape(t) 对于某些形状取决于批大小的张量 t,批大小可能作为常量存储在图中).解决方案是稍微更改您的程序,以便 tf.train.batch() 返回具有可变批量大小的张量.

It sounds like the problem is that TensorFlow is "baking in" the batch size to other tensors in the graph (e.g. if the graph contains tf.shape(t) for some tensor t whose shape depends on the batch size, the batch size might be stored in the graph as a constant). The solution is to change your program slightly so that tf.train.batch() returns tensors with a variable batch size.

tf.train.batch() 方法接受 tf.Tensor 作为 batch_size 参数.也许修改可变大小批次的程序的最简单方法是为批次大小定义占位符:

The tf.train.batch() method accepts a tf.Tensor for the batch_size argument. Perhaps the simplest way to modify your program for variable-sized batches would be to define a placeholder for the batch size:

# Define a scalar tensor for the batch size, so that you can alter it at
# Session.run()-time.
batch_size_tensor = tf.placeholder(tf.int32, shape=[])
input_tensors = tf.train.batch(..., batch_size=batch_size_tensor, ...)

这将防止批量大小被烘焙到您的 GraphDef 中,因此您应该能够在 C++ 中提供任何批量大小的值.但是,此修改将要求您在每一步都输入批处理大小的值,这有点乏味.

This would prevent the batch size from being baked into your GraphDef, so you should be able to feed values of any batch size in C++. However, this modification would require you to feed a value for the batch size on every step, which is slightly tedious.

假设您总是希望使用批量大小 128 进行训练,但保留以后更改批量大小的灵活性,您可以使用 tf.placeholder_with_default() 指定批处理大小应为 128, 提供一个替代值:

Assuming that you always want to train with batch size 128, but retain the flexibility to change the batch size later, you could use a tf.placeholder_with_default() to specify that the batch size should be 128 when you don't feed an alternative value:

# Define a scalar tensor for the batch size, so that you can alter it at
# Session.run()-time.
batch_size_tensor = tf.placeholder_with_default(128, shape=[])
input_tensors = tf.train.batch(..., batch_size=batch_size_tensor, ...)

这篇关于在 TensorFlow 中运行具有不同批量大小的保存模型的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆