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()
x_mean = tf.reduce_mean(inputs_tensor, axis=[1, 2], keepdims=True)
x = tf.layers.conv2d(
x_mean, C // reduction, kernel_size=1, strides=1, padding='valid', activation=tf.nn.relu,
name='se1'
)
x = tf.layers.conv2d(
x, C, kernel_size=1, strides=1, padding='valid', activation=tf.nn.sigmoid, name='se2'
)
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