PatWie / tensorflow-cmake

TensorFlow examples in C, C++, Go and Python without bazel but with cmake and FindTensorFlow.cmake
Apache License 2.0
439 stars 91 forks source link

Understanding C Inference code #40

Closed iamsurya closed 5 years ago

iamsurya commented 5 years ago

This isn't a bug, but I'd like some help understanding this particular section of the code in inference/c/inference_c.c (lines 61 - 78). It looks like a string to the checkpoint file is setup but nothing is done with it?

  char* checkpoint_path_str = "./exported/my_model";
  size_t checkpoint_path_str_len = strlen(checkpoint_path_str);
  size_t encoded_size = TF_StringEncodedSize(checkpoint_path_str_len);

  // The format for TF_STRING tensors is:
  //   start_offset: array[uint64]
  //   data:         byte[...]
  size_t total_size = sizeof(int64_t) + encoded_size;
  char* input_encoded = (char*)malloc(total_size);
  memset(input_encoded, 0, total_size);
  TF_StringEncode(checkpoint_path_str, checkpoint_path_str_len,
                  input_encoded + sizeof(int64_t), encoded_size, status);
  if (TF_GetCode(status) != TF_OK) {
    fprintf(stderr, "ERROR: something wrong with encoding: %s",
            TF_Message(status));
    return 1;
   }
PatWie commented 5 years ago

The checkpoint_path_str as a C-string is converted to a TF-String input_encoded and then used to load a checkpoint file. TensorFlow does not understand a plain C-String (as far as I know).

iamsurya commented 5 years ago

Thanks that makes sense. However, assuming the graph is frozen using Tensorflow python, what use is a checkpoint? Shouldn't the frozen graph already contain the latest weights?

I'd also like to thank you for this project. I was able to use a variation of your C inference code with libtensorflow on Windows / Visual Studio to make an inference. I see some users are having issues trying to run on Windows, I might be able to try and help them a little bit.

PatWie commented 5 years ago

Shouldn't the frozen graph already contain the latest weights?

The graph isn't a frozen graph:

https://github.com/PatWie/tensorflow-cmake/blob/dc0b4eeee274849abc1effdb29df6b3cd5f39d06/inference/example.py#L16-L17

I see some users are having issues trying to run on Windows, I might be able to try and help them a little bit.

That would be indeed nice. I would like to adjust the CMakeLists.txt to support windows if possible.