Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
added niftyreg resampling for linear and cubic B-splines. Fixed compi…
Browse files Browse the repository at this point in the history
…lations issue (tested on Linux gcc 5.4 cuda 10.2). Resampling and Gradient are supported on cpu and gpu (but no gradient wrt the warped image itself)
  • Loading branch information
LucasFidon committed Jun 5, 2019
1 parent 0f0d736 commit 382ed49
Show file tree
Hide file tree
Showing 49 changed files with 17,751 additions and 0 deletions.
32 changes: 32 additions & 0 deletions niftynet/contrib/layer/resampler_optional_niftyreg.py
@@ -0,0 +1,32 @@
# Flag stating whether C++/CUDA image resampling is available
HAS_NIFTYREG_RESAMPLING = False

try:
from niftyreg_image_resampling import NiftyregImageResamplingLayer
import niftyreg_image_resampling as resampler_module

ResamplerOptionalNiftyRegLayer = NiftyregImageResamplingLayer

HAS_NIFTYREG_RESAMPLING = True
except ImportError:
import tensorflow as tf

tf.logging.warning('''
niftyreg_image_resampling is not installed; falling back onto
niftynet.layer.resampler.ResamplerLayer. To install
niftyreg_image_resampling please see
niftynet/contrib/niftyreg_image_resampling/README.md
''')

from niftynet.layer.resampler import ResamplerLayer
import niftynet.layer.resampler as resampler_module

ResamplerOptionalNiftyRegLayer = ResamplerLayer


# Passthrough of supported boundary types
SUPPORTED_BOUNDARY = resampler_module.SUPPORTED_BOUNDARY


# Passthrough of supported interpolation types
SUPPORTED_INTERPOLATION = resampler_module.SUPPORTED_INTERPOLATION
1 change: 1 addition & 0 deletions niftynet/contrib/niftyreg_image_resampling/.gitignore
@@ -0,0 +1 @@
*~
28 changes: 28 additions & 0 deletions niftynet/contrib/niftyreg_image_resampling/LICENSE
@@ -0,0 +1,28 @@
Copyright (c) 2009, University College London, United-Kingdom
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

Neither the name of the University College London nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.

25 changes: 25 additions & 0 deletions niftynet/contrib/niftyreg_image_resampling/README.md
@@ -0,0 +1,25 @@
# NiftyNet GPU Image Resampling Module

## Purpose and Scope

This module provides a faster implementation of image resampling. For most usage scenarios, it is a drop-in replacement for niftynet.layer.resampler.ResamplerLayer, however, its feature set is limited to:

* ZERO (zero-padding), REPLICATE (clamping of intensities at edges), and SYMMETRIC (mirroring) boundaries
* NEAREST (constant), LINEAR, and BSPLINE (cubic spline) interpolation.
* Differentiation with respect to the floating image is a CPU-only operation.

To provide compatibility where this module is not installed, the following module can be used: niftynet.contrib.layer.resampler_optional_niftyreg.ResamplerOptionalNiftyRegLayer. This module will try to load NiftyregImageResamplingLayer, if that fails, it defaults to niftynet.layer.resampler.ResamplerLayer.

## Building and Installing

Building and installing is performed as usual via setup.py.

Building the module requires that a CUDA toolkit and CMake be installed, and nvcc and cmake can be found on the executables search path.
CMake variables can be overriden through the `override` command and `--settings`/`-s` switch as a list of colon (':') separated variable-value pairs. E.g., `python setup.py override -s "CMAKE_CXX_COMPILER:/usr/bin/g++-6:CMAKE_C_COMPILER:/usr/bin/gcc-6" build`.

Building was only tested with CUDA 9.0/gcc 5.4 and gcc 6.4, on Ubuntu 16.04 and 18.04.

## Acknowledgements

The image resampling code contained in this module is heavily based on code extracted from NiftyReg (https://sourceforge.net/projects/niftyreg/).

@@ -0,0 +1,64 @@
project(NiftyNet_gpu_resampling)
cmake_minimum_required(VERSION 3.5)

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules;${CMAKE_MODULE_PATH}")

set(GPU_RESAMPLING_CONFIGFILE_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE STRING "Destination directory for configured files.")

add_definitions("-DGOOGLE_CUDA")
set(GPU_RESAMPLING_LIB_TARGET niftyreg_image_resampling_ops)
set(CMAKE_CXX_STANDARD 11)
# This may not be portable (almost certainly isn't)!
# -DNDEBUG: a constexpr applied to function returning std::string is causing the build to fail; the code appears to be inside an assert, so disabling asserts "solves" the problem.
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -std=c++11 --expt-relaxed-constexpr -DNDEBUG --disable-warnings")

find_package(CUDA)
find_package(Tensorflow REQUIRED)

include_directories(nifti)
include_directories(SYSTEM "${Tensorflow_INCLUDE_DIRS}")
link_directories("${Tensorflow_LIBRARY_DIRS}")
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} ${Tensorflow_CFLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Tensorflow_CFLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${Tensorflow_CFLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")

set(GPU_RESAMPLING_CXX_SRC
_reg_resampling.cpp
_reg_tools.cpp
_reg_maths.cpp

niftyreg_cpu_resample_op.cpp
niftyreg_cpu_resample_gradient_op.cpp
niftyreg_cpu_resample_image_gradient_op.cpp

nifti/nifti1_io.c
nifti/znzlib.c
)

if (CUDA_FOUND)
set(GPU_RESAMPLING_CU_SRC
resampleKernel.cu
_reg_common_cuda.cu
_reg_resampling_gpu.cu

niftyreg_gpu_resample_op.cu
niftyreg_gpu_resample_gradient_op.cu
)

cuda_add_library(${GPU_RESAMPLING_LIB_TARGET} SHARED
${GPU_RESAMPLING_CXX_SRC}
${GPU_RESAMPLING_CU_SRC}
)
else ()
message(WARNING "No CUDA toolkit was found, if your tensorflow install was built with CUDA support, the build will fail.")
add_library(${GPU_RESAMPLING_LIB_TARGET} SHARED
${GPU_RESAMPLING_CXX_SRC}
)
endif (CUDA_FOUND)

set_target_properties(${GPU_RESAMPLING_LIB_TARGET} PROPERTIES PREFIX "")
target_link_libraries(${GPU_RESAMPLING_LIB_TARGET}
${Tensorflow_LIBRARIES}
)
file(GENERATE OUTPUT "${GPU_RESAMPLING_CONFIGFILE_DIR}/niftyreg_module_loader.py" INPUT "${CMAKE_CURRENT_SOURCE_DIR}/../niftyreg_module_loader.py.in")

0 comments on commit 382ed49

Please sign in to comment.