/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #pragma once #include #include #include #include "common/log.h" #include "framework/ddim.h" #include "framework/tensor.h" static const std::string g_mobilenet_ssd = "../models/mobilenet+ssd"; static const std::string g_squeezenet = "../models/squeezenet"; static const std::string g_googlenet = "../models/googlenet"; static const std::string g_mobilenet = "../models/mobilenet"; static const std::string g_resnet_50 = "../models/resnet_50"; static const std::string g_resnet = "../models/resnet"; static const std::string g_googlenet_combine = "../models/googlenet_combine"; static const std::string g_yolo = "../models/yolo"; static const std::string g_test_image_1x3x224x224 = "../images/test_image_1x3x224x224_float"; using paddle_mobile::framework::DDim; using paddle_mobile::framework::Tensor; using Time = decltype(std::chrono::high_resolution_clock::now()); Time time() { return std::chrono::high_resolution_clock::now(); } double time_diff(Time t1, Time t2) { typedef std::chrono::microseconds ms; auto diff = t2 - t1; ms counter = std::chrono::duration_cast(diff); return counter.count() / 1000.0; } template void SetupTensor(paddle_mobile::framework::Tensor *input, paddle_mobile::framework::DDim dims, T lower, T upper) { static unsigned int seed = 100; std::mt19937 rng(seed++); std::uniform_real_distribution uniform_dist(0, 1); T *input_ptr = input->mutable_data(dims); for (int i = 0; i < input->numel(); ++i) { input_ptr[i] = static_cast(uniform_dist(rng) * (upper - lower) + lower); } } template T *CreateInput(Tensor *input, DDim dims, T low, T up) { SetupTensor(input, dims, static_cast(low), static_cast(up)); return input->data(); } template void GetInput(const std::string &input_name, std::vector *input, const std::vector &dims) { int size = 1; for (const auto &dim : dims) { size *= dim; } T *input_ptr = (T *)malloc(sizeof(T) * size); std::ifstream in(input_name, std::ios::in | std::ios::binary); in.read((char *)(input_ptr), size * sizeof(T)); in.close(); for (int i = 0; i < size; ++i) { input->push_back(input_ptr[i]); } free(input_ptr); } template void GetInput(const std::string &input_name, paddle_mobile::framework::Tensor *input, paddle_mobile::framework::DDim dims) { T *input_ptr = input->mutable_data(dims); std::ifstream in(input_name, std::ios::in | std::ios::binary); in.read((char *)(input_ptr), input->numel() * sizeof(T)); in.close(); }