Tf.slim Use Method __tensorflow

Source: Internet
Author: User

First to import Slim:

From Tensorflow.contrib Import Slim
Tf-slim mainly consists of the following:

Arg_scope
Data
Evluation
Layers
Learning
Losses
Metrics
Nets
Queues
Regularizers
Variables Layer

The most commonly used is the slim Layers, the creation of Layer is very convenient:

Input = ...
NET = slim.conv2d (input, 128, [3, 3], scope= ' conv1_1 ')
net = slim.max_pool2d (NET, kernel_size=[2,2], stride=2, scope= ' Pool1 ')
# generally (inputs=, kernel_size=, stride=, padding=?, ....)
NET = Slim.repeat (NET, 3, slim.conv2d, 256, [3, 3], scope= ' conv3 ')
# Repeat operation is to create a duplicate of a layer
Layer Tf-slim
biasadd slim.bias_add
batchnorm slim.batch_norm
conv2d slim.conv2d
conv2dinplane slim.conv2d_i N_plane
conv2dtranspose (deconv) slim.conv2d_transpose
F ullyconnected slim.fully_connected
avgpool2d slim.avg_pool2d
dropout slim.dropout
flatten slim.flatten
maxpool2d slim.max_pool2d
onehotencoding slim.one_hot_encoding
SeparableConv2 slim.separable_conv2d
unitnorm slim.unit_norm
# All parameters are as follows
@add_arg_scope
def convolution2d_in_plane (
    inputs,
    kernel_size,
    stride=1,
    Padding= ' SAME ',
    Activation_fn=nn.relu,
    normalizer_fn=none,
    normalizer_params=none,
    weights_ Initializer=initializers.xavier_initializer (),
    Weights_regularizer=none,
    biases_initializer=init_ Ops.zeros_initializer (),
    Biases_regularizer=none,
    reuse=none,
    Variables_collections=none,
    Outputs_collections=none,
    trainable=true,
    scope=none):
Scopes:

The use of scope is also very convenient
After using Arg_scope, you can avoid adding duplicate parameters (such as weight_initializer=xxx, etc.) to each slim.conv2d.

# Add the same parameters to each slim.conv2d inside 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, $, [one, one], scope= ' conv1 ')
    net = slim.conv2d (NET, 128, [one, one], padding= ' VALID ', scope= ' conv2 '
    net = slim.conv2d (NET, 256, [one, one], scope= ' conv3 ')
Variables:

Can be created as follows:

weights = slim.variable (' weights ',
                             shape=[10, 3, 3],
                             Initializer=tf.truncated_normal_initializer (StdDev =0.1),
                             Regularizer=slim.l2_regularizer (0.05),
                             device= '/cpu:0 ')
# Model Variables
weights = slim.model_variable (' weights ',
                              shape=[10, 3, 3],
                              initializer=tf.truncated _normal_initializer (stddev=0.1),
                              Regularizer=slim.l2_regularizer (0.05),
                              device= '/cpu:0 ')
model_ variables = Slim.get_model_variables ()

# Regular variables
my_var = slim.variable (' My_var ',
                       shape=[20, 1] ,
                       Initializer=tf.zeros_initializer ())
regular_variables_and_model_variables = Slim.get_variables ()
Training Loop

Simple training methods, call Slim.learning.create_train_op and Slim.learning.train to achieve

# call Slim.learning.create_train_op and Slim.learning.train to implement
g = tf. Graph ()

# Create The model and specify the losses
... ...

Total_loss = Slim.losses.get_total_loss ()
optimizer = Tf.train.GradientDescentOptimizer (learning_rate)

# CREATE_TRAIN_OP ensures that each time we ask for the loss, the Update_ops
# are run and the gradients being are applied too.
Train_op = Slim.learning.create_train_op (Total_loss, optimizer)
LogDir = ... # Where checkpoints are.

Slim.learning.train (
    train_op,
    logdir,
    number_of_steps=1000,
    save_summaries_secs=300,
    save_interval_secs=600):
other

There are other methods not written in this article, only the author (small white) used more listed
The other can refer to
Https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/slim Reference:

Tensorflow-slim:
Https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/slim

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.