tensorflow卷积神经Inception V3网络结构代码解析

目录
  • 前言
  • 1 非Inception Module的普通卷积层
  • 2 三个Inception模块组
  • 3 Auxiliary Logits、全局平均池化、Softmax分类

前言

学习了Inception V3卷积神经网络,总结一下对Inception V3网络结构和主要代码的理解。

GoogLeNet对网络中的传统卷积层进行了修改,提出了被称为 Inception 的结构,用于增加网络深度和宽度,提高深度神经网络性能。从Inception V1到Inception V4有4个更新版本,每一版的网络在原来的基础上进行改进,提高网络性能。本文介绍Inception V3的网络结构和主要代码。

1 非Inception Module的普通卷积层

首先定义一个非Inception Module的普通卷积层函数inception_v3_base,输入参数inputs为图片数据的张量。第1个卷积层的输出通道数为32,卷积核尺寸为【3x3】,步长为2,padding模式是默认的VALID,第1个卷积层之后的张量尺寸变为(299-3)/2+1=149,即【149x149x32】。

后面的卷积层采用相同的形式,最后张量尺寸变为【35x35x192】。这几个普通的卷积层主要使用了3x3的小卷积核,小卷积核可以低成本的跨通道的对特征进行组合。

def inception_v3_base(inputs,scepe=None):
    with tf.variable_scope(scope,'InceptionV3',[inputs]):
        with slim.arg_scope([slim.conv2d,slim.max_pool2d,slim.avg_pool2d],stride=1,padding='VALID'):
            # 149 x 149 x 32
            net = slim.conv2d(inputs,32,[3,3],stride=2,scope='Conv2d_1a_3x3')
            # 147 x 147 x 32'
            net = slim.conv2d(net,32),[3,3],scope='Conv2d_2a_3x3')
            # 147 x 147 x 64
            net = slim.conv2d(net,64,[3,3],padding='SAME',scope='Conv2d_2b_3x3')
            # 73 x 73 x 64
            net = slim.max_pool2d(net, [3, 3], stride=2, scope='MaxPool_3a_3x3')
            # 73 x 73 x 80
            net = slim.conv2d(net, 80, [1, 1], scope= 'Conv2d_3b_1x1')
            # 71 x 71 x 192.
            net = slim.conv2d(net, 192, [3, 3], scope='Conv2d_4a_3x3',reuse=tf.AUTO_REUSE)
            # 35 x 35 x 192
            net = slim.max_pool2d(net, [3, 3], stride=2, scope= 'MaxPool_5a_3x3')   

2 三个Inception模块组

接下来是三个连续的Inception模块组,每个模块组有多个Inception module组成。

下面是第1个Inception模块组,包含了3个类似的Inception module,分别是:Mixed_5b,Mixed_5c,Mixed_5d。第1个Inception module有4个分支,

第1个分支是输出通道为64的【1x1】卷积,

第2个分支是输出通道为48的【1x1】卷积,再连接输出通道为64的【5x5】卷积,

第3个分支是输出通道为64的【1x1】卷积,再连接2个输出通道为96的【3x3】卷积,

第4个分支是【3x3】的平均池化,再连接输出通道为32的【1x1】卷积。

最后用tf.concat将4个分支的输出合并在一起,输出通道之和为54+64+96+32=256,最后输出的张量尺寸为【35x35x256】。

第2个Inception module也有4个分支,与第1个模块类似,只是最后连接输出通道数为64的【1x1】卷积,最后输出的张量尺寸为【35x35x288】。

第3个模块与第2个模块一样。

    with slim.arg_scope([slim.conv2d,slim.max_pool2d,slim.avg_pool2d],stride=1,padding='SAME'):
        # 35 x 35 x 256
        end_point = 'Mixed_5b'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                branch_0 = slim.conv2d(net,depth(64),[1,1],scope='Conv2d_0a_1x1')
            with tf.variable_scope('Branch_1'):
                branch_1 = slim.conv2d(net, depth(48), [1, 1], scope='Conv2d_0a_1x1')
                branch_1 = slim.conv2d(branch_1, depth(64), [5, 5], scope='Conv2d_0b_5x5')
            with tf.variable_scope('Branch_2'):
                branch_2 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
                branch_2 = slim.conv2d(branch_2, depth(96), [3, 3],scope='Conv2d_0b_3x3')
                branch_2 = slim.conv2d(branch_2, depth(96), [3, 3], scope='Conv2d_0c_3x3')
            with tf.variable_scope('Branch_3'):
                branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
                branch_3 = slim.conv2d(branch_3, depth(32), [1, 1], scope='Conv2d_0b_1x1')
            net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3]) # 64+64+96+32=256
        end_points[end_point] = net
        # 35 x 35 x 288
        end_point = 'Mixed_5c'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                branch_0 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
            with tf.variable_scope('Branch_1'):
                branch_1 = slim.conv2d(net, depth(48), [1, 1], scope='Conv2d_0b_1x1')
                branch_1 = slim.conv2d(branch_1, depth(64), [5, 5],scope='Conv_1_0c_5x5')
            with tf.variable_scope('Branch_2'):
                branch_2 = slim.conv2d(net, depth(64), [1, 1],scope='Conv2d_0a_1x1')
                branch_2 = slim.conv2d(branch_2, depth(96), [3, 3],scope='Conv2d_0b_3x3')
                branch_2 = slim.conv2d(branch_2, depth(96), [3, 3],scope='Conv2d_0c_3x3')
            with tf.variable_scope('Branch_3'):
                branch_3 = slim.avg_pool2d(net, [3, 3],scope='AvgPool_0a_3x3')
                branch_3 = slim.conv2d(branch_3, depth(64), [1, 1],scope='Conv2d_0b_1x1')
            net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net
        # 35 x 35 x 288
        end_point = 'Mixed_5d'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                branch_0 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
            with tf.variable_scope('Branch_1'):
                branch_1 = slim.conv2d(net, depth(48), [1, 1], scope='Conv2d_0a_1x1')
                branch_1 = slim.conv2d(branch_1, depth(64), [5, 5],scope='Conv2d_0b_5x5')
            with tf.variable_scope('Branch_2'):
                branch_2 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
                branch_2 = slim.conv2d(branch_2, depth(96), [3, 3],scope='Conv2d_0b_3x3')
                branch_2 = slim.conv2d(branch_2, depth(96), [3, 3],scope='Conv2d_0c_3x3')
            with tf.variable_scope('Branch_3'):
                branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
                branch_3 = slim.conv2d(branch_3, depth(64), [1, 1],scope='Conv2d_0b_1x1')
            net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net

第2个Inception模块组包含了5个Inception module,分别是Mixed_6a,Mixed_6b,Mixed_6ac,Mixed_6d,Mixed_6e。

每个Inception module包含有多个分支,第1个Inception module的步长为2,因此图片尺寸被压缩,最后输出的张量尺寸为【17x17x768】。

第2个Inception module采用了Fractorization into small convolutions思想,串联了【1x7】和【7x1】卷积,最后也是将多个通道合并。

第3、4个Inception module与第2个类似,都是用来增加卷积和非线性变化,提炼特征。张量尺寸不变,多个module后仍旧是【17x17x768】。

        # 17 x 17 x 768.
        end_point = 'Mixed_6a'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                branch_0 = slim.conv2d(net, depth(384), [3, 3], stride=2,padding='VALID', scope='Conv2d_1a_1x1')
            with tf.variable_scope('Branch_1'):
                branch_1 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
                branch_1 = slim.conv2d(branch_1, depth(96), [3, 3],scope='Conv2d_0b_3x3')
                branch_1 = slim.conv2d(branch_1, depth(96), [3, 3], stride=2,padding='VALID', scope='Conv2d_1a_1x1')
            with tf.variable_scope('Branch_2'):
                branch_2 = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID',scope='MaxPool_1a_3x3')
            net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2]) # (35-3)/2+1=17
        end_points[end_point] = net
        # 17 x 17 x 768.
        end_point = 'Mixed_6b'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                branch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
            with tf.variable_scope('Branch_1'):
                branch_1 = slim.conv2d(net, depth(128), [1, 1], scope='Conv2d_0a_1x1')
                branch_1 = slim.conv2d(branch_1, depth(128), [1, 7],scope='Conv2d_0b_1x7')
                branch_1 = slim.conv2d(branch_1, depth(192), [7, 1],scope='Conv2d_0c_7x1')
            with tf.variable_scope('Branch_2'):
                branch_2 = slim.conv2d(net, depth(128), [1, 1], scope='Conv2d_0a_1x1')
                branch_2 = slim.conv2d(branch_2, depth(128), [7, 1],scope='Conv2d_0b_7x1')
                branch_2 = slim.conv2d(branch_2, depth(128), [1, 7],scope='Conv2d_0c_1x7')
                branch_2 = slim.conv2d(branch_2, depth(128), [7, 1], scope='Conv2d_0d_7x1')
                branch_2 = slim.conv2d(branch_2, depth(192), [1, 7],scope='Conv2d_0e_1x7')
            with tf.variable_scope('Branch_3'):
                branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
                branch_3 = slim.conv2d(branch_3, depth(192), [1, 1],scope='Conv2d_0b_1x1')
            net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net
        print(net.shape)
        # 17 x 17 x 768.
        end_point = 'Mixed_6c'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                ranch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
            with tf.variable_scope('Branch_1'):
                branch_1 = slim.conv2d(net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
                branch_1 = slim.conv2d(branch_1, depth(160), [1, 7],scope='Conv2d_0b_1x7')
                branch_1 = slim.conv2d(branch_1, depth(192), [7, 1],scope='Conv2d_0c_7x1')
            with tf.variable_scope('Branch_2'):
                branch_2 = slim.conv2d(net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
                branch_2 = slim.conv2d(branch_2, depth(160), [7, 1],scope='Conv2d_0b_7x1')
                branch_2 = slim.conv2d(branch_2, depth(160), [1, 7],scope='Conv2d_0c_1x7')
                branch_2 = slim.conv2d(branch_2, depth(160), [7, 1],scope='Conv2d_0d_7x1')
                branch_2 = slim.conv2d(branch_2, depth(192), [1, 7],scope='Conv2d_0e_1x7')
            with tf.variable_scope('Branch_3'):
                branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
                branch_3 = slim.conv2d(branch_3, depth(192), [1, 1],scope='Conv2d_0b_1x1')
            net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net
        # 17 x 17 x 768.
        end_point = 'Mixed_6d'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                branch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
            with tf.variable_scope('Branch_1'):
                branch_1 = slim.conv2d(net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
                branch_1 = slim.conv2d(branch_1, depth(160), [1, 7], scope='Conv2d_0b_1x7')
                branch_1 = slim.conv2d(branch_1, depth(192), [7, 1], scope='Conv2d_0c_7x1')
            with tf.variable_scope('Branch_2'):
                branch_2 = slim.conv2d(net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
                branch_2 = slim.conv2d(branch_2, depth(160), [7, 1], scope='Conv2d_0b_7x1')
                branch_2 = slim.conv2d(branch_2, depth(160), [1, 7], scope='Conv2d_0c_1x7')
                branch_2 = slim.conv2d(branch_2, depth(160), [7, 1], scope='Conv2d_0d_7x1')
                branch_2 = slim.conv2d(branch_2, depth(192), [1, 7], scope='Conv2d_0e_1x7')
            with tf.variable_scope('Branch_3'):
                branch_3 = slim.avg_pool2d(net, [3, 3], sco e='AvgPool_0a_3x3')
                branch_3 = slim.conv2d(branch_3, depth(192), [1, 1],scope='Conv2d_0b_1x1')
            net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net
        # 17 x 17 x 768.
        end_point = 'Mixed_6e'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                branch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
            with tf.variable_scope('Branch_1'):
                branch_1 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
                branch_1 = slim.conv2d(branch_1, depth(192), [1, 7],
                                     scope='Conv2d_0b_1x7')
                branch_1 = slim.conv2d(branch_1, depth(192), [7, 1],
                                     scope='Conv2d_0c_7x1')
            with tf.variable_scope('Branch_2'):
                branch_2 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
                branch_2 = slim.conv2d(branch_2, depth(192), [7, 1],
                                     scope='Conv2d_0b_7x1')
                branch_2 = slim.conv2d(branch_2, depth(192), [1, 7],
                                     scope='Conv2d_0c_1x7')
                branch_2 = slim.conv2d(branch_2, depth(192), [7, 1],
                                     scope='Conv2d_0d_7x1')
                branch_2 = slim.conv2d(branch_2, depth(192), [1, 7],
                                     scope='Conv2d_0e_1x7')
            with tf.variable_scope('Branch_3'):
                branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
                branch_3 = slim.conv2d(branch_3, depth(192), [1, 1],
                                     scope='Conv2d_0b_1x1')
            net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net

第3个Inception模块组包含了3个Inception module,分别是Mxied_7a,Mixed_7b,Mixed_7c。

第1个Inception module包含了3个分支,与上面的结构类似,主要也是通过改变通道数、卷积核尺寸,包括【1x1】、【3x3】、【1x7】、【7x1】来增加卷积和非线性变化,提升网络性能。

最后3个分支在输出通道上合并,输出张量的尺寸为【8 x 8 x 1280】。第3个Inception module后得到的张量尺寸为【8 x 8 x 2048】。

        # 8 x 8 x 1280.
        end_point = 'Mixed_7a'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                branch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
                branch_0 = slim.conv2d(branch_0, depth(320), [3, 3], stride=2,
                                     padding='VALID', scope='Conv2d_1a_3x3')
            with tf.variable_scope('Branch_1'):
                branch_1 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
                branch_1 = slim.conv2d(branch_1, depth(192), [1, 7],
                                     scope='Conv2d_0b_1x7')
                branch_1 = slim.conv2d(branch_1, depth(192), [7, 1],
                                     scope='Conv2d_0c_7x1')
                branch_1 = slim.conv2d(branch_1, depth(192), [3, 3], stride=2,
                                     padding='VALID', scope='Conv2d_1a_3x3')
            with tf.variable_scope('Branch_2'):
                branch_2 = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID',
                                         scope='MaxPool_1a_3x3')
            net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2])
        end_points[end_point] = net
        # 8 x 8 x 2048.
        end_point = 'Mixed_7b'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                branch_0 = slim.conv2d(net, depth(320), [1, 1], scope='Conv2d_0a_1x1')
            with tf.variable_scope('Branch_1'):
                branch_1 = slim.conv2d(net, depth(384), [1, 1], scope='Conv2d_0a_1x1')
                branch_1 = tf.concat(axis=3, values=[
                  slim.conv2d(branch_1, depth(384), [1, 3], scope='Conv2d_0b_1x3'),
                  slim.conv2d(branch_1, depth(384), [3, 1], scope='Conv2d_0b_3x1')])
            with tf.variable_scope('Branch_2'):
                branch_2 = slim.conv2d(net, depth(448), [1, 1], scope='Conv2d_0a_1x1')
                branch_2 = slim.conv2d(
                  branch_2, depth(384), [3, 3], scope='Conv2d_0b_3x3')
                branch_2 = tf.concat(axis=3, values=[
                  slim.conv2d(branch_2, depth(384), [1, 3], scope='Conv2d_0c_1x3'),
                  slim.conv2d(branch_2, depth(384), [3, 1], scope='Conv2d_0d_3x1')])
            with tf.variable_scope('Branch_3'):
                branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
                branch_3 = slim.conv2d(
                  branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
            net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net)
        # 8 x 8 x 2048.
        end_point = 'Mixed_7c'
        with tf.variable_scope(end_point):
            with tf.variable_scope('Branch_0'):
                branch_0 = slim.conv2d(net, depth(320), [1, 1], scope='Conv2d_0a_1x1')
            with tf.variable_scope('Branch_1'):
                branch_1 = slim.conv2d(net, depth(384), [1, 1], scope='Conv2d_0a_1x1')
                branch_1 = tf.concat(axis=3, values=[
                  slim.conv2d(branch_1, depth(384), [1, 3], scope='Conv2d_0b_1x3'),
                  slim.conv2d(branch_1, depth(384), [3, 1], scope='Conv2d_0c_3x1')])
            with tf.variable_scope('Branch_2'):
                branch_2 = slim.conv2d(net, depth(448), [1, 1], scope='Conv2d_0a_1x1')
                branch_2 = slim.conv2d(
                  branch_2, depth(384), [3, 3], scope='Conv2d_0b_3x3')
                branch_2 = tf.concat(axis=3, values=[
                  slim.conv2d(branch_2, depth(384), [1, 3], scope='Conv2d_0c_1x3'),
                  slim.conv2d(branch_2, depth(384), [3, 1], scope='Conv2d_0d_3x1')])
            with tf.variable_scope('Branch_3'):
                branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
                branch_3 = slim.conv2d(
                  branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
            net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
        end_points[end_point] = net

3 Auxiliary Logits、全局平均池化、Softmax分类

Inception V3网络的最后一部分是Auxiliary Logits、全局平均池化、Softmax分类。

首先是Auxiliary Logits,作为辅助分类的节点,对分类结果预测有很大帮助。

先通过end_points['Mixed_6e']得到Mixed_6e后的特征张量,之后接一个【5x5】的平均池化,步长为3,padding为VALID,张量尺寸从第2个模块组的【17x17x768】变为【5x5x768】。

接着连接一个输出通道为128的【1x1】卷积和输出通道为768的【5x5】卷积,输出尺寸变为【1x1x768】。

然后连接输出通道数为num_classes的【1x1】卷积,输出变为【1x1x1000】。最后将辅助分类节点的输出存储到字典表end_points中。

       with slim.arg_scope([slim.conv2d,slim.max_pool2d,slim.avg_pool2d],stride=1,padding='SAME'):
            aux_logits = end_points['Mixed_6e']
            print(aux_logits.shape)
            with tf.variable_scope('AuxLogits'):
                aux_logits = slim.avg_pool2d(aux_logits,[5,5],stride=3,padding='VALID',scope='AvgPool_1a_5x5')
                aux_logits = slim.conv2d(aux_logits,depth(128),[1,1],scope='Conv2d_1b_1x1')  # (17-5)/3+1=5
            kernel_size = _reduced_kernel_size_for_small_input(aux_logits, [5, 5])
            aux_logits = slim.conv2d(aux_logits, depth(768), kernel_size, weights_initializer=trunc_normal(0.01),
                                     padding='VALID', scope='Conv2d_2a_{}x{}'.format(*kernel_size))
            aux_logits = slim.conv2d( aux_logits, num_classes, [1, 1], activation_fn=None,
                                      normalizer_fn=None, weights_initializer=trunc_normal(0.001),
                                      scope='Conv2d_2b_1x1')
            aux_logits = tf.squeeze(aux_logits, [1, 2], name='SpatialSqueeze')
            end_points['AuxLogits'] = aux_logits

最后对最后一个卷积层的输出Mixed_7c进行一个【8x8】的全局平均池化,padding为VALID,输出张量从【8 x 8 x 2048】变为【1 x 1 x 2048】,然后连接一个Dropout层,接着连接一个输出通道数为1000的【1x1】卷积。

使用tf.squeeze去掉输出张量中维数为1的维度。最后用Softmax得到最终分类结果。返回分类结果logits和包含各个卷积后的特征图字典表end_points。

        with tf.variable_scope('Logits'):
            kernel_size = _reduced_kernel_size_for_small_input(net, [8, 8])
            net = slim.avg_pool2d(net, kernel_size, padding='VALID',scope='AvgPool_1a_{}x{}'.format(*kernel_size))
            end_points['AvgPool_1a'] = net
            net = slim.dropout(net, keep_prob=dropout_keep_prob, scope='Dropout_1b')
            end_points['PreLogits'] = net
            logits = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, normalizer_fn=None, scope='Conv2d_1c_1x1')
            logits = tf.squeeze(logits, [1, 2], name='SpatialSqueeze')
            end_points['Logits'] = logits
            end_points['Predictions'] = slim.softmax(logits, scope='Predictions')
  return logits,end_points

参考文献:

1. 《TensorFlow实战》

以上就是卷积神经Inception V3网络结构代码解析的详细内容,更多关于Inception V3卷积神经网络的资料请关注我们其它相关文章!

(0)

相关推荐

  • TensorFlow keras卷积神经网络 添加L2正则化方式

    我就废话不多说了,大家还是直接看代码吧! model = keras.models.Sequential([ #卷积层1 keras.layers.Conv2D(32,kernel_size=5,strides=1,padding="same",data_format="channels_last",activation=tf.nn.relu,kernel_regularizer=keras.regularizers.l2(0.01)), #池化层1 keras.l

  • tensorflow卷积神经Inception V3网络结构代码解析

    目录 前言 1 非Inception Module的普通卷积层 2 三个Inception模块组 3 Auxiliary Logits.全局平均池化.Softmax分类 前言 学习了Inception V3卷积神经网络,总结一下对Inception V3网络结构和主要代码的理解. GoogLeNet对网络中的传统卷积层进行了修改,提出了被称为 Inception 的结构,用于增加网络深度和宽度,提高深度神经网络性能.从Inception V1到Inception V4有4个更新版本,每一版的网络

  • pytorch之inception_v3的实现案例

    如下所示: from __future__ import print_function from __future__ import division import torch import torch.nn as nn import torch.optim as optim import numpy as np import torchvision from torchvision import datasets, models, transforms import matplotlib.py

  • python神经网络InceptionV3模型复现详解

    目录 神经网络学习小记录21——InceptionV3模型的复现详解 学习前言什么是InceptionV3模型InceptionV3网络部分实现代码图片预测 学习前言 Inception系列的结构和其它的前向神经网络的结构不太一样,每一层的内容不是直直向下的,而是分了很多的块. 什么是InceptionV3模型 InceptionV3模型是谷歌Inception系列里面的第三代模型,其模型结构与InceptionV2模型放在了同一篇论文里,其实二者模型结构差距不大,相比于其它神经网络模型,Inc

  • 使用卷积神经网络(CNN)做人脸识别的示例代码

    上回书说到了对人脸的检测,这回就开始正式进入人脸识别的阶段. 关于人脸识别,目前有很多经典的算法,当我大学时代,我的老师给我推荐的第一个算法是特征脸法,原理是先将图像灰度化,然后将图像每行首尾相接拉成一个列向量,接下来为了降低运算量要用PCA降维, 最后进分类器分类,可以使用KNN.SVM.神经网络等等,甚至可以用最简单的欧氏距离来度量每个列向量之间的相似度.OpenCV中也提供了相应的EigenFaceRecognizer库来实现该算法,除此之外还有FisherFaceRecognizer.L

  • Tensorflow卷积实现原理+手写python代码实现卷积教程

    从一个通道的图片进行卷积生成新的单通道图的过程很容易理解,对于多个通道卷积后生成多个通道的图理解起来有点抽象.本文以通俗易懂的方式讲述卷积,并辅以图片解释,能快速理解卷积的实现原理.最后手写python代码实现卷积过程,让Tensorflow卷积在我们面前不再是黑箱子! 注意: 本文只针对batch_size=1,padding='SAME',stride=[1,1,1,1]进行实验和解释,其他如果不是这个参数设置,原理也是一样. 1 Tensorflow卷积实现原理 先看一下卷积实现原理,对于

  • 卷积神经网络的网络结构图Inception V3

    目录 1.基于大滤波器尺寸分解卷积 1.1分解到更小的卷积 1.2. 空间分解为不对称卷积 2. 利用辅助分类器 3.降低特征图大小 Inception-V3模型: 总结: <Rethinking the Inception Architecture for Computer Vision> 2015,Google,Inception V3 1.基于大滤波器尺寸分解卷积 GoogLeNet性能优异很大程度在于使用了降维.降维可以看做卷积网络的因式分解.例如1x1卷积层后跟着3x3卷积层.在网络

  • Python通过TensorFlow卷积神经网络实现猫狗识别

    这份数据集来源于Kaggle,数据集有12500只猫和12500只狗.在这里简单介绍下整体思路 处理数据 设计神经网络 进行训练测试 1. 数据处理 将图片数据处理为 tf 能够识别的数据格式,并将数据设计批次. 第一步get_files() 方法读取图片,然后根据图片名,添加猫狗 label,然后再将 image和label 放到 数组中,打乱顺序返回 将第一步处理好的图片 和label 数组 转化为 tensorflow 能够识别的格式,然后将图片裁剪和补充进行标准化处理,分批次返回. 新建

  • TensorFlow卷积神经网络AlexNet实现示例详解

    2012年,Hinton的学生Alex Krizhevsky提出了深度卷积神经网络模型AlexNet,它可以算是LeNet的一种更深更宽的版本.AlexNet以显著的优势赢得了竞争激烈的ILSVRC 2012比赛,top-5的错误率降低至了16.4%,远远领先第二名的26.2%的成绩.AlexNet的出现意义非常重大,它证明了CNN在复杂模型下的有效性,而且使用GPU使得训练在可接受的时间范围内得到结果,让CNN和GPU都大火了一把.AlexNet可以说是神经网络在低谷期后的第一次发声,确立了深

  • TensorFlow卷积神经网络之使用训练好的模型识别猫狗图片

    本文是Python通过TensorFlow卷积神经网络实现猫狗识别的姊妹篇,是加载上一篇训练好的模型,进行猫狗识别 本文逻辑: 我从网上下载了十几张猫和狗的图片,用于检验我们训练好的模型. 处理我们下载的图片 加载模型 将图片输入模型进行检验 代码如下: #coding=utf-8 import tensorflow as tf from PIL import Image import matplotlib.pyplot as plt import input_data import numpy

  • Tensorflow卷积神经网络实例进阶

    在Tensorflow卷积神经网络实例这篇博客中,我们实现了一个简单的卷积神经网络,没有复杂的Trick.接下来,我们将使用CIFAR-10数据集进行训练. CIFAR-10是一个经典的数据集,包含60000张32*32的彩色图像,其中训练集50000张,测试集10000张.CIFAR-10如同其名字,一共标注为10类,每一类图片6000张. 本文实现了进阶的卷积神经网络来解决CIFAR-10分类问题,我们使用了一些新的技巧: 对weights进行了L2的正则化 对图片进行了翻转.随机剪切等数据

  • Tensorflow 卷积的梯度反向传播过程

    一. valid卷积的梯度 我们分两种不同的情况讨论valid卷积的梯度:第一种情况,在已知卷积核的情况下,对未知张量求导(即对张量中每一个变量求导):第二种情况,在已知张量的情况下,对未知卷积核求导(即对卷积核中每一个变量求导) 1.已知卷积核,对未知张量求导 我们用一个简单的例子理解valid卷积的梯度反向传播.假设有一个3x3的未知张量x,以及已知的2x2的卷积核K Tensorflow提供函数tf.nn.conv2d_backprop_input实现了valid卷积中对未知变量的求导,以

  • TensorFlow卷积神经网络MNIST数据集实现示例

    这里使用TensorFlow实现一个简单的卷积神经网络,使用的是MNIST数据集.网络结构为:数据输入层–卷积层1–池化层1–卷积层2–池化层2–全连接层1–全连接层2(输出层),这是一个简单但非常有代表性的卷积神经网络. import tensorflow as tf import numpy as np import input_data mnist = input_data.read_data_sets('data/', one_hot=True) print("MNIST ready&q

  • Tensorflow 实现线性回归模型的示例代码

    目录 1.线性与非线性回归 案例讲解 1.数据集 2.读取训练数据Income.csv并可视化展示 3.利用Tensorflow搭建和训练神经网络模型[线性回归模型的建立] 4. 模型预测 1.线性与非线性回归 线性回归 Linear Regression:两个变量之间的关系是一次函数关系的——图像是直线,叫做线性.线性是指广义的线性,也就是数据与数据之间的关系,如图x1. 非线性回归:两个变量之间的关系不是一次函数关系的——图像不是直线,叫做非线性,如图x2. 一元线性回归:只包括一个自变量和

随机推荐