test_perturbation.cpp 11.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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. */

#ifndef PADDLE_ONLY_CPU

Y
Yu Yang 已提交
17
#include <cuda_runtime.h>
Z
zhangjinchao01 已提交
18
#include <gtest/gtest.h>
Y
Yu Yang 已提交
19
#include <cmath>
Z
zhangjinchao01 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <vector>
#include "hl_cuda.h"
#include "hl_perturbation_util.cuh"

using namespace std;  // NOLINT

#define _USE_MATH_DEFINES

const int NUM_IMAGES = 2;
const int SAMPLING_RATE = 2;
const int IMG_SIZE = 41;
const int TGT_SIZE = 21;
const int CHANNELS = 3;

class PerturbationTest : public testing::Test {
protected:
  virtual void SetUp() { generateTestImages(gpuImages_); }

  virtual void TearDown() {}

40 41 42
  void allocateMem(real*& gpuAngle,
                   real*& gpuScale,
                   int*& gpuCenterR,
Z
zhangjinchao01 已提交
43 44 45 46 47 48 49 50 51 52
                   int*& gpuCenterC) {
    gpuAngle = (real*)hl_malloc_device(sizeof(real) * NUM_IMAGES);
    gpuScale = (real*)hl_malloc_device(sizeof(real) * NUM_IMAGES);
    gpuCenterR =
        (int*)hl_malloc_device(sizeof(int) * NUM_IMAGES * SAMPLING_RATE);
    gpuCenterC =
        (int*)hl_malloc_device(sizeof(int) * NUM_IMAGES * SAMPLING_RATE);
  }

  // Generate translation parameters for testing.
53 54
  void generateTranslationParams(int*& gpuCenterR,
                                 int*& gpuCenterC,
Z
zhangjinchao01 已提交
55 56 57 58 59 60 61 62 63 64
                                 int imgSize) {
    int cpuCenterR[NUM_IMAGES * SAMPLING_RATE];
    int cpuCenterC[NUM_IMAGES * SAMPLING_RATE];
    for (int i = 0; i < NUM_IMAGES * SAMPLING_RATE; ++i) {
      cpuCenterR[i] = (imgSize - 1) / 2;
      cpuCenterC[i] = (imgSize - 1) / 2 - 1;
    }

    gpuCenterR =
        (int*)hl_malloc_device(sizeof(int) * NUM_IMAGES * SAMPLING_RATE);
65 66
    hl_memcpy_host2device(
        gpuCenterR, cpuCenterR, sizeof(int) * NUM_IMAGES * SAMPLING_RATE);
Z
zhangjinchao01 已提交
67 68 69

    gpuCenterC =
        (int*)hl_malloc_device(sizeof(int) * NUM_IMAGES * SAMPLING_RATE);
70 71
    hl_memcpy_host2device(
        gpuCenterC, cpuCenterC, sizeof(int) * NUM_IMAGES * SAMPLING_RATE);
Z
zhangjinchao01 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  }

  // Generate rotation parameters for testing.
  void generateRotationParams(real*& gpuAngle) {
    real cpuAngle[NUM_IMAGES];
    for (int i = 0; i < NUM_IMAGES; ++i) {
      cpuAngle[i] = 90.0 * M_PI / 180.0;
    }
    gpuAngle = (real*)hl_malloc_device(sizeof(real) * NUM_IMAGES);
    hl_memcpy_host2device(gpuAngle, cpuAngle, sizeof(real) * NUM_IMAGES);
  }

  void generateScaleParams(real*& gpuScale) {
    real cpuScale[NUM_IMAGES];
    for (int i = 0; i < NUM_IMAGES; ++i) {
      cpuScale[i] = static_cast<real>(TGT_SIZE - 2) / TGT_SIZE;
    }
    gpuScale = (real*)hl_malloc_device(sizeof(real) * NUM_IMAGES);
90
    hl_memcpy_host2device(gpuScale, cpuScale, sizeof(real) * NUM_IMAGES);
Z
zhangjinchao01 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  }

  // Generate the test images, only the center regions are set to 1.
  // The other parts are set to 0.
  void generateTestImages(real*& gpuImages) {
    const int IMAGE_MEM_SIZE = NUM_IMAGES * IMG_SIZE * IMG_SIZE * CHANNELS;
    real cpuImages[IMAGE_MEM_SIZE];
    // Set the middle of each image to 1.
    real* ptr = cpuImages;
    for (int i = 0; i < NUM_IMAGES; ++i) {
      for (int r = 0; r < IMG_SIZE; ++r) {
        for (int c = 0; c < IMG_SIZE; ++c) {
          for (int ch = 0; ch < CHANNELS; ++ch) {
            if (r >= IMG_SIZE / 4 && r < IMG_SIZE - IMG_SIZE / 4 &&
                c >= IMG_SIZE / 4 && c < IMG_SIZE - IMG_SIZE / 4) {
              *ptr = 1.0;
            } else {
              *ptr = 0.0;
            }
            ++ptr;
          }
        }
      }
    }
    gpuImages = (real*)hl_malloc_device(sizeof(real) * IMAGE_MEM_SIZE);
116
    hl_memcpy_host2device(gpuImages, cpuImages, sizeof(real) * IMAGE_MEM_SIZE);
Z
zhangjinchao01 已提交
117 118 119 120 121 122 123
  }

  real* gpuImages_;
};

// Random perturbation. Only to make sure the code does not break.
TEST_F(PerturbationTest, random_perturb) {
124 125
  real *gpuAngle, *gpuScaleRatio;
  int *gpuCenterR, *gpuCenterC;
Z
zhangjinchao01 已提交
126 127 128 129 130 131
  allocateMem(gpuAngle, gpuScaleRatio, gpuCenterR, gpuCenterC);

  real* targets = NULL;
  const int TARGET_MEM_SIZE =
      NUM_IMAGES * SAMPLING_RATE * TGT_SIZE * TGT_SIZE * CHANNELS;
  targets = (real*)hl_malloc_device(sizeof(real) * TARGET_MEM_SIZE);
132 133 134 135 136 137 138 139 140 141 142 143 144 145
  hl_conv_random_disturb(gpuImages_,
                         IMG_SIZE,
                         TGT_SIZE,
                         CHANNELS,
                         NUM_IMAGES,
                         1.0,
                         1.0,
                         SAMPLING_RATE,
                         gpuAngle,
                         gpuScaleRatio,
                         gpuCenterR,
                         gpuCenterC,
                         2,
                         true,
Z
zhangjinchao01 已提交
146 147
                         targets);
  real cpuTargets[TARGET_MEM_SIZE];
148
  hl_memcpy_device2host(cpuTargets, targets, sizeof(real) * TARGET_MEM_SIZE);
Z
zhangjinchao01 已提交
149 150 151
}

TEST_F(PerturbationTest, identity_perturb) {
152 153
  real *gpuAngle, *gpuScaleRatio;
  int *gpuCenterR, *gpuCenterC;
Z
zhangjinchao01 已提交
154 155 156 157 158 159
  allocateMem(gpuAngle, gpuScaleRatio, gpuCenterR, gpuCenterC);

  real* targets = NULL;
  const int TARGET_MEM_SIZE =
      NUM_IMAGES * SAMPLING_RATE * TGT_SIZE * TGT_SIZE * CHANNELS;
  targets = (real*)hl_malloc_device(sizeof(real) * TARGET_MEM_SIZE);
160 161 162 163 164 165 166 167 168 169 170 171 172 173
  hl_conv_random_disturb(gpuImages_,
                         IMG_SIZE,
                         TGT_SIZE,
                         CHANNELS,
                         NUM_IMAGES,
                         1.0,
                         1.0,
                         SAMPLING_RATE,
                         gpuAngle,
                         gpuScaleRatio,
                         gpuCenterR,
                         gpuCenterC,
                         2,
                         false,
Z
zhangjinchao01 已提交
174 175
                         targets);
  real cpuTargets[TARGET_MEM_SIZE];
176
  hl_memcpy_device2host(cpuTargets, targets, sizeof(real) * TARGET_MEM_SIZE);
Z
zhangjinchao01 已提交
177 178 179 180 181 182
  for (int i = 0; i < TARGET_MEM_SIZE; ++i) {
    EXPECT_FLOAT_EQ(1.0, cpuTargets[i]);
  }
}

TEST_F(PerturbationTest, translation_test) {
183 184
  real *gpuAngle, *gpuScaleRatio;
  int *gpuCenterR, *gpuCenterC;
Z
zhangjinchao01 已提交
185
  allocateMem(gpuAngle, gpuScaleRatio, gpuCenterR, gpuCenterC);
186 187 188 189 190 191 192 193 194 195
  hl_generate_disturb_params(gpuAngle,
                             gpuScaleRatio,
                             gpuCenterR,
                             gpuCenterC,
                             NUM_IMAGES,
                             IMG_SIZE,
                             0.0,
                             0.0,
                             SAMPLING_RATE,
                             false);
Z
zhangjinchao01 已提交
196 197 198 199 200 201
  generateTranslationParams(gpuCenterR, gpuCenterC, IMG_SIZE);

  real* targets = NULL;
  const int TARGET_MEM_SIZE =
      NUM_IMAGES * SAMPLING_RATE * TGT_SIZE * TGT_SIZE * CHANNELS;
  targets = (real*)hl_malloc_device(sizeof(real) * TARGET_MEM_SIZE);
202 203 204 205 206 207 208 209 210 211 212 213
  hl_conv_random_disturb_with_params(gpuImages_,
                                     IMG_SIZE,
                                     TGT_SIZE,
                                     CHANNELS,
                                     NUM_IMAGES,
                                     SAMPLING_RATE,
                                     gpuAngle,
                                     gpuScaleRatio,
                                     gpuCenterR,
                                     gpuCenterC,
                                     2,
                                     targets);
Z
zhangjinchao01 已提交
214 215

  real cpuTargets[TARGET_MEM_SIZE];
216
  hl_memcpy_device2host(cpuTargets, targets, sizeof(real) * TARGET_MEM_SIZE);
Z
zhangjinchao01 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229
  for (int i = 0; i < SAMPLING_RATE * NUM_IMAGES; ++i) {
    for (int p = 0; p < TGT_SIZE * TGT_SIZE * CHANNELS; ++p) {
      const int offset = i * TGT_SIZE * TGT_SIZE * CHANNELS + p;
      if (p < TGT_SIZE * CHANNELS) {
        EXPECT_FLOAT_EQ(0.0, cpuTargets[offset]);
      } else {
        EXPECT_FLOAT_EQ(1.0, cpuTargets[offset]);
      }
    }
  }
}

TEST_F(PerturbationTest, rotation_test) {
230 231
  real *gpuAngle, *gpuScaleRatio;
  int *gpuCenterR, *gpuCenterC;
Z
zhangjinchao01 已提交
232
  allocateMem(gpuAngle, gpuScaleRatio, gpuCenterR, gpuCenterC);
233 234 235 236 237 238 239 240 241 242
  hl_generate_disturb_params(gpuAngle,
                             gpuScaleRatio,
                             gpuCenterR,
                             gpuCenterC,
                             NUM_IMAGES,
                             IMG_SIZE,
                             0.0,
                             0.0,
                             SAMPLING_RATE,
                             false);
Z
zhangjinchao01 已提交
243 244 245 246 247 248
  generateRotationParams(gpuAngle);

  real* targets = NULL;
  const int TARGET_MEM_SIZE =
      NUM_IMAGES * SAMPLING_RATE * TGT_SIZE * TGT_SIZE * CHANNELS;
  targets = (real*)hl_malloc_device(sizeof(real) * TARGET_MEM_SIZE);
249 250 251 252 253 254 255 256 257 258 259 260
  hl_conv_random_disturb_with_params(gpuImages_,
                                     IMG_SIZE,
                                     TGT_SIZE,
                                     CHANNELS,
                                     NUM_IMAGES,
                                     SAMPLING_RATE,
                                     gpuAngle,
                                     gpuScaleRatio,
                                     gpuCenterR,
                                     gpuCenterC,
                                     2,
                                     targets);
Z
zhangjinchao01 已提交
261 262

  real cpuTargets[TARGET_MEM_SIZE];
263
  hl_memcpy_device2host(cpuTargets, targets, sizeof(real) * TARGET_MEM_SIZE);
Z
zhangjinchao01 已提交
264 265 266 267 268 269
  for (int i = 0; i < TARGET_MEM_SIZE; ++i) {
    EXPECT_FLOAT_EQ(1.0, cpuTargets[i]);
  }
}

TEST_F(PerturbationTest, scale_test) {
270 271
  real *gpuAngle, *gpuScaleRatio;
  int *gpuCenterR, *gpuCenterC;
Z
zhangjinchao01 已提交
272
  allocateMem(gpuAngle, gpuScaleRatio, gpuCenterR, gpuCenterC);
273 274 275 276 277 278 279 280 281 282
  hl_generate_disturb_params(gpuAngle,
                             gpuScaleRatio,
                             gpuCenterR,
                             gpuCenterC,
                             NUM_IMAGES,
                             IMG_SIZE,
                             0.0,
                             0.0,
                             SAMPLING_RATE,
                             false);
Z
zhangjinchao01 已提交
283 284 285 286 287 288
  generateScaleParams(gpuScaleRatio);

  real* targets = NULL;
  const int TARGET_MEM_SIZE =
      NUM_IMAGES * SAMPLING_RATE * TGT_SIZE * TGT_SIZE * CHANNELS;
  targets = (real*)hl_malloc_device(sizeof(real) * TARGET_MEM_SIZE);
289 290 291 292 293 294 295 296 297 298 299 300
  hl_conv_random_disturb_with_params(gpuImages_,
                                     IMG_SIZE,
                                     TGT_SIZE,
                                     CHANNELS,
                                     NUM_IMAGES,
                                     SAMPLING_RATE,
                                     gpuAngle,
                                     gpuScaleRatio,
                                     gpuCenterR,
                                     gpuCenterC,
                                     2,
                                     targets);
Z
zhangjinchao01 已提交
301 302

  real cpuTargets[TARGET_MEM_SIZE];
303
  hl_memcpy_device2host(cpuTargets, targets, sizeof(real) * TARGET_MEM_SIZE);
Z
zhangjinchao01 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
  for (int i = 0; i < SAMPLING_RATE * NUM_IMAGES; ++i) {
    for (int p = 0; p < TGT_SIZE * TGT_SIZE * CHANNELS; ++p) {
      const int offset = i * TGT_SIZE * TGT_SIZE * CHANNELS + p;
      int c = (p / CHANNELS) % TGT_SIZE;
      int r = (p / CHANNELS) / TGT_SIZE;
      if (r == 0 || r == TGT_SIZE - 1 || c == 0 || c == TGT_SIZE - 1) {
        EXPECT_FLOAT_EQ(0.0, cpuTargets[offset]);
      } else {
        EXPECT_FLOAT_EQ(1.0, cpuTargets[offset]);
      }
    }
  }
}

#endif