Tensorflow convolutional net error: Invalid argument: logits and labels must be same size: 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]

我根据此处的 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的作用。你能帮我吗?

图片示例:

由于您提供 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])

让我们先试试这个改变。