资源简介
构建一个四层神经网络识别手写体数据集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
相关资源
- DeepLabV3-Tensorflow-master
- 基于TensorFlow实现CNN文本分类实验指导
- tensorflow2.0 yolo3目标检测算法
- tensorflow制作自己的灰度图像数据集并
- anaconda下安装tensorflow(注:不同版本
- 北京大学曹健老师-人工智能实践:
- Deep Learning With Python - Jason Brownlee
- Python-自然场景文本检测PSENet的一个
- Python-高效准确的EAST文本检测器的一个
- Python-TensorFlow弱监督图像分割
- Python-基于tensorflow实现的用textcnn方法
- Python-subpixel利用Tensorflow的一个子像素
- 【官方文档】TensorFlow Python API docume
-
tensorflow画风迁移代码 st
yle transfer - 简单粗暴 TensorFlow
- [PDF] Reinforcement Learning With Open AI Tens
- tensorflow目标检测代码
- 基于Python的手写字体识别系统
- 基于Tensorflow的人脸识别源码
- python TensorFlow 官方文档中文版
- Python-在TensorFlow中实现实现图像卷积网
- tensorflow-1.9.0-cp37-cp37m-win_amd64.whl
- Faster-RCNN-TensorFlow-Python3.5-master
- 聊天机器人tensorflow
- caffe模型转化为tensorflow模型
- Python-一个非常简单的BiLSTMCRF模型用于
- Python-Tensorflow仿AlphaGo框架实现的AI围棋
- Mask R-CNN源码(TensorFlow版本)
- 基于python3 tensorflow DBN_and_RNN的实现
- tensorflow-0.8.0-cp34-cp34m-linux_x86_64.whl
评论
共有 条评论