Tensorflow 卷积网络错误:无效参数:logits 和标签必须相同大小:logits_size=[512,4] labels_size=[128,4]

Posted

技术标签:

【中文标题】Tensorflow 卷积网络错误:无效参数:logits 和标签必须相同大小:logits_size=[512,4] labels_size=[128,4]【英文标题】:Tensorflow convolutional net error: Invalid argument: logits and labels must be same size: logits_size=[512,4] labels_size=[128,4] 【发布时间】:2016-08-12 17:14:12 【问题描述】:

我根据此处找到的 5_convolutional_net.py 示例制作了一个卷积网络:https://github.com/nlintz/TensorFlow-Tutorials。我尝试对棋子进行分类。我加载了我的图片:每张图片都有 1136 张 60x60 灰度图像。我将它们分成训练和测试图像,为每一块制作热向量,然后合并它们。所以我的testimages.shape=(40,60,60),testlabels.shape=(40,4),trainimages.shape=(4504,60,60),trainlabels.shape=(4504,4)。 4504=4*(1136-10)

#!/usr/bin/env python
from os import listdir
from os.path import isfile, join
import tensorflow as tf
import numpy as np
# import input_data
import cv2

def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))


def model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden):
    l1a = tf.nn.relu(tf.nn.conv2d(X, w,                       # l1a shape=(?, 28, 28, 32)
                        strides=[1, 1, 1, 1], padding='SAME'))
    l1 = tf.nn.max_pool(l1a, ksize=[1, 2, 2, 1],              # l1 shape=(?, 14, 14, 32)
                        strides=[1, 2, 2, 1], padding='SAME')
    l1 = tf.nn.dropout(l1, p_keep_conv)

    l2a = tf.nn.relu(tf.nn.conv2d(l1, w2,                     # l2a shape=(?, 14, 14, 64)
                        strides=[1, 1, 1, 1], padding='SAME'))
    l2 = tf.nn.max_pool(l2a, ksize=[1, 2, 2, 1],              # l2 shape=(?, 7, 7, 64)
                        strides=[1, 2, 2, 1], padding='SAME')
    l2 = tf.nn.dropout(l2, p_keep_conv)

    l3a = tf.nn.relu(tf.nn.conv2d(l2, w3,                     # l3a shape=(?, 7, 7, 128)
                        strides=[1, 1, 1, 1], padding='SAME'))
    l3 = tf.nn.max_pool(l3a, ksize=[1, 2, 2, 1],              # l3 shape=(?, 4, 4, 128)
                        strides=[1, 2, 2, 1], padding='SAME')
    l3 = tf.reshape(l3, [-1, w4.get_shape().as_list()[0]])    # reshape to (?, 2048)
    l3 = tf.nn.dropout(l3, p_keep_conv)

    l4 = tf.nn.relu(tf.matmul(l3, w4))
    l4 = tf.nn.dropout(l4, p_keep_hidden)

    pyx = tf.matmul(l4, w_o)
    return pyx

def add_images(folder,lista):

    onlyfiles = [f for f in listdir(folder) if isfile(join(folder, f))]
    for file in onlyfiles:
        img = cv2.imread(mypath + file, 0)  # 60x60 numpy ndarray
        lista.append(img)
    return lista

trainimages = []
testimages = []
folders=['TRAININGIMAGES/bw/rooks/','TRAININGIMAGES/bw/knights/','TRAININGIMAGES/bw/bishops/','TRAININGIMAGES/bw/pawns/']

for folder in folders:
    print ( folder)
    onlyfiles = [f for f in listdir(folder) if isfile(join(folder, f))]
    images = []
    for file in onlyfiles:
        img = cv2.imread(folder + file, 0)  # 60x60 numpy ndarray
        images.append(img)
    trainimages.extend(images[10:])
    testimages.extend(images[:10])

size=len(onlyfiles)



trainlabels = []
testlabels = []
rook_label   = np.array([0, 0, 0, 1], dtype=bool)
bishop_label = np.array([0, 0, 1, 0], dtype=bool)
pawn_label   = np.array([0, 1, 0, 0], dtype=bool)
knight_label = np.array([1, 0, 0, 0], dtype=bool)
hotvectors = [rook_label,pawn_label,knight_label,bishop_label]
for label in hotvectors:
    labels=[]
    for x in range(size):
        labels.append(label)
    trainlabels.extend(labels[10:])
    testlabels.extend(labels[:10])

trainimages = np.asarray(trainimages)  # shape : (4544,60,60)
testimages = np.asarray(testimages)
trainlabels = np.asarray(trainlabels)
testlabels = np.asarray(testlabels)

trainimages=trainimages.reshape(-1,60,60,1)
testimages=testimages.reshape(-1,60,60,1)

X = tf.placeholder("float", [None, 60, 60, 1])
Y = tf.placeholder("float", [None, 4])

w = init_weights([3, 3, 1, 32])       # 3x3x1 conv, 32 outputs
w2 = init_weights([3, 3, 32, 64])     # 3x3x32 conv, 64 outputs
w3 = init_weights([3, 3, 64, 128])    # 3x3x32 conv, 128 outputs
w4 = init_weights([128 * 4 * 4, 625]) # FC 128 * 4 * 4 inputs, 625 outputs
w_o = init_weights([625, 4])         # FC 625 inputs, 10 outputs (labels)

p_keep_conv = tf.placeholder("float")
p_keep_hidden = tf.placeholder("float")
py_x = model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y))
train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)
predict_op = tf.argmax(py_x, 1)

with tf.Session() as sess:
    # you need to initialize all variables
    tf.initialize_all_variables().run()
    for i in range(100):
        for start, end in zip(range(0, len(trainimages), 128), range(128, len(trainimages), 128)):
            sess.run(train_op, feed_dict=X: trainimages[start:end], Y: trainlabels[start:end],
                                          p_keep_conv: 0.8, p_keep_hidden: 0.5)

        test_indices = np.arange(len(testimages))  # Get A Test Batch
        np.random.shuffle(test_indices)
        test_indices = test_indices[0:256]

        print(i, np.mean(np.argmax(testlabels[test_indices], axis=1) ==
                         sess.run(predict_op, feed_dict=X: testimages[test_indices],
                                                         Y: testlabels[test_indices],
                                                         p_keep_conv: 1.0,
                                                         p_keep_hidden: 1.0)))

当我运行脚本时,在第 100 行出现以下错误:

tensorflow.python.framework.errors.InvalidArgumentError: logits and labels must be same size: logits_size=[512,4] labels_size=[128,4]
     [[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](MatMul_1, _recv_Placeholder_1_0)]]
Caused by op 'SoftmaxCrossEntropyWithLogits', defined at:
  File "/home/matyi/OneDrive/PYTHON/PYTHON3/chess_vision/5_convolutional_net_chess.py", line 100, in <module>
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y))

我也不明白第 108 行的 128 的作用。你能帮我解决这个问题吗?

图片示例:

【问题讨论】:

128 in the 'zip(range(0, len(trainimages), 128), range(128, len(trainimages), 128))' 是训练批量大小。你能把整个堆栈跟踪吗? 【参考方案1】:

由于您输入 60x60x1 的图像,您的张量形状将是这些:

Tensor("Relu:0", shape=(?, 60, 60, 32), dtype=float32)
Tensor("MaxPool:0", shape=(?, 30, 30, 32), dtype=float32)
Tensor("Relu_1:0", shape=(?, 30, 30, 64), dtype=float32)
Tensor("MaxPool_1:0", shape=(?, 15, 15, 64), dtype=float32)
Tensor("Relu_2:0", shape=(?, 15, 15, 128), dtype=float32)
Tensor("MaxPool_2:0", shape=(?, 8, 8, 128), dtype=float32)

所以你最后的体重 w4 应该是:

w4 = init_weights([128 * 8 * 8, 625])

让我们先试试这个变化。

【讨论】:

感谢它成功了!它现在训练。准确度虽然没有增加,但恰好保持在 0.25。我的图像是订购的,这可能是个问题吗? (1126 辆乌鸦,然后是 1126 辆主教……)因此,对于一批 128 幅图像,大多数情况下所有图像都是相同的(例如乌鸦)。或者我的网络对于这项任务来说是原始的? 我也会对训练集进行洗牌。顺便说一句,你只有 4 个标签? 是的,只有现在的车、主教、棋子和骑士。我会洗牌,谢谢!我添加了图片示例。 告诉我进展如何。这真有趣。我想你会得到 99% 以上的准确率。 你的意思是我有 4 个类别的 4 个标签吗?我有 4544 个标签,每个图像一个

以上是关于Tensorflow 卷积网络错误:无效参数:logits 和标签必须相同大小:logits_size=[512,4] labels_size=[128,4]的主要内容,如果未能解决你的问题,请参考以下文章

TensorFlow一文弄懂CNN中的padding参数

字符型图片验证码,使用tensorflow实现卷积神经网络,进行验证码识别CNN

原创 深度学习与TensorFlow 动手实践系列 - 3第三课:卷积神经网络 - 基础篇

利用Tensorflow实现卷积神经网络模型

TensorFlow-简单的卷积神经网络

基于tensorflow的MNIST手写字识别--白话卷积神经网络模型