1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
#include "caffe2/operators/roi_align_op.h"
#include "caffe2/utils/eigen_utils.h"
#include <c10/test/util/Macros.h>
#include "caffe2/core/context_gpu.h"
#include "caffe2/core/flags.h"
#include "caffe2/utils/eigen_utils.h"
#include "caffe2/utils/math.h"
#include "gtest/gtest.h"
namespace caffe2 {
namespace {
template <class Context>
void AddConstInput(
const vector<int64_t>& shape,
const float value,
const string& name,
Context* context,
Workspace* ws) {
Blob* blob = ws->CreateBlob(name);
auto* tensor = BlobGetMutableTensor(blob, Context::GetDeviceType());
tensor->Resize(shape);
math::Set<float, Context>(
tensor->size(), value, tensor->template mutable_data<float>(), context);
return;
}
template <class Context>
void AddInput(
const vector<int64_t>& shape,
const vector<float>& values,
const string& name,
Workspace* ws);
template <>
void AddInput<CPUContext>(
const vector<int64_t>& shape,
const vector<float>& values,
const string& name,
Workspace* ws) {
Blob* blob = ws->CreateBlob(name);
auto* tensor = BlobGetMutableTensor(blob, CPU);
tensor->Resize(shape);
EigenVectorMap<float> tensor_vec(
tensor->template mutable_data<float>(), tensor->numel());
tensor_vec.array() = utils::AsEArrXt(values);
}
template <>
void AddInput<CUDAContext>(
const vector<int64_t>& shape,
const vector<float>& values,
const string& name,
Workspace* ws) {
Tensor tmp(shape, CPU);
EigenVectorMap<float> tmp_vec(tmp.mutable_data<float>(), tmp.numel());
tmp_vec.array() = utils::AsEArrXt(values);
Blob* blob = ws->CreateBlob(name);
auto* tensor = BlobGetMutableTensor(blob, CUDA);
tensor->CopyFrom(tmp);
}
template <class Context>
DeviceType GetDeviceType() {
return CPU;
}
template <>
DeviceType GetDeviceType<CUDAContext>() {
return CUDA;
}
int randInt(int a, int b) {
static std::random_device rd;
static std::mt19937 gen(rd());
return std::uniform_int_distribution<int>(a, b)(gen);
}
struct TestParams {
int N;
int C;
int H;
int W;
int n_rois;
vector<float> rois_array;
};
template <class Context>
void CreateAndRun(
TensorCPU* outResult,
const string& order,
const TestParams& test_params,
bool random_test) {
Workspace ws;
Context context;
if (random_test) {
const int N = test_params.N;
const int C = test_params.C;
const int H = test_params.H;
const int W = test_params.W;
vector<float> features(N * C * H * W);
std::iota(features.begin(), features.end(), 0);
// utils::AsEArrXt(features) /= features.size();
AddInput<Context>(vector<int64_t>{N, C, H, W}, features, "X", &ws);
const int n_rois = test_params.n_rois;
const vector<float>& rois = test_params.rois_array;
AddInput<Context>(vector<int64_t>{n_rois, 5}, rois, "R", &ws);
} else {
const int N = 2;
const int C = 3;
const int H = 100;
const int W = 110;
vector<float> features(N * C * H * W);
std::iota(features.begin(), features.end(), 0);
// utils::AsEArrXt(features) /= features.size();
AddInput<Context>(vector<int64_t>{N, C, H, W}, features, "X", &ws);
vector<float> rois{0, 0, 0, 79, 59,
0, 0, 5.0005703f, 52.63237f, 43.69501495f,
0, 24.13628387f, 7.51243401f, 79, 46.06628418f,
0, 0, 7.50924301f, 68.47792816f, 46.03357315f,
0, 0, 23.09477997f, 51.61448669f, 59,
0, 0, 39.52141571f, 52.44710541f, 59,
0, 23.57396317f, 29.98791885f, 79, 59,
0, 0, 41.90219116f, 79, 59,
0, 0, 23.30098343f, 79, 59};
AddInput<Context>(vector<int64_t>{9, 5}, rois, "R", &ws);
}
std::vector<unique_ptr<OperatorBase>> ops;
EXPECT_TRUE(order == "NCHW" || order == "NHWC");
if (order == "NCHW") {
OperatorDef def;
def.set_name("test");
def.set_type("RoIAlign");
def.add_input("X");
def.add_input("R");
def.add_output("Y");
def.mutable_device_option()->set_device_type(
TypeToProto(GetDeviceType<Context>()));
def.add_arg()->CopyFrom(MakeArgument("spatial_scale", 1.0f / 16.0f));
def.add_arg()->CopyFrom(MakeArgument("pooled_h", 6));
def.add_arg()->CopyFrom(MakeArgument("pooled_w", 8));
def.add_arg()->CopyFrom(MakeArgument("sampling_ratio", 2));
ops.push_back(CreateOperator(def, &ws));
} else if (order == "NHWC") {
OperatorDef def_roialign;
def_roialign.set_name("test");
def_roialign.set_type("RoIAlign");
def_roialign.add_input("X_NHWC");
def_roialign.add_input("R");
def_roialign.add_output("Y_NHWC");
def_roialign.mutable_device_option()->set_device_type(
TypeToProto(GetDeviceType<Context>()));
def_roialign.add_arg()->CopyFrom(
MakeArgument("spatial_scale", 1.0f / 16.0f));
def_roialign.add_arg()->CopyFrom(MakeArgument("pooled_h", 6));
def_roialign.add_arg()->CopyFrom(MakeArgument("pooled_w", 8));
def_roialign.add_arg()->CopyFrom(MakeArgument("sampling_ratio", 2));
def_roialign.add_arg()->CopyFrom(MakeArgument<string>("order", "NHWC"));
OperatorDef def_x;
def_x.set_name("test_x");
def_x.set_type("NCHW2NHWC");
def_x.add_input("X");
def_x.add_output("X_NHWC");
def_x.mutable_device_option()->set_device_type(
TypeToProto(GetDeviceType<Context>()));
OperatorDef def_y;
def_y.set_name("test_y");
def_y.set_type("NHWC2NCHW");
def_y.add_input("Y_NHWC");
def_y.add_output("Y");
def_y.mutable_device_option()->set_device_type(
TypeToProto(GetDeviceType<Context>()));
ops.push_back(CreateOperator(def_x, &ws));
ops.push_back(CreateOperator(def_roialign, &ws));
ops.push_back(CreateOperator(def_y, &ws));
}
for (auto const& op : ops) {
EXPECT_NE(nullptr, op.get());
EXPECT_TRUE(op->Run());
}
Blob* Y_blob = ws.GetBlob("Y");
EXPECT_NE(nullptr, Y_blob);
auto& Y = Y_blob->Get<Tensor>();
outResult->CopyFrom(Y);
}
} // namespace
TEST(RoiAlignTest, CheckCPUGPUEqual) {
if (!caffe2::HasCudaGPU())
return;
Tensor y_cpu(CPU);
Tensor y_gpu(CPU);
Tensor y_cpu_nhwc(CPU);
// tests using FAIR example
{
TestParams test_params;
CreateAndRun<CPUContext>(&y_cpu, "NCHW", test_params, false);
CreateAndRun<CUDAContext>(&y_gpu, "NCHW", test_params, false);
CreateAndRun<CPUContext>(&y_cpu_nhwc, "NHWC", test_params, false);
EXPECT_EQ(y_cpu.sizes(), y_gpu.sizes());
EXPECT_EQ(y_cpu.sizes(), y_cpu_nhwc.sizes());
ConstEigenVectorMap<float> y_cpu_vec(y_cpu.data<float>(), y_cpu.numel());
ConstEigenVectorMap<float> y_gpu_vec(y_gpu.data<float>(), y_gpu.numel());
ConstEigenVectorMap<float> y_cpu_nhwc_vec(
y_cpu_nhwc.data<float>(), y_cpu_nhwc.numel());
int max_diff_idx = -1;
(y_cpu_vec - y_gpu_vec).cwiseAbs().maxCoeff(&max_diff_idx);
EXPECT_FLOAT_EQ(y_cpu_vec[max_diff_idx], y_gpu_vec[max_diff_idx]);
max_diff_idx = -1;
(y_cpu_vec - y_cpu_nhwc_vec).cwiseAbs().maxCoeff(&max_diff_idx);
EXPECT_FLOAT_EQ(y_cpu_vec[max_diff_idx], y_cpu_nhwc_vec[max_diff_idx]);
}
// random tests
const int random_test_numbers = 100;
for (int i = 0; i < random_test_numbers; i++) {
const int N = randInt(1, 5);
const int C = randInt(1, 5);
const int H = randInt(1, 50);
const int W = randInt(1, 50);
const int n_rois = randInt(1, 30);
vector<float> rois_array;
for (int n = 0; n < n_rois; n++) {
rois_array.push_back(randInt(0, N - 1));
int w1 = randInt(-20, W + 20);
int w2 = randInt(-20, W + 20);
int h1 = randInt(-20, H + 20);
int h2 = randInt(-20, H + 20);
rois_array.push_back(std::min(w1, w2));
rois_array.push_back(std::max(h1, h2));
rois_array.push_back(std::min(w1, w2));
rois_array.push_back(std::max(h1, h2));
}
TestParams test_params{N, C, H, W, n_rois, rois_array};
CreateAndRun<CPUContext>(&y_cpu, "NCHW", test_params, true);
CreateAndRun<CUDAContext>(&y_gpu, "NCHW", test_params, true);
CreateAndRun<CPUContext>(&y_cpu_nhwc, "NHWC", test_params, true);
EXPECT_EQ(y_cpu.sizes(), y_gpu.sizes());
EXPECT_EQ(y_cpu.sizes(), y_cpu_nhwc.sizes());
ConstEigenVectorMap<float> y_cpu_vec(y_cpu.data<float>(), y_cpu.numel());
ConstEigenVectorMap<float> y_gpu_vec(y_gpu.data<float>(), y_gpu.numel());
ConstEigenVectorMap<float> y_cpu_nhwc_vec(
y_cpu_nhwc.data<float>(), y_cpu_nhwc.numel());
int max_diff_idx = -1;
(y_cpu_vec - y_gpu_vec).cwiseAbs().maxCoeff(&max_diff_idx);
EXPECT_NEAR(y_cpu_vec[max_diff_idx], y_gpu_vec[max_diff_idx], 1e-1);
max_diff_idx = -1;
(y_cpu_vec - y_cpu_nhwc_vec).cwiseAbs().maxCoeff(&max_diff_idx);
EXPECT_FLOAT_EQ(y_cpu_vec[max_diff_idx], y_cpu_nhwc_vec[max_diff_idx]);
}
}
} // namespace caffe2
|