4-CNN-demo-0602-SeNet_block.py


import tensorflow as tf

"""
实现Senet block模块
"""

def Se_block(inputs_tensor, name, reduction=8):
    """
    :param inputs_tensor:  4-D tensor  [N, H, W, C]
    :param reduction:
    :return:
    """
    with tf.variable_scope(name):
        _, H, W, C = inputs_tensor.get_shape()

        # 1、挤压
        x_mean = tf.reduce_mean(inputs_tensor, axis=[1, 2], keepdims=True)
        # [N, 1, 1, C]

        # 2、激励
        x = tf.layers.conv2d(
            x_mean, C // reduction, kernel_size=1, strides=1, padding='valid', activation=tf.nn.relu,
            name='se1'
        )  # [N, 1, 1, C/r]
        x = tf.layers.conv2d(
            x, C, kernel_size=1, strides=1, padding='valid', activation=tf.nn.sigmoid, name='se2'
        )  # [N, 1, 1, C]

        # 3、特征重标定。
        y = tf.multiply(inputs_tensor, x)
    return y


def test():
    data = tf.ones(shape=[2, 64, 64, 128], dtype=tf.float32)
    se_out = Se_block(data, name='se_block', reduction=8)
    print(se_out)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print(sess.run(se_out))


if __name__ == '__main__':
    test()
D:\Anaconda\python.exe D:/AI20/HJZ/04-深度学习/3-CNN/20191215__AI20_CNN/06_SeNet_block.py
Tensor("se_block/Mul:0", shape=(2, 64, 64, 128), dtype=float32)
2020-01-31 12:30:18.151070: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
[[[[0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   ...
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]]

  [[0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   ...
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]]

  [[0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   ...
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]]

  ...

  [[0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   ...
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]]

  [[0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   ...
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]]

  [[0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   ...
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]]]


 [[[0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   ...
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]]

  [[0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   ...
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]]

  [[0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   ...
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]]

  ...

  [[0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   ...
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]]

  [[0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   ...
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]]

  [[0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   ...
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]
   [0.41282308 0.34352598 0.33103785 ... 0.43650055 0.45205465
    0.61846423]]]]

Process finished with exit code 0

你可能感兴趣的:(4-CNN-demo-0602-SeNet_block.py)