TensorFlow中的未连接流

时间:2018-12-15 17:30:32

标签: tensorflow

我正在创建一个用于图像分类的卷积神经网络。我是Tensorflow的新手,只是想了解它的控制流程,但我在TensorBoard上的结果如下。

enter image description here

没有节点。可能是错误的。为什么会这样呢?下面是我的程序的一个片段,

def VGG16(x, n_classes, is_pretrain=True):
    # using the name scope, the tensorboard maybe look better
    with tf.name_scope('VGG16'):

    x = tools.conv('conv1_1', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
    x = tools.conv('conv1_2', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
    with tf.name_scope('pool1'):
        x = tools.pool('pool1', x, ksize=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

    x = tools.conv('conv2_1', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
    x = tools.conv('conv2_2', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
    with tf.name_scope('pool2'):
        x = tools.pool('pool2', x, ksize=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

    x = tools.conv('conv3_1', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
    x = tools.conv('conv3_2', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
    x = tools.conv('conv3_3', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain)
    with tf.name_scope('pool3'):
        x = tools.pool('pool3', x, ksize=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

    x = tools.conv('conv4_1', x, 512, kernel_size=[3, 3], stride=[1, 2, 2, 1], is_pretrain=is_pretrain)
    x = tools.conv('conv4_2', x, 512, kernel_size=[3, 3], stride=[1, 2, 2, 1], is_pretrain=is_pretrain)
    x = tools.conv('conv4_3', x, 512, kernel_size=[3, 3], stride=[1, 2, 2, 1], is_pretrain=is_pretrain)
    with tf.name_scope('pool4'):
        x = tools.pool('pool4', x, ksize=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

    x = tools.conv('conv5_1', x, 512, kernel_size=[3, 3], stride=[1, 2, 2, 1], is_pretrain=is_pretrain)
    x = tools.conv('conv5_2', x, 512, kernel_size=[3, 3], stride=[1, 2, 2, 1], is_pretrain=is_pretrain)
    x = tools.conv('conv5_3', x, 512, kernel_size=[3, 3], stride=[1, 2, 2, 1], is_pretrain=is_pretrain)
    with tf.name_scope('pool5'):
        x = tools.pool('pool5', x, ksize=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)

    x = tools.FC_layer('fc6', x, out_nodes=4096)
    with tf.name_scope('batch_norm1'):
        x = tools.batch_norm(x)     # batch norm can avoid overfit, more efficient than dropout
    x = tools.FC_layer('fc7', x, out_nodes=4096)
    with tf.name_scope('batch_norm2'):
        x = tools.batch_norm(x)
    x = tools.FC_layer('fc8', x, out_nodes=n_class)

    return x

及以下是“工具”的详细信息。

def conv(layer_name, x, out_channels, kernel_size=None, stride=None, is_pretrain=True):
    """
Convolution op wrapper, the Activation id ReLU
:param layer_name: layer name, eg: conv1, conv2, ...
:param x: input tensor, size = [batch_size, height, weight, channels]
:param out_channels: number of output channel (convolution kernel)
:param kernel_size: convolution kernel size, VGG use [3,3]
:param stride: paper default = [1,1,1,1]
:param is_pretrain: whether you need pre train, if you get parameter from 
other, you don not want to train again, so trainable = false. if not trainable = true
:return: 4D tensor
"""
    kernel_size = kernel_size if kernel_size else [3, 3]
    stride = stride if stride else [1, 1, 1, 1]

    in_channels = x.get_shape()[-1]

    with tf.variable_scope(layer_name):
        w = tf.get_variable(name="weights",
                        shape=[kernel_size[0], kernel_size[1], in_channels, out_channels],
                        dtype=tf.float32,
                        initializer=tf.contrib.layers.xavier_initializer(),
                        trainable=is_pretrain)
        b = tf.get_variable(name='biases',
                        shape=[out_channels],
                        dtype=tf.float32,
                        initializer=tf.constant_initializer(0.0),
                        trainable=is_pretrain)
        x = tf.nn.conv2d(x, w, stride, padding='SAME', name='conv')
        x = tf.nn.bias_add(x, b, name='bias_add')
        x = tf.nn.relu(x, name='relu')

    return x

0 个答案:

没有答案