qsyao / cuda_spatial_deform Goto Github PK
View Code? Open in Web Editor NEWA fast tool to do image augmentation on GPU(especially elastic_deform), can be helpful to research on Medical Image.
License: Other
A fast tool to do image augmentation on GPU(especially elastic_deform), can be helpful to research on Medical Image.
License: Other
This package is exactly what I need to speed up the data augmentation in my code! Unfortunately, I am unable to get the elastic deformation to work. Here is a tentative minimal working example for the FLAIR image included with the code..
import nibabel as nib
import time
import numpy as np
from cuda_backend.py_api import Handle
img = nib.load('data/FLAIR.nii.gz')
data = img.get_fdata().astype('float32')
start = time.time()
cuda_handle = Handle(data.shape, RGB=False, mode='constant')
cuda_handle.elastic(sigma=1., alpha=20., mode='constant')
cuda_handle.end_flag()
data_aug = cuda_handle.augment(data)
print('%f' % (time.time() - start))
nib.save(nib.Nifti1Image(np.array(data_aug[0]), img.affine, img.header),
'Flair.deformed.nii.gz')
And here are the input/output:
Any idea what's is going on?
Could you please update the example so that people can see the verification is successful for augment ? Thank you.
cuda_handle = Handle(array_image.shape, mode="mirror")
# cuda_handle.test()
# cuda_handle.scale(0.5)
# cuda_handle.flip(do_y=True, do_x=True, do_z=True)
# cuda_handle.translate(100, 100, 20)
# cuda_handle.rotate(0.75 * np.pi, 0.75 * np.pi, 0.75 * np.pi)
cuda_handle.elastic(sigma=5., alpha=200., mode='constant')
cuda_handle.end_flag()
# correct_ret = deform.spatial_augment(array_image, mode="mirror")
# Warm up and Unit test
for i in range(100):
output = cuda_handle.augment(array_image, order=1)
volOut=sitk.GetImageFromArray(output[0])
sitk.WriteImage( volOut,"data/FLAIR_Elastic.nii.gz", True)
import ipdb; ipdb.set_trace()
# check(correct_ret, output[0])
Can I set which GPU to run on manually, that is so realistic
I am building the code in cuda 9.2 but I got the error
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9220): error: argument of type "const void *" is incompatible with parameter of type "const float *"
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9231): error: argument of type "const void *" is incompatible with parameter of type "const float *"
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9244): error: argument of type "const void *" is incompatible with parameter of type "const double *"
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9255): error: argument of type "const void *" is incompatible with parameter of type "const double *"
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9268): error: argument of type "const void *" is incompatible with parameter of type "const float *"
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9279): error: argument of type "const void *" is incompatible with parameter of type "const float *"
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9292): error: argument of type "const void *" is incompatible with parameter of type "const double *"
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9220): error: argument of type "const void *" is incompatible with parameter of type "const float *"
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9303): error: argument of type "const void *" is incompatible with parameter of type "const double *"
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9231): error: argument of type "const void *" is incompatible with parameter of type "const float *"
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9316): error: argument of type "const void *" is incompatible with parameter of type "const int *"
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9244): error: argument of type "const void *" is incompatible with parameter of type "const double *"
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9327): error: argument of type "const void *" is incompatible with parameter of type "const int *"
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9255): error: argument of type "const void *" is incompatible with parameter of type "const double *"
/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512fintrin.h(9340): error: argument of type "const void *" is incompatible with parameter of type "const long long *"
How to fix it? Thanks
I am a novice using cmake, and am stuck on the install.
The doc says:
cd cuda_backend
cmake -D CUDA_TOOLKIT_ROOT_DIR=/path/to/cuda .
make -j8
I have installed cmake, I cd to the directory I have cloned this project (.\cuda_spatial_deform\cuda_backend), but in the second step when I replace CUDA_TOOLKIT_ROOT_DIR with path to the version of CUDA I have installed (11.2)
cmake -D CUDA_TOOLKIT_ROOT_DIR=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2
I get the following errors:
CMake Warning:
Ignoring extra path from command line:
Toolkit\CUDA\v11.2
CMake Error: The source directory "./cuda_spatial_deform/cuda_backend/Files/NVIDIA" does not exist.
Specify --help for usage, or press the help button on the CMake GUI.
Any help would be greatly appreciated!
您好,
工程写的真好,考虑出个教程或者博客仔细讲解一下每部分的实现吗?
不胜感激!
Does the library support applying the same transformation (elastic) to different 2D slices? Because we want to apply the same transformation to each slice of 3D data.
Thank you so much for your contribution!
My codes:
import os
import SimpleITK as sitk
from PIL import Image
import numpy as np
from batchgenerators.utilities.file_and_folder_operations import join, subfolders, isfile
from cuda_backend.py_api import Handle
import deform
`
def test_3D():
filenames = get_list_of_files('/home/xwl/Data/aug')
for files in filenames:
if 'LGG' in files[0]:
for file in files:
print(file)
sp = file.split('.nii.gz')
sitk_image = sitk.ReadImage(file)
array_image = sitk.GetArrayFromImage(sitk_image).copy()
array_image = array_image.astype(np.float32)
cuda_handle = Handle(array_image.shape, mode="constant")
# cuda_handle.test()
# cuda_handle.scale(1.2)
np.random.seed(0)
p = np.array([0.8, 0.2])
is_flipx = np.random.choice([False, True], p=p.ravel())
is_flipy = np.random.choice([False, True], p=p.ravel())
is_flipz = np.random.choice([False, True], p=p.ravel())
cuda_handle.flip(do_y=is_flipx, do_x=is_flipy, do_z=is_flipz)
# cuda_handle.translate(100, 100, 20)
# cuda_handle.rotate(0.75 * np.pi, 0.75 * np.pi, 0.75 * np.pi)
cuda_handle.elastic(sigma=5., alpha=200., mode='constant')
cuda_handle.end_flag()
# correct_ret = deform.spatial_augment(array_image, mode="mirror")
# Warm up and Unit test
# for i in range(100):
# if 'seg' not in sp[0].split('/')[-1].split('_'):
# output = cuda_handle.augment(array_image, order=1)
# else:
# output = cuda_handle.augment(array_image, order=0)
output = cuda_handle.augment(array_image, order=0)
volOut = sitk.GetImageFromArray(output[0])
id = sp[0].split('/')[-2]
dirname = "/home/xwl/Data/training/preprocessed/pre_" + id
if not os.path.exists(dirname):
os.makedirs(dirname)
print("dir create ok")
if os.path.exists(dirname):
sitk.WriteImage(volOut, dirname + "/" + "pre_" + file.split('/')[-1], True)
def get_list_of_files(base_dir):
"""
returns a list of lists containing the filenames. The outer list contains all training examples. Each entry in the
outer list is again a list pointing to the files of that training example in the following order:
T1, T1c, T2, FLAIR, segmentation
:param base_dir:
:return:
"""
list_of_lists = []
for glioma_type in ['LGG']: # ['HGG', 'LGG']:
current_directory = join(base_dir, glioma_type)
patients = subfolders(current_directory, join=False)
for p in patients:
patient_directory = join(current_directory, p)
t1_file = join(patient_directory, p + "_t1.nii.gz")
t1c_file = join(patient_directory, p + "_t1ce.nii.gz")
t2_file = join(patient_directory, p + "_t2.nii.gz")
flair_file = join(patient_directory, p + "_flair.nii.gz")
seg_file = join(patient_directory, p + "_seg.nii.gz")
this_case = [t1_file, t1c_file, t2_file, flair_file, seg_file]
assert all((isfile(i) for i in this_case)), "some file is missing for patient %s; make sure the following "
"files are there: %s" % (p, str(this_case))
list_of_lists.append(this_case)
print("Found %d patients" % len(list_of_lists))
return list_of_lists
test_3D()
`
and second problem:
after runing the above codes, we can get the below errors:
CUDA error at /home/xwl/cuda_spatial_deform/cuda_backend/kernel/utils.cu:97 code=2(cudaErrorMemoryAllocation) "cudaMalloc((void **)&random, coords_size * sizeof(float))"
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.