qsyao / cuda_spatial_deform

A fast tool to do image augmentation on GPU(especially elastic_deform), can be helpful to research on Medical Image.
Other
137 stars 26 forks source link

How to ensure the consistency of the transformation of different modal images and the corresponding label #6

Closed 675492062 closed 4 years ago

675492062 commented 4 years ago

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

if name == "main":

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))"

qsyao commented 4 years ago

Keep the coordinates unchanged, according to the doc.