资源简介
构建一个四层神经网络识别手写体数据集MNIST,然后将注意力模块CBAM插入到网络的第一层之后,查看注意力模块的性能。可以改变CBAM模块插入的位置,做到任意插入。
代码片段和文件信息
import warnings
warnings.filterwarnings(‘ignore‘ category=FutureWarning)
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
slim = tf.contrib.slim
#create weights for each layer
def get_weights(shape):
data = tf.truncated_normal(shapestddev=0.1)
return tf.Variable(data)
def get_biases(shape):
data = tf.constant(0.1shape=shape)
return tf.Variable(data)
#2d convolutional function
def convolution_2d(xw):
return tf.nn.conv2d(xwstrides=[1111]padding=‘SAME‘)
#2*2 max pooling
def max_pooling(x):
return tf.nn.max_pool(xksize=[1221]strides=[1221]padding=‘SAME‘)
def combined_static_and_dynamic_shape(tensor):
“““Returns a list containing static and dynamic values for the dimensions. Returns a list of static
and dynamic values for shape dimensions. This is useful to preserve static shapes when available in reshape operation.
Args: tensor: A tensor of any type.
Returns: A list of size tensor.shape.ndims containing integers or a scalar tensor. “““
static_tensor_shape = tensor.shape.as_list()
dynamic_tensor_shape = tf.shape(tensor)
combined_shape = []
for index dim in enumerate(static_tensor_shape):
if dim is not None:
combined_shape.append(dim)
else:
combined_shape.append(dynamic_tensor_shape[index])
return combined_shape
def convolutional_block_attention_module(feature_map index reduction_ratio = 0.5):
“““CBAM:convolutional block attention module
Args:
feature_map:input feature map
index:the index of the module
reduction_ratio:output units number of first MLP layer:reduction_ratio * feature map
Return:
feature map with channel and spatial attention“““
with tf.variable_scope(“cbam_%s“ % (index)):
feature_map_shape = combined_static_and_dynamic_shape(feature_map)
# channel attention module
channel_avg_weights = tf.nn.avg_pool(value=feature_map
ksize=[1 feature_map_shape[1] feature_map_shape[2] 1]
strides=[1 1 1 1]
padding=‘VALID‘) # global average pool
channel_max_weights = tf.nn.max_pool(value=feature_map
ksize=[1 feature_map_shape[1] feature_map_shape[2] 1]
strides=[1 1 1 1]
padding=‘VALID‘)
channel_avg_reshape = tf.reshape(channel_avg_weights
[feature_map_shape[0] 1 feature_map_shape[3]])
channel_max_reshape = tf.reshape(channel_max_weights
[feature_map_shape[0] 1 feature_map_shape[3]])
channel_w_reshape = tf.concat([channel_avg_reshape channel_max_re
相关资源
- TensorFlow 实现 Yolo
- 基于tensorflow的遥感影像分类
- 安装步骤。提取码也在里面
- 神经网络模型python模板
- autoencoder自编码器tensorflow代码
- CNN卷积神经网络TensorFlow代码
- Tensorflow笔记-中国大学全部讲义源代码
- 基于tensorflow的二分类的python实现注释
- tensorflow的ckpt文件转pb模型文件
- tensorflow-C3D-ucf101网络
- lstm_tensorflow
- 《TensorFlow2深度学习》
- TensorFlow Python API documentation.pdf
- Python-使用最新版本的tensorflow实现se
- BP神经网络及代码分析(python+tensorf
- Python-用TensorFlow实现神经网络实体关系
- 莫烦全部代码Reinforcement-learning-with-
- Python-基于TensorFlow和BERT的管道式实体
- Tensorflow gpu_accelerate
- python tensorFlow AND和XOR
- word2vec.py(来自黄文坚的“tensorflow实
- keras+tensorflow CNN
- 基于机器学习框架tensorflow的图像分类
- 机器学习实战:基于 Scikit-Learn 和 T
- 《白话深度学习与TensorFlow》
- Tensorflow+实战Google深度学习框架
- python MNIST分类 tensorflow
- tensorflow下用LSTM网络进行时间序列预测
- 深度学习之二:用Tensorflow实现卷积神
- tensorflow版本的YOLO v3,在Windows系统下
评论
共有 条评论