dongtou5557 2019-02-01 23:23
浏览 434

Golang Tensorflow批处理图像输入

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.
  • 写回答

1条回答 默认 最新

  • dpwdsmbvm496180204 2019-02-18 00:11
    关注

    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)

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?