TensorFlow Serving C++ API Documentation
classification_service.cc
1 /* Copyright 2017 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow_serving/servables/tensorflow/classification_service.h"
17 
18 #include <memory>
19 
20 #include "tensorflow/core/lib/core/errors.h"
21 #include "tensorflow/core/platform/tracing.h"
22 #include "tensorflow_serving/apis/classifier.h"
23 #include "tensorflow_serving/core/servable_handle.h"
24 #include "tensorflow_serving/servables/tensorflow/classifier.h"
25 #include "tensorflow_serving/servables/tensorflow/util.h"
26 
27 namespace tensorflow {
28 namespace serving {
29 
30 Status TensorflowClassificationServiceImpl::Classify(
31  const RunOptions& run_options, ServerCore* core,
32  const thread::ThreadPoolOptions& thread_pool_options,
33  const ClassificationRequest& request, ClassificationResponse* response) {
34  // Verify Request Metadata and create a ServableRequest
35  if (!request.has_model_spec()) {
36  return tensorflow::Status(
37  static_cast<absl::StatusCode>(absl::StatusCode::kInvalidArgument),
38  "Missing ModelSpec");
39  }
40 
41  return ClassifyWithModelSpec(run_options, core, thread_pool_options,
42  request.model_spec(), request, response);
43 }
44 
45 Status TensorflowClassificationServiceImpl::ClassifyWithModelSpec(
46  const RunOptions& run_options, ServerCore* core,
47  const thread::ThreadPoolOptions& thread_pool_options,
48  const ModelSpec& model_spec, const ClassificationRequest& request,
49  ClassificationResponse* response) {
50  TRACELITERAL("TensorflowClassificationServiceImpl::ClassifyWithModelSpec");
51 
52  ServableHandle<SavedModelBundle> saved_model_bundle;
53  TF_RETURN_IF_ERROR(core->GetServableHandle(model_spec, &saved_model_bundle));
54  return RunClassify(run_options, saved_model_bundle->meta_graph_def,
55  saved_model_bundle.id().version,
56  saved_model_bundle->session.get(), request, response,
57  thread_pool_options);
58 }
59 
60 } // namespace serving
61 } // namespace tensorflow