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