コード例 #1
0
def test_init_GlobalNet():
    """
    Testing init of GlobalNet is built as expected.
    """
    #  Initialising GlobalNet instance
    global_test = g.GlobalNet(
        image_size=[1, 2, 3],
        out_channels=3,
        num_channel_initial=3,
        extract_levels=[1, 2, 3],
        out_kernel_initializer="softmax",
        out_activation="softmax",
    )

    # Asserting initialised var for extract_levels is the same - Pass
    assert global_test._extract_levels == [1, 2, 3]
    # Asserting initialised var for extract_max_level is the same - Pass
    assert global_test._extract_max_level == 3

    # self reference grid
    # assert global_test.reference_grid correct shape, Pass
    assert global_test.reference_grid.shape == [1, 2, 3, 3]
    #  assert correct reference grid returned, Pass
    expected_ref_grid = tf.convert_to_tensor(
        [[
            [[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 2.0]],
            [[0.0, 1.0, 0.0], [0.0, 1.0, 1.0], [0.0, 1.0, 2.0]],
        ]],
        dtype=tf.float32,
    )
    assert is_equal_tf(global_test.reference_grid, expected_ref_grid)

    # Testing constant initializer
    #  We initialize the expected tensor and initialise another from the
    #  class variable using tf.Variable
    test_tensor_return = tf.convert_to_tensor(
        [[1.0, 0.0], [0.0, 0.0], [0.0, 1.0], [0.0, 0.0], [0.0, 0.0],
         [1.0, 0.0]],
        dtype=tf.float32,
    )
    global_return = tf.Variable(
        global_test.transform_initial(shape=[6, 2], dtype=tf.float32))

    # Asserting they are equal - Pass
    assert is_equal_tf(test_tensor_return,
                       tf.convert_to_tensor(global_return, dtype=tf.float32))

    # Assert downsample blocks type is correct, Pass
    assert all(
        isinstance(item, type(layer.DownSampleResnetBlock(12)))
        for item in global_test._downsample_blocks)
    #  Assert number of downsample blocks is correct (== max level), Pass
    assert len(global_test._downsample_blocks) == 3

    #  Assert conv3dBlock type is correct, Pass
    assert isinstance(global_test._conv3d_block, type(layer.Conv3dBlock(12)))

    #  Asserting type is dense_layer, Pass
    assert isinstance(global_test._dense_layer, type(layer.Dense(12)))
コード例 #2
0
ファイル: test_layer.py プロジェクト: knvsmadhav/DeepReg
def test_dense():
    """
    Test the layer.Dense class and its default attributes. No need to test the call() function since a
    concatenation of tensorflow classes
    """
    model = layer.Dense(8)

    assert isinstance(model._flatten, type(tf.keras.layers.Flatten()))
    assert isinstance(model._dense, type(tf.keras.layers.Dense(8)))
コード例 #3
0
def test_dense():
    """
    Test the layer.Dense class and its default attributes.
    concatenation of tensorflow classes
    """
    model = layer.Dense(8)

    assert isinstance(model._flatten, tf.keras.layers.Flatten)
    assert isinstance(model._dense, tf.keras.layers.Dense)
コード例 #4
0
    def __init__(
        self,
        image_size: tuple,
        out_channels: int,
        num_channel_initial: int,
        extract_levels: List[int],
        out_kernel_initializer: str,
        out_activation: str,
        name: str = "GlobalNet",
        **kwargs,
    ):
        """
        Image is encoded gradually, i from level 0 to E.
        Then, a densely-connected layer outputs an affine
        transformation.

        :param image_size: tuple, such as (dim1, dim2, dim3)
        :param out_channels: int, number of channels for the output
        :param num_channel_initial: int, number of initial channels
        :param extract_levels: list, which levels from net to extract
        :param out_kernel_initializer: not used
        :param out_activation: not used
        :param name: name of the backbone.
        :param kwargs: additional arguments.
        """
        super().__init__(
            image_size=image_size,
            out_channels=out_channels,
            num_channel_initial=num_channel_initial,
            out_kernel_initializer=out_kernel_initializer,
            out_activation=out_activation,
            name=name,
            **kwargs,
        )

        # save parameters
        assert out_channels == 3
        self._extract_levels = extract_levels
        self._extract_max_level = max(self._extract_levels)  # E
        self.reference_grid = layer_util.get_reference_grid(image_size)
        self.transform_initial = tf.constant_initializer(
            value=list(np.eye(4, 3).reshape((-1))))
        # init layer variables
        num_channels = [
            num_channel_initial * (2**level)
            for level in range(self._extract_max_level + 1)
        ]  # level 0 to E
        self._downsample_blocks = [
            layer.DownSampleResnetBlock(filters=num_channels[i],
                                        kernel_size=7 if i == 0 else 3)
            for i in range(self._extract_max_level)
        ]  # level 0 to E-1
        self._conv3d_block = layer.Conv3dBlock(
            filters=num_channels[-1])  # level E
        self._dense_layer = layer.Dense(
            units=12, bias_initializer=self.transform_initial)
コード例 #5
0
ファイル: global_net.py プロジェクト: acasamitjana/DeepReg
    def __init__(
        self,
        image_size,
        out_channels,
        num_channel_initial,
        extract_levels,
        out_kernel_initializer,
        out_activation,
        **kwargs,
    ):
        """
        Image is encoded gradually, i from level 0 to E.
        Then, a densely-connected layer outputs an affine
        transformation.

        :param out_channels: int, number of channels for the output
        :param num_channel_initial: int, number of initial channels
        :param extract_levels: list, which levels from net to extract
        :param out_activation: str, activation at last layer
        :param out_kernel_initializer: str, which kernel to use as initialiser
        :param kwargs:
        """
        super(GlobalNet, self).__init__(**kwargs)

        # save parameters
        self._extract_levels = extract_levels
        self._extract_max_level = max(self._extract_levels)  # E
        self.reference_grid = layer_util.get_reference_grid(image_size)
        self.transform_initial = tf.constant_initializer(
            value=[1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0]
        )

        # init layer variables
        num_channels = [
            num_channel_initial * (2 ** level)
            for level in range(self._extract_max_level + 1)
        ]  # level 0 to E
        self._downsample_blocks = [
            layer.DownSampleResnetBlock(
                filters=num_channels[i], kernel_size=7 if i == 0 else 3
            )
            for i in range(self._extract_max_level)
        ]  # level 0 to E-1
        self._conv3d_block = layer.Conv3dBlock(filters=num_channels[-1])  # level E
        self._dense_layer = layer.Dense(
            units=12, bias_initializer=self.transform_initial
        )
        self._reshape = tf.keras.layers.Reshape(target_shape=(4, 3))