Skip to content

Commit e56186b

Browse files
committed
doc
1 parent b1f4422 commit e56186b

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ A fast tool to do image augmentation on GPU, can be helpful to research on Medic
1515
- Doing Spation_Defrom by doing calculates on coordinates, all transformations get combined before they are applied to the image
1616
- Implement map_coordinates by linear interpolation.
1717
- Unit test pass when over 99% pixels has L1_loss < 1e-3.
18+
- Flexible with coordinates, users can fetch them from CUDA and do cubic interpolation at CPU by scipy.map_coordinates(order = 3)
1819

1920
## Speed Test
2021
Test on 3D image , shape = [48, 240, 240]
@@ -39,7 +40,7 @@ make -j8
3940
from cuda_spatial_defrom import Cuda_Spatial_Deform
4041

4142
# Init Handle
42-
cuda_handle = Handle(array_image.shape, mode="constant")
43+
cuda_handle = Cuda_Spatial_Deform(array_image.shape, mode="constant")
4344
'''
4445
Shape: cuda_backend will malloc according to shape
4546
RGB: bool (Only Support 2D-RGB)
@@ -63,6 +64,9 @@ cuda_handle.end_flag()
6364
# The shape must be equal to cuda_handle.shape
6465
array_image = load_np_array(data_pth)
6566
output = cuda_handle.augment(array_image)
67+
# done_list will list the translations actually done
68+
done_list = output[1]
69+
output_array = output[0]
6670
```
6771

6872
## Example_Image

cuda_backend/py_api.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def translate(self, seg_x=0.0, seg_y=0.0, seg_z=0.0, prob=1.0):
234234
def rotate(self, angel_x=0, angle_y=0, angle_z=0, prob=1.0):
235235
self.deform_list.append(Rotate(not self.is_3D, angel_x, angle_y, angle_z, prob))
236236

237-
def elastic(self, sigma, alpha, mode='reflect', c_val=0, truncate=4.0, prob=1.0):
237+
def elastic(self, sigma, alpha, mode='constant', c_val=0, truncate=4.0, prob=1.0):
238238
self.deform_list.append(Elastic(sigma, alpha, mode, c_val, truncate, prob))
239239

240240
def end_flag(self):
@@ -246,7 +246,6 @@ def get_coords(self):
246246
coords = np.ones(coords_shape).astype(np.float32)
247247
check(self.cuda_handle, coords)
248248
# coords = coords.astype(np.int)
249-
import ipdb; ipdb.set_trace()
250249
return coords
251250

252251
def deform_coords(self):

doc/doc.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)