简体   繁体   中英

How to create a random tensor with a given shape with the C++ Tensorflow API?

I have a neural network (a GAN that saves a generated image from random noise), which uses during the inference a random tensor as an input. My model or GraphDef in .pb format shows the following signature of inputs and outputs using saved_model_cli .

    The given SavedModel SignatureDef contains the following input(s):
      inputs['dense_1_input'] tensor_info:
          dtype: DT_FLOAT
          shape: (-1, 100)
          name: serving_default_dense_1_input:0
    The given SavedModel SignatureDef contains the following output(s):
      outputs['conv2d_2'] tensor_info:
          dtype: DT_FLOAT
          shape: (-1, 28, 28, 1)
          name: StatefulPartitionedCall:0
    Method name is: tensorflow/serving/predict

In my inference file in python, I have a function that creates a simple random normal vector ( generate_latent_data ).

def generate_latent_data(latent_dim, num_samples):
    """
    Prepare latent dimensions for Generator.
    It creates random gaussian values for "latent_dim" dimensions.
    The number of dimensions can be changed.
    :return: random latent data
    """
    x_input_generator = randn(latent_dim * num_samples)
    x_input_generator = x_input_generator.reshape(num_samples, latent_dim)
    return x_input_generator

Now, I am trying to deploy my inference file (Python) into in a C++ executable. I was already able to build Tensorflow using bazel . I am doing now the conversion to the gan_loader.cc file that later on is built using bazel . I will put both files that I did so far so you can compare:

inference.py

from pathlib import Path
from numpy.random import randn
from matplotlib import pyplot as plt
from tensorflow.keras.models import load_model

# ================ #
LATENT_DIM = 100
SAMPLES_PER_ROW = 5
# ================ #


def generate_latent_data(latent_dim, num_samples):
    """
    Prepare latent dimensions for Generator.
    It creates random gaussian values for "latent_dim" dimensions.
    The number of dimensions can be changed.
    :return: random latent data
    """
    x_input_generator = randn(latent_dim * num_samples)
    x_input_generator = x_input_generator.reshape(num_samples, latent_dim)
    return x_input_generator


def save_fig_inference(image, row_num_images=10):
    """
    Save generated "fake" images during inference in root directory when project is located.
    Each time is called, it will save a set of subplots (size: row_num_images ** 2) with grayscale generated images.
    Function used as well for the inference.
    :return: fake dataset X and fake labels Y
    """
    filename = "generated_images_inference/generated_image_inference.png"
    for i in range(row_num_images * row_num_images):
        plt.subplot(row_num_images, row_num_images, 1 + i)
        plt.axis("off")
        plt.imshow(image[i, :, :, 0], cmap="gray_r")
    plt.savefig(filename)
    plt.close()


# Create folder for images
print("[INFO] Create folder for saving images during inference...")
Path("generated_images_inference").mkdir(parents=True, exist_ok=True)

# Load pre-trained Keras model
print("[INFO] Loading pre-trained model...")
#gan_model = load_model('generator_model_015.h5')
gan_model = load_model('generator_model_final')

# Generate input for Generator
print("[INFO] Generating latent data...")
x_latent = generate_latent_data(LATENT_DIM, 25)

# Inference
print("[INFO] Creating and saving prediction...")
generated_image = gan_model.predict(x_latent)
save_fig_inference(generated_image, SAMPLES_PER_ROW)

gan_loader.cc

/*
The given SavedModel SignatureDef contains the following input(s):
  inputs['dense_1_input'] tensor_info:
      dtype: DT_FLOAT
      shape: (-1, 100)
      name: serving_default_dense_1_input:0
The given SavedModel SignatureDef contains the following output(s):
  outputs['conv2d_2'] tensor_info:
      dtype: DT_FLOAT
      shape: (-1, 28, 28, 1)
      name: StatefulPartitionedCall:0
Method name is: tensorflow/serving/predict
*/


#include <fstream>
#include <utility>
#include <vector>

#include "tensorflow/cc/ops/const_op.h"
#include "tensorflow/cc/ops/image_ops.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/graph/default_device.h"
#include "tensorflow/core/graph/graph_def_builder.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/core/threadpool.h"
#include "tensorflow/core/lib/io/path.h"
#include "tensorflow/core/lib/strings/str_util.h"
#include "tensorflow/core/lib/strings/stringprintf.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/init_main.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/types.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/util/command_line_flags.h"

// These are all common classes it's handy to reference with no namespace.
using tensorflow::Flag;
using tensorflow::int32;
using tensorflow::Status;
using tensorflow::string;
using tensorflow::Tensor;
using tensorflow::tstring;


Status CreateLatentSpace(const int latent_dim, const int num_samples) {
/*
TODO: Create random vector, equivalent to generate_latent_data in python file
*/
}


int main(int argc, char* argv[]) {
  // These are the command-line flags the program can understand.
  // They define where the graph and input data is located, and what kind of
  // input the model expects. If you train your own model, or use something
  // other than inception_v3, then you'll need to update these.

  string graph =
      "generator_model_final/saved_model.pb";

  int32 latent_dim = 100;
  int32 samples_per_row = 5;
  int32 num_samples = 25;

  string input_layer = "serving_default_dense_1_input";
  string output_layer = "StatefulPartitionedCall";
  string root_dir = "";

  std::vector<Flag> flag_list = {
      Flag("graph", &graph, "graph to be executed"),
      Flag("latent_dim", &latent_dim, "latent dimensions"),
      Flag("samples_per_row", &samples_per_row, "samples per row"),
      Flag("num_samples", &num_samples, "number of samples"),
      Flag("input_layer", &input_layer, "name of input layer"),
      Flag("output_layer", &output_layer, "name of output layer"),
      Flag("root_dir", &root_dir, "interpret image and graph file names relative to this directory"),
  };

  string usage = tensorflow::Flags::Usage(argv[0], flag_list);
  const bool parse_result = tensorflow::Flags::Parse(&argc, argv, flag_list);
  if (!parse_result) {
    LOG(ERROR) << usage;
    return -1;
  }

  // We need to call this to set up global state for TensorFlow.
  tensorflow::port::InitMain(argv[0], &argc, &argv);
  if (argc > 1) {
    LOG(ERROR) << "Unknown argument " << argv[1] << "\n" << usage;
    return -1;
  }

  // First we load and initialize the model.
  std::unique_ptr<tensorflow::Session> session;
  string graph_path = tensorflow::io::JoinPath(root_dir, graph);
  Status load_graph_status = LoadGraph(graph_path, &session);
  if (!load_graph_status.ok()) {
    LOG(ERROR) << load_graph_status;
    return -1;
  }

  // TODO: Call function to create latent space


  // TODO: Run the latent space through the model


  // TODO: Save the figure


  return 0;
}

I have already done the initialization of the variables and loading the graph. However I am still struggling to create the random vector and running it through the model, as well as to save the figure, in C++. Could you please give me some guide or advice? Do you have some example about how can be this done?

Thank you!

I think something like this could work:

Tensor CreateLatentSpace(const int latent_dim, const int num_samples) {
  Tensor tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({num_samples, latent_dim}));
  auto tensor_mapped = tensor.tensor<float, 2>(); 
  for (int idx = 0; idx < tensor.dim_size(0); ++idx) {
    for (int i = 0; i < tensor.dim_size(1); ++i) {
      tensor_mapped(idx, i) = drand48() - 0.5;
    }
  }
  return tensor;
}

a nuance is that drand48() samples from a uniform, rather than a normal distribution.

And then to actually run the model:

auto latent_space_tensor = CreateLatentSpace(100, 1);
std::vector<Tensor> outputs;
TF_CHECK_OK(session->Run({"dense_1_input", latent_space_tensor}, {"conv2d_2"}, {}, &outputs));

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