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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
|
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* 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.txt
*
* 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.
*
*=========================================================================*/
//
// \author Denis P. Shamonin and Marius Staring. Division of Image Processing,
// Department of Radiology, Leiden, The Netherlands
//
// \note This work was funded by the Netherlands Organisation for
// Scientific Research (NWO NRG-2010.02 and NWO 639.021.124).
//
#ifndef __itkTestHelper_h
#define __itkTestHelper_h
#include <string>
#include <vector>
#include <iomanip>
#include <time.h>
#include <fstream>
#include <itksys/SystemTools.hxx>
#if defined( _WIN32 )
#include <io.h>
#endif
// ITK includes
#include "itkImage.h"
#include "itkTimeProbe.h"
#include "itkImageRegionConstIterator.h"
#include "itkImageToImageFilter.h"
// OpenCL includes
#include "itkOpenCLContext.h"
#include "itkOpenCLDevice.h"
#include "itkOpenCLLogger.h"
#include "itkOpenCLKernels.h"
#include "itkTestOutputWindow.h"
//------------------------------------------------------------------------------
// Definition of the OCLImageDims
struct OCLImageDims
{
itkStaticConstMacro( Support1D, bool, false );
itkStaticConstMacro( Support2D, bool, false );
itkStaticConstMacro( Support3D, bool, true );
};
#define ITK_OPENCL_COMPARE( actual, expected ) \
if( !itk::Compare( actual, expected, #actual, #expected, __FILE__, __LINE__ ) ) \
itkGenericExceptionMacro( << "Compared values are not the same" ) \
namespace itk
{
//------------------------------------------------------------------------------
template< typename T >
inline bool
Compare( T const & t1, T const & t2, const char * actual, const char * expected,
const char * file, int line )
{
return ( t1 == t2 ) ? true : false;
}
//------------------------------------------------------------------------------
bool
CreateContext()
{
// Create and check OpenCL context
OpenCLContext::Pointer context = OpenCLContext::GetInstance();
#if defined( OPENCL_USE_INTEL_CPU ) || defined( OPENCL_USE_AMD_CPU )
context->Create( OpenCLContext::DevelopmentSingleMaximumFlopsDevice );
#else
context->Create( OpenCLContext::SingleMaximumFlopsDevice );
#endif
if( !context->IsCreated() )
{
std::cerr << "OpenCL-enabled device is not present." << std::endl;
return false;
}
return true;
}
//------------------------------------------------------------------------------
void
ReleaseContext()
{
itk::OpenCLContext::Pointer context = itk::OpenCLContext::GetInstance();
if( context->IsCreated() )
{
context->Release();
}
}
//------------------------------------------------------------------------------
void
CreateOpenCLLogger( const std::string & prefixFileName )
{
/** Create the OpenCL logger */
OpenCLLogger::Pointer logger = OpenCLLogger::GetInstance();
logger->SetLogFileNamePrefix( prefixFileName );
logger->SetOutputDirectory( OpenCLKernelsDebugDirectory );
}
//------------------------------------------------------------------------------
void
SetupForDebugging()
{
TestOutputWindow::Pointer tow = TestOutputWindow::New();
OutputWindow::SetInstance( tow );
#if ( defined( _WIN32 ) && defined( _DEBUG ) ) || !defined( NDEBUG )
Object::SetGlobalWarningDisplay( true );
std::cout << "INFO: test called Object::SetGlobalWarningDisplay(true)\n";
#endif
}
//------------------------------------------------------------------------------
void
ITKObjectEnableWarnings( Object * object )
{
#if ( defined( _WIN32 ) && defined( _DEBUG ) ) || !defined( NDEBUG )
object->SetDebug( true );
std::cout << "INFO: " << object->GetNameOfClass() << " called SetDebug(true);\n";
#endif
}
//------------------------------------------------------------------------------
// Get current date, format is m-d-y
const std::string
GetCurrentDate()
{
time_t now = time( 0 );
struct tm tstruct;
char buf[ 80 ];
#if !defined( _WIN32 ) || defined( __CYGWIN__ )
tstruct = *localtime( &now );
#else
localtime_s( &tstruct, &now );
#endif
// Visit http://www.cplusplus.com/reference/clibrary/ctime/strftime/
// for more information about date/time format
strftime( buf, sizeof( buf ), "%m-%d-%y", &tstruct );
return buf;
}
//------------------------------------------------------------------------------
// Get the name of the log file
const std::string
GetLogFileName()
{
OpenCLContext::Pointer context = OpenCLContext::GetInstance();
std::string fileName = "CPUGPULog-" + itk::GetCurrentDate() + "-";
std::string deviceName = context->GetDefaultDevice().GetName();
std::replace( deviceName.begin(), deviceName.end(), ' ', '-' ); // replace spaces
deviceName.erase( deviceName.end() - 1, deviceName.end() ); // remove end of line
switch( context->GetDefaultDevice().GetDeviceType() )
{
case OpenCLDevice::Default:
fileName.append( "Default" ); break;
case OpenCLDevice::CPU:
fileName.append( "CPU" ); break;
case OpenCLDevice::GPU:
fileName.append( "GPU" ); break;
case OpenCLDevice::Accelerator:
fileName.append( "Accelerator" ); break;
case OpenCLDevice::All:
fileName.append( "All" ); break;
default:
fileName.append( "Unknown" ); break;
}
fileName.append( "-" );
fileName.append( deviceName );
fileName.append( ".txt" );
return fileName;
}
//------------------------------------------------------------------------------
// Helper function to compute RMSE
template< class TScalarType, class CPUImageType, class GPUImageType >
TScalarType
ComputeRMSE( const CPUImageType * cpuImage, const GPUImageType * gpuImage,
TScalarType & rmsRelative )
{
ImageRegionConstIterator< CPUImageType > cit(
cpuImage, cpuImage->GetLargestPossibleRegion() );
ImageRegionConstIterator< GPUImageType > git(
gpuImage, gpuImage->GetLargestPossibleRegion() );
TScalarType rmse = 0.0;
TScalarType sumCPUSquared = 0.0;
for( cit.GoToBegin(), git.GoToBegin(); !cit.IsAtEnd(); ++cit, ++git )
{
TScalarType cpu = static_cast< TScalarType >( cit.Get() );
TScalarType err = cpu - static_cast< TScalarType >( git.Get() );
rmse += err * err;
sumCPUSquared += cpu * cpu;
}
rmse = std::sqrt( rmse / cpuImage->GetLargestPossibleRegion().GetNumberOfPixels() );
rmsRelative = rmse / std::sqrt( sumCPUSquared / cpuImage->GetLargestPossibleRegion().GetNumberOfPixels() );
return rmse;
} // end ComputeRMSE()
//------------------------------------------------------------------------------
// Helper function to compute RMSE
template< class TScalarType, class CPUImageType, class GPUImageType >
TScalarType
ComputeRMSE2( const CPUImageType * cpuImage, const GPUImageType * gpuImage,
const float & threshold )
{
ImageRegionConstIterator< CPUImageType > cit(
cpuImage, cpuImage->GetLargestPossibleRegion() );
ImageRegionConstIterator< GPUImageType > git(
gpuImage, gpuImage->GetLargestPossibleRegion() );
TScalarType rmse = 0.0;
for( cit.GoToBegin(), git.GoToBegin(); !cit.IsAtEnd(); ++cit, ++git )
{
TScalarType err = static_cast< TScalarType >( cit.Get() ) - static_cast< TScalarType >( git.Get() );
if( err > threshold )
{
rmse += err * err;
}
}
rmse = std::sqrt( rmse / cpuImage->GetLargestPossibleRegion().GetNumberOfPixels() );
return rmse;
} // end ComputeRMSE2()
//------------------------------------------------------------------------------
// Helper function to get test result from output images
template< class TScalarType, class CPUImageType, class GPUImageType >
void
GetTestOutputResult(
const CPUImageType * cpuImage, const GPUImageType * gpuImage,
const float allowedRMSerror, TScalarType & rmsError, TScalarType & rmsRelative,
bool & testPassed,
const bool skipCPU, const bool skipGPU,
const TimeProbe::TimeStampType cpuTime,
const TimeProbe::TimeStampType gpuTime,
const bool updateExceptionCPU, const bool updateExceptionGPU )
{
rmsError = 0.0;
rmsRelative = 0.0;
testPassed = true;
if( updateExceptionCPU || updateExceptionGPU )
{
testPassed = false;
}
else
{
if( !skipCPU && !skipGPU && cpuImage && gpuImage )
{
rmsError = ComputeRMSE< TScalarType, CPUImageType, GPUImageType >
( cpuImage, gpuImage, rmsRelative );
std::cout << ", speed up " << ( cpuTime / gpuTime ) << std::endl;
std::cout << std::fixed << std::setprecision( 8 );
std::cout << "Maximum allowed RMS Error: " << allowedRMSerror << std::endl;
std::cout << "Computed real RMS Error: " << rmsError << std::endl;
std::cout << "Computed real nRMS Error: " << rmsRelative << std::endl;
testPassed = ( rmsError <= allowedRMSerror );
}
}
}
//------------------------------------------------------------------------------
// Helper function to get test result from filters
template< class TScalarType,
class ImageToImageFilterType, class OutputImage >
void
GetTestFilterResult(
typename ImageToImageFilterType::Pointer & cpuFilter,
typename ImageToImageFilterType::Pointer & gpuFilter,
const float allowedRMSerror, TScalarType & rmsError, TScalarType & rmsRelative,
bool & testPassed, const bool skipCPU, const bool skipGPU,
const TimeProbe::TimeStampType cpuTime,
const TimeProbe::TimeStampType gpuTime,
const bool updateExceptionCPU, const bool updateExceptionGPU,
const unsigned int outputindex = 0 )
{
rmsError = 0.0;
testPassed = true;
if( updateExceptionCPU || updateExceptionGPU )
{
testPassed = false;
}
else
{
if( !skipCPU && !skipGPU && cpuFilter.IsNotNull() && gpuFilter.IsNotNull() )
{
if( outputindex == 0 )
{
rmsError = ComputeRMSE< TScalarType, OutputImage, OutputImage >
( cpuFilter->GetOutput(), gpuFilter->GetOutput(), rmsRelative );
}
else
{
rmsError = ComputeRMSE< TScalarType, OutputImage, OutputImage >
( cpuFilter->GetOutput( outputindex ), gpuFilter->GetOutput( outputindex ), rmsRelative );
}
std::cout << ", speed up " << ( cpuTime / gpuTime ) << std::endl;
std::cout << std::fixed << std::setprecision( 8 );
std::cout << "Maximum allowed RMS Error: " << allowedRMSerror << std::endl;
std::cout << "Computed real RMS Error: " << rmsError << std::endl;
std::cout << "Computed real nRMS Error: " << rmsRelative << std::endl;
testPassed = ( rmsError <= allowedRMSerror );
}
}
}
//------------------------------------------------------------------------------
// Helper function to compute RMSE with masks
template< class TScalarType, class CPUImageType, class GPUImageType, class MaskImageType >
TScalarType
ComputeRMSE( const CPUImageType * cpuImage, const GPUImageType * gpuImage,
const MaskImageType * cpuImageMask, const MaskImageType * gpuImageMask,
TScalarType & rmsRelative )
{
ImageRegionConstIterator< CPUImageType > cit(
cpuImage, cpuImage->GetLargestPossibleRegion() );
ImageRegionConstIterator< GPUImageType > git(
gpuImage, gpuImage->GetLargestPossibleRegion() );
ImageRegionConstIterator< MaskImageType > mcit(
cpuImageMask, cpuImageMask->GetLargestPossibleRegion() );
ImageRegionConstIterator< MaskImageType > mgit(
gpuImageMask, gpuImageMask->GetLargestPossibleRegion() );
TScalarType rmse = 0.0;
TScalarType sumCPUSquared = 0.0;
std::size_t count = 0;
for( cit.GoToBegin(), git.GoToBegin(), mcit.GoToBegin(), mgit.GoToBegin();
!cit.IsAtEnd(); ++cit, ++git, ++mcit, ++mgit )
{
if( ( mcit.Get() == NumericTraits< typename MaskImageType::PixelType >::OneValue() )
&& ( mgit.Get() == NumericTraits< typename MaskImageType::PixelType >::OneValue() ) )
{
TScalarType cpu = static_cast< TScalarType >( cit.Get() );
TScalarType err = cpu - static_cast< TScalarType >( git.Get() );
rmse += err * err;
sumCPUSquared += cpu * cpu;
count++;
}
}
if( count == 0 )
{
rmsRelative = 0.0;
return 0.0;
}
rmse = std::sqrt( rmse / count );
rmsRelative = rmse / std::sqrt( sumCPUSquared / count );
return rmse;
} // end ComputeRMSE()
//------------------------------------------------------------------------------
// Helper function to compute RMSE with masks and threshold
template< class TScalarType, class CPUImageType, class GPUImageType, class MaskImageType >
TScalarType
ComputeRMSE2( const CPUImageType * cpuImage, const GPUImageType * gpuImage,
const MaskImageType * cpuImageMask, const MaskImageType * gpuImageMask,
const float threshold, TScalarType & rmsRelative )
{
ImageRegionConstIterator< CPUImageType > cit(
cpuImage, cpuImage->GetLargestPossibleRegion() );
ImageRegionConstIterator< GPUImageType > git(
gpuImage, gpuImage->GetLargestPossibleRegion() );
ImageRegionConstIterator< MaskImageType > mcit(
cpuImageMask, cpuImageMask->GetLargestPossibleRegion() );
ImageRegionConstIterator< MaskImageType > mgit(
gpuImageMask, gpuImageMask->GetLargestPossibleRegion() );
TScalarType rmse = 0.0;
TScalarType sumCPUSquared = 0.0;
std::size_t count = 0;
for( cit.GoToBegin(), git.GoToBegin(), mcit.GoToBegin(), mgit.GoToBegin();
!cit.IsAtEnd(); ++cit, ++git, ++mcit, ++mgit )
{
if( mcit.Get() == NumericTraits< typename MaskImageType::PixelType >::One
&& mgit.Get() == NumericTraits< typename MaskImageType::PixelType >::OneValue() )
{
++count;
TScalarType cpu = static_cast< TScalarType >( cit.Get() );
TScalarType err = cpu - static_cast< TScalarType >( git.Get() );
if( vnl_math_abs( err ) > threshold )
{
rmse += err * err;
}
sumCPUSquared += cpu * cpu;
}
}
if( count == 0 )
{
rmsRelative = 0.0;
return 0.0;
}
rmse = std::sqrt( rmse / count );
rmsRelative = rmse / std::sqrt( sumCPUSquared / count );
return rmse;
} // end ComputeRMSE()
//----------------------------------------------------------------------------
// Write log file in Microsoft Excel semicolon separated format.
template< class ImageType >
void
WriteLog(
const std::string & filename,
const unsigned int dim,
const typename ImageType::SizeType & imagesize,
const double rmsError,
const double rmsRelative,
const bool testPassed,
const bool exceptionGPU,
const unsigned int numThreads,
const unsigned int runTimes,
const std::string & filterName,
const TimeProbe::TimeStampType cpuTime,
const TimeProbe::TimeStampType gpuTime,
const std::string & comments = "" )
{
const std::string s( " ; " ); // separator
std::ofstream fout;
const bool fileExists = itksys::SystemTools::FileExists( filename.c_str() );
fout.open( filename.c_str(), std::ios_base::app );
// If file does not exist, then print table header
if( !fileExists )
{
fout << "Filter Name" << s << "Dimension" << s << "Image Size"
<< s << "CPU(" << numThreads << ") (s)" << s << "GPU (s)" << s << "CPU/GPU Speed Ratio"
<< s << "RMS Error" << s << "RMS Relative" << s << "Test Passed" << s << "Run Times" << s << "Comments" << std::endl;
}
fout << filterName << s << dim << s;
for( unsigned int i = 0; i < dim; i++ )
{
fout << imagesize.GetSize()[ i ];
if( i < dim - 1 ) { fout << "x"; }
}
fout << s << cpuTime << s;
if( !exceptionGPU )
{
fout << gpuTime << s;
}
else
{
fout << "na" << s;
}
if( !exceptionGPU )
{
if( gpuTime != 0.0 )
{
fout << ( cpuTime / gpuTime ) << s;
}
else
{
fout << "0" << s;
}
}
else
{
fout << "na" << s;
}
fout << rmsError << s;
fout << rmsRelative << s;
fout << ( testPassed ? "Yes" : "No" ) << s;
fout << runTimes << s;
if( comments.size() > 0 )
{
fout << comments;
}
else
{
fout << "none";
}
fout << std::endl;
fout.close();
return;
}
}
#endif // end #ifndef __itkTestHelper_h
|