获取tf.nn.sparse_softmax_cross_entropy_with_logits

时间:2017-04-21 13:29:02

标签: python tensorflow neural-network conv-neural-network softmax

我正在使用TensorFlow使用卷积/反卷积来获取单个像素的类,类似于https://arxiv.org/pdf/1505.04366.pdf中描述的架构。但是,我的tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels)函数返回非常高的负值。我正在使用的只有两个类,因此标签的条目为0或1.我的架构是否存在问题?这是我正在运行的代码 -

def new_weights(shape):
return tf.Variable(tf.truncated_normal(shape, mean = 1, stddev=0.05))

def new_biases(length):
    return tf.Variable(tf.constant(0.05, shape=[length]))

def new_conv_layer(input,num_input_channels,filter_size,num_filters,use_pooling=True):
    shape = [filter_size, filter_size, num_input_channels, num_filters]
    weights = new_weights(shape=shape)
    biases = new_biases(length=num_filters)
    layer = tf.nn.conv2d(input=input,
                         filter=weights,
                         strides=[1, 1, 1, 1],
                         padding='SAME')
    layer += biases
    if use_pooling:
        layer = tf.nn.max_pool(value=layer,
                               ksize=[1, 2, 2, 1],
                               strides=[1, 2, 2, 1],
                               padding='SAME')
    layer = tf.nn.relu(layer)
    return layer, weights

def new_deconv_layer(input, num_input_channels, filter_size, num_filters, output_shape, resize_width, use_unpooling=True):
    shape = [filter_size, filter_size, num_filters, num_input_channels]
    weights = new_weights(shape=shape)
    biases = new_biases(length=num_filters)
    layer = tf.nn.conv2d_transpose(value=input,filter=weights,output_shape=output_shape,strides=[1, 1, 1, 1],padding='SAME')
    layer += biases
    if use_unpooling:
        layer = tf.image.resize_images(layer, size = [resize_width, resize_width], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
    layer = tf.nn.relu(layer)
    return layer, weights

这是我的架构 -

batch_size = 164
img_size = 128
img_size_flat = img_size * img_size
img_shape = (img_size, img_size)
num_channels = 3
num_classes = 2
filter_size1 = 5 
num_filters1 = 16
filter_size2 = 5 
num_filters2 = 32
filter_size3 = 5 
num_filters3 = 36
num_filters5 = 36
filter_size5 = 5
num_filters6 = 32
filter_size6 = 5
num_filters7 = 16
filter_size7 = 5
num_filters8 = 2
filter_size8 = 5


layer_conv1, weights_conv1 = new_conv_layer(input=inputs, num_input_channels=num_channels, filter_size=filter_size1, num_filters=num_filters1, use_pooling=True)

layer_conv2, weights_conv2 = new_conv_layer(input=layer_conv1, num_input_channels=num_filters1, filter_size=filter_size2, num_filters=num_filters2, use_pooling=True)

layer_conv3, weights_conv3 = new_conv_layer(input=layer_conv2, num_input_channels=num_filters2, filter_size=filter_size3, num_filters=num_filters3, use_pooling=True)

layer_deconv5, weights_deconv5 = new_deconv_layer(input=layer_conv3, num_input_channels=num_filters5, filter_size=filter_size5, num_filters=num_filters5, output_shape=[batch_size,16,16,36], resize_width=32, use_unpooling=True)

layer_deconv6, weights_deconv6 = new_deconv_layer(input=layer_deconv5, num_input_channels=num_filters5, filter_size=filter_size6, num_filters=num_filters6, output_shape=[batch_size,32,32,32], resize_width=64, use_unpooling=True)

layer_deconv7, weights_deconv7 = new_deconv_layer(input=layer_deconv6, num_input_channels=num_filters6, filter_size=filter_size7, num_filters=num_filters7, output_shape=[batch_size,64,64,16], resize_width=128, use_unpooling=True)

layer_deconv8, weights_deconv8 = new_deconv_layer(input=layer_deconv7, num_input_channels=num_filters7, filter_size=filter_size8, num_filters=num_filters8, output_shape=[batch_size,128,128,2], resize_width=128, use_unpooling=False)

loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=layer_deconv8, labels=labels)

optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(loss)

我是TensorFlow的新手,所以任何帮助都将受到赞赏。非常感谢你!

修改 - 确切的错误消息为Invalid argument: Received a label value of -9191425209310543503 which is outside the valid range of [0, 2)

编辑2 - 基本上,除了RGB图像之外,我还有图像的黑白掩模。如果掩码的像素值不是0,则它属于类1,否则它属于背景(类0)。代码如下 -

for i in range(batch_size):
    for j in range(128):
        for k in range(128):
            if y_true[i][j][k] != 0:
                labels[i][j][k] = 1

0 个答案:

没有答案