0

我正在尝试创建一个模型来识别面孔。但是我一直遇到这个错误,并且类似问题的其他答案都没有解决这个特定问题。代码如下:

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:无法为具有形状“(?,2)”的张量“TargetsData / Y:0”提供形状(10、250、250、3)的值。

在这一点上我已经尝试了一切,并且不能完全理解如何解决这个问题。

4

1 回答 1

1

Your input is of shape (?, 250, 250, 3) (based on the comment at the beginning and on the fact that you use Convolution layers early on), your output is of shape (?, 2) (based on the fast that your last layer is a fully connected layer with 2 output neurons). Yet, you feed the same dataset to both:

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

^^ Note that you load the same file for both X and Y.

Since I don't know what you try to achieve, there are two possible solutions:

  1. If you are trying to build an auto encoder of some sort (in which case feeding the same data set to both input and output would make sense), you need to change the architecture of your network, the convolution layers should feed into deconvolution layers. How to do it is beyond the scope of a single Stack Overflow answer

  2. If you are trying to build some sort of a classifier, then you are not reading the right file for Y. Y should contain the labels you are trying to predict, not the images.

于 2017-02-03T23:53:20.880 回答