1. 程式人生 > >深度學習--Inception-ResNet-v1網路結構

深度學習--Inception-ResNet-v1網路結構

  Inception V4的網路結構如下:   
這裡寫圖片描述
  從圖中可以看出,輸入部分與V1到V3的輸入部分有較大的差別,這樣設計的目的為了:使用並行結構、不對稱卷積核結構,可以在保證資訊損失足夠小的情況下,降低計算量。結構中1*1的卷積核也用來降維,並且也增加了非線性。
  Inception-ResNet-v2與Inception-ResNet-v1的結構類似,除了stem部分。Inception-ResNet-v2的stem與V4的結構類似,Inception-ResNet-v2的輸出chnnel要高。Reduction-A相同,Inception-ResNet-A、Inception-ResNet-B、Inception-ResNet-C和Reduction-B的結構與v1的類似,只不過輸出的channel數量更多。
  這裡寫圖片描述


  Inception-ResNet-v1的總體網路結構如下所示:
Inception-ResNet-v1
Inception-ResNet-v1的Stem與V3的結構是一致的。
  接下來主要說一下Inception-ResNet-v1的網路結構及程式碼的實現部分。

Stem結構

Stem結構
  stem結構與V3的Stem結構類似。
對應的程式碼為

with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d],stride=1, padding='SAME'):

    # 149 x 149 x 32
    net = slim.conv2d(inputs, 32
, 3, stride=2, padding='VALID', scope='Conv2d_1a_3x3') end_points['Conv2d_1a_3x3'] = net # 147 x 147 x 32 net = slim.conv2d(net, 32, 3, padding='VALID', scope='Conv2d_2a_3x3') end_points['Conv2d_2a_3x3'] = net # 147 x 147 x 64 net = slim.conv2d(net, 64, 3, scope='Conv2d_2b_3x3'
) end_points['Conv2d_2b_3x3'] = net # 73 x 73 x 64 net = slim.max_pool2d(net, 3, stride=2, padding='VALID', scope='MaxPool_3a_3x3') end_points['MaxPool_3a_3x3'] = net # 73 x 73 x 80 net = slim.conv2d(net, 80, 1, padding='VALID', scope='Conv2d_3b_1x1') end_points['Conv2d_3b_1x1'] = net # 71 x 71 x 192 net = slim.conv2d(net, 192, 3, padding='VALID', scope='Conv2d_4a_3x3') end_points['Conv2d_4a_3x3'] = net # 35 x 35 x 256 net = slim.conv2d(net, 256, 3, stride=2, padding='VALID', scope='Conv2d_4b_3x3') end_points['Conv2d_4b_3x3'] = net

Inception-resnet-A模組

  Inception-resnet-A模組是要重複5次的,網路結構為:
Inception-resnet-A
  對應的程式碼表示為:

# Inception-Renset-A
def block35(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None):
    """Builds the 35x35 resnet block."""
    with tf.variable_scope(scope, 'Block35', [net], reuse=reuse):
        with tf.variable_scope('Branch_0'):
            # 35 × 35 × 32
            tower_conv = slim.conv2d(net, 32, 1, scope='Conv2d_1x1')
        with tf.variable_scope('Branch_1'):
            # 35 × 35 × 32
            tower_conv1_0 = slim.conv2d(net, 32, 1, scope='Conv2d_0a_1x1')
            # 35 × 35 × 32
            tower_conv1_1 = slim.conv2d(tower_conv1_0, 32, 3, scope='Conv2d_0b_3x3')
        with tf.variable_scope('Branch_2'):
            # 35 × 35 × 32
            tower_conv2_0 = slim.conv2d(net, 32, 1, scope='Conv2d_0a_1x1')
            # 35 × 35 × 32
            tower_conv2_1 = slim.conv2d(tower_conv2_0, 32, 3, scope='Conv2d_0b_3x3')
            # 35 × 35 × 32
            tower_conv2_2 = slim.conv2d(tower_conv2_1, 32, 3, scope='Conv2d_0c_3x3')
        # 35 × 35 × 96
        mixed = tf.concat([tower_conv, tower_conv1_1, tower_conv2_2], 3)
        # 35 × 35 × 256
        up = slim.conv2d(mixed, net.get_shape()[3], 1, normalizer_fn=None,activation_fn=None, scope='Conv2d_1x1')
        # 使用殘差網路scale = 0.17
        net += scale * up
        if activation_fn:
            net = activation_fn(net)
    return net

# 5 x Inception-resnet-A
net = slim.repeat(net, 5, block35, scale=0.17)
end_points['Mixed_5a'] = net

Reduction-A結構

  Reduction-A中含有4個引數k、l、 m、 n,它們對應的值分別為:192, 192, 256, 384,在該層網路結構,輸入為35×35×256,輸出為17×17×896.
Reduction-A

def reduction_a(net, k, l, m, n):
    # 192, 192, 256, 384
    with tf.variable_scope('Branch_0'):
        # 17×17×384
        tower_conv = slim.conv2d(net, n, 3, stride=2, padding='VALID',
                                 scope='Conv2d_1a_3x3')
    with tf.variable_scope('Branch_1'):
        # 35×35×192
        tower_conv1_0 = slim.conv2d(net, k, 1, scope='Conv2d_0a_1x1')
        # 35×35×192
        tower_conv1_1 = slim.conv2d(tower_conv1_0, l, 3,
                                    scope='Conv2d_0b_3x3')
        # 17×17×256
        tower_conv1_2 = slim.conv2d(tower_conv1_1, m, 3,
                                    stride=2, padding='VALID',
                                    scope='Conv2d_1a_3x3')
    with tf.variable_scope('Branch_2'):
        # 17×17×256
        tower_pool = slim.max_pool2d(net, 3, stride=2, padding='VALID',
                                     scope='MaxPool_1a_3x3')
    # 17×17×896
    net = tf.concat([tower_conv, tower_conv1_2, tower_pool], 3)
    return net

# Reduction-A
with tf.variable_scope('Mixed_6a'):
    net = reduction_a(net, 192, 192, 256, 384)
    end_points['Mixed_6a'] = net

Inception-Resnet-B

  Inception-Resnet-B模組是要重複10次,輸入為17×17×896,輸出為17×17×896,網路結構為:
Inception-Resnet-B

# Inception-Renset-B
def block17(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None):
    """Builds the 17x17 resnet block."""
    with tf.variable_scope(scope, 'Block17', [net], reuse=reuse):
        with tf.variable_scope('Branch_0'):
            # 17*17*128
            tower_conv = slim.conv2d(net, 128, 1, scope='Conv2d_1x1')
        with tf.variable_scope('Branch_1'):
            # 17*17*128
            tower_conv1_0 = slim.conv2d(net, 128, 1, scope='Conv2d_0a_1x1')
            # 17*17*128
            tower_conv1_1 = slim.conv2d(tower_conv1_0, 128, [1, 7],
                                        scope='Conv2d_0b_1x7')
            # 17*17*128
            tower_conv1_2 = slim.conv2d(tower_conv1_1, 128, [7, 1],
                                        scope='Conv2d_0c_7x1')
        # 17*17*256
        mixed = tf.concat([tower_conv, tower_conv1_2], 3)
        # 17*17*896
        up = slim.conv2d(mixed, net.get_shape()[3], 1, normalizer_fn=None,activation_fn=None, scope='Conv2d_1x1')
        net += scale * up
        if activation_fn:
            net = activation_fn(net)
    return net

# 10 x Inception-Resnet-B
net = slim.repeat(net, 10, block17, scale=0.10)
end_points['Mixed_6b'] = net

Reduction-B

  Reduction-B的輸入為17*17*896,輸出為8*8*1792。網路結構為:
Reduction-B
對應的程式碼為:

def reduction_b(net):
    with tf.variable_scope('Branch_0'):
        # 17*17*256
        tower_conv = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1')
        # 8*8*384
        tower_conv_1 = slim.conv2d(tower_conv, 384, 3, stride=2,
                                   padding='VALID', scope='Conv2d_1a_3x3')
    with tf.variable_scope('Branch_1'):
        # 17*17*256
        tower_conv1 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1')
        # 8*8*256
        tower_conv1_1 = slim.conv2d(tower_conv1, 256, 3, stride=2,
                                    padding='VALID', scope='Conv2d_1a_3x3')
    with tf.variable_scope('Branch_2'):
        # 17*17*256
        tower_conv2 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1')
        # 17*17*256
        tower_conv2_1 = slim.conv2d(tower_conv2, 256, 3,
                                    scope='Conv2d_0b_3x3')
        # 8*8*256
        tower_conv2_2 = slim.conv2d(tower_conv2_1, 256, 3, stride=2,
                                    padding='VALID', scope='Conv2d_1a_3x3')
    with tf.variable_scope('Branch_3'):
        # 8*8*896
        tower_pool = slim.max_pool2d(net, 3, stride=2, padding='VALID',
                                     scope='MaxPool_1a_3x3')
    # 8*8*1792
    net = tf.concat([tower_conv_1, tower_conv1_1,
                        tower_conv2_2, tower_pool], 3)
    return net
# Reduction-B
with tf.variable_scope('Mixed_7a'):
     net = reduction_b(net)
end_points['Mixed_7a'] = net

Inception-Resnet-C結構

  Inception-Resnet-C結構重複5次。它輸入為8*8*1792,輸出為8*8*1792。對應的結構為:
Inception-Resnet-C
  對應的程式碼為:

# Inception-Resnet-C
def block8(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None):
    """Builds the 8x8 resnet block."""
    with tf.variable_scope(scope, 'Block8', [net], reuse=reuse):
        with tf.variable_scope('Branch_0'):
            # 8*8*192
            tower_conv = slim.conv2d(net, 192, 1, scope='Conv2d_1x1')
        with tf.variable_scope('Branch_1'):
            # 8*8*192
            tower_conv1_0 = slim.conv2d(net, 192, 1, scope='Conv2d_0a_1x1')
            # 8*8*192
            tower_conv1_1 = slim.conv2d(tower_conv1_0, 192, [1, 3],
                                        scope='Conv2d_0b_1x3')
            # 8*8*192
            tower_conv1_2 = slim.conv2d(tower_conv1_1, 192, [3, 1],
                                        scope='Conv2d_0c_3x1')
        # 8*8*384
        mixed = tf.concat([tower_conv, tower_conv1_2], 3)
        # 8*8*1792
        up = slim.conv2d(mixed, net.get_shape()[3], 1, normalizer_fn=None,activation_fn=None, scope='Conv2d_1x1')
        # scale=0.20
        net += scale * up
        if activation_fn:
            net = activation_fn(net)
    return net
# 5 x Inception-Resnet-C
net = slim.repeat(net, 5, block8, scale=0.20)
end_points['Mixed_8a'] = net

  但是在facenet中,接下來又是一層Inception-Resnet-C,但是它沒有重複,並且沒有啟用函式。輸入與輸出大小相同。

net = block8(net, activation_fn=None)
end_points['Mixed_8b'] = net

結果輸出

  結果輸出包含Average Pooling和Dropout (keep 0.8)及Softmax三層,這裡我們以facenet中為例:具體的程式碼如下:

with tf.variable_scope('Logits'):
     end_points['PrePool'] = net
     #pylint: disable=no-member
     # Average Pooling層,輸出為8×8×1792
     net = slim.avg_pool2d(net, net.get_shape()[1:3], padding='VALID',scope='AvgPool_1a_8x8')
     #扁平除了batch_size維度的其它維度。使輸出變為:[batch_size, ...]
     net = slim.flatten(net)
     #dropout層
     net = slim.dropout(net, dropout_keep_prob, is_training=is_training,scope='Dropout')
     end_points['PreLogitsFlatten'] = net
     # 全連結層。輸出為batch_size×128
     net = slim.fully_connected(net, bottleneck_layer_size, activation_fn=None,scope='Bottleneck', reuse=False)

  至此,inception_resnet_v1網路結構就結束了,但facenet的程式碼分析未完,待續~~~~