问题陈述:无法在GO Tensorflow中进行批量图像处理。
我一直在GoLang Tensorflow上浏览以下URL。https://github.com/tensorflow/tensorflow/blob/master/tensorflow/go/example_inception_inference_test.go
我在制作批量图像以输入模型时遇到问题。检查此行https://github.com/tensorflow/tensorflow/blob/master/tensorflow/go/example_inception_inference_test.go#L199
任何帮助将不胜感激!
result, err := classifier.Session.Run(
map[tf.Output]*tf.Tensor{
inputTensor.Output(0): imageTensor,
},
[]tf.Output{
outputTensorOne.Output(0),
outputTensorTwo.Output(0),
},
nil, /*targets*/
)
// How to make that imageTensor a batch of images in GO Tensorflow.
Go API 并不那么全面,为了使事情变得更容易,可以添加一些内容。但是,给定当前的 API,可以使用如下内容构造批处理张量:
var buf bytes.Buffer
for i, img := range images {
bytes, err := gocv.IMEncode(gocv.JPEGFileExt, img)
if err != nil {
fmt.Println("Error")
}
tensor, err = tf.NewTensor(string(bytes))
if err != nil {
fmt.Println("Error")
}
normalized, err := session.Run(
map[tf.Output]*Tensor: { input: tensor },
[]tf.Output{output},
nil)
if _, err := normalized[0].WriteContentsTo(&buf); err != nil {
// Handle error
}
}
batchShape := []int64{len(images), 224, 224, 3}
batch, err := tf.ReadTensor(tf.Float, batchShape, &buf)
if err != nil {
// Handle error
}
// Now feed "batch" to the model
另一种选择是通过构建一个图来在图中执行此批处理,该图使用 Pack
操作将多个单图像张量打包在一起(。
希望有帮助。
(附言似乎您在 GitHub 问题中也问过这个问题,并且那里提出了相同的答案:https://github.com/tensorflow/tensorflow/issues/25440(