alanlxl | 30f15bd | 2020-08-11 21:26:12 +1000 | [diff] [blame^] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "federated/utils.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | namespace federated { |
| 11 | |
| 12 | namespace { |
| 13 | using chromeos::federated::mojom::ExamplePtr; |
| 14 | using chromeos::federated::mojom::FloatList; |
| 15 | using chromeos::federated::mojom::Int64List; |
| 16 | using chromeos::federated::mojom::ValueList; |
| 17 | } // namespace |
| 18 | |
| 19 | tensorflow::Example ConvertToTensorFlowExampleProto(const ExamplePtr& example) { |
| 20 | tensorflow::Example tf_example; |
| 21 | auto& feature = *tf_example.mutable_features()->mutable_feature(); |
| 22 | |
| 23 | for (const auto& iter : example->features->feature) { |
| 24 | if (iter.second->which() == ValueList::Tag::INT64_LIST) { |
| 25 | std::vector<int64_t>& value_list = iter.second->get_int64_list()->value; |
| 26 | *feature[iter.first].mutable_int64_list()->mutable_value() = { |
| 27 | value_list.begin(), value_list.end()}; |
| 28 | } else if (iter.second->which() == ValueList::Tag::FLOAT_LIST) { |
| 29 | std::vector<double>& value_list = iter.second->get_float_list()->value; |
| 30 | *feature[iter.first].mutable_float_list()->mutable_value() = { |
| 31 | value_list.begin(), value_list.end()}; |
| 32 | } else if (iter.second->which() == ValueList::Tag::STRING_LIST) { |
| 33 | std::vector<std::string>& value_list = |
| 34 | iter.second->get_string_list()->value; |
| 35 | *feature[iter.first].mutable_bytes_list()->mutable_value() = { |
| 36 | value_list.begin(), value_list.end()}; |
| 37 | } |
| 38 | } |
| 39 | return tf_example; |
| 40 | } |
| 41 | |
| 42 | } // namespace federated |