|
| 1 | +# Cuda_Spatial_Deform |
| 2 | + |
| 3 | +## How to Use |
| 4 | + |
| 5 | +### CMake |
| 6 | +```shell |
| 7 | +cd cuda_backend |
| 8 | +cmake -D /path/to/cuda . |
| 9 | +make -j8 |
| 10 | +``` |
| 11 | + |
| 12 | +### Set_Config |
| 13 | +```python |
| 14 | +# Import cuda_spation_deform Handle |
| 15 | +from cuda_spatial_defrom import Cuda_Spatial_Deform |
| 16 | + |
| 17 | +# Init Handle |
| 18 | +cuda_handle = Handle(array_image.shape, mode="constant") |
| 19 | +''' |
| 20 | + Shape: cuda_backend will malloc according to shape |
| 21 | + RGB: bool (Only Support 2D-RGB) |
| 22 | + mode: The rules of map_coordinates. Refernce to https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html |
| 23 | + cval: default is 0.0. Only be useful when mode == 'constant' |
| 24 | + id_gpu: choose the number of GPU |
| 25 | +''' |
| 26 | + |
| 27 | +# Choose your Rules of spatial_defrom |
| 28 | + |
| 29 | +# cuda_handle.scale(0.5) |
| 30 | +# cuda_handle.flip(do_y=True, do_x=True, do_z=True) |
| 31 | +# cuda_handle.translate(100, 100, 20) |
| 32 | +# cuda_handle.rotate(0.75 * np.pi, 0.75 * np.pi, 0.75 * np.pi) |
| 33 | +cuda_handle.elastic(sigma=12., alpha=200., mode='constant') |
| 34 | +cuda_handle.end_flag() |
| 35 | +``` |
| 36 | + |
| 37 | +### DO augmentation |
| 38 | +```python |
| 39 | +# The shape must be equal to cuda_handle.shape |
| 40 | +array_image = load_np_array(data_pth) |
| 41 | +output = cuda_handle.augment(array_image) |
| 42 | +# done_list will list the translations actually done |
| 43 | +done_list = output[1] |
| 44 | +output_array = output[0] |
| 45 | +``` |
| 46 | + |
| 47 | +## Class Cuda_Spatial_Deform |
| 48 | +```python |
| 49 | +def __init__(self, shape, RGB=False, mode='constant', cval=0.0, id_gpu=0): |
| 50 | +``` |
| 51 | +- shape: Support: 3D 2D RGB-2D |
| 52 | +- mode: Support: 'constant' 'reflect' 'mirror' 'wrap' 'nearest' |
| 53 | + |
| 54 | +```python |
| 55 | +def scale(self, sc, prob=1.0): |
| 56 | +``` |
| 57 | +- sc : rate of Scale , must in [0, 1] |
| 58 | +- prob : The probability of doing this augmentation |
| 59 | + |
| 60 | +```python |
| 61 | +def translate(self, seg_x=0.0, seg_y=0.0, seg_z=0.0, prob=1.0): |
| 62 | +``` |
| 63 | +- seg_x : coordinates += seg_x when dim == x |
| 64 | +- prob : The probability of doing this augmentation |
| 65 | + |
| 66 | +```python |
| 67 | +def flip(self, do_x=False, do_y=False, do_z=False, prob=1.0): |
| 68 | +``` |
| 69 | +- do_x. etc : bool default is False |
| 70 | +- prob : The probability of doing this augmentation |
| 71 | + |
| 72 | +```python |
| 73 | +def rotate(self, angel_x=0, angle_y=0, angle_z=0, prob=1.0): |
| 74 | +``` |
| 75 | +- angel_x. etc : Clockwise rotation at dim_x with angle_x |
| 76 | +- prob : The probability of doing this augmentation |
| 77 | + |
| 78 | +```python |
| 79 | +def elastic(self, sigma, alpha, mode='constant', c_val=0, truncate=4.0, prob=1.0): |
| 80 | +``` |
| 81 | +- sigma, alpha : coordinates += guassian_fiter(np.random.random(shape) * 2 - 1, sigma, truncate) * alpha |
| 82 | +- mode : Rules of padding , only support 'mirror' and 'constant' |
| 83 | +- prob : The probability of doing this augmentation |
| 84 | + |
| 85 | +```python |
| 86 | +def end_flag(self): |
| 87 | +``` |
| 88 | +Must be added at the end of translations. |
| 89 | + |
| 90 | +```python |
| 91 | +def get_coords(self): |
| 92 | +``` |
| 93 | +Return the coodinates. Can be used as cubic interpolation with scipy.map_coordinates. |
| 94 | + |
| 95 | +```python |
| 96 | +def reset(self): |
| 97 | +``` |
| 98 | +Call to redifine the translations. |
| 99 | + |
| 100 | +```python |
| 101 | +def deform_coords(self): |
| 102 | +``` |
| 103 | +Only calculates on coordinates. |
| 104 | + |
| 105 | +```python |
| 106 | +def augment(self, img): |
| 107 | +``` |
| 108 | +- assert(img.shape == self.shape) |
| 109 | +- return the list of output_array and done_list |
0 commit comments