【问题标题】:Golang Tensorflow Batch Image InputGolang TensorFlow 批量图像输入
【发布时间】:2019-02-18 08:11:21
【问题描述】:

问题说明:无法在 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.

【问题讨论】:

    标签: tensorflow go


    【解决方案1】:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多