Skip to content

Commit 269f0c6

Browse files
committed
Added comments, scripts and updated example to show how to pass config options to TF.
1 parent 9264575 commit 269f0c6

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import tensorflow as tf
2+
3+
def create_serialized_options(fraction, growth):
4+
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=fraction, allow_growth=growth)
5+
config = tf.ConfigProto(gpu_options=gpu_options)
6+
serialized = config.SerializeToString()
7+
return list(map(hex, serialized))
8+
9+
if __name__ == "__main__":
10+
print("Create serialized options which allow TF to use a certain percentage of GPU memory and allow TF to expand this memory if required.")
11+
for i in range(1, 10):
12+
memory_fraction_to_use = 0.1
13+
enable_memory_growth = True
14+
print("GPU memory to be used: ", i * 10.0, "%")
15+
print(create_serialized_options(memory_fraction_to_use, enable_memory_growth))

examples/load_model/main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
#include <iomanip>
1010

1111
int main() {
12+
// Load model with a path to the .pb file.
13+
// An optional std::vector<uint8_t> parameter can be used to supply Tensorflow with
14+
// session options. The vector must represent a serialized ConfigProto which can be
15+
// generated manually in python. See create_config_options.py.
16+
// Example:
17+
// const std::vector<uint8_t> ModelConfigOptions = { 0x32, 0xb, 0x9, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xb9, 0x3f, 0x20, 0x1 };
18+
// Model model("../model.pb", ModelConfigOptions);
1219
Model model("../model.pb");
1320
model.init();
1421

include/Model.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Tensor;
1919

2020
class Model {
2121
public:
22+
// Pass a path to the model file and optional Tensorflow config options. See examples/load_model/main.cpp.
2223
explicit Model(const std::string& model_filename, const std::vector<uint8_t>& config_options = {});
2324

2425
// Rule of five, moving is easy as the pointers can be copied, copying not as i have no idea how to copy

0 commit comments

Comments
 (0)