程序项目代做,有需求私信(小程序、网站、爬虫、电路板设计、驱动、应用程序开发、毕设疑难问题处理等)

第十七节,深度学习模型的训练技巧-优化卷积核,多通道卷积

在使用卷积神经网络时,我们也总结了一些训练技巧,下面就来介绍如何对卷积核进行优化,以及多通道卷积技术的使用。

一 优化卷积核

 在实际的卷积训练中,为了加快速度,常常把卷积核裁开。比如一个3x3的卷积核,可以裁成一个3x1和1x3的卷积核(通过矩阵乘法得知),分别对原有输入做卷积运算,这样可以大大提升运算的速度。

原理:在浮点运算中乘法消耗的资源比较多,我们目的就是尽量减少乘法运算。

  • 比如对一个5x2的原始图片进行一次3x3的SAME卷积,相当于生成的5x2的像素中,每一个像素都需要经历3x3次乘法,那么一共是90次。
  • 同样是这样图片,如果先进行一次3X1的SAME卷积,相当于生成的5x2的像素中,每一个像素都需要经历3x1次乘法,那么一共是30次。再进行一次1x3的SAME卷积也是计算30次,在一起总共60次。

这仅仅是一个很小的数据张量,而且随着张量维度的增大,层数的增多,减少的运算更多。运算量减少了,运算速度会更快。

接下来我会演示一个例子,仍然是改写第十三节对cifar10数据集分类的例子。

复制代码
# -*- coding: utf-8 -*-
"""
Created on Thu May  3 12:29:16 2018

@author: zy
"""

'''
优化卷积核 提高运算速度
'''

'''
建立一个带有全局平均池化层的卷积神经网络  并对CIFAR-10数据集进行分类
1.使用3个卷积层的同卷积操作,滤波器大小为5x5,每个卷积层后面都会跟一个步长为2x2的池化层,滤波器大小为2x2
2.对输出的10个feature map进行全局平均池化,得到10个特征
3.对得到的10个特征进行softmax计算,得到分类
'''

import cifar10_input
import tensorflow as tf
import numpy as np


def weight_variable(shape):
    '''
    初始化权重
    
    args:
        shape:权重shape
    '''
    initial = tf.truncated_normal(shape=shape,mean=0.0,stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    '''
    初始化偏置
    
    args:
        shape:偏置shape
    '''
    initial =tf.constant(0.1,shape=shape)
    return tf.Variable(initial)


def conv2d(x,W):
    '''
    卷积运算 ,使用SAME填充方式   卷积层后
         out_height = in_hight / strides_height(向上取整)
         out_width = in_width / strides_width(向上取整)
    
    args:
        x:输入图像 形状为[batch,in_height,in_width,in_channels] 
        W:权重 形状为[filter_height,filter_width,in_channels,out_channels]        
    '''
    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')


def max_pool_2x2(x):
    '''
    最大池化层,滤波器大小为2x2,'SAME'填充方式  池化层后
         out_height = in_hight / strides_height(向上取整)
         out_width = in_width / strides_width(向上取整)
    
    args:
        x:输入图像 形状为[batch,in_height,in_width,in_channels] 
    '''
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
    

def avg_pool_6x6(x):
    '''
    全局平均池化层,使用一个与原有输入同样尺寸的filter进行池化,'SAME'填充方式  池化层后
         out_height = in_hight / strides_height(向上取整)
         out_width = in_width / strides_width(向上取整)
    
    args;
        x:输入图像 形状为[batch,in_height,in_width,in_channels] 
    '''
    return tf.nn.avg_pool(x,ksize=[1,6,6,1],strides=[1,6,6,1],padding='SAME')

def print_op_shape(t):
    '''
    输出一个操作op节点的形状
    
    args:
        t:必须是一个tensor类型
        t.get_shape()返回一个元组  .as_list()转换为list
    '''
    print(t.op.name,'',t.get_shape().as_list())

'''
一 引入数据集
'''
batch_size = 128
learning_rate = 1e-4
training_step = 15000
display_step = 200
#数据集目录
data_dir = './cifar10_data/cifar-10-batches-bin'
print('begin')
#获取训练集数据
images_train,labels_train = cifar10_input.inputs(eval_data=False,data_dir = data_dir,batch_size=batch_size)
print('begin data')


'''
二 定义网络结构
'''
#定义占位符
input_x = tf.placeholder(dtype=tf.float32,shape=[None,24,24,3])   #图像大小24x24x
input_y = tf.placeholder(dtype=tf.float32,shape=[None,10])        #0-9类别 

x_image = tf.reshape(input_x,[batch_size,24,24,3])

#1.卷积层 ->池化层
W_conv1 = weight_variable([5,5,3,64])
b_conv1 = bias_variable([64])


h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1) + b_conv1)    #输出为[-1,24,24,64]
print_op_shape(h_conv1)
h_pool1 = max_pool_2x2(h_conv1)                            #输出为[-1,12,12,64]
print_op_shape(h_pool1)


#2.卷积层 ->池化层   卷积核做优化
W_conv21 = weight_variable([5,1,64,64])
b_conv21 = bias_variable([64])


h_conv21 = tf.nn.relu(conv2d(h_pool1,W_conv21) + b_conv21)    #输出为[-1,12,12,64]
print_op_shape(h_conv21)

W_conv2 = weight_variable([1,5,64,64])
b_conv2 = bias_variable([64])


h_conv2 = tf.nn.relu(conv2d(h_conv21,W_conv2) + b_conv2)     #输出为[-1,12,12,64]
print_op_shape(h_conv2)

h_pool2 = max_pool_2x2(h_conv2)                              #输出为[-1,6,6,64]
print_op_shape(h_pool2)



#3.卷积层 ->全局平均池化层
W_conv3 = weight_variable([5,5,64,10])
b_conv3 = bias_variable([10])

h_conv3 = tf.nn.relu(conv2d(h_pool2,W_conv3) + b_conv3)   #输出为[-1,6,6,10]
print_op_shape(h_conv3)

nt_hpool3 = avg_pool_6x6(h_conv3)                         #输出为[-1,1,1,10]
print_op_shape(nt_hpool3)
nt_hpool3_flat = tf.reshape(nt_hpool3,[-1,10])            

y_conv = tf.nn.softmax(nt_hpool3_flat)

'''
三 定义求解器
'''

#softmax交叉熵代价函数
cost = tf.reduce_mean(-tf.reduce_sum(input_y * tf.log(y_conv),axis=1))

#求解器
train = tf.train.AdamOptimizer(learning_rate).minimize(cost)

#返回一个准确度的数据
correct_prediction = tf.equal(tf.arg_max(y_conv,1),tf.arg_max(input_y,1))
#准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,dtype=tf.float32))

'''
四 开始训练
'''
sess = tf.Session();
sess.run(tf.global_variables_initializer())
# 启动计算图中所有的队列线程 调用tf.train.start_queue_runners来将文件名填充到队列,否则read操作会被阻塞到文件名队列中有值为止。
tf.train.start_queue_runners(sess=sess)

for step in range(training_step):
    #获取batch_size大小数据集
    image_batch,label_batch = sess.run([images_train,labels_train])
    
    #one hot编码
    label_b = np.eye(10,dtype=np.float32)[label_batch]
    
    #开始训练
    train.run(feed_dict={input_x:image_batch,input_y:label_b},session=sess)
    
    if step % display_step == 0:
        train_accuracy = accuracy.eval(feed_dict={input_x:image_batch,input_y:label_b},session=sess)
        print('Step {0} tranining accuracy {1}'.format(step,train_accuracy))
复制代码

运行结果如下:

 

 

二 多通道卷积技术

多通道卷积技术详细内容可以点击:第十四节,卷积神经网络之经典网络Inception(四)

多通道卷积可以理解为一种新的CNN网络模型,在原有的卷积模型基础上扩展。

亲爱的读者和支持者们,自动博客加入了打赏功能,陆陆续续收到了各位老铁的打赏。在此,我想由衷地感谢每一位对我们博客的支持和打赏。你们的慷慨与支持,是我们前行的动力与源泉。

日期姓名金额
2023-09-06*源19
2023-09-11*朝科88
2023-09-21*号5
2023-09-16*真60
2023-10-26*通9.9
2023-11-04*慎0.66
2023-11-24*恩0.01
2023-12-30I*B1
2024-01-28*兴20
2024-02-01QYing20
2024-02-11*督6
2024-02-18一*x1
2024-02-20c*l18.88
2024-01-01*I5
2024-04-08*程150
2024-04-18*超20
2024-04-26.*V30
2024-05-08D*W5
2024-05-29*辉20
2024-05-30*雄10
2024-06-08*:10
2024-06-23小狮子666
2024-06-28*s6.66
2024-06-29*炼1
2024-06-30*!1
2024-07-08*方20
2024-07-18A*16.66
2024-07-31*北12
2024-08-13*基1
2024-08-23n*s2
2024-09-02*源50
2024-09-04*J2
2024-09-06*强8.8
2024-09-09*波1
2024-09-10*口1
2024-09-10*波1
2024-09-12*波10
2024-09-18*明1.68
2024-09-26B*h10
2024-09-3010
2024-10-02M*i1
2024-10-14*朋10
2024-10-22*海10
2024-10-23*南10
2024-10-26*节6.66
2024-10-27*o5
2024-10-28W*F6.66
2024-10-29R*n6.66
2024-11-02*球6
2024-11-021*鑫6.66
2024-11-25*沙5
2024-11-29C*n2.88
posted @   大奥特曼打小怪兽  阅读(6021)  评论(1编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
如果有任何技术小问题,欢迎大家交流沟通,共同进步

公告 & 打赏

>>

欢迎打赏支持我 ^_^

最新公告

程序项目代做,有需求私信(小程序、网站、爬虫、电路板设计、驱动、应用程序开发、毕设疑难问题处理等)。

了解更多

点击右上角即可分享
微信分享提示