def test_localNetUpSampleResnetBlock(): """ Test the layer.LocalNetUpSampleResnetBlock class, its default attributes and its call() function. """ batch_size = 5 channels = 4 input_size = (32, 32, 16) output_size = (64, 64, 32) nonskip_tensor_size = (batch_size, ) + input_size + (channels, ) skip_tensor_size = (batch_size, ) + output_size + (channels, ) # Test __init__() and build() model = layer.LocalNetUpSampleResnetBlock(8) model.build([nonskip_tensor_size, skip_tensor_size]) assert model._filters == 8 assert model._use_additive_upsampling is True assert isinstance(model._deconv3d_block, type(layer.Deconv3dBlock(8))) assert isinstance(model._additive_upsampling, type(layer.AdditiveUpSampling(output_size))) assert isinstance(model._conv3d_block, type(layer.Conv3dBlock(8))) assert isinstance(model._residual_block, type(layer.LocalNetResidual3dBlock(8)))
def test_init_LocalNet(): """ Testing init of LocalNet as expected """ local_test = loc.LocalNet( image_size=[1, 2, 3], out_channels=3, num_channel_initial=3, extract_levels=[1, 2, 3], out_kernel_initializer="he_normal", out_activation="softmax", ) # Asserting initialised var for extract_levels is the same - Pass assert local_test._extract_levels == [1, 2, 3] # Asserting initialised var for extract_max_level is the same - Pass assert local_test._extract_max_level == 3 # Asserting initialised var for extract_min_level is the same - Pass assert local_test._extract_min_level == 1 # Assert downsample blocks type is correct, Pass assert all( isinstance(item, type(layer.DownSampleResnetBlock(12))) for item in local_test._downsample_blocks ) # Assert number of downsample blocks is correct (== max level), Pass assert len(local_test._downsample_blocks) == 3 # Assert upsample blocks type is correct, Pass assert all( isinstance(item, type(layer.LocalNetUpSampleResnetBlock(12))) for item in local_test._upsample_blocks ) # Assert number of upsample blocks is correct (== max level - min level), Pass assert len(local_test._upsample_blocks) == 3 - 1 # Assert upsample blocks type is correct, Pass assert all( isinstance(item, type(layer.Conv3dWithResize(12, filters=3))) for item in local_test._extract_layers ) # Assert number of upsample blocks is correct (== extract_levels), Pass assert len(local_test._extract_layers) == 3
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 it is decoded gradually, j from level E to D some of the decoded level are used for generating extractions so extract_levels are between [0, E] with E = max(extract_levels) and D = min(extract_levels) :param out_channels: number of channels for the extractions :param num_channel_initial: :param extract_levels: :param out_kernel_initializer: :param out_activation: :param kwargs: """ super(LocalNet, self).__init__(**kwargs) # save parameters self._extract_levels = extract_levels self._extract_max_level = max(self._extract_levels) # E self._extract_min_level = min(self._extract_levels) # D # init layer variables nc = [num_channel_initial * (2 ** level) for level in range(self._extract_max_level + 1)] # level 0 to E self._downsample_blocks = [layer.DownSampleResnetBlock(filters=nc[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=nc[-1]) # level E self._upsample_blocks = [layer.LocalNetUpSampleResnetBlock(nc[level]) for level in range(self._extract_max_level - 1, self._extract_min_level - 1, -1)] # level D to E-1 self._extract_layers = [ # if kernels are not initialized by zeros, with init NN, extract may be too large layer.Conv3dWithResize(output_shape=image_size, filters=out_channels, kernel_initializer=out_kernel_initializer, activation=out_activation) for _ in self._extract_levels]
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 = "LocalNet", **kwargs, ): """ Image is encoded gradually, i from level 0 to E, then it is decoded gradually, j from level E to D. Some of the decoded levels are used for generating extractions. So, extract_levels are between [0, E] with E = max(extract_levels), and D = min(extract_levels). :param image_size: such as (dim1, dim2, dim3) :param out_channels: number of channels for the extractions :param num_channel_initial: number of initial channels. :param extract_levels: number of extraction levels. :param out_kernel_initializer: initializer to use for kernels. :param out_activation: activation to use at end layer. :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 self._extract_levels = extract_levels self._extract_max_level = max(self._extract_levels) # E self._extract_min_level = min(self._extract_levels) # D # 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._upsample_blocks = [ layer.LocalNetUpSampleResnetBlock(num_channels[level]) for level in range(self._extract_max_level - 1, self._extract_min_level - 1, -1) ] # level D to E-1 self._extract_layers = [ # if kernels are not initialized by zeros, with init NN, extract may be too large layer.Conv3dWithResize( output_shape=image_size, filters=out_channels, kernel_initializer=out_kernel_initializer, activation=out_activation, ) for _ in self._extract_levels ]
def __init__( self, image_size: tuple, out_channels: int, num_channel_initial: int, extract_levels: List[int], out_kernel_initializer: str, out_activation: str, control_points: (tuple, None) = None, **kwargs, ): """ Image is encoded gradually, i from level 0 to E, then it is decoded gradually, j from level E to D. Some of the decoded levels are used for generating extractions. So, extract_levels are between [0, E] with E = max(extract_levels), and D = min(extract_levels). :param image_size: tuple, such as (dim1, dim2, dim3) :param out_channels: int, number of channels for the extractions :param num_channel_initial: int, number of initial channels. :param extract_levels: list of int, number of extraction levels. :param out_kernel_initializer: str, initializer to use for kernels. :param out_activation: str, activation to use at end layer. :param control_points: (tuple, None), specify the distance between control points (in voxels). :param kwargs: """ super(LocalNet, self).__init__(**kwargs) # save parameters self._extract_levels = extract_levels self._extract_max_level = max(self._extract_levels) # E self._extract_min_level = min(self._extract_levels) # D # 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._upsample_blocks = [ layer.LocalNetUpSampleResnetBlock(num_channels[level]) for level in range(self._extract_max_level - 1, self._extract_min_level - 1, -1) ] # level D to E-1 self._extract_layers = [ # if kernels are not initialized by zeros, with init NN, extract may be too large layer.Conv3dWithResize( output_shape=image_size, filters=out_channels, kernel_initializer=out_kernel_initializer, activation=out_activation, ) for _ in self._extract_levels ] self.resize = (layer.ResizeCPTransform(control_points) if control_points is not None else False) self.interpolate = (layer.BSplines3DTransform(control_points, image_size) if control_points is not None else False)