test_helper.h 3.1 KB
Newer Older
E
eclipsess 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

E
eclipsess 已提交
15
#pragma once
L
liuruilong 已提交
16

W
wangliu 已提交
17
#include <chrono>
L
liuruilong 已提交
18
#include <fstream>
L
liuruilong 已提交
19
#include <random>
L
liuruilong 已提交
20

21
#include "common/log.h"
朔-望's avatar
朔-望 已提交
22 23 24
#include "framework/ddim.h"
#include "framework/tensor.h"

E
eclipsess 已提交
25
static const std::string g_mobilenet_ssd = "../models/mobilenet+ssd";
L
liuruilong 已提交
26
static const std::string g_squeezenet = "../models/squeezenet";
L
liuruilong 已提交
27 28 29
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";
L
liuruilong 已提交
30
static const std::string g_resnet = "../models/resnet";
E
eclipsess 已提交
31
static const std::string g_yolo = "../models/yolo";
L
liuruilong 已提交
32 33
static const std::string g_test_image_1x3x224x224 =
    "../images/test_image_1x3x224x224_float";
E
eclipsess 已提交
34
using paddle_mobile::framework::DDim;
E
eclipsess 已提交
35
using paddle_mobile::framework::Tensor;
W
wangliu 已提交
36 37 38 39 40 41 42 43 44 45 46 47

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<ms>(diff);
  return counter.count() / 1000.0;
}

朔-望's avatar
朔-望 已提交
48
template <typename T>
E
eclipsess 已提交
49
void SetupTensor(paddle_mobile::framework::Tensor *input,
朔-望's avatar
朔-望 已提交
50
                 paddle_mobile::framework::DDim dims, T lower, T upper) {
51 52 53
  static unsigned int seed = 100;
  std::mt19937 rng(seed++);
  std::uniform_real_distribution<double> uniform_dist(0, 1);
朔-望's avatar
朔-望 已提交
54

55 56 57 58
  T *input_ptr = input->mutable_data<T>(dims);
  for (int i = 0; i < input->numel(); ++i) {
    input_ptr[i] = static_cast<T>(uniform_dist(rng) * (upper - lower) + lower);
  }
朔-望's avatar
朔-望 已提交
59
}
L
liuruilong 已提交
60

E
eclipsess 已提交
61 62 63 64 65 66
template <typename T>
T *CreateInput(Tensor *input, DDim dims, T low, T up) {
  SetupTensor<T>(input, dims, static_cast<float>(low), static_cast<float>(up));
  return input->data<T>();
}

L
liuruilong 已提交
67
template <typename T>
L
liuruilong 已提交
68 69
void GetInput(const std::string &input_name, std::vector<T> *input,
              const std::vector<int64_t> &dims) {
L
liuruilong 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83
  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);
}
L
liuruilong 已提交
84 85

template <typename T>
L
liuruilong 已提交
86 87
void GetInput(const std::string &input_name,
              paddle_mobile::framework::Tensor *input,
L
liuruilong 已提交
88 89 90 91 92 93 94
              paddle_mobile::framework::DDim dims) {
  T *input_ptr = input->mutable_data<T>(dims);

  std::ifstream in(input_name, std::ios::in | std::ios::binary);
  in.read((char *)(input_ptr), input->numel() * sizeof(T));
  in.close();
}