500字范文,内容丰富有趣,生活中的好帮手!
500字范文 > tensorflow实现卷积神经网络——经典网络(LeNet5 AlexNet VGG-16 ResNet)

tensorflow实现卷积神经网络——经典网络(LeNet5 AlexNet VGG-16 ResNet)

时间:2022-07-01 07:00:58

相关推荐

tensorflow实现卷积神经网络——经典网络(LeNet5 AlexNet VGG-16 ResNet)

网络介绍:

/loveliuzz/article/details/79131131

/jiaoyangwm/article/details/80011656

N---LeNet5

/happyorg/article/details/78274066

/chizi15/p/9808330.html

/Alvin_FZW/article/details/81240247

import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_dataimport time# 声明输入图片数据,类别x = tf.placeholder('float', [None, 784])y_ = tf.placeholder('float', [None, 10])# 输入图片数据转化x_image = tf.reshape(x, [-1, 28, 28, 1])#第一层卷积层,初始化卷积核参数、偏置值,该卷积层5*5大小,一个通道,共有6个不同卷积核filter1 = tf.Variable(tf.truncated_normal([5, 5, 1, 6]))bias1 = tf.Variable(tf.truncated_normal([6]))conv1 = tf.nn.conv2d(x_image, filter1, strides=[1, 1, 1, 1], padding='SAME')h_conv1 = tf.nn.sigmoid(conv1 + bias1)#池化层maxPool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')#第二层卷积filter2 = tf.Variable(tf.truncated_normal([5, 5, 6, 16]))bias2 = tf.Variable(tf.truncated_normal([16]))conv2 = tf.nn.conv2d(maxPool1, filter2, strides=[1, 1, 1, 1], padding='SAME')h_conv2 = tf.nn.sigmoid(conv2 + bias2)#池化层maxPool2 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')#全连接层1:本质是卷积,由于尺寸跟内核一样,跟全连接没区别,因此也叫全连接层filter3 = tf.Variable(tf.truncated_normal([5, 5, 16, 120]))bias3 = tf.Variable(tf.truncated_normal([120]))conv3 = tf.nn.conv2d(maxPool2, filter3, strides=[1, 1, 1, 1], padding='SAME')h_conv3 = tf.nn.sigmoid(conv3 + bias3)# 全连接层2# 权值参数W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 120, 80]))# 偏置值b_fc1 = tf.Variable(tf.truncated_normal([80]))# 将卷积的产出展开h_pool2_flat = tf.reshape(h_conv3, [-1, 7 * 7 * 120])# 神经网络计算,并添加sigmoid激活函数h_fc1 = tf.nn.sigmoid(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)# 输出层3,使用softmax进行多分类W_fc2 = tf.Variable(tf.truncated_normal([80, 10]))b_fc2 = tf.Variable(tf.truncated_normal([10]))y_conv = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)# 损失函数cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))# 使用GDO优化算法来调整参数train_step = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy)sess = tf.InteractiveSession()# 测试正确率correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))# 所有变量进行初始化sess.run(tf.initialize_all_variables())# 获取mnist数据mnist_data_set = input_data.read_data_sets('mnist', one_hot=True)# 进行训练start_time = time.time()for i in range(2000):# 获取训练数据batch_xs, batch_ys = mnist_data_set.train.next_batch(200)# 每迭代10个 batch,对当前训练数据进行测试,输出当前预测准确率if i % 10 == 0:train_accuracy = accuracy.eval(feed_dict={x: batch_xs, y_: batch_ys})print("step %d, training accuracy %g" % (i, train_accuracy))# 计算间隔时间end_time = time.time()print('time: ', (end_time - start_time))start_time = end_time# 训练数据train_step.run(feed_dict={x: batch_xs, y_: batch_ys})# 关闭会话sess.close()

N---AlexNet

/u012679707/article/details/80793916

/qq_24695385/article/details/80368618

/qq_28123095/article/details/79776329

# -*- coding:utf-8 -*-"""@author:Lisa@file:alexNet.py@function:实现Alexnet深度模型@note:learn from《tensorflow实战》@time:/6/24 0024下午 5:26"""import tensorflow as tfimport timeimport mathfrom datetime import datetimebatch_size=32num_batch=100keep_prob=0.5def print_architecture(t):"""print the architecture information of the network,include name and size"""print(t.op.name," ",t.get_shape().as_list())def inference(images):""" 构建网络 :5个conv+3个FC"""parameters=[] #储存参数with tf.name_scope('conv1') as scope:"""images:227*227*3kernel: 11*11 *64stride:4*4padding:name#通过with tf.name_scope('conv1') as scope可以将scope内生成的Variable自动命名为conv1/xxx便于区分不同卷积层的组建input: images[227*227*3]middle: conv1[55*55*96]output: pool1 [27*27*96]"""kernel=tf.Variable(tf.truncated_normal([11,11,3,96],dtype=tf.float32,stddev=0.1),name="weights")conv=tf.nn.conv2d(images,kernel,[1,4,4,1],padding='SAME')biases=tf.Variable(tf.constant(0.0, shape=[96], dtype=tf.float32),trainable=True,name="biases")bias=tf.nn.bias_add(conv,biases) # w*x+bconv1=tf.nn.relu(bias,name=scope) # reLuprint_architecture(conv1)parameters +=[kernel,biases]#添加LRN层和max_pool层"""LRN会让前馈、反馈的速度大大降低(下降1/3),但最终效果不明显,所以只有ALEXNET用LRN,其他模型都放弃了"""lrn1=tf.nn.lrn(conv1,depth_radius=4,bias=1,alpha=0.001/9,beta=0.75,name="lrn1")pool1=tf.nn.max_pool(lrn1,ksize=[1,3,3,1],strides=[1,2,2,1],padding="VALID",name="pool1")print_architecture(pool1)with tf.name_scope('conv2') as scope:"""input: pool1[27*27*96]middle: conv2[27*27*256]output: pool2 [13*13*256]"""kernel = tf.Variable(tf.truncated_normal([5, 5, 96, 256],dtype=tf.float32, stddev=0.1), name="weights")conv = tf.nn.conv2d(pool1, kernel, [1, 1, 1, 1], padding='SAME')biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32),trainable=True, name="biases")bias = tf.nn.bias_add(conv, biases) # w*x+bconv2 = tf.nn.relu(bias, name=scope) # reLuparameters += [kernel, biases]# 添加LRN层和max_pool层"""LRN会让前馈、反馈的速度大大降低(下降1/3),但最终效果不明显,所以只有ALEXNET用LRN,其他模型都放弃了"""lrn2 = tf.nn.lrn(conv2, depth_radius=4, bias=1, alpha=0.001 / 9, beta=0.75, name="lrn1")pool2 = tf.nn.max_pool(lrn2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],padding="VALID", name="pool2")print_architecture(pool2)with tf.name_scope('conv3') as scope:"""input: pool2[13*13*256]output: conv3 [13*13*384]"""kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 384],dtype=tf.float32, stddev=0.1), name="weights")conv = tf.nn.conv2d(pool2, kernel, [1, 1, 1, 1], padding='SAME')biases = tf.Variable(tf.constant(0.0, shape=[384], dtype=tf.float32),trainable=True, name="biases")bias = tf.nn.bias_add(conv, biases) # w*x+bconv3 = tf.nn.relu(bias, name=scope) # reLuparameters += [kernel, biases]print_architecture(conv3)with tf.name_scope('conv4') as scope:"""input: conv3[13*13*384]output: conv4 [13*13*384]"""kernel = tf.Variable(tf.truncated_normal([3, 3, 384, 384],dtype=tf.float32, stddev=0.1), name="weights")conv = tf.nn.conv2d(conv3, kernel, [1, 1, 1, 1], padding='SAME')biases = tf.Variable(tf.constant(0.0, shape=[384], dtype=tf.float32),trainable=True, name="biases")bias = tf.nn.bias_add(conv, biases) # w*x+bconv4 = tf.nn.relu(bias, name=scope) # reLuparameters += [kernel, biases]print_architecture(conv4)with tf.name_scope('conv5') as scope:"""input: conv4[13*13*384]output: conv5 [6*6*256]"""kernel = tf.Variable(tf.truncated_normal([3, 3, 384, 256],dtype=tf.float32, stddev=0.1), name="weights")conv = tf.nn.conv2d(conv4, kernel, [1, 1, 1, 1], padding='SAME')biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32),trainable=True, name="biases")bias = tf.nn.bias_add(conv, biases) # w*x+bconv5 = tf.nn.relu(bias, name=scope) # reLupool5 = tf.nn.max_pool(conv5, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],padding="VALID", name="pool5")parameters += [kernel, biases]print_architecture(pool5)#全连接层6with tf.name_scope('fc6') as scope:"""input:pool5 [6*6*256]output:fc6 [4096]"""kernel = tf.Variable(tf.truncated_normal([6*6*256,4096],dtype=tf.float32, stddev=0.1), name="weights")biases = tf.Variable(tf.constant(0.0, shape=[4096], dtype=tf.float32),trainable=True, name="biases")# 输入数据变换flat = tf.reshape(pool5, [-1, 6*6*256] ) # 整形成m*n,列n为7*7*64# 进行全连接操作fc = tf.nn.relu(tf.matmul(flat, kernel) + biases,name='fc6')# 防止过拟合 nn.dropoutfc6 = tf.nn.dropout(fc, keep_prob)parameters += [kernel, biases]print_architecture(fc6)# 全连接层7with tf.name_scope('fc7') as scope:"""input:fc6 [4096]output:fc7 [4096]"""kernel = tf.Variable(tf.truncated_normal([4096, 4096],dtype=tf.float32, stddev=0.1), name="weights")biases = tf.Variable(tf.constant(0.0, shape=[4096], dtype=tf.float32),trainable=True, name="biases")# 进行全连接操作fc = tf.nn.relu(tf.matmul(fc6, kernel) + biases, name='fc7')# 防止过拟合 nn.dropoutfc7 = tf.nn.dropout(fc, keep_prob)parameters += [kernel, biases]print_architecture(fc7)# 全连接层8with tf.name_scope('fc8') as scope:"""input:fc7 [4096]output:fc8 [1000]"""kernel = tf.Variable(tf.truncated_normal([4096, 1000],dtype=tf.float32, stddev=0.1), name="weights")biases = tf.Variable(tf.constant(0.0, shape=[1000], dtype=tf.float32),trainable=True, name="biases")# 进行全连接操作fc8 = tf.nn.xw_plus_b(fc7, kernel, biases, name='fc8')parameters += [kernel, biases]print_architecture(fc8)return fc8,parametersdef time_compute(session,target,info_string):num_step_burn_in=10 #预热轮数,头几轮迭代有显存加载、cache命中等问题可以因此跳过total_duration=0.0 #总时间total_duration_squared=0.0for i in range(num_batch+num_step_burn_in):start_time=time.time()_ = session.run(target)duration= time.time() -start_timeif i>= num_step_burn_in:if i%10==0: #每迭代10次显示一次durationprint("%s: step %d,duration=%.5f "% (datetime.now(),i-num_step_burn_in,duration))total_duration += durationtotal_duration_squared += duration *durationtime_mean=total_duration /num_batchtime_variance=total_duration_squared / num_batch - time_mean*time_meantime_stddev=math.sqrt(time_variance)#迭代完成,输出print("%s: %s across %d steps,%.3f +/- %.3f sec per batch "%(datetime.now(),info_string,num_batch,time_mean,time_stddev))def main():with tf.Graph().as_default():"""仅使用随机图片数据 测试前馈和反馈计算的耗时"""image_size =224images=tf.Variable(tf.random_normal([batch_size,image_size,image_size,3],dtype=tf.float32,stddev=0.1 ) )fc8,parameters=inference(images)init=tf.global_variables_initializer()sess=tf.Session()sess.run(init)"""AlexNet forward 计算的测评传入的target:fc8(即最后一层的输出)优化目标:loss使用tf.gradients求相对于loss的所有模型参数的梯度AlexNet Backward 计算的测评target:grad"""time_compute(sess,target=fc8,info_string="Forward")obj=tf.nn.l2_loss(fc8)grad=tf.gradients(obj,parameters)(sess,grad,"Forward-backward")if __name__=="__main__":main()

N---VGG-16

/u012679707/article/details/80807406

/gybheroin/article/details/79903044

/gybheroin/article/details/79806096

from datetime import datetimeimport mathimport timeimport tensorflow as tfbatch_size = 16 #一个批次的数据num_batches = 100 #测试一百个批次的数据'''卷积层创建函数,并将本层参数存入参数列表input_op:输入的tensor name:这一层的名称 kh:kernel height即卷积核的高 kw:kernel width即卷积核的宽n_out:卷积核数量即输出通道数 dh:步长的高dw:步长的宽p:参数列表'''def conv_op(input_op, name, kh, kw, n_out, dh, dw, p):# 获取输入数据的通道数n_in = input_op.get_shape()[-1].valuewith tf.name_scope(name) as scope:#创建卷积核,shape的值的意义参见alexNetkernel = tf.get_variable(scope+"w", shape=[kh, kw, n_in, n_out], dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer_conv2d())#卷积操作conv = tf.nn.conv2d(input_op, kernel, (1, dh, dw, 1), padding='SAME')#初始化bias为0bias_init_val = tf.constant(0.0, shape=[n_out], dtype=tf.float32)biases = tf.Variable(bias_init_val, trainable=True, name='b')#将卷积后结果与biases加起来z = tf.nn.bias_add(conv, biases)#使用激活函数relu进行非线性处理activation = tf.nn.relu(z, name=scope)#将卷积核和biases加入到参数列表p += [kernel, biases]# tf.image.resize_images()#卷积层输出作为函数结果返回return activation'''全连接层FC创建函数'''def fc_op(input_op, name, n_out, p):#获取input_op的通道数n_in = input_op.get_shape()[-1].valuewith tf.name_scope(name) as scope:#初始化全连接层权重kernel = tf.get_variable(scope+"w", shape=[n_in, n_out], dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer())#初始化biases为0.1而不为0,避免dead neuronbiases = tf.Variable(tf.constant(0.1, shape=[n_out], dtype=tf.float32), name='b')#Computes Relu(x * weight + biases)activation = tf.nn.relu_layer(input_op, kernel, biases, name=scope)#将权重和biases加入到参数列表p += [kernel, biases]#activation作为函数结果返回return activation'''最大池化层创建函数'''def mpool_op(input_op, name, kh, kw, dh, dw):return tf.nn.max_pool(input_op,ksize=[1, kh, kw, 1], #池化窗口大小strides=[1, dh, dw, 1], #池化步长padding='SAME',name= name)'''创建VGGNet-16-D的网络结构input_op为输入数据,keep_prob为控制dropoout比率的一个placeholder'''def inference_op(input_op, keep_prob):p = []'''D-第一段'''#第一段卷积网络第一个卷积层,输出尺寸224*224*64,卷积后通道数(厚度)由3变为64conv1_1 = conv_op(input_op, name="conv1_1", kh=3, kw=3, n_out=64, dh=1, dw=1, p=p)#第一段卷积网络第二个卷积层,输出尺寸224*224*64conv1_2 = conv_op(conv1_1, name="conv1_2", kh=3, kw=3, n_out=64, dh=1, dw=1, p=p)#第一段卷积网络的最大池化层,经过池化后输出尺寸变为112*112*64pool1 = mpool_op(conv1_2, name="pool1", kh=2, kw=2, dw=2, dh=2)'''D-第二段'''#第二段卷积网络第一个卷积层,输出尺寸112*112*128,卷积后通道数由64变为128conv2_1 = conv_op(pool1, name="conv2_1", kh=3, kw=3, n_out=128, dh=1, dw=1, p=p)#第二段卷积网络第二个卷积层,输出尺寸112*112*128conv2_2 = conv_op(conv2_1, name="conv2_1", kh=3, kw=3, n_out=128, dh=1, dw=1, p=p)#第二段卷积网络的最大池化层,经过池化后输出尺寸变为56*56*128pool2 = mpool_op(conv2_2, name="pool2", kh=2, kw=2, dw=2, dh=2)'''D-第三段'''#第三段卷积网络第一个卷积层,输出尺寸为56*56*256,卷积后通道数由128变为256conv3_1 = conv_op(pool2, name="conv3_1", kh=3, kw=3, n_out=256, dh=1, dw=1, p=p)#第三段卷积网络第二个卷积层,输出尺寸为56*56*256conv3_2 = conv_op(conv3_1, name="conv3_2", kh=3, kw=3, n_out=256, dh=1, dw=1, p=p)#第三段卷积网络第三个卷积层,输出尺寸为56*56*256conv3_3 = conv_op(conv3_2, name="conv3_3", kh=3, kw=3, n_out=256, dh=1, dw=1, p=p)#第三段卷积网络的最大池化层,池化后输出尺寸变为28*28*256pool3 = mpool_op(conv3_3, name="pool3", kh=2, kw=2, dh=2, dw=2)'''D-第四段'''#第四段卷积网络第一个卷积层,输出尺寸为28*28*512,卷积后通道数由256变为512conv4_1 = conv_op(pool3, name="conv4_1", kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)# 第四段卷积网络第二个卷积层,输出尺寸为28*28*512conv4_2 = conv_op(conv4_1, name="conv4_2", kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)# 第四段卷积网络第三个卷积层,输出尺寸为28*28*512conv4_3 = conv_op(conv4_2, name="conv4_3", kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)#第四段卷积网络的最大池化层,池化后输出尺寸为14*14*512pool4 = mpool_op(conv4_3, name="pool4", kh=2, kw=2, dh=2, dw=2)'''D-第五段'''#第五段卷积网络第一个卷积层,输出尺寸为14*14*512conv5_1 = conv_op(pool4, name="conv5_1", kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)# 第五段卷积网络第二个卷积层,输出尺寸为14*14*512conv5_2 = conv_op(conv5_1, name="conv5_2", kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)# 第五段卷积网络第三个卷积层,输出尺寸为14*14*512conv5_3 = conv_op(conv5_2, name="conv5_3", kh=3, kw=3, n_out=512, dh=1, dw=1, p=p)#第五段卷积网络的最大池化层,池化后尺寸为7*7*512pool5 = mpool_op(conv5_3, name="conv5_3", kh=2, kw=2, dh=2, dw=2)'''对卷积网络的输出结果进行扁平化,将每个样本化为一个长度为25088的一维向量'''shp = pool5.get_shape()flattened_shape = shp[1].value * shp[2].value * shp[3].value #图像的长、宽、厚度相乘,即7*7*512=25088# -1表示该样本有多少个是自动计算得出的,得到一个矩阵,准备传入全连接层resh1 = tf.reshape(pool5, [-1,flattened_shape], name="resh1")'''全连接层,共三个'''fc6 = fc_op(resh1, name="fc6", n_out=4096, p=p)fc6_drop = tf.nn.dropout(fc6, keep_prob, name="fc6_drop") #dropout层,keep_prob数据待外部传入fc7 = fc_op(fc6_drop, name="fc7", n_out=4096, p=p)fc7_drop = tf.nn.dropout(fc7, keep_prob, name="fc7_drop")#最后一个全连接层fc8 = fc_op(fc7_drop, name="fc8", n_out=1000, p=p)softmax = tf.nn.softmax(fc8) #使用softmax进行处理得到分类输出概率predictions = tf.argmax(softmax,1) #求概率最大的类别#返回参数return predictions, softmax, fc8, p'''评测函数'''def time_tensorflow_run(session, target, feed, info_string): #target:需要评测的运算算字, info_string:测试的名称num_steps_burn_in = 10 #给程序热身,头几轮迭代有显存的加载、cache命中等问题因此可以跳过,我们只考量10轮迭代之后的计算时间total_duration = 0.0 #总时间total_duration_squared = 0.0 #平方和for i in range(num_batches + num_steps_burn_in):start_time = time.time()_ = session.run(target, feed_dict=feed)duration = time.time() - start_timeif i>= num_steps_burn_in: #程序热身完成后,记录时间if not i % 10: #每10轮 显示 当前时间,迭代次数(不包括热身),用时print('%s: step %d, duration = %.3f' % (datetime.now(), i-num_steps_burn_in, duration))# 累加total_duration和total_duration_squaredtotal_duration += durationtotal_duration_squared += duration * duration# 循环结束后,计算每轮迭代的平均耗时mn和标准差sd,最后将结果显示出来mn = total_duration / num_batchesvr = total_duration_squared / num_batches - mn * mnsd = math.sqrt(vr)print('%s: %s across %d steps, %.3f +/- %.3f sec / batch' %(datetime.now(), info_string, num_batches, mn, sd))'''评测的主函数,不使用ImageNet数据集来训练,只使用随机图片测试前馈和反馈计算的耗时'''def run_benchmaek():with tf.Graph().as_default():image_size = 224#利用tf.random_normal()生成随机图片images = tf.Variable(tf.random_normal([batch_size, #每轮迭代的样本数image_size, image_size, #图片的size:image_size x image_size3], #图片的通道数dtype=tf.float32,stddev=1e-1))#创建keep_prob的placeholderkeep_prob = tf.placeholder(tf.float32)predictions, softmax, fc8, p = inference_op(images, keep_prob)#创建Session并初始化全局参数init = tf.global_variables_initializer()sess = tf.Session()sess.run(init)#前向计算测评time_tensorflow_run(sess, predictions, {keep_prob:1.0}, "Forward")#前向和反向计算测评objective = tf.nn.l2_loss(fc8)grad = tf.gradients(objective, p)time_tensorflow_run(sess, grad, {keep_prob:0.5}, "Forward-backward")run_benchmaek()

N---ResNet

/m0_37917271/article/details/82346233

/qq1483661204/article/details/79244051

/p/3fd3df2e1830

/qq_23981335/article/details/103469497

/tensorflow/models/blob/master/research/slim/nets/resnet_v2.py

from datetime import datetimeimport timeimport mathimport collectionsimport tensorflow as tfslim = tf.contrib.slim# 使用collections.namedtuple设计ResNet的Block模块# scope参数是block的名称# unit_fn是功能单元(如残差单元)# args是一个列表,如([256, 64, 1]) X 2 + [256, 64, 2]),代表两个(256, 64, 1)单元# 和一个(256, 64, 2)单元Block = collections.namedtuple("Block", ['scope', 'unit_fn', 'args'])# 定义下采样的方法,通过max_pool2d实现def subsample(inputs, factor, scope=None):if factor == 1:return inputselse:return slim.max_pool2d(inputs, [1, 1], stride=factor, scope=scope)# 定义一个创建卷积层的函数def conv2d_same(inputs, num_outputs, kernel_size, stride, scope=None):if stride == 1:return slim.conv2d(inputs, num_outputs, kernel_size, stride=1,padding='SAME', scope=scope)else:pad_total = kernel_size - 1pad_beg = pad_total // 2pad_end = pad_total - pad_beginputs = tf.pad(inputs, [[0, 0], [pad_beg, pad_end],[pad_beg, pad_end], [0, 0]])return slim.conv2d(inputs, num_outputs, kernel_size, stride=stride,padding='VALID', scope=scope)# 定义堆叠的block函数@slim.add_arg_scopedef stack_blocks_dense(net, blocks, outputs_collections=None):for block in blocks:with tf.variable_scope(block.scope, 'block', [net]) as sc:for i, unit in enumerate(block.args):with tf.variable_scope('unit_%d' % (i + 1), values=[net]):unit_depth, unit_depth_bottleneck, unit_stride = unitnet = block.unit_fn(net,depth=unit_depth,depth_bottleneck=unit_depth_bottleneck,stride=unit_stride)net = slim.utils.collect_named_outputs(outputs_collections, sc.name,net)return net# 用于设定默认值def resnet_arg_scope(is_training=True,weight_decay=0.0001,batch_norm_decay=0.997,batch_norm_epsilon=1e-5,batch_norm_scale=True):batch_norm_params = {'is_training': is_training,'decay': batch_norm_decay,'epsilon': batch_norm_epsilon,'scale': batch_norm_scale,'updates_collections': tf.GraphKeys.UPDATE_OPS,}with slim.arg_scope([slim.conv2d],weights_regularizer=slim.l2_regularizer(weight_decay),weights_initializer=slim.variance_scaling_initializer(),activation_fn=tf.nn.relu,normalizer_fn=slim.batch_norm,normalizer_params=batch_norm_params):with slim.arg_scope([slim.batch_norm], **batch_norm_params):with slim.arg_scope([slim.max_pool2d], padding='SAME') as arg_sc:return arg_sc# 定义残差学习单元@slim.add_arg_scopedef bottleneck(inputs, depth, depth_bottleneck, stride,outputs_collections=None, scope=None):with tf.variable_scope(scope, 'bottleneck_v2', [inputs]) as sc:depth_in = slim.utils.last_dimension(inputs.get_shape(), min_rank=4)preact = slim.batch_norm(inputs, activation_fn=tf.nn.relu,scope='preact')# shortcut为直连的Xif depth == depth_in:shortcut = subsample(inputs, stride, 'shortcut')else:shortcut = slim.conv2d(preact, depth, [1, 1], stride=stride,normalizer_fn=None, activation_fn=None,scope='shortcut')residual = slim.conv2d(preact, depth_bottleneck, [1, 1], stride=1,scope='conv1')residual = conv2d_same(residual, depth_bottleneck, 3, stride,scope='conv2')residual = slim.conv2d(residual, depth, [1, 1], stride=1,normalizer_fn=None, activation_fn=None,scope='conv3')# 将直连的X加到残差上,得到outputoutput = shortcut + residualreturn slim.utils.collect_named_outputs(outputs_collections,sc.name, output)# 定义ResNet的主函数def resnet_v2(inputs,blocks,num_classes=None,global_pool=True,include_root_block=True,reuse=None,scope=None):with tf.variable_scope(scope, 'resnet_v2', [inputs], reuse=reuse) as sc:end_points_collection = sc.original_name_scope + '_end_points'with slim.arg_scope([slim.conv2d, bottleneck,stack_blocks_dense],outputs_collections=end_points_collection):net = inputsif include_root_block:with slim.arg_scope([slim.conv2d], activation_fn=None,normalizer_fn=None):net = conv2d_same(net, 64, 7, stride=2, scope='conv1')net = slim.max_pool2d(net, [3, 3], stride=2, scope='pool1')net = stack_blocks_dense(net, blocks)net = slim.batch_norm(net, activation_fn=tf.nn.relu, scope='postnorm')if global_pool:net = tf.reduce_mean(net, [1, 2], name='pool5', keep_dims=True)if num_classes is not None:net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None,normalizer_fn=None, scope='logits')end_points = slim.utils.convert_collection_to_dict(end_points_collection)if num_classes is not None:end_points['predictions'] = slim.softmax(net, scope='predictions')return net, end_points# 定义50层的ResNetdef resnet_v2_50(inputs,num_classes=None,global_pool=True,reuse=None,scope='resnet_v2_152'):blocks = [Block('block1', bottleneck, [(256, 64, 1)] * 2 + [(256, 64, 2)]),Block('block2', bottleneck, [(512, 128, 1)] * 4 + [(512, 128, 2)]),Block('block3', bottleneck, [(1024, 256, 1)] * 6 + [(1024, 256, 2)]),Block('block4', bottleneck, [(2048, 512, 1)] * 3)]return resnet_v2(inputs, blocks, num_classes, global_pool,include_root_block=True, reuse=reuse, scope=scope)num_batches = 100# 测评性能def time_tensorflow_run(session, target, info_string):num_steps_burn_in = 10total_duration = 0.0total_duration_squared = 0.0for i in range(num_batches + num_steps_burn_in):start_time = time.time()_ = session.run(target)duration = time.time() - start_timeif i >= num_steps_burn_in:if not i % 10:print('%s: step %d, duration = %.3f' %(datetime.now(), i - num_steps_burn_in, duration))total_duration += durationtotal_duration_squared += duration * durationmn = total_duration / num_batchesvr = total_duration_squared / num_batches - mn * mnsd = math.sqrt(vr)print('%s: %s across %d step, %.3f +/- %.3f sec / batch' %(datetime.now(), info_string, num_batches, mn, sd))batch_size = 32height, width = 224, 224inputs = tf.random_uniform([batch_size, height, width, 3])with slim.arg_scope(resnet_arg_scope(is_training=False)):net, end_points = resnet_v2_50(inputs, 1000)init = tf.global_variables_initializer()sess = tf.Session()sess.run(init)num_batches = 100time_tensorflow_run(sess, net, "Forward")

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。