1 /* Copyright 2016 The TensorFlow Authors. 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/core/example/feature_util.h"
17
18 namespace tensorflow {
19
20 namespace internal {
ExampleFeature(const string & name,Example * example)21 Feature& ExampleFeature(const string& name, Example* example) {
22 return *GetFeature(name, example);
23 }
24
25 } // namespace internal
26
27 template <>
HasFeature(const string & key,const Features & features)28 bool HasFeature<>(const string& key, const Features& features) {
29 return (features.feature().find(key) != features.feature().end());
30 }
31
32 template <>
HasFeature(const string & key,const Features & features)33 bool HasFeature<protobuf_int64>(const string& key, const Features& features) {
34 auto it = features.feature().find(key);
35 return (it != features.feature().end()) &&
36 (it->second.kind_case() == Feature::KindCase::kInt64List);
37 }
38
39 template <>
HasFeature(const string & key,const Features & features)40 bool HasFeature<float>(const string& key, const Features& features) {
41 auto it = features.feature().find(key);
42 return (it != features.feature().end()) &&
43 (it->second.kind_case() == Feature::KindCase::kFloatList);
44 }
45
46 template <>
HasFeature(const string & key,const Features & features)47 bool HasFeature<string>(const string& key, const Features& features) {
48 auto it = features.feature().find(key);
49 return (it != features.feature().end()) &&
50 (it->second.kind_case() == Feature::KindCase::kBytesList);
51 }
52
HasFeatureList(const string & key,const SequenceExample & sequence_example)53 bool HasFeatureList(const string& key,
54 const SequenceExample& sequence_example) {
55 auto& feature_list = sequence_example.feature_lists().feature_list();
56 return (feature_list.find(key) != feature_list.end());
57 }
58
59 template <>
GetFeatureValues(const Feature & feature)60 const protobuf::RepeatedField<protobuf_int64>& GetFeatureValues<protobuf_int64>(
61 const Feature& feature) {
62 return feature.int64_list().value();
63 }
64
65 template <>
GetFeatureValues(Feature * feature)66 protobuf::RepeatedField<protobuf_int64>* GetFeatureValues<protobuf_int64>(
67 Feature* feature) {
68 return feature->mutable_int64_list()->mutable_value();
69 }
70
71 template <>
GetFeatureValues(const Feature & feature)72 const protobuf::RepeatedField<float>& GetFeatureValues<float>(
73 const Feature& feature) {
74 return feature.float_list().value();
75 }
76
77 template <>
GetFeatureValues(Feature * feature)78 protobuf::RepeatedField<float>* GetFeatureValues<float>(Feature* feature) {
79 return feature->mutable_float_list()->mutable_value();
80 }
81
82 template <>
GetFeatureValues(const Feature & feature)83 const protobuf::RepeatedPtrField<string>& GetFeatureValues<string>(
84 const Feature& feature) {
85 return feature.bytes_list().value();
86 }
87
88 template <>
GetFeatureValues(Feature * feature)89 protobuf::RepeatedPtrField<string>* GetFeatureValues<string>(Feature* feature) {
90 return feature->mutable_bytes_list()->mutable_value();
91 }
92
GetFeatureList(const string & key,const SequenceExample & sequence_example)93 const protobuf::RepeatedPtrField<Feature>& GetFeatureList(
94 const string& key, const SequenceExample& sequence_example) {
95 return sequence_example.feature_lists().feature_list().at(key).feature();
96 }
97
GetFeatureList(const string & feature_list_key,SequenceExample * sequence_example)98 protobuf::RepeatedPtrField<Feature>* GetFeatureList(
99 const string& feature_list_key, SequenceExample* sequence_example) {
100 return (*sequence_example->mutable_feature_lists()
101 ->mutable_feature_list())[feature_list_key]
102 .mutable_feature();
103 }
104
105 template <>
GetFeatures(Features * proto)106 Features* GetFeatures<Features>(Features* proto) {
107 return proto;
108 }
109
110 template <>
GetFeatures(Example * proto)111 Features* GetFeatures<Example>(Example* proto) {
112 return proto->mutable_features();
113 }
114
115 template <>
GetFeatures(const Features & proto)116 const Features& GetFeatures<Features>(const Features& proto) {
117 return proto;
118 }
119
120 template <>
GetFeatures(const Example & proto)121 const Features& GetFeatures<Example>(const Example& proto) {
122 return proto.features();
123 }
124
125 template <>
126 const protobuf::RepeatedField<protobuf_int64>& GetFeatureValues<protobuf_int64>(
127 const Feature& feature);
128
129 template <>
130 protobuf::RepeatedField<protobuf_int64>* GetFeatureValues<protobuf_int64>(
131 Feature* feature);
132
133 template <>
134 const protobuf::RepeatedField<float>& GetFeatureValues<float>(
135 const Feature& feature);
136
137 template <>
138 protobuf::RepeatedField<float>* GetFeatureValues<float>(Feature* feature);
139
140 template <>
141 const protobuf::RepeatedPtrField<string>& GetFeatureValues<string>(
142 const Feature& feature);
143
144 template <>
145 protobuf::RepeatedPtrField<string>* GetFeatureValues<string>(Feature* feature);
146 } // namespace tensorflow
147