TensorFlow之slim

作者: 奔跑_7182 | 来源:发表于2018-01-15 15:38 被阅读0次

slim就是TensorFlow的简洁版。本篇就是介绍如何使用slim,因为自己也是刚开始接触TensorFlow,slim更是用得少,因此,本篇就当做是slim的学习记录,后面会不断更新。
先贴出slim的github,这里更详细
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/slim

首先,导入,在安装TensorFlow时,slim包也一起安装了,因此不需要再安装。

import tensorflow.contrib.slim as slim

基础操作
slim是TensorFlow的简洁版,因此在实际操作过程中要简单得多。
原生TensorFlow的一个卷积层:

inputs = ...
    ####conv1
    with tf.variable_scope('conv1') as scope:
        weights = tf.get_variable(scope.name+'_w1',
                               [3,3,3,16],
                               dtype=tf.float32,
                               initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32))
        biases = tf.get_variable(scope.name+'_b1',
                               [16],
                               dtype=tf.float32,
                               initializer=tf.constant_initializer(0.1))

        conv = tf.nn.conv2d(inputs,weights,strides=[1,1,1,1],padding='SAME')
        pre_activation = tf.nn.bias_add(conv,biases)
        conv1 = tf.nn.relu(pre_activation,name=scope.name)

在slim中:

inputs = ...
net = slim.conv2d(inputs,16,[3,3],scope='conv1')

inputs就是网络输入;
16是输出神经元个数;
[3,3]是该层卷积核大小

很简单!!更简单的是,Slim也提供了两个元运算符----repeat和stack,允许用户可以重复地使用相同的运算符。

net = ...
net = slim.conv2d(net,16,[3,3],scope='conv1')
net = slim.conv2d(net,16,[3,3],scope='conv1')
net = slim.conv2d(net,16,[3,3],scope='conv1')

上面可以替换为:

net = ...
net = slim.repeat(net,3,slim.conv2d,16,[3,3],scope='conv1')

这就是slim.repeat的作用。
解释:3表示重复slim.conv2d这个操作3次,每次输出神经元个数都是16,卷积核都是3*3;
那么,要用不同的参数怎么办?
可以用slim.stack:
输出神经元个数不同:

# Verbose way:  
x = slim.fully_connected(x, 32, scope='fc/fc_1')  
x = slim.fully_connected(x, 64, scope='fc/fc_2')  
x = slim.fully_connected(x, 128, scope='fc/fc_3')  
  
# Equivalent, TF-Slim way using slim.stack:  
slim.stack(x, slim.fully_connected, [32, 64, 128], scope='fc')

每层网络的输出神经元个数和卷积核都不同:

# Verbose way:  
x = slim.conv2d(x, 32, [3, 3], scope='core/core_1')  
x = slim.conv2d(x, 32, [1, 1], scope='core/core_2')  
x = slim.conv2d(x, 64, [3, 3], scope='core/core_3')  
x = slim.conv2d(x, 64, [1, 1], scope='core/core_4')  
  
# Using stack:  
slim.stack(x, slim.conv2d, [(32, [3, 3]), (32, [1, 1]), (64, [3, 3]), (64, [1, 1])], scope='core')

scope
除了tensorflow中自带的scope机制类型(name_scope, variable_scope)外, TF-Slim添加了一种叫做arg_scope的scope机制。这种scope允许用户在arg_scope中指定若干操作符以及一批参数,这些参数会传给前面所有的操作符中。如果后面不需要这种参数,可以重写,覆盖。
原始繁琐的操作:

net = slim.conv2d(inputs, 64, [11, 11], 4, padding='SAME',  
                  weights_initializer=tf.truncated_normal_initializer(stddev=0.01),  
                  weights_regularizer=slim.l2_regularizer(0.0005), scope='conv1')  
net = slim.conv2d(net, 128, [11, 11], padding='VALID',  
                  weights_initializer=tf.truncated_normal_initializer(stddev=0.01),  
                  weights_regularizer=slim.l2_regularizer(0.0005), scope='conv2')  
net = slim.conv2d(net, 256, [11, 11], padding='SAME',  
                  weights_initializer=tf.truncated_normal_initializer(stddev=0.01),  
                  weights_regularizer=slim.l2_regularizer(0.0005), scope='conv3')

简单操作:

with slim.arg_scope([slim.conv2d], padding='SAME',  
                      weights_initializer=tf.truncated_normal_initializer(stddev=0.01)  
                      weights_regularizer=slim.l2_regularizer(0.0005)):  
    net = slim.conv2d(inputs, 64, [11, 11], scope='conv1')  
    net = slim.conv2d(net, 128, [11, 11], padding='VALID', scope='conv2')    ##这里的padding='VALID'会覆盖原来默认的padding='SAME'
    net = slim.conv2d(net, 256, [11, 11], scope='conv3')

嵌套操作:
这里最外面的一层scope包含slim.conv2d和slim.fully_connected两个共有参数,里面一层scope则只包括slim.conv2d的参数。

with slim.arg_scope([slim.conv2d, slim.fully_connected],  
                      activation_fn=tf.nn.relu,  
                      weights_initializer=tf.truncated_normal_initializer(stddev=0.01),  
                      weights_regularizer=slim.l2_regularizer(0.0005)):  
  with slim.arg_scope([slim.conv2d], stride=1, padding='SAME'):  
    net = slim.conv2d(inputs, 64, [11, 11], 4, padding='VALID', scope='conv1')  
    net = slim.conv2d(net, 256, [5, 5],  
                      weights_initializer=tf.truncated_normal_initializer(stddev=0.03),  
                      scope='conv2')  
    net = slim.fully_connected(net, 1000, activation_fn=None, scope='fc') 

下面来一个slim写的例子,vgg16的实现:

def vgg16(inputs):  
  with slim.arg_scope([slim.conv2d, slim.fully_connected],  
                      activation_fn=tf.nn.relu,  
                      weights_initializer=tf.truncated_normal_initializer(0.0, 0.01),  
                      weights_regularizer=slim.l2_regularizer(0.0005)):  
    net = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], scope='conv1')  
    net = slim.max_pool2d(net, [2, 2], scope='pool1')  
    net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3], scope='conv2')  
    net = slim.max_pool2d(net, [2, 2], scope='pool2')  
    net = slim.repeat(net, 3, slim.conv2d, 256, [3, 3], scope='conv3')  
    net = slim.max_pool2d(net, [2, 2], scope='pool3')  
    net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv4')  
    net = slim.max_pool2d(net, [2, 2], scope='pool4')  
    net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv5')  
    net = slim.max_pool2d(net, [2, 2], scope='pool5')  
    net = slim.fully_connected(net, 4096, scope='fc6')  
    net = slim.dropout(net, 0.5, scope='dropout6')  
    net = slim.fully_connected(net, 4096, scope='fc7')  
    net = slim.dropout(net, 0.5, scope='dropout7')  
    net = slim.fully_connected(net, 1000, activation_fn=None, scope='fc8')  
  return net 

是不是特别精简?但其中包含的东西却很多,需要细细体会,还好之前走过原生TensorFlow的路。
这里特别要注意:
在最后一个全连接层中activation_fn=None,因为最后一层不需激活函数,而slim.fully_connected()是默认使用激活函数tf.nn.relu的,因此在网络模型最后一层中要添加activation_fn=None。

上面的这个vgg16网络就是slim内已经实现的网络,里面还有很多,可以直接拿来用,最好还是自己写一下,等有空了一定要全部自己重写一遍,先贴这里https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/slim/python/slim/nets

目前我接触的就这些,后面还会碰到,等自己理解了再来更新吧!!

参考文章:
原po写得更多更详细,学习了
http://blog.csdn.net/guvcolie/article/details/77686555

相关文章

  • TensorFlow中Slim模块

    TensorFlow中Slim模块

  • TensorFlow之slim

    slim就是TensorFlow的简洁版。本篇就是介绍如何使用slim,因为自己也是刚开始接触TensorFlow...

  • TensorFlow-Slim图像分类模型库

    1 TF-slim简介 TF-slim是一个轻型的TensorFlow高层API (tensorflow.c...

  • 2018-08-23

    Tensorflow Slim用法 TF-Slim 是Tensorflow为了定义、训练和评估复杂模型的一个轻量级...

  • TensorFlow-Slim

    TensorFlow-Slim TF-Slim 是一个用于定义、训练、和评估复杂模型的TensorFlow高级库。...

  • TensorFlow-Slim README翻译

    英文版传送门 TensorFlow-Slim TF-Slim是为了定义、训练和评估用tensorflow构建的复杂...

  • Tensorflow slim库使用小记

    转自Tensorflow slim库使用小记 看fensorflow的书发现使用的是slim库,那就要研究slim...

  • TF-Slim、TFLearn、Keras和TensorLaye

    TF-Slim、TFLearn、Keras和TensorLayer都是尝试简化TensorFlow/Theano的...

  • tf.slim模块学习

    翻译自《TF-slim官方文档》 tf.slim是一个用于定义、训练和评估复杂模型的tensorflow轻量级库。...

  • TF.slim --学习笔记

    F-Slim的优势:slim作为一种轻量级的tensorflow库,使得模型的构建,训练,测试都变得更加简单。 1...

网友评论

    本文标题:TensorFlow之slim

    本文链接:https://www.haomeiwen.com/subject/xlaroxtx.html