Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit bf0abcc

Browse files
committed
testing rand bias field
1 parent 4936b1f commit bf0abcc

File tree

3 files changed

+125
-48
lines changed

3 files changed

+125
-48
lines changed

niftynet/layer/rand_bias_field.py

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class RandomBiasFieldLayer(RandomisedLayer):
1414
def __init__(self, name='random_bias_field'):
1515
super(RandomBiasFieldLayer, self).__init__(name=name)
1616
self._bf_coeffs = None
17-
self.min_coeff = None
18-
self.max_coeff = None
19-
self.order = None
17+
self.min_coeff = -10.0
18+
self.max_coeff = 10.0
19+
self.order = 3
2020

2121
def init_uniform_coeff(self, coeff_range=(-10.0, 10.0)):
2222
assert coeff_range[0] < coeff_range[1]
@@ -30,84 +30,81 @@ def randomise(self, spatial_rank=3):
3030
self._generate_bias_field_coeffs(spatial_rank)
3131

3232
def _generate_bias_field_coeffs(self, spatial_rank):
33-
'''
33+
"""
3434
Sampling of the appropriate number of coefficients for the creation
3535
of the bias field map
3636
:param spatial_rank: spatial rank of the image to modify
3737
:return:
38-
'''
38+
"""
3939
rand_coeffs = []
4040
if spatial_rank == 3:
41-
for order_x in range(0, self.order+1):
42-
for order_y in range(0, self.order +1- order_x):
43-
for order_z in range(0, self.order+1 -(order_x + order_y)):
41+
for order_x in range(0, self.order + 1):
42+
for order_y in range(0, self.order + 1 - order_x):
43+
for order_z in range(0,
44+
self.order + 1 - (order_x + order_y)):
4445
rand_coeff_new = np.random.uniform(self.min_coeff,
45-
self.max_coeff)
46+
self.max_coeff)
4647
rand_coeffs.append(rand_coeff_new)
4748
else:
48-
for order_x in range(0, self.order+1):
49-
for order_y in range(0, self.order +1- order_x):
49+
for order_x in range(0, self.order + 1):
50+
for order_y in range(0, self.order + 1 - order_x):
5051
rand_coeff_new = np.random.uniform(self.min_coeff,
5152
self.max_coeff)
5253
rand_coeffs.append(rand_coeff_new)
5354
self._bf_coeffs = rand_coeffs
5455

5556
def _generate_bias_field_map(self, shape):
56-
'''
57+
"""
5758
Create the bias field map using a linear combination polynomial
5859
functions and the coefficients previously sampled
5960
:param shape: shape of the image in order to create the polynomial
60-
functions
61+
functions
6162
:return: bias field map to apply
62-
'''
63+
"""
6364
spatial_rank = len(shape)
6465
x_range = np.arange(-shape[0] / 2, shape[0] / 2)
6566
y_range = np.arange(-shape[1] / 2, shape[1] / 2)
6667
bf_map = np.zeros(shape)
6768
i = 0
6869
if spatial_rank == 3:
6970
z_range = np.arange(-shape[2] / 2, shape[2] / 2)
70-
x_mesh, y_mesh, z_mesh = np.asarray(np.meshgrid(x_range, y_range,
71-
z_range), dtype=float)
71+
x_mesh, y_mesh, z_mesh = np.asarray(
72+
np.meshgrid(x_range, y_range, z_range), dtype=float)
7273
x_mesh /= float(np.max(x_mesh))
7374
y_mesh /= float(np.max(y_mesh))
7475
z_mesh /= float(np.max(z_mesh))
75-
for order_x in range(0, self.order+1):
76-
for order_y in range(0, self.order + 1 - order_x):
77-
for order_z in range(0, self.order + 1 - (order_x+order_y)):
76+
for order_x in range(self.order + 1):
77+
for order_y in range(self.order + 1 - order_x):
78+
for order_z in range(self.order + 1 - (order_x + order_y)):
7879
rand_coeff = self._bf_coeffs[i]
79-
80-
new_map = rand_coeff * np.power(x_mesh, order_x) * \
81-
np.power(y_mesh, order_y) * \
82-
np.power(z_mesh, order_z)
83-
# print(np.asarray(np.where(np.abs(new_map) >
84-
# 0)).shape, np.unique(
85-
# new_map).shape)
80+
new_map = rand_coeff * \
81+
np.power(x_mesh, order_x) * \
82+
np.power(y_mesh, order_y) * \
83+
np.power(z_mesh, order_z)
8684
bf_map += np.transpose(new_map, (1, 0, 2))
8785
i += 1
8886
if spatial_rank == 2:
89-
x_mesh, y_mesh = np.asarray(np.meshgrid(x_range, y_range),
90-
dtype=float)
87+
x_mesh, y_mesh = np.asarray(
88+
np.meshgrid(x_range, y_range), dtype=float)
9189
x_mesh /= np.max(x_mesh)
9290
y_mesh /= np.max(y_mesh)
93-
for order_x in range(0, self.order+1):
94-
for order_y in range(0, self.order+1 - order_x):
91+
for order_x in range(self.order + 1):
92+
for order_y in range(self.order + 1 - order_x):
9593
rand_coeff = self._bf_coeffs[i]
96-
new_map = rand_coeff * np.power(x_mesh, order_x) * \
97-
np.power(y_mesh, order_y)
94+
new_map = rand_coeff * \
95+
np.power(x_mesh, order_x) * \
96+
np.power(y_mesh, order_y)
9897
bf_map += np.transpose(new_map, (1, 0))
9998
i += 1
10099
return np.exp(bf_map)
101100

102101
def _apply_transformation(self, image):
103-
'''
102+
"""
104103
Create the bias field map based on the randomly sampled coefficients
105-
and apply it (
106-
multiplicative) to the image
107-
to augment
108-
:param image image on which to apply the bias field augmentation:
104+
and apply it (multiplicative) to the image to augment
105+
:param image: image on which to apply the bias field augmentation
109106
:return: modified image
110-
'''
107+
"""
111108
assert self._bf_coeffs is not None
112109
bf_map = self._generate_bias_field_map(image.shape)
113110
bf_image = image * bf_map
@@ -117,20 +114,15 @@ def layer_op(self, inputs, interp_orders, *args, **kwargs):
117114
if inputs is None:
118115
return inputs
119116
for (field, image) in inputs.items():
120-
print(field)
121117
if field == 'image':
122118
for mod_i in range(image.shape[-1]):
123119
if image.ndim == 4:
124120
inputs[field][..., mod_i] = \
125-
self._apply_transformation(
126-
image[..., mod_i])
121+
self._apply_transformation(image[..., mod_i])
127122
elif image.ndim == 5:
128123
for t in range(image.shape[-2]):
129124
inputs[field][..., t, mod_i] = \
130-
self._apply_transformation(
131-
image[..., t, mod_i])
125+
self._apply_transformation(image[..., t, mod_i])
132126
else:
133127
raise NotImplementedError("unknown input format")
134128
return inputs
135-
136-

tests/rand_bias_field_test.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from __future__ import absolute_import, print_function
2+
3+
import numpy as np
4+
import tensorflow as tf
5+
6+
from niftynet.layer.rand_bias_field import RandomBiasFieldLayer
7+
8+
SHAPE_4D = (10, 16, 16, 2)
9+
SHAPE_5D = (10, 32, 32, 8, 1)
10+
11+
12+
class RandDeformationTests(tf.test.TestCase):
13+
# def get_3d_input(self):
14+
# input_3d = {'image': np.random.randn(10, 16, 2)}
15+
# interp_order = {'image': (3,) * 2}
16+
# return input_3d, interp_order
17+
18+
def get_4d_input(self):
19+
input_4d = {'image': np.random.randn(*SHAPE_4D)}
20+
interp_order = {'image': (3,) * 2}
21+
return input_4d, interp_order
22+
23+
def get_5d_input(self):
24+
input_5d = {'image': np.random.randn(*SHAPE_5D)}
25+
interp_order = {'image': (3,)}
26+
return input_5d, interp_order
27+
28+
def test_4d_shape(self):
29+
x, interp_orders = self.get_4d_input()
30+
rand_bias_field_layer = RandomBiasFieldLayer()
31+
rand_bias_field_layer.randomise()
32+
out = rand_bias_field_layer(x, interp_orders)
33+
self.assertEqual(out['image'].shape, SHAPE_4D)
34+
35+
def test_5d_shape(self):
36+
x, interp_orders = self.get_5d_input()
37+
rand_bias_field_layer = RandomBiasFieldLayer()
38+
rand_bias_field_layer.randomise()
39+
out = rand_bias_field_layer(x, interp_orders)
40+
self.assertEqual(out['image'].shape, SHAPE_5D)
41+
42+
def test_augmentation(self):
43+
rand_bias_field_layer = RandomBiasFieldLayer()
44+
x, interp_orders = self.get_5d_input()
45+
x_old = np.copy(x['image'])
46+
for _ in range(10):
47+
x['image'] = np.copy(x_old)
48+
49+
rand_bias_field_layer.randomise()
50+
out = rand_bias_field_layer(x, interp_orders)
51+
out = np.copy(out['image'])
52+
53+
rand_bias_field_layer.init_order(2)
54+
rand_bias_field_layer.init_uniform_coeff((-5.0, 5.0))
55+
56+
rand_bias_field_layer.randomise()
57+
x['image'] = np.copy(x_old)
58+
out2 = rand_bias_field_layer(x, interp_orders)
59+
60+
self.assertFalse(np.array_equal(out, x_old))
61+
self.assertFalse(np.array_equal(out2['image'], x_old))
62+
self.assertFalse(np.array_equal(out, out2['image']))
63+
64+
# def test_gugmentation_on_2d_imgs(self):
65+
# rand_bias_field_layer = RandomBiasFieldLayer()
66+
# for _ in range(100):
67+
# x, interp_orders = self.get_3d_input()
68+
# x_old = np.copy(x['image'])
69+
70+
# rand_bias_field_layer.randomise(x)
71+
# out = rand_bias_field_layer(x, interp_orders)
72+
73+
# with self.test_session():
74+
# self.assertFalse(np.array_equal(out['image'], x_old))
75+
76+
77+
if __name__ == "__main__":
78+
tf.test.main()

tests/rand_elastic_deformation_test.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@
77
from niftynet.layer.rand_elastic_deform import RandomElasticDeformationLayer
88
from niftynet.layer.rand_elastic_deform import sitk
99

10+
SHAPE_3D = (10, 16, 2)
11+
SHAPE_4D = (10, 16, 16, 2)
12+
SHAPE_5D = (10, 32, 32, 8, 1)
13+
14+
1015
@unittest.skipIf(not sitk, 'SimpleITK not found')
1116
class RandDeformationTests(tf.test.TestCase):
1217
def get_3d_input(self):
13-
input_3d = {'testdata': np.random.randn(10, 16, 2)}
18+
input_3d = {'testdata': np.random.randn(*SHAPE_3D)}
1419
interp_order = {'testdata': (3,) * 2}
1520
return input_3d, interp_order
1621

1722
def get_4d_input(self):
18-
input_4d = {'testdata': np.random.randn(10, 16, 16, 2)}
23+
input_4d = {'testdata': np.random.randn(*SHAPE_4D)}
1924
interp_order = {'testdata': (3,) * 2}
2025
return input_4d, interp_order
2126

2227
def get_5d_input(self):
23-
input_5d = {'testdata': np.random.randn(10, 32, 32, 8, 1)}
28+
input_5d = {'testdata': np.random.randn(*SHAPE_5D)}
2429
interp_order = {'testdata': (3,)}
2530
return input_5d, interp_order
2631

@@ -31,6 +36,7 @@ def test_4d_shape(self):
3136
proportion_to_augment=0.5)
3237
rand_deformation_layer.randomise(x)
3338
out = rand_deformation_layer(x, interp_orders)
39+
self.assertEqual(out['testdata'].shape, SHAPE_4D)
3440

3541
def test_5d_shape(self):
3642
x, interp_orders = self.get_5d_input()
@@ -39,6 +45,7 @@ def test_5d_shape(self):
3945
proportion_to_augment=0.5)
4046
rand_deformation_layer.randomise(x)
4147
out = rand_deformation_layer(x, interp_orders)
48+
self.assertEqual(out['testdata'].shape, SHAPE_5D)
4249

4350
def test_no_deformation(self):
4451
# testing the 'proportion_to_augment' parameter

0 commit comments

Comments
 (0)