|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import absolute_import, print_function |
| 3 | + |
| 4 | +import tensorflow as tf |
| 5 | + |
| 6 | +from niftynet.layer.base_layer import Layer |
| 7 | +from niftynet.layer.fully_connected import FullyConnectedLayer |
| 8 | +from niftynet.layer.convolution import ConvolutionalLayer |
| 9 | +from niftynet.utilities.util_common import look_up_operations |
| 10 | + |
| 11 | +SUPPORTED_OP = set(['AVG', 'MAX']) |
| 12 | + |
| 13 | + |
| 14 | +class ChannelSELayer(Layer): |
| 15 | + """ |
| 16 | + Re-implementation of Squeeze-and-Excitation (SE) block described in:: |
| 17 | +
|
| 18 | + Hu et al., Squeeze-and-Excitation Networks, arXiv:1709.01507 |
| 19 | + """ |
| 20 | + def __init__(self, |
| 21 | + func='AVG', |
| 22 | + reduction_ratio=16, |
| 23 | + name='channel_squeeze_excitation'): |
| 24 | + self.func = func.upper() |
| 25 | + self.reduction_ratio = reduction_ratio |
| 26 | + super(ChannelSELayer, self).__init__(name=name) |
| 27 | + |
| 28 | + look_up_operations(self.func, SUPPORTED_OP) |
| 29 | + |
| 30 | + def layer_op(self, input_tensor): |
| 31 | + # spatial squeeze |
| 32 | + input_rank = len(input_tensor.shape) |
| 33 | + reduce_indices = list(range(input_rank))[1:-1] |
| 34 | + if self.func == 'AVG': |
| 35 | + squeeze_tensor = tf.reduce_mean(input_tensor, axis=reduce_indices) |
| 36 | + elif self.func == 'MAX': |
| 37 | + squeeze_tensor = tf.reduce_max(input_tensor, axis=reduce_indices) |
| 38 | + else: |
| 39 | + raise NotImplementedError("pooling function not supported") |
| 40 | + |
| 41 | + # channel excitation |
| 42 | + num_channels = int(squeeze_tensor.shape[-1]) |
| 43 | + reduction_ratio = self.reduction_ratio |
| 44 | + if num_channels % reduction_ratio != 0: |
| 45 | + raise ValueError( |
| 46 | + "reduction ratio incompatible with " |
| 47 | + "number of input tensor channels") |
| 48 | + |
| 49 | + num_channels_reduced = num_channels / reduction_ratio |
| 50 | + fc1 = FullyConnectedLayer(num_channels_reduced, |
| 51 | + with_bias=False, |
| 52 | + with_bn=False, |
| 53 | + acti_func='relu', |
| 54 | + name='se_fc_1') |
| 55 | + fc2 = FullyConnectedLayer(num_channels, |
| 56 | + with_bias=False, |
| 57 | + with_bn=False, |
| 58 | + acti_func='sigmoid', |
| 59 | + name='se_fc_2') |
| 60 | + |
| 61 | + fc_out_1 = fc1(squeeze_tensor) |
| 62 | + fc_out_2 = fc2(fc_out_1) |
| 63 | + |
| 64 | + while len(fc_out_2.shape) < input_rank: |
| 65 | + fc_out_2 = tf.expand_dims(fc_out_2, axis=1) |
| 66 | + |
| 67 | + output_tensor = tf.multiply(input_tensor, fc_out_2) |
| 68 | + |
| 69 | + return output_tensor |
| 70 | + |
| 71 | + |
| 72 | +class SpatialSELayer(Layer): |
| 73 | + """ |
| 74 | + Re-implementation of SE block -- squeezing spatially |
| 75 | + and exciting channel-wise described in:: |
| 76 | +
|
| 77 | + Roy et al., Concurrent Spatial and Channel Squeeze & Excitation |
| 78 | + in Fully Convolutional Networks, arXiv:1803.02579 |
| 79 | +
|
| 80 | + """ |
| 81 | + def __init__(self, |
| 82 | + name='spatial_squeeze_excitation'): |
| 83 | + super(SpatialSELayer, self).__init__(name=name) |
| 84 | + |
| 85 | + def layer_op(self, input_tensor): |
| 86 | + # channel squeeze |
| 87 | + conv = ConvolutionalLayer(n_output_chns=1, |
| 88 | + kernel_size=1, |
| 89 | + with_bn=False, |
| 90 | + acti_func='sigmoid', |
| 91 | + name="se_conv") |
| 92 | + |
| 93 | + squeeze_tensor = conv(input_tensor) |
| 94 | + |
| 95 | + # spatial excitation |
| 96 | + output_tensor = tf.multiply(input_tensor, squeeze_tensor) |
| 97 | + |
| 98 | + return output_tensor |
| 99 | + |
| 100 | + |
| 101 | +class ChannelSpatialSELayer(Layer): |
| 102 | + """ |
| 103 | + Re-implementation of concurrent spatial and channel |
| 104 | + squeeze & excitation:: |
| 105 | +
|
| 106 | + Roy et al., Concurrent Spatial and Channel Squeeze & Excitation |
| 107 | + in Fully Convolutional Networks, arXiv:1803.02579 |
| 108 | +
|
| 109 | + """ |
| 110 | + def __init__(self, |
| 111 | + func='AVG', |
| 112 | + reduction_ratio=16, |
| 113 | + name='channel_spatial_squeeze_excitation'): |
| 114 | + self.func = func.upper() |
| 115 | + self.reduction_ratio = reduction_ratio |
| 116 | + super(ChannelSpatialSELayer, self).__init__(name=name) |
| 117 | + |
| 118 | + look_up_operations(self.func, SUPPORTED_OP) |
| 119 | + |
| 120 | + def layer_op(self, input_tensor): |
| 121 | + cSE = ChannelSELayer(func=self.func, |
| 122 | + reduction_ratio=self.reduction_ratio, |
| 123 | + name='cSE') |
| 124 | + sSE = SpatialSELayer(name='sSE') |
| 125 | + |
| 126 | + output_tensor = tf.add(cSE(input_tensor), sSE(input_tensor)) |
| 127 | + |
| 128 | + return output_tensor |
0 commit comments