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
53 template <>
HasFeature(const string & key,const Features & features)54 bool HasFeature<tstring>(const string& key, const Features& features) {
55 auto it = features.feature().find(key);
56 return (it != features.feature().end()) &&
57 (it->second.kind_case() == Feature::KindCase::kBytesList);
58 }
59
HasFeatureList(const string & key,const SequenceExample & sequence_example)60 bool HasFeatureList(const string& key,
61 const SequenceExample& sequence_example) {
62 auto& feature_list = sequence_example.feature_lists().feature_list();
63 return (feature_list.find(key) != feature_list.end());
64 }
65
66 template <>
GetFeatureValues(const Feature & feature)67 const protobuf::RepeatedField<protobuf_int64>& GetFeatureValues<protobuf_int64>(
68 const Feature& feature) {
69 return feature.int64_list().value();
70 }
71
72 template <>
GetFeatureValues(Feature * feature)73 protobuf::RepeatedField<protobuf_int64>* GetFeatureValues<protobuf_int64>(
74 Feature* feature) {
75 return feature->mutable_int64_list()->mutable_value();
76 }
77
78 template <>
GetFeatureValues(const Feature & feature)79 const protobuf::RepeatedField<float>& GetFeatureValues<float>(
80 const Feature& feature) {
81 return feature.float_list().value();
82 }
83
84 template <>
GetFeatureValues(Feature * feature)85 protobuf::RepeatedField<float>* GetFeatureValues<float>(Feature* feature) {
86 return feature->mutable_float_list()->mutable_value();
87 }
88
89 template <>
GetFeatureValues(const Feature & feature)90 const protobuf::RepeatedPtrField<string>& GetFeatureValues<tstring>(
91 const Feature& feature) {
92 return feature.bytes_list().value();
93 }
94
95 template <>
GetFeatureValues(const Feature & feature)96 const protobuf::RepeatedPtrField<string>& GetFeatureValues<string>(
97 const Feature& feature) {
98 return feature.bytes_list().value();
99 }
100
101 template <>
GetFeatureValues(Feature * feature)102 protobuf::RepeatedPtrField<string>* GetFeatureValues<tstring>(
103 Feature* feature) {
104 return feature->mutable_bytes_list()->mutable_value();
105 }
106
107 template <>
GetFeatureValues(Feature * feature)108 protobuf::RepeatedPtrField<string>* GetFeatureValues<string>(Feature* feature) {
109 return feature->mutable_bytes_list()->mutable_value();
110 }
111
GetFeatureList(const string & key,const SequenceExample & sequence_example)112 const protobuf::RepeatedPtrField<Feature>& GetFeatureList(
113 const string& key, const SequenceExample& sequence_example) {
114 return sequence_example.feature_lists().feature_list().at(key).feature();
115 }
116
GetFeatureList(const string & feature_list_key,SequenceExample * sequence_example)117 protobuf::RepeatedPtrField<Feature>* GetFeatureList(
118 const string& feature_list_key, SequenceExample* sequence_example) {
119 return (*sequence_example->mutable_feature_lists()
120 ->mutable_feature_list())[feature_list_key]
121 .mutable_feature();
122 }
123
124 template <>
ClearFeatureValues(Feature * feature)125 void ClearFeatureValues<protobuf_int64>(Feature* feature) {
126 feature->mutable_int64_list()->Clear();
127 }
128
129 template <>
ClearFeatureValues(Feature * feature)130 void ClearFeatureValues<float>(Feature* feature) {
131 feature->mutable_float_list()->Clear();
132 }
133
134 template <>
ClearFeatureValues(Feature * feature)135 void ClearFeatureValues<string>(Feature* feature) {
136 feature->mutable_bytes_list()->Clear();
137 }
138
139 template <>
ClearFeatureValues(Feature * feature)140 void ClearFeatureValues<tstring>(Feature* feature) {
141 feature->mutable_bytes_list()->Clear();
142 }
143
144 template <>
GetFeatures(Features * proto)145 Features* GetFeatures<Features>(Features* proto) {
146 return proto;
147 }
148
149 template <>
GetFeatures(Example * proto)150 Features* GetFeatures<Example>(Example* proto) {
151 return proto->mutable_features();
152 }
153
154 template <>
GetFeatures(const Features & proto)155 const Features& GetFeatures<Features>(const Features& proto) {
156 return proto;
157 }
158
159 template <>
GetFeatures(const Example & proto)160 const Features& GetFeatures<Example>(const Example& proto) {
161 return proto.features();
162 }
163
164 template <>
165 const protobuf::RepeatedField<protobuf_int64>& GetFeatureValues<protobuf_int64>(
166 const Feature& feature);
167
168 template <>
169 protobuf::RepeatedField<protobuf_int64>* GetFeatureValues<protobuf_int64>(
170 Feature* feature);
171
172 template <>
173 const protobuf::RepeatedField<float>& GetFeatureValues<float>(
174 const Feature& feature);
175
176 template <>
177 protobuf::RepeatedField<float>* GetFeatureValues<float>(Feature* feature);
178
179 template <>
180 const protobuf::RepeatedPtrField<string>& GetFeatureValues<string>(
181 const Feature& feature);
182
183 template <>
184 const protobuf::RepeatedPtrField<string>& GetFeatureValues<tstring>(
185 const Feature& feature);
186
187 template <>
188 protobuf::RepeatedPtrField<string>* GetFeatureValues<string>(Feature* feature);
189
190 template <>
191 protobuf::RepeatedPtrField<string>* GetFeatureValues<tstring>(Feature* feature);
192
193 } // namespace tensorflow
194