python tflearn - ValueError: Cannot feed value of shape (10, 250, 250, 3) for Tensor 'TargetsData/Y:0', which has shape '(?, 2)'

python tflearn - ValueError: Cannot feed value of shape (10, 250, 250, 3) for Tensor 'TargetsData/Y:0', which has shape '(?, 2)'

我正在尝试创建一个模型来识别人脸。但是我将 运行 保留在这个错误中,并且 none 类似问题的其他答案已经解决了这个特定问题。代码如下:

X = pickle.load(open('dataset.pkl', 'rb')).astype('float32')
Y = pickle.load(open('dataset.pkl', 'rb')).astype('float32')
X_test = pickle.load(open('dataset.pkl', 'rb')).astype('float32')
Y_test = pickle.load(open('dataset.pkl', 'rb')).astype('float32')

# Input is a 250x250 image with 3 color channels (red, green and blue)       

network = input_data(shape=[None, 250, 250, 3],
                 data_preprocessing=img_prep,
                 data_augmentation=img_aug)

# Step 1: Convolution
network = conv_2d(network, 32, 3, activation='relu')

# Step 2: Max pooling
network = max_pool_2d(network, 2)

# Step 3: Convolution again
network = conv_2d(network, 64, 3, activation='relu')

# Step 4: Convolution yet again
network = conv_2d(network, 64, 3, activation='relu')

# Step 5: Max pooling again
network = max_pool_2d(network, 2)

# Step 6: Fully-connected 512 node neural network
network = fully_connected(network, 512, activation='relu')

# Step 7: Dropout - throw away some data randomly during training to prevent over-fitting
network = dropout(network, 0.5)

# Step 8: Fully-connected neural network with two outputs to make the final prediction
network = fully_connected(network, 2, activation='softmax')

# Tell tflearn how we want to train the network
network = regression(network, optimizer='adam',
                 loss='categorical_crossentropy',
                 learning_rate=0.001)

# Wrap the network in a model object
model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='faceRecog.tfl.ckpt')

# Train it! We'll do 100 training passes and monitor it as it goes.
model.fit(X, Y, n_epoch=10, shuffle=True, validation_set=(X_test, Y_test),
      show_metric=True, batch_size=10,
      snapshot_epoch=True,
      run_id='faceRecog')

我不断收到

ValueError: Cannot feed value of shape (10, 250, 250, 3) for Tensor 'TargetsData/Y:0', which has shape '(?, 2)'.

此时我已经尝试了所有方法,但无法完全理解如何解决该问题。

你的输入是 (?, 250, 250, 3) 的形状(基于开头的评论和你早期使用卷积层的事实),你的输出是 (?, 2) 的形状(基于快点,你的最后一层是一个全连接层,有 2 个输出神经元)。然而,您将相同的数据集提供给两者:

X = pickle.load(open('dataset.pkl', 'rb')).astype('float32')
Y = pickle.load(open('dataset.pkl', 'rb')).astype('float32')

^^ 请注意,您为 XY 加载了相同的文件。

由于我不知道你想达到什么目的,所以有两种可能的解决方案:

  1. 如果您正在尝试构建某种自动编码器(在这种情况下,将相同的数据集同时输入和输出是有意义的),您需要更改网络的架构,卷积层应该馈入反卷积层。如何做到这一点超出了单个 Stack Overflow 答案的范围

  2. 如果您正在尝试构建某种分类器,那么您没有阅读 Y 的正确文件。 Y 应该包含您要预测的标签,而不是图像。