'''
Model.py
含3层卷积池化层,2层全连接层,激活函数ReLU,采用dropout和softmax函数做分类器
2018/7/18完成搭建,2018/7/19实现文件调用,2018/7/22修改网络结构
-------copyright@GCN-------
'''
import tensorflow as tf
'''
在TensorFlow的世界里,变量的定义和初始化是分开的,所有关于图变量的赋值和计算都要通过tf.Session的run来进行。
想要将所有图变量进行集体初始化时应该使用tf.global_variables_initializer
tf.placehold与tf.Variable的区别:
tf.placehold 占位符
主要为真实输入数据和输出标签的输入, 用于在 feed_dict中的变量,不需要指定初始值,具体值在feed_dict中的变量给出。
tf.Variable 主要用于定义weights bias等可训练会改变的变量,必须指定初始值。
通过Variable()构造函数后,此variable的类型和形状固定不能修改了,但值可以用assign方法修改。
tf.get_variable和tf.Variable函数差别
相同点:通过两函数创建变量的过程基本一样,
tf.variable函数调用时提供的维度(shape)信息以及初始化方法(initializer)的参数和tf.Variable函数调用时提供的初始化过程中的参数基本类似。
不同点:两函数指定变量名称的参数不同,
对于tf.Variable函数,变量名称是一个可选的参数,通过name="v"的形式给出
tf.get_variable函数,变量名称是一个必填的参数,它会根据变量名称去创建或者获取变量
'''
# 函数申明
def weight_variable(shape, n):
# tf.truncated_normal(shape, mean, stddev)这个函数产生正态分布,均值和标准差自己设定。
# shape表示生成张量的维度,mean是均值
# stddev是标准差,,默认最大为1,最小为-1,均值为0
initial = tf.truncated_normal(shape, stddev=n, dtype=tf.float32)
return initial
def bias_variable(shape):
# 创建一个结构为shape矩阵也可以说是数组shape声明其行列,初始化所有值为0.1
initial = tf.constant(0.1, shape=shape, dtype=tf.float32)
return initial
def conv2d(x, W):
# 卷积遍历各方向步数为1,SAME:边缘外自动补0,遍历相乘
# padding 一般只有两个值
# 卷积层后输出图像大小为:(W+2P-f)/stride+1并向下取整
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x, name):
# 池化卷积结果(conv2d)池化层采用kernel大小为3*3,步数也为2,SAME:周围补0,取最大值。数据量缩小了4倍
# x 是 CNN 第一步卷积的输出量,其shape必须为[batch, height, weight, channels];
# ksize 是池化窗口的大小, shape为[batch, height, weight, channels]
# stride 步长,一般是[1,stride, stride,1]
# 池化层输出图像的大小为(W-f)/stride+1,向上取整
return tf.nn.max_pool(x, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)
# 一个简单的卷积神经网络,卷积+池化层 x2,全连接层x2,最后一个softmax层做分类。
# 64个3x3的卷积核(3通道),padding=’SAME’,表示padding后卷积的图与原图尺寸一致,激活函数relu()
def inference(images, batch_size, n_classes):
# 搭建网络
# 第一层卷积
# 第一二参数值得卷积核尺寸大小,即patch;第三个参数是通道数;第四个是卷积核个数
with tf.variable_scope('conv1') as scope:
# 所谓名字的scope,指当绑定了一个名字到一个对象的时候,该名字在程序文本中的可见范围
w_conv1 = tf.Variable(weight_variable([3, 3, 3, 64], 1.0), name='weights', dtype=tf.float32)
b_conv1 = tf.Variable(bias_variable([64]), name='biases', dtype=tf.float32) # 64个偏置值
# tf.nn.bias_add 是 tf.add 的一个特例:tf.add(tf.matmul(x, w), b) == tf.matmul(x, w) + b
# h_conv1 = tf.nn.relu(tf.nn.bias_add(conv2d(images, w_conv1), b_conv1), name=scope.name)
h_conv1 = tf.nn.relu(conv2d(images, w_conv1)+b_conv1, name='conv1') # 得到128*128*64(假设原始图像是128*128)
# 第一层池化
# 3x3最大池化,步长strides为2,池化后执行lrn()操作,局部响应归一化,增强了模型的泛化能力。
# tf.nn.lrn(input,depth_radius=None,bias=None,alpha=None,beta=None,name=None)
with tf.variable_scope('pooling1_lrn') as scope:
pool1 = max_pool_2x2(h_conv1, 'pooling1') # 得到64*64*64
norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1')
# 第二层卷积
# 32个3x3的卷积核(16通道),padding=’SAME’,表示padding后卷积的图与原图尺寸一致,激活函数relu()
with tf.variable_scope('conv2') as scope:
w_conv2 = tf.Variable(weight_variable([3, 3, 64, 32], 0.1), name='weights', dtype=tf.float32)
b_conv2 = tf.Variable(bias_variable([32]), name='biases', dtype=tf.float32) # 32个偏置值
h_conv2 = tf.nn.relu(conv2d(norm1, w_conv2)+b_conv2, name='conv2') # 得到64*64*32
# 第二层池化
# 3x3最大池化,步长strides为2,池化后执行lrn()操作
with tf.variable_scope('pooling2_lrn') as scope:
pool2 = max_pool_2x2(h_conv2, 'pooling2') # 得到32*32*32
norm2 = tf.nn.lrn(pool2, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2')
# 第三层卷积
# 16个3x3的卷积核(16通道),padding=’SAME’,表示padding后卷积的图与原图尺寸一致,激活函数relu()
with tf.variable_scope('conv3') as scope:
w_conv3 = tf.Variable(weight_variable([3, 3, 32, 16], 0.1), name='weights', dtype=tf.float32)
b_conv3 = tf.Variable(bias_variable([16]), name='biases', dtype=tf.float32) # 16个偏置值
h_conv3 = tf.nn.relu(conv2d(norm2, w_conv3)+b_conv3, name='conv3') # 得到32*32*16
# 第三层池化
# 3x3最大池化,步长strides为2,池化后执行lrn()操作
with tf.variable_scope('pooling3_lrn') as scope:
pool3 = max_pool_2x2(h_conv3, 'pooling3') # 得到16*16*16
norm3 = tf.nn.lrn(pool3, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm3')
# 第四层全连接层
# 128个神经元,将之前pool层的输出reshape成一行,激活函数relu()
with tf.variable_scope('local3') as scope:
reshape = tf.reshape(norm3, shape=[batch_size, -1])
dim = reshape.get_shape()[1].value
w_fc1 = tf.Variable(weight_variable([dim, 128], 0.005), name='weights', dtype=tf.float32)
b_fc1 = tf.Variable(bias_variable([128]), name='biases', dtype=tf.float32)
h_fc1 = tf.nn.relu(tf.matmul(reshape, w_fc1) + b_fc1, name=scope.name)
# 第五层全连接层
# 128个神经元,激活函数relu()
with tf.variable_scope('local4') as scope:
w_fc2 = tf.Variable(weight_variable([128 ,128], 0.005),name='weights', dtype=tf.float32)
b_fc2 = tf.Variable(bias_variable([128]), name='biases', dtype=tf.float32)
h_fc2 = tf.nn.relu(tf.matmul(h_fc1, w_fc2) + b_fc1, name=scope.name)
# 对卷积结果执行dropout操作
# keep_prob = tf.placeholder(tf.float32)
h_fc2_dropout = tf.nn.dropout(h_fc2, 0.5)
# tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None)
# 第二个参数keep_prob: 设置神经元被选中的概率,在初始化时keep_prob是一个占位符
# Softmax回归层
# 将前面的FC层输出,做一个线性回归,计算出每一类的得分,在这里是2类,所以这个层输出的是两个得分。
with tf.variable_scope('softmax_linear') as scope:
weights = tf.Variable(weight_variable([128, n_classes], 0.005), name='softmax_linear', dtype=tf.float32)
biases = tf.Variable(bias_var
没有合适的资源?快使用搜索试试~ 我知道了~
Tensorflow实现一个简单的CNN
共914个文件
jpg:911个
py:3个
需积分: 2 17 下载量 129 浏览量
2018-07-26
17:07:50
上传
评论
收藏 5.74MB RAR 举报
温馨提示
Tensorflow实现一个简单的CNN,含3层卷积池化层,2层全连接层,激活函数ReLU,采用dropout和softmax函数做分类器
资源推荐
资源详情
资源评论
收起资源包目录
Tensorflow实现一个简单的CNN (914个子文件)
5931.jpg 8KB
5933.jpg 8KB
2723.jpg 8KB
2724.jpg 8KB
5934.jpg 8KB
2721.jpg 8KB
5731.jpg 8KB
2731.jpg 8KB
5734.jpg 8KB
5733.jpg 8KB
2733.jpg 8KB
2734.jpg 8KB
2701.jpg 8KB
2703.jpg 8KB
2704.jpg 8KB
3011.jpg 7KB
3013.jpg 7KB
3014.jpg 7KB
963.jpg 7KB
5743.jpg 7KB
2744.jpg 7KB
961.jpg 7KB
5833.jpg 7KB
5891.jpg 7KB
2741.jpg 7KB
5831.jpg 7KB
5834.jpg 7KB
1081.jpg 7KB
2743.jpg 7KB
5744.jpg 7KB
3783.jpg 7KB
5723.jpg 7KB
964.jpg 7KB
5741.jpg 7KB
3781.jpg 7KB
5893.jpg 7KB
5921.jpg 7KB
2653.jpg 7KB
5721.jpg 7KB
1031.jpg 7KB
1083.jpg 7KB
5724.jpg 7KB
3784.jpg 7KB
5894.jpg 7KB
1033.jpg 7KB
2651.jpg 7KB
1034.jpg 7KB
5521.jpg 7KB
3791.jpg 7KB
3583.jpg 7KB
5473.jpg 7KB
1831.jpg 7KB
5474.jpg 7KB
2654.jpg 7KB
5923.jpg 7KB
5471.jpg 7KB
971.jpg 7KB
5844.jpg 7KB
1581.jpg 7KB
4191.jpg 7KB
1213.jpg 7KB
1363.jpg 7KB
5801.jpg 7KB
2253.jpg 7KB
5924.jpg 7KB
3794.jpg 7KB
1214.jpg 7KB
1084.jpg 7KB
2534.jpg 7KB
1331.jpg 7KB
5524.jpg 7KB
5843.jpg 7KB
5511.jpg 7KB
1364.jpg 7KB
2251.jpg 7KB
3793.jpg 7KB
2531.jpg 7KB
381.jpg 7KB
2533.jpg 7KB
5813.jpg 7KB
383.jpg 7KB
5803.jpg 7KB
1361.jpg 7KB
3981.jpg 7KB
5804.jpg 7KB
5523.jpg 7KB
5841.jpg 7KB
991.jpg 7KB
1693.jpg 7KB
1583.jpg 7KB
3213.jpg 7KB
5814.jpg 7KB
3581.jpg 7KB
2254.jpg 7KB
1151.jpg 7KB
2851.jpg 7KB
5513.jpg 7KB
1334.jpg 7KB
1834.jpg 7KB
1041.jpg 7KB
共 914 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
故沉
- 粉丝: 2736
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功