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

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
Solved some typo bugs
Renamed a variable to fall in line with ConvLayer
  • Loading branch information
danieltudosiu committed Oct 28, 2019
1 parent abd8e70 commit d3b2bf0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 59 deletions.
35 changes: 20 additions & 15 deletions niftynet/layer/subpixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ class SubPixelLayer(TrainableLayer):
def __init__(
self,
upsample_factor=2,
no_img_channels=1,
n_output_chns=1,
kernel_size=3,
acti_func="tanh",
feature_normalization=None,
group_size=-1,
with_bias=True,
padding="REFLECT",
use_icnr=True,
use_avg=True,
use_icnr=False,
use_avg=False,
w_initializer=None,
w_regularizer=None,
b_initializer=None,
Expand All @@ -76,7 +76,7 @@ def __init__(
):
"""
:param upsample_factor: zoom-factor/image magnification factor
:param no_img_channels: the desired ammount of channels for the
:param n_output_chns: the desired ammount of channels for the
upsampled image
:param kernel_size: the size of the convolutional kernels
:param acti_func: activation function applied to first N - 1 layers
Expand All @@ -93,14 +93,20 @@ def __init__(

if upsample_factor <= 0:
raise ValueError("The upsampling factor must be strictly positive.")
if int(upsample_factor)!=float(upsample_factor) and use_icnr:
raise ValueError("If ICNR initialization is used the sample factor must be an integer")
if int(upsample_factor) != float(upsample_factor) and use_icnr:
raise ValueError(
"If ICNR initialization is used the sample factor must be an integer"
)
if w_initializer is None and use_icnr:
raise ValueError(
"If ICNR initialization is used the weights initializer must be specified"
)

self.upsample_factor = upsample_factor
self.kernel_size = kernel_size
self.acti_func = acti_func
self.use_avg = use_avg
self.no_img_channels = no_img_channels
self.n_output_chns = n_output_chns

self.conv_layer_params = {
"with_bias": with_bias,
Expand All @@ -109,7 +115,7 @@ def __init__(
"padding": padding,
"w_initializer": w_initializer
if not use_icnr
else ICNR(
else _ICNR(
initializer=tf.keras.initializers.get(w_initializer),
upsample_factor=upsample_factor,
),
Expand All @@ -131,8 +137,7 @@ def layer_op(self, lr_image, is_training=True, keep_prob=1.0):
# periodic shuffling
features = ConvolutionalLayer(
n_output_chns=(
self.no_img_channels *
self.upsample_factor ** (len(input_shape) - 1)
self.n_output_chns * self.upsample_factor ** (len(input_shape) - 1)
),
kernel_size=self.kernel_size,
acti_func=None,
Expand Down Expand Up @@ -167,7 +172,7 @@ def layer_op(self, lr_image, is_training=True, keep_prob=1.0):
return sr_image


class ICNR:
class _ICNR:
def __init__(self, initializer, upsample_factor=1):
"""
:param initializer: initializer used for sub kernels (orthogonal, glorot uniform, etc.)
Expand All @@ -182,14 +187,14 @@ def __call__(self, shape, dtype, partition_info=None):
return self.initializer(shape)

# Initializing W0 (enough kernels for one output channel)
new_shape = shape[:-1] + [shape[-1] // (self.upsample_factor ** (len(shape)-2))]
new_shape = shape[:-1] + [
shape[-1] // (self.upsample_factor ** (len(shape) - 2))
]
x = self.initializer(new_shape, dtype, partition_info)

# Repeat the elements along the output dimension
x = tf.keras.backend.repeat_elements(
x=x,
rep=self.upsample_factor ** len(shape)-2,
axis=-1
x=x, rep=self.upsample_factor ** (len(shape) - 2), axis=-1
)

return x
77 changes: 33 additions & 44 deletions tests/subpixel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,84 +7,73 @@
from niftynet.layer.subpixel import SubPixelLayer
from tests.niftynet_testcase import NiftyNetTestCase


class SubPixelTest(NiftyNetTestCase):
"""
Test for niftynet.layer.subpixel.SubPixelLayer.
Mostly adapted from convolution_test.py
"""

def get_3d_input(self):
input_shape = (2, 16, 16, 16, 8)
x_3d = tf.ones(input_shape)
def get_3d_input(self, shape=(2, 16, 16, 16, 8)):
x_3d = tf.ones(shape)
return x_3d

def get_2d_input(self):
input_shape = (2, 16, 16, 8)
x_2d = tf.ones(input_shape)
def get_2d_input(self, shape=(2, 16, 16, 4)):
x_2d = tf.ones(shape)
return x_2d

def _test_subpixel_output_shape(self,
input_data,
param_dict,
output_shape):
layer = SubPixelLayer(**param_dict)
output_data = layer(input_data)
print(layer)
with self.cached_session() as sess:
sess.run(tf.global_variables_initializer())
output_value = sess.run(output_data)
self.assertAllClose(output_shape, output_value.shape)

def _make_output_shape(self, data, upsampling):
data_shape = data.shape.as_list()
output_shape = [data_shape[0]]
output_shape += [upsampling * d for d in data_shape[1:-1]]
output_shape += [data_shape[-1]]

output_shape += [data_shape[-1] // (upsampling ** (len(data_shape) - 2))]
return output_shape

def _test_subpixel_output_shape(self, input_data, param_dict, output_shape):
layer = SubPixelLayer(**param_dict)
output_data = layer(lr_image=input_data, is_training=True, keep_prob=1.0)
with self.cached_session() as sess:
sess.run(tf.global_variables_initializer())
output_value = sess.run(output_data)
self.assertAllClose(output_shape, output_value.shape)

def test_3d_default(self):
data = self.get_3d_input()

output_shape = self._make_output_shape(data, 3)
output_shape = self._make_output_shape(data, 2)

self._test_subpixel_output_shape(data, {}, output_shape)

self._test_subpixel_output_shape(data,
{},
output_shape)
def test_2d_default(self):
data = self.get_2d_input()

output_shape = self._make_output_shape(data, 2)

self._test_subpixel_output_shape(data, {}, output_shape)

def test_3d_bespoke(self):
data = self.get_3d_input()
upsampling = 4
data = self.get_3d_input(
shape=(2, 16, 16, 16, upsampling**3)
)

output_shape = self._make_output_shape(data, upsampling)

params = {'upsample_factor': upsampling,
'layer_configurations': ((6, 32),
(3, 32),
(2, 16),
(4, -1)),
'padding': 'SAME'}
params = {"upsample_factor": upsampling, "kernel_size": 4, "padding": "SAME"}

self._test_subpixel_output_shape(data,
params,
output_shape)
self._test_subpixel_output_shape(data, params, output_shape)

def test_2d_bespoke(self):
data = self.get_2d_input()
upsampling = 6
data = self.get_2d_input(
shape=(2, 16, 16, upsampling ** 2)
)

output_shape = self._make_output_shape(data, upsampling)

params = {'upsample_factor': upsampling,
'layer_configurations': ((6, 32),
(3, 32),
(2, 16),
(4, -1)),
'padding': 'SAME'}
params = {"upsample_factor": upsampling, "kernel_size": 4, "padding": "SAME"}

self._test_subpixel_output_shape(data,
params,
output_shape)
self._test_subpixel_output_shape(data, params, output_shape)


if __name__ == "__main__":
Expand Down

0 comments on commit d3b2bf0

Please sign in to comment.