Problem Statement: Could not able to make batch image processing in GO Tensorflow.
I have been going through following URL on GoLang Tensorflow. https://github.com/tensorflow/tensorflow/blob/master/tensorflow/go/example_inception_inference_test.go
I am Facing problem while making batch of images for input to model. Check this line https://github.com/tensorflow/tensorflow/blob/master/tensorflow/go/example_inception_inference_test.go#L199
Any help will be appreciated!
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.
The Go API isn't as comprehensive and to make things easier there are a few additions that could be made. However, given the current API, it is possible to construct the batched tensor using something like this:
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
Another alternative would be to do this batching in the graph by constructing a graph that packs multiple single-image tensors together in a batch using the Pack
operation).
Hope that helps.
(P.S. Seems like you also asked this in a GitHub issue and the same answer is posited there: https://github.com/tensorflow/tensorflow/issues/25440)