Skip to content

Commit

Permalink
Modified the tests as a workaround the TF #29439 issue and modified t…
Browse files Browse the repository at this point in the history
…he crf.py and misc_io.py to work with TF 1.13.1 and 1.14.0rc0.
  • Loading branch information
danieltudosiu committed Jun 19, 2019
1 parent 3a4d545 commit b691a92
Show file tree
Hide file tree
Showing 107 changed files with 639 additions and 521 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from niftynet.io.image_reader import ImageReader
from niftynet.io.image_sets_partitioner import ImageSetsPartitioner
from niftynet.utilities.util_common import ParserNamespace
from tests.niftynet_testcase import NiftyNetTestCase


### utility function for testing purposes
Expand Down Expand Up @@ -192,7 +193,7 @@ def get_dynamic_window_reader():
return reader


class SelectiveSamplerTest(tf.test.TestCase):
class SelectiveSamplerTest(NiftyNetTestCase):
def test_3d_init(self):
constraint_built = Constraint(compulsory_labels=[1],
min_ratio=0.000001,
Expand All @@ -203,7 +204,7 @@ def test_3d_init(self):
constraint=constraint_built,
windows_per_image=2,
queue_length=10)
with self.test_session() as sess:
with self.cached_session() as sess:
sampler.run_threads(sess, num_threads=1)
out = sess.run(sampler.pop_batch_op())
self.assertTrue(check_constraint(out['label'], constraint_built))
Expand All @@ -218,7 +219,7 @@ def test_3d_init(self):
# batch_size=2,
# windows_per_image=10,
# queue_length=10)
# with self.test_session() as sess:
# with self.cached_session() as sess:
# sampler.run_threads(sess, num_threads=2)
# out = sess.run(sampler.pop_batch_op())
# self.assertAllClose(out['image'].shape, (2, 10, 9, 1))
Expand All @@ -234,7 +235,7 @@ def test_3d_init(self):
# min_num_labels=2),
# windows_per_image=2,
# queue_length=2)
# with self.test_session() as sess:
# with self.cached_session() as sess:
# sampler.run_threads(sess, num_threads=2)
# out = sess.run(sampler.pop_batch_op())
# test = np.zeros_like(out['label'])
Expand Down Expand Up @@ -262,7 +263,7 @@ def test_3d_init(self):
# sampler.close_all()


# class RandomCoordinatesTest(tf.test.TestCase):
# class RandomCoordinatesTest(NiftyNetTestCase):
# def test_coordinates(self):
# coords = rand_choice_coordinates(
# subject_id=1,
Expand Down
1 change: 0 additions & 1 deletion niftynet/engine/handler_early_stopping.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def check_should_stop(performance_history, mode='mean', min_delta=0.03,
dividing.
:param performance_history: a list of size patience with the performance
history
:param patience: see above
:param min_delta: threshold for smoothness
:param kernel_size: hyperparameter for median smoothing
:param k_splits: number of splits if using 'validation_up'
Expand Down
27 changes: 22 additions & 5 deletions niftynet/io/misc_io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
"""Utilities functions for file and path management"""

from __future__ import absolute_import, print_function, unicode_literals

import errno
Expand Down Expand Up @@ -766,9 +765,18 @@ def set_logger(file_name=None):
:return:
"""
# pylint: disable=no-name-in-module
from tensorflow.python.platform.tf_logging import _get_logger
# This is done so if the user had TF 1.12.1 or a new version the code
# does not brake. First part of the try is renaming the TF 1.12.1 to
# fit the TF 1.13.1>= naming scheme, while the second is just a normal
# import for TF 1.13.1>=
try:
# pylint: disable=no-name-in-module
from tensorflow.python.platform.tf_logging import \
_get_logger as get_logger
except ImportError:
from tensorflow.python.platform.tf_logging import get_logger

logger = _get_logger()
logger = get_logger()
tf.logging.set_verbosity(tf.logging.INFO)
logger.handlers = []

Expand All @@ -793,9 +801,18 @@ def close_logger():
:return:
"""
# pylint: disable=no-name-in-module
from tensorflow.python.platform.tf_logging import _get_logger
# This is done so if the user had TF 1.12.1 or a new version the code
# does not brake. First part of the try is renaming the TF 1.12.1 to
# fit the TF 1.13.1>= naming scheme, while the second is just a normal
# import for TF 1.13.1>=
try:
# pylint: disable=no-name-in-module
from tensorflow.python.platform.tf_logging import \
_get_logger as get_logger
except ImportError:
from tensorflow.python.platform.tf_logging import get_logger

logger = _get_logger()
logger = get_logger()
for handler in reversed(logger.handlers):
try:
handler.flush()
Expand Down
50 changes: 38 additions & 12 deletions niftynet/layer/crf.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,44 @@ def _simple_hash(key):
hash_vector = tf.constant(hash_vector, dtype=tf.int64)
return tf.reduce_sum(tf.to_int64(key) * hash_vector, 1)

hash_table = tf.contrib.lookup.MutableDenseHashTable(
tf.int64, tf.int64,
default_value=tf.constant([-1] * n_ch, dtype=tf.int64),
empty_key=-2,
initial_num_buckets=8,
checkpoint=False)
index_table = tf.contrib.lookup.MutableDenseHashTable(
tf.int64, tf.int64,
default_value=0,
empty_key=-1,
initial_num_buckets=8,
checkpoint=False)
# This is done so if the user had TF 1.12.1 or a new version the code
# does not brake. First part of the try is for TF 1.12.1 where the
# deleted_key keyword was missing, while the second is just a normal
# usage for TF 1.13.1>=
try:
hash_table = tf.contrib.lookup.MutableDenseHashTable(
tf.int64, tf.int64,
default_value=tf.constant([-1] * 100, dtype=tf.int64),
empty_key=-2,
initial_num_buckets=8,
checkpoint=False
)
except TypeError:
hash_table = tf.contrib.lookup.MutableDenseHashTable(
tf.int64, tf.int64,
default_value=tf.constant([-1] * n_ch, dtype=tf.int64),
empty_key=-3,
deleted_key=-2,
initial_num_buckets=8,
checkpoint=False
)
try:
index_table = tf.contrib.lookup.MutableDenseHashTable(
tf.int64, tf.int64,
default_value=0,
empty_key=-1,
initial_num_buckets=8,
checkpoint=False
)
except TypeError:
index_table = tf.contrib.lookup.MutableDenseHashTable(
tf.int64, tf.int64,
default_value=0,
empty_key=-2,
deleted_key=-1,
initial_num_buckets=8,
checkpoint=False
)

# canonical simplex (p.4 in [Adams et al 2010])
canonical = \
Expand Down
2 changes: 1 addition & 1 deletion requirements-cpu.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ nibabel>=2.1.0
numpy>=1.13.3, <= 1.14.5
scipy>=0.18
configparser
tensorflow==1.12.2
tensorflow>=1.12.2
pandas
pillow
blinker
Expand Down
2 changes: 1 addition & 1 deletion requirements-gpu.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ nibabel>=2.1.0
numpy>=1.13.3, <= 1.14.5
scipy>=0.18
configparser
tensorflow-gpu==1.12.2
tensorflow-gpu>=1.12.2
pandas
pillow
blinker
Expand Down
14 changes: 7 additions & 7 deletions tests/activation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from tensorflow.contrib.layers.python.layers import regularizers

from niftynet.layer.activation import ActiLayer
from tests.niftynet_testcase import NiftyNetTestCase


class ActivationTest(tf.test.TestCase):
class ActivationTest(NiftyNetTestCase):
def get_3d_input(self):
input_shape = (2, 16, 16, 16, 8)
x = tf.ones(input_shape)
Expand All @@ -26,7 +26,7 @@ def run_test(self, is_3d, type_str, expected_shape):
activation_layer = ActiLayer(func=type_str)
out_acti = activation_layer(x)
print(activation_layer)
with self.test_session() as sess:
with self.cached_session() as sess:
sess.run(tf.global_variables_initializer())
out = sess.run(out_acti)
self.assertAllClose(out.shape, expected_shape)
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_3d_prelu_reg_shape(self):
name='regularized')
out_prelu = prelu_layer(x)
print(prelu_layer)
with self.test_session() as sess:
with self.cached_session() as sess:
sess.run(tf.global_variables_initializer())
out = sess.run(out_prelu)
self.assertAllClose((2, 16, 16, 16, 8), out.shape)
Expand All @@ -76,7 +76,7 @@ def test_3d_dropout_shape(self):
dropout_layer = ActiLayer(func='dropout')
out_dropout = dropout_layer(x, keep_prob=0.8)
print(dropout_layer)
with self.test_session() as sess:
with self.cached_session() as sess:
sess.run(tf.global_variables_initializer())
out = sess.run(out_dropout)
self.assertAllClose((2, 16, 16, 16, 8), out.shape)
Expand Down Expand Up @@ -119,7 +119,7 @@ def test_2d_prelu_reg_shape(self):
name='regularized')
out_prelu = prelu_layer(x)
print(prelu_layer)
with self.test_session() as sess:
with self.cached_session() as sess:
sess.run(tf.global_variables_initializer())
out = sess.run(out_prelu)
self.assertAllClose((2, 16, 16, 8), out.shape)
Expand All @@ -129,7 +129,7 @@ def test_2d_dropout_shape(self):
dropout_layer = ActiLayer(func='dropout')
out_dropout = dropout_layer(x, keep_prob=0.8)
print(dropout_layer)
with self.test_session() as sess:
with self.cached_session() as sess:
sess.run(tf.global_variables_initializer())
out = sess.run(out_dropout)
self.assertAllClose((2, 16, 16, 8), out.shape)
Expand Down
5 changes: 3 additions & 2 deletions tests/additive_upsample_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tensorflow as tf

from niftynet.layer.additive_upsample import AdditiveUpsampleLayer
from tests.niftynet_testcase import NiftyNetTestCase

def get_3d_input():
input_shape = (2, 16, 16, 16, 4)
Expand All @@ -15,7 +16,7 @@ def get_2d_input():
x = tf.ones(input_shape)
return x

class AdditiveUpsampleTest(tf.test.TestCase):
class AdditiveUpsampleTest(NiftyNetTestCase):
def run_test(self, new_size, n_splits, expected_shape, is_3d=True):
if is_3d:
x = get_3d_input()
Expand All @@ -26,7 +27,7 @@ def run_test(self, new_size, n_splits, expected_shape, is_3d=True):
new_size=new_size, n_splits=n_splits)
resized = resize_layer(x)
print(resize_layer)
with self.test_session() as sess:
with self.cached_session() as sess:
out = sess.run(resized)
self.assertAllClose(out.shape, expected_shape)

Expand Down
8 changes: 4 additions & 4 deletions tests/affine_augmentation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import tensorflow as tf

from niftynet.layer.affine_augmentation import AffineAugmentationLayer
from tests.niftynet_testcase import NiftyNetTestCase


class RandRotationTest(tf.test.TestCase):
class RandRotationTest(NiftyNetTestCase):
def get_3d_image(self):
image_shape = (1, 200, 200, 1)
elements = tf.range(np.prod(image_shape), dtype=tf.float32)
Expand All @@ -28,7 +28,7 @@ def test_2d_shape(self):
inverse_augment_layer = augment_layer.inverse()
inverse = inverse_augment_layer(deformed)

with self.test_session() as sess:
with self.cached_session() as sess:
test_out = sess.run([input_tensor, deformed, inverse])
original, deformed_image, resumed_image = test_out
to_compare = resumed_image > 0
Expand All @@ -49,7 +49,7 @@ def test_3d_shape(self):
inverse = inverse_augment_layer(deformed)

# with tf.Session() as sess:
with self.test_session() as sess:
with self.cached_session() as sess:
test_out = sess.run([input_tensor, deformed, inverse])
original, deformed_image, resumed_image = test_out
to_compare = resumed_image > 0
Expand Down
Loading

0 comments on commit b691a92

Please sign in to comment.