简体   繁体   中英

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. 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++. How can I approach this problem if there is no tf api available?

I think what you want is DecodeBase64 first and DecodeRaw after.

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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