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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
|
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include <string>
#include <fstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <limits>
#include <stdexcept>
#include <cfloat>
#include <arrayfire.h>
#include <af/dim4.hpp>
#include <af/array.h>
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned short ushort;
template<typename inType, typename outType, typename FileElementType>
void readTests(const std::string &FileName, std::vector<af::dim4> &inputDims,
std::vector<std::vector<inType> > &testInputs,
std::vector<std::vector<outType> > &testOutputs)
{
using std::vector;
std::ifstream testFile(FileName.c_str());
if(testFile.good()) {
unsigned inputCount;
testFile >> inputCount;
for(unsigned i=0; i<inputCount; i++) {
af::dim4 temp(1);
testFile >> temp;
inputDims.push_back(temp);
}
unsigned testCount;
testFile >> testCount;
testOutputs.resize(testCount);
vector<unsigned> testSizes(testCount);
for(unsigned i = 0; i < testCount; i++) {
testFile >> testSizes[i];
}
testInputs.resize(inputCount,vector<inType>(0));
for(unsigned k=0; k<inputCount; k++) {
unsigned nElems = inputDims[k].elements();
testInputs[k].resize(nElems);
FileElementType tmp;
for(unsigned i = 0; i < nElems; i++) {
testFile >> tmp;
testInputs[k][i] = tmp;
}
}
testOutputs.resize(testCount, vector<outType>(0));
for(unsigned i = 0; i < testCount; i++) {
testOutputs[i].resize(testSizes[i]);
FileElementType tmp;
for(unsigned j = 0; j < testSizes[i]; j++) {
testFile >> tmp;
testOutputs[i][j] = tmp;
}
}
}
else {
FAIL() << "TEST FILE NOT FOUND";
}
}
template<typename inType, typename outType>
void readTestsFromFile(const std::string &FileName, std::vector<af::dim4> &inputDims,
std::vector<std::vector<inType> > &testInputs,
std::vector<std::vector<outType> > &testOutputs)
{
using std::vector;
std::ifstream testFile(FileName.c_str());
if(testFile.good()) {
unsigned inputCount;
testFile >> inputCount;
for(unsigned i=0; i<inputCount; i++) {
af::dim4 temp(1);
testFile >> temp;
inputDims.push_back(temp);
}
unsigned testCount;
testFile >> testCount;
testOutputs.resize(testCount);
vector<unsigned> testSizes(testCount);
for(unsigned i = 0; i < testCount; i++) {
testFile >> testSizes[i];
}
testInputs.resize(inputCount,vector<inType>(0));
for(unsigned k=0; k<inputCount; k++) {
unsigned nElems = inputDims[k].elements();
testInputs[k].resize(nElems);
inType tmp;
for(unsigned i = 0; i < nElems; i++) {
testFile >> tmp;
testInputs[k][i] = tmp;
}
}
testOutputs.resize(testCount, vector<outType>(0));
for(unsigned i = 0; i < testCount; i++) {
testOutputs[i].resize(testSizes[i]);
outType tmp;
for(unsigned j = 0; j < testSizes[i]; j++) {
testFile >> tmp;
testOutputs[i][j] = tmp;
}
}
}
else {
FAIL() << "TEST FILE NOT FOUND";
}
}
inline void readImageTests(const std::string &pFileName,
std::vector<af::dim4> &pInputDims,
std::vector<std::string> &pTestInputs,
std::vector<dim_t> &pTestOutSizes,
std::vector<std::string> &pTestOutputs)
{
using std::vector;
std::ifstream testFile(pFileName.c_str());
if(testFile.good()) {
unsigned inputCount;
testFile >> inputCount;
for(unsigned i=0; i<inputCount; i++) {
af::dim4 temp(1);
testFile >> temp;
pInputDims.push_back(temp);
}
unsigned testCount;
testFile >> testCount;
pTestOutputs.resize(testCount);
pTestOutSizes.resize(testCount);
for(unsigned i = 0; i < testCount; i++) {
testFile >> pTestOutSizes[i];
}
pTestInputs.resize(inputCount, "");
for(unsigned k=0; k<inputCount; k++) {
std::string temp = "";
while(std::getline(testFile, temp)) {
if (temp!="")
break;
}
if (temp=="")
throw std::runtime_error("Test file might not be per format, please check.");
pTestInputs[k] = temp;
}
pTestOutputs.resize(testCount, "");
for(unsigned i = 0; i < testCount; i++) {
std::string temp = "";
while(std::getline(testFile, temp)) {
if (temp!="")
break;
}
if (temp=="")
throw std::runtime_error("Test file might not be per format, please check.");
pTestOutputs[i] = temp;
}
}
else {
FAIL() << "TEST FILE NOT FOUND";
}
}
template<typename outType>
void readImageTests(const std::string &pFileName,
std::vector<af::dim4> &pInputDims,
std::vector<std::string> &pTestInputs,
std::vector<std::vector<outType> > &pTestOutputs)
{
using std::vector;
std::ifstream testFile(pFileName.c_str());
if(testFile.good()) {
unsigned inputCount;
testFile >> inputCount;
for(unsigned i=0; i<inputCount; i++) {
af::dim4 temp(1);
testFile >> temp;
pInputDims.push_back(temp);
}
unsigned testCount;
testFile >> testCount;
pTestOutputs.resize(testCount);
vector<unsigned> testSizes(testCount);
for(unsigned i = 0; i < testCount; i++) {
testFile >> testSizes[i];
}
pTestInputs.resize(inputCount, "");
for(unsigned k=0; k<inputCount; k++) {
std::string temp = "";
while(std::getline(testFile, temp)) {
if (temp!="")
break;
}
if (temp=="")
throw std::runtime_error("Test file might not be per format, please check.");
pTestInputs[k] = temp;
}
pTestOutputs.resize(testCount, vector<outType>(0));
for(unsigned i = 0; i < testCount; i++) {
pTestOutputs[i].resize(testSizes[i]);
outType tmp;
for(unsigned j = 0; j < testSizes[i]; j++) {
testFile >> tmp;
pTestOutputs[i][j] = tmp;
}
}
}
else {
FAIL() << "TEST FILE NOT FOUND";
}
}
template<typename descType>
void readImageFeaturesDescriptors(const std::string &pFileName,
std::vector<af::dim4> &pInputDims,
std::vector<std::string> &pTestInputs,
std::vector<std::vector<float> > &pTestFeats,
std::vector<std::vector<descType> > &pTestDescs)
{
using std::vector;
std::ifstream testFile(pFileName.c_str());
if(testFile.good()) {
unsigned inputCount;
testFile >> inputCount;
for(unsigned i=0; i<inputCount; i++) {
af::dim4 temp(1);
testFile >> temp;
pInputDims.push_back(temp);
}
unsigned attrCount, featCount, descLen;
testFile >> featCount;
testFile >> attrCount;
testFile >> descLen;
pTestFeats.resize(attrCount);
pTestInputs.resize(inputCount, "");
for(unsigned k=0; k<inputCount; k++) {
std::string temp = "";
while(std::getline(testFile, temp)) {
if (temp!="")
break;
}
if (temp=="")
throw std::runtime_error("Test file might not be per format, please check.");
pTestInputs[k] = temp;
}
pTestFeats.resize(attrCount, vector<float>(0));
for(unsigned i = 0; i < attrCount; i++) {
pTestFeats[i].resize(featCount);
float tmp;
for(unsigned j = 0; j < featCount; j++) {
testFile >> tmp;
pTestFeats[i][j] = tmp;
}
}
pTestDescs.resize(featCount, vector<descType>(0));
for(unsigned i = 0; i < featCount; i++) {
pTestDescs[i].resize(descLen);
descType tmp;
for(unsigned j = 0; j < descLen; j++) {
testFile >> tmp;
pTestDescs[i][j] = tmp;
}
}
}
else {
FAIL() << "TEST FILE NOT FOUND";
}
}
/**
* Below is not a pair wise comparition method, rather
* it computes the accumulated error of the computed
* output and gold output.
*
* The cut off is decided based on root mean square
* deviation from cpu result
*
* For images, the maximum possible error will happen if all
* the observed values are zeros and all the predicted values
* are 255's. In such case, the value of NRMSD will be 1.0
* Similarly, we can deduce that 0.0 will be the minimum
* value of NRMSD. Hence, the range of RMSD is [0,255] for image inputs.
*/
template<typename T>
bool compareArraysRMSD(dim_t data_size, T *gold, T *data, double tolerance)
{
double accum = 0.0;
double maxion = FLT_MAX;//(double)std::numeric_limits<T>::lowest();
double minion = FLT_MAX;//(double)std::numeric_limits<T>::max();
for(dim_t i=0;i<data_size;i++)
{
double dTemp = (double)data[i];
double gTemp = (double)gold[i];
double diff = gTemp-dTemp;
double err = std::abs(diff) > 1.0e-4 ? diff : 0.0f;
accum += std::pow(err,2.0);
maxion = std::max(maxion, dTemp);
minion = std::min(minion, dTemp);
}
accum /= data_size;
double NRMSD = std::sqrt(accum)/(maxion-minion);
std::cout<<"NRMSD = "<<NRMSD<<std::endl;
if (NRMSD > tolerance)
return false;
return true;
}
template<typename T, typename Other>
struct is_same_type{
static const bool value = false;
};
template<typename T>
struct is_same_type<T, T> {
static const bool value = true;
};
template<bool, typename T, typename O>
struct cond_type;
template<typename T, typename Other>
struct cond_type<true, T, Other> {
typedef T type;
};
template<typename T, typename Other>
struct cond_type<false, T, Other> {
typedef Other type;
};
template<typename T>
inline double real(T val) { return (double)val; }
template<>
inline double real<af::cdouble>(af::cdouble val) { return real(val); }
template<>
inline double real<af::cfloat> (af::cfloat val) { return real(val); }
template<typename T>
inline double imag(T val) { return (double)val; }
template<>
inline double imag<af::cdouble>(af::cdouble val) { return imag(val); }
template<>
inline double imag<af::cfloat> (af::cfloat val) { return imag(val); }
template<typename T>
bool noDoubleTests()
{
bool isTypeDouble = is_same_type<T, double>::value || is_same_type<T, af::cdouble>::value;
int dev = af::getDevice();
bool isDoubleSupported = af::isDoubleAvailable(dev);
return ((isTypeDouble && !isDoubleSupported) ? true : false);
}
inline bool noImageIOTests()
{
bool ret = !af::isImageIOAvailable();
if(ret) printf("Image IO Not Configured. Test will exit\n");
return ret;
}
inline bool noLAPACKTests()
{
bool ret = !af::isLAPACKAvailable();
if(ret) printf("LAPACK Not Configured. Test will exit\n");
return ret;
}
// TODO: perform conversion on device for CUDA and OpenCL
template<typename T>
af_err conv_image(af_array *out, af_array in)
{
af_array outArray;
dim_t d0, d1, d2, d3;
af_get_dims(&d0, &d1, &d2, &d3, in);
af::dim4 idims(d0, d1, d2, d3);
dim_t nElems = 0;
af_get_elements(&nElems, in);
float *in_data = new float[nElems];
af_get_data_ptr(in_data, in);
T *out_data = new T[nElems];
for (int i = 0; i < (int)nElems; i++)
out_data[i] = (T)in_data[i];
af_create_array(&outArray, out_data, idims.ndims(), idims.get(), (af_dtype) af::dtype_traits<T>::af_type);
std::swap(*out, outArray);
delete [] in_data;
delete [] out_data;
return AF_SUCCESS;
}
template<typename T>
af::array cpu_randu(const af::dim4 dims)
{
typedef typename af::dtype_traits<T>::base_type BT;
bool isTypeCplx = is_same_type<T, af::cfloat>::value || is_same_type<T, af::cdouble>::value;
bool isTypeFloat = is_same_type<BT, float>::value || is_same_type<BT, double>::value;
dim_t elements = (isTypeCplx ? 2 : 1) * dims.elements();
std::vector<BT> out(elements);
for(int i = 0; i < (int)elements; i++) {
out[i] = isTypeFloat ? (BT)(rand())/RAND_MAX : rand() % 100;
}
return af::array(dims, (T *)&out[0]);
}
#pragma GCC diagnostic pop
|