tensorflow_cpp 1.0.6
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1/*
2==============================================================================
3MIT License
4Copyright 2022 Institute for Automotive Engineering of RWTH Aachen University.
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19SOFTWARE.
20==============================================================================
21*/
22
28#pragma once
29
30#include <stdexcept>
31#include <string>
32
33#include <tensorflow/core/platform/env.h>
34#include <tensorflow/core/public/session.h>
35
36
37namespace tensorflow_cpp {
38
39
40namespace tf = tensorflow;
41
42
52inline tf::SessionOptions makeSessionOptions(
53 const bool allow_growth = true,
54 const double per_process_gpu_memory_fraction = 0,
55 const std::string& visible_device_list = "") {
56
57 tf::SessionOptions options = tf::SessionOptions();
58 tf::ConfigProto* config = &options.config;
59 tf::GPUOptions* gpu_options = config->mutable_gpu_options();
60 gpu_options->set_allow_growth(allow_growth);
61 gpu_options->set_per_process_gpu_memory_fraction(
62 per_process_gpu_memory_fraction);
63 gpu_options->set_visible_device_list(visible_device_list);
64
65 return options;
66}
67
68
78inline tf::Session* createSession(
79 const bool allow_growth = true,
80 const double per_process_gpu_memory_fraction = 0,
81 const std::string& visible_device_list = "") {
82
83 tf::Session* session;
84 tf::SessionOptions options = makeSessionOptions(
85 allow_growth, per_process_gpu_memory_fraction, visible_device_list);
86 tf::Status status = tf::NewSession(options, &session);
87 if (!status.ok())
88 throw std::runtime_error("Failed to create new session: " +
89 status.ToString());
90
91 return session;
92}
93
94
95} // namespace tensorflow_cpp
Namespace for tensorflow_cpp library.
Definition graph_utils.h:40
tf::SessionOptions makeSessionOptions(const bool allow_growth=true, const double per_process_gpu_memory_fraction=0, const std::string &visible_device_list="")
Helps to quickly create SessionOptions.
Definition utils.h:52
tf::Session * createSession(const bool allow_growth=true, const double per_process_gpu_memory_fraction=0, const std::string &visible_device_list="")
Creates a new TensorFlow session.
Definition utils.h:78