简体   繁体   English

在 tensorflow c++ 中是否有将 base64 字符串解码为张量的函数?

[英]Is there a function to decode a base64 string into tensors in tensorflow c++?

I am trying to decode a base64 string data into tensors for feeding into a model for prediction.我正在尝试将 base64 字符串数据解码为张量,以便输入模型进行预测。 There is a function(tf.image.decode_image) in tf python for converting string into tensors but could not find any api in tf c++. tf python 中有一个函数(tf.image.decode_image) 用于将字符串转换为张量,但在 tf c++ 中找不到任何 api。 How can I approach this problem if there is no tf api available?如果没有可用的 tf api,我该如何解决这个问题?

I think what you want is DecodeBase64 first and DecodeRaw after.我想你想要的是首先是DecodeBase64DecodeRawDecodeBase64

#include <vector>
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/cc/ops/string_ops.h"
#include "tensorflow/cc/ops/parsing_ops.h"
#include "tensorflow/core/framework/tensor.h"

int main() {
  using namespace tensorflow;
  using namespace tensorflow::ops;
  Scope root = Scope::NewRootScope();
  // Float32 array [1. 2. 3. 4. 5. 6.] base64 encoded
  auto b64 = Const(root, "AACAPwAAAEAAAEBAAACAQAAAoEAAAMBA");
  // Decode base64
  auto decoded = DecodeBase64(root, b64);
  // Parse bytes
  auto parsed = DecodeRaw(root, decoded, DT_FLOAT32);
  // Run
  std::vector<Tensor> outputs;
  ClientSession session(root);
  // Get parsed data
  TF_CHECK_OK(session.Run({parsed}, &outputs));
  // outputs[0] == [1. 2. 3. 4. 5. 6.]
  LOG(INFO) << outputs[0].flat<float>();
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM