test_helper.h 5.6 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

L
liuruilong 已提交
17
#include <fstream>
L
liuruilong 已提交
18
#include <random>
19 20
#include <string>
#include <vector>
L
liuruilong 已提交
21

W
wangliu 已提交
22
#include "common/common.h"
23
#include "common/log.h"
朔-望's avatar
朔-望 已提交
24
#include "framework/ddim.h"
H
hjchen2 已提交
25
#include "framework/lod_tensor.h"
朔-望's avatar
朔-望 已提交
26

L
liuruilong 已提交
27
static const char *g_ocr = "../models/ocr";
28
static const char *g_mobilenet_ssd = "../models/mobilenet+ssd";
29
static const char *g_genet_combine = "../models/enet";
E
debug  
eclipsess 已提交
30
static const char *g_eng = "../models/eng_20conv_1_9_fc";
31
static const char *g_mobilenet_ssd_gesture = "../models/mobilenet+ssd_gesture";
32
static const char *g_mobilenet_combined = "../models/mobilenet_combine";
33
static const char *g_googlenetv1_combined = "../models/googlenetv1_combine";
L
liuruilong 已提交
34
static const char *g_mobilenet_detect = "../models/mobilenet-detect";
35 36
static const char *g_squeezenet = "../models/squeezenet";
static const char *g_googlenet = "../models/googlenet";
37
static const char *g_googlenet_quali = "../models/googlenet_combine_quali";
38
static const char *g_mobilenet = "../models/mobilenet";
39
static const char *g_mobilenet_mul = "../models/r";
Y
yangfei 已提交
40 41
static const char *g_alexnet = "../models/alexnet";
static const char *g_inceptionv4 = "../models/inceptionv4";
42 43
static const char *g_inceptionv3 =
    "../models/InceptionV3_Spatial_Attention_Model";
xiebaiyuan's avatar
xiebaiyuan 已提交
44
static const char *g_nlp = "../models/nlp";
45
static const char *g_super = "../models/superresoltion";
46 47 48 49
static const char *g_resnet_50 = "../models/resnet_50";
static const char *g_resnet = "../models/resnet";
static const char *g_googlenet_combine = "../models/googlenet_combine";
static const char *g_yolo = "../models/yolo";
xiebaiyuan's avatar
xiebaiyuan 已提交
50
static const char *g_yolo_combined = "../models/yolo_combined";
51
static const char *g_yolo_mul = "../models/d";
52
static const char *g_fluid_fssd_new = "../models/fluid_fssd_new";
Z
zhaojiaying01 已提交
53
static const char *g_vgg16_ssd_combined = "../models/vgg16_ssd_combined";
L
liuruilong 已提交
54 55
static const char *g_mobilenet_vision = "../models/vision_mobilenet";
static const char *g_yolo_vision = "../models/vision_yolo";
56
static const char *g_test_image_1x3x224x224 =
L
liuruilong 已提交
57
    "../images/test_image_1x3x224x224_float";
58 59
static const char *g_test_image_1x3x224x224_banana =
    "../images/input_3x224x224_banana";
xiebaiyuan's avatar
xiebaiyuan 已提交
60 61
static const char *g_test_image_desktop_1_3_416_416_nchw_float =
    "../images/in_put_1_3_416_416_2";
62
static const char *g_hand = "../images/hand_image";
E
debug  
eclipsess 已提交
63
static const char *g_moto = "../images/moto_300x300_float";
xiebaiyuan's avatar
xiebaiyuan 已提交
64 65
static const char *g_imgfssd_ar = "../images/test_image_ssd_ar";
static const char *g_imgfssd_ar1 = "../images/003_0001.txt";
66
static const char *g_img = "../images/img.bin";
Y
yangfei 已提交
67
static const char *g_yolo_img = "../images/in_put_1_3_416_416_2";
L
liuruilong 已提交
68
static const char *g_super_img = "../images/mingren_input_data";
Y
yangfei 已提交
69
static const char *g_mobilenet_img = "../images/image";
L
liuruilong 已提交
70 71 72 73
static const char *g_test_image_1x3x224x224_vision_mobilenet_input =
    "../images/vision_mobilenet_input";
static const char *g_test_image_1x3x416x416_vision_yolo_input =
    "../images/yolo_input";
74

H
hjchen2 已提交
75
using namespace paddle_mobile;  // NOLINT
E
eclipsess 已提交
76
using paddle_mobile::framework::DDim;
H
hjchen2 已提交
77
using paddle_mobile::framework::LoDTensor;
E
eclipsess 已提交
78
using paddle_mobile::framework::Tensor;
W
wangliu 已提交
79

朔-望's avatar
朔-望 已提交
80
template <typename T>
E
eclipsess 已提交
81
void SetupTensor(paddle_mobile::framework::Tensor *input,
朔-望's avatar
朔-望 已提交
82
                 paddle_mobile::framework::DDim dims, T lower, T upper) {
83 84 85
  static unsigned int seed = 100;
  std::mt19937 rng(seed++);
  std::uniform_real_distribution<double> uniform_dist(0, 1);
朔-望's avatar
朔-望 已提交
86

87 88 89 90
  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
朔-望 已提交
91
}
L
liuruilong 已提交
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
template <>
void SetupTensor<bool>(paddle_mobile::framework::Tensor *input,
                       paddle_mobile::framework::DDim dims, bool lower,
                       bool upper) {
  static unsigned int seed = 100;
  std::mt19937 rng(seed++);
  std::uniform_real_distribution<double> uniform_dist(0, 1);

  bool *input_ptr = input->mutable_data<bool>(dims);
  if (lower == upper) {
    for (int i = 0; i < input->numel(); ++i) {
      input_ptr[i] = lower;
    }
  } else {
    for (int i = 0; i < input->numel(); ++i) {
      input_ptr[i] = uniform_dist(rng) > 0.5;
    }
  }
}

E
eclipsess 已提交
113 114 115 116 117 118
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 已提交
119
template <typename T>
L
liuruilong 已提交
120 121
void GetInput(const std::string &input_name, std::vector<T> *input,
              const std::vector<int64_t> &dims) {
L
liuruilong 已提交
122 123 124 125 126
  int size = 1;
  for (const auto &dim : dims) {
    size *= dim;
  }

127
  T *input_ptr = reinterpret_cast<T *>(malloc(sizeof(T) * size));
L
liuruilong 已提交
128
  std::ifstream in(input_name, std::ios::in | std::ios::binary);
129
  in.read(reinterpret_cast<char *>(input_ptr), size * sizeof(T));
L
liuruilong 已提交
130 131 132 133 134 135
  in.close();
  for (int i = 0; i < size; ++i) {
    input->push_back(input_ptr[i]);
  }
  free(input_ptr);
}
L
liuruilong 已提交
136 137

template <typename T>
L
liuruilong 已提交
138 139
void GetInput(const std::string &input_name,
              paddle_mobile::framework::Tensor *input,
L
liuruilong 已提交
140 141 142 143
              paddle_mobile::framework::DDim dims) {
  T *input_ptr = input->mutable_data<T>(dims);

  std::ifstream in(input_name, std::ios::in | std::ios::binary);
144
  in.read(reinterpret_cast<char *>(input_ptr), input->numel() * sizeof(T));
L
liuruilong 已提交
145 146
  in.close();
}