Skip to content

Commit f24cf72

Browse files
committed
debug
1 parent 0bc2797 commit f24cf72

File tree

2 files changed

+69
-17
lines changed

2 files changed

+69
-17
lines changed

cuda_backend/kernel/utils.cu

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ void Handle::set_2D(size_t y, size_t x){
1717
total_size * sizeof(float)));
1818
checkCudaErrors(cudaMalloc((void **)&output,
1919
total_size * sizeof(float)));
20-
checkCudaErrors(cudaMalloc((void **)&pin_img,
20+
checkCudaErrors(cudaMallocHost((void **)&pin_img,
2121
total_size * sizeof(float)));
22-
checkCudaErrors(cudaMalloc((void **)&pin_output,
22+
checkCudaErrors(cudaMallocHost((void **)&pin_output,
2323
total_size * sizeof(float)));
2424
}
2525

@@ -42,15 +42,14 @@ void Handle::set_3D(size_t z, size_t y, size_t x){
4242
total_size * sizeof(float)));
4343
checkCudaErrors(cudaMalloc((void **)&output,
4444
total_size * sizeof(float)));
45-
checkCudaErrors(cudaMalloc((void **)&pin_img,
45+
checkCudaErrors(cudaMallocHost((void **)&pin_img,
4646
total_size * sizeof(float)));
47-
checkCudaErrors(cudaMalloc((void **)&pin_output,
47+
checkCudaErrors(cudaMallocHost((void **)&pin_output,
4848
total_size * sizeof(float)));
4949
}
50-
}
5150

5251
void Handle::copy_input(float* input){
53-
memcpy(pin_img, img, total_size * sizeof(float));
52+
memcpy(pin_img, input, total_size * sizeof(float));
5453
checkCudaErrors(cudaMemcpyAsync(img, pin_img, total_size * sizeof(float),
5554
cudaMemcpyHostToDevice));
5655
}

deform.py

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,43 @@ def create_zero_centered_coordinate_mesh(shape):
1414
coords[d] -= ((np.array(shape).astype(float)) / 2.)[d]
1515
return coords
1616

17+
def create_matrix_rotation_x_3d(angle, matrix=None):
18+
rotation_x = np.array([[1, 0, 0],
19+
[0, np.cos(angle), -np.sin(angle)],
20+
[0, np.sin(angle), np.cos(angle)]])
21+
if matrix is None:
22+
return rotation_x
23+
24+
return np.dot(matrix, rotation_x)
25+
26+
27+
def create_matrix_rotation_y_3d(angle, matrix=None):
28+
rotation_y = np.array([[np.cos(angle), 0, np.sin(angle)],
29+
[0, 1, 0],
30+
[-np.sin(angle), 0, np.cos(angle)]])
31+
if matrix is None:
32+
return rotation_y
33+
34+
return np.dot(matrix, rotation_y)
35+
36+
37+
def create_matrix_rotation_z_3d(angle, matrix=None):
38+
rotation_z = np.array([[np.cos(angle), -np.sin(angle), 0],
39+
[np.sin(angle), np.cos(angle), 0],
40+
[0, 0, 1]])
41+
if matrix is None:
42+
return rotation_z
43+
44+
return np.dot(matrix, rotation_z)
45+
46+
def rotate_coords_3d(coords, angle_x, angle_y, angle_z):
47+
rot_matrix = np.identity(len(coords))
48+
rot_matrix = create_matrix_rotation_x_3d(angle_x, rot_matrix)
49+
rot_matrix = create_matrix_rotation_y_3d(angle_y, rot_matrix)
50+
rot_matrix = create_matrix_rotation_z_3d(angle_z, rot_matrix)
51+
coords = np.dot(coords.reshape(len(coords), -1).transpose(), rot_matrix).transpose().reshape(coords.shape)
52+
return coords
53+
1754
def elastic_deform_coordinates(coordinates, alpha, sigma):
1855
n_dim = len(coordinates)
1956
offsets = []
@@ -37,21 +74,37 @@ def elastic_deform_coordinates(coordinates, alpha, sigma):
3774
start = time.time()
3875
coords = create_zero_centered_coordinate_mesh(array_image.shape)
3976

40-
# alpha=(0., 1000.)
41-
# sigma=(10., 13.)
42-
# a = np.random.uniform(alpha[0], alpha[1])
43-
# s = np.random.uniform(sigma[0], sigma[1])
44-
# coords = elastic_deform_coordinates(coords, a, s)
45-
46-
47-
for d in range(len(array_image.shape)):
48-
ctr = int(np.round(array_image.shape[d] / 2.))
49-
coords[d] += ctr
77+
alpha=(0., 1000.)
78+
sigma=(10., 13.)
79+
a = np.random.uniform(alpha[0], alpha[1])
80+
s = np.random.uniform(sigma[0], sigma[1])
81+
coords = elastic_deform_coordinates(coords, a, s)
82+
83+
# angle_x=(0, 2 * np.pi)
84+
# angle_y=(0, 2 * np.pi)
85+
# angle_z=(0, 2 * np.pi)
86+
# if angle_x[0] == angle_x[1]:
87+
# a_x = angle_x[0]
88+
# else:
89+
# a_x = np.random.uniform(angle_x[0], angle_x[1])
90+
# if angle_y[0] == angle_y[1]:
91+
# a_y = angle_y[0]
92+
# else:
93+
# a_y = np.random.uniform(angle_y[0], angle_y[1])
94+
# if angle_z[0] == angle_z[1]:
95+
# a_z = angle_z[0]
96+
# else:
97+
# a_z = np.random.uniform(angle_z[0], angle_z[1])
98+
# coords = rotate_coords_3d(coords, a_x, a_y, a_z)
99+
100+
# for d in range(len(array_image.shape)):
101+
# ctr = int(np.round(array_image.shape[d] / 2.))
102+
# coords[d] += ctr
50103
e_time = time.time()
51104
elastic_time += e_time - start
52105

53106
ret = np.zeros_like(array_image)
54-
map_coordinates(ret.astype(float), coords, order=3, mode='mirror').astype(ret.dtype)
107+
map_coordinates(ret.astype(float), coords, order=1, mode='mirror').astype(ret.dtype)
55108
m_time = time.time()
56109
map_coordinates_time += m_time - e_time
57110

0 commit comments

Comments
 (0)