tensorflow ValueError:检查目标时出错:预期activation_6具有形状(70,),但获得的阵列具有形状(71,)

ukdjmx9f  于 2023-01-26  发布在  其他
关注(0)|答案(2)|浏览(106)

我正在使用CNN创建人脸识别。我正在学习教程。我使用的是Tensorflow == 1.15。
该程序将采取70快照用户的脸,并保存在文件夹'数据集'
我一直收到错误:
ValueError:检查目标时出错:预期activation_6具有形状(70,),但获得的阵列具有形状(71,)
输入形状-(32,32,1)
类(n_类)-70

K.clear_session()
n_faces = len(set(ids))

model = model((32,32,1),n_faces) #Calling Model given in next code block
faces = np.asarray(faces)
faces = np.array([downsample_image(ab) for ab in faces])
ids = np.asarray(ids)
faces = faces[:,:,:,np.newaxis]
print("Shape of Data: " + str(faces.shape))
print("Number of unique faces : " + str(n_faces))

ids = to_categorical(ids)

faces = faces.astype('float32')
faces /= 255.

x_train, x_test, y_train, y_test = train_test_split(faces,ids, test_size = 0.2, random_state = 0)

print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)

checkpoint = callbacks.ModelCheckpoint('trained_model.h5', monitor='val_acc',
                                           save_best_only=True, save_weights_only=True, verbose=1)
                                    
model.fit(x_train, y_train,
             batch_size=32,
             epochs=10,
             validation_data=(x_test, y_test),
             shuffle=True,callbacks=[checkpoint])
def model(input_shape,num_classes):    

    model = Sequential()

    model.add(Conv2D(32, (3, 3), input_shape=input_shape))
    model.add(Activation("relu"))

    model.add(Conv2D(64, (3, 3)))
    model.add(BatchNormalization())
    model.add(Activation("relu"))

    model.add(Conv2D(64, (1, 1)))
    model.add(Dropout(0.5))
    model.add(BatchNormalization())
    model.add(Activation("relu"))

    model.add(Conv2D(128, (3, 3)))
    model.add(Dropout(0.5))
    model.add(Activation("relu"))

    model.add(MaxPooling2D(pool_size=(2,2)))

    model.add(Conv2D(64, (1, 1)))
    model.add(Activation("relu"))

    model.add(Flatten())
    model.add(Dense(32))
    model.add(Dense(num_classes))
    model.add(Activation("softmax"))
    
    model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

    model.summary()
    return model

产出

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 30, 30, 32)        320       
_________________________________________________________________
activation_1 (Activation)    (None, 30, 30, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 28, 28, 64)        18496     
_________________________________________________________________
batch_normalization_1 (Batch (None, 28, 28, 64)        256       
_________________________________________________________________
activation_2 (Activation)    (None, 28, 28, 64)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 28, 28, 64)        4160      
_________________________________________________________________
dropout_1 (Dropout)          (None, 28, 28, 64)        0         
_________________________________________________________________
batch_normalization_2 (Batch (None, 28, 28, 64)        256       
_________________________________________________________________
activation_3 (Activation)    (None, 28, 28, 64)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 26, 26, 128)       73856     
_________________________________________________________________
dropout_2 (Dropout)          (None, 26, 26, 128)       0         
_________________________________________________________________
activation_4 (Activation)    (None, 26, 26, 128)       0         
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 13, 13, 128)       0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 13, 13, 64)        8256      
_________________________________________________________________
activation_5 (Activation)    (None, 13, 13, 64)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 10816)             0         
_________________________________________________________________
dense_1 (Dense)              (None, 32)                346144    
_________________________________________________________________
dense_2 (Dense)              (None, 70)                2310      
_________________________________________________________________
activation_6 (Activation)    (None, 70)                0         
=================================================================
Total params: 454,054
Trainable params: 453,798
Non-trainable params: 256
_________________________________________________________________
Shape of Data: (70, 32, 32, 1)
Number of unique faces : 70

我正在计算x_train、x_test、y_train、y_test,如下所示

x_train, x_test, y_train, y_test = train_test_split(faces,ids, test_size = 0.2, random_state = 0)

产出
x_序列-(56、32、32、1)
y_列车-(56,71)
x_测试-(14、32、32、1)
y_检验-(14,71)
我做错了什么与CNN层的尺寸?请帮助

sqxo8psd

sqxo8psd1#

在model.summary()输出中,您可以看到最终的密集层具有shape(None,70),None代表批量大小,目前尚不清楚。70是每张图像输出的维数。
根据y_train和y_pred,您似乎希望输出71个类,而不是70个类,因此维度不匹配。

model.add(Dense(num_classes+1))

这应该会起作用。我不知道为什么你的y值的长度和你的类数不一样。一个原因可能是,有一个类是“nothing”,所以应该在其他类中选择的类是正确的。这可以解释为什么你有70个类,你需要一个71维的输出。

gwbalxhn

gwbalxhn2#

我怀疑ids具有(70,71)的形状(行,列)-其中70是示例数,71是类的softmax向量(我通过添加x_train.shape[0]=56和x_test.shape[0]=14得到这个值)。
n_faces = len(set(ids))这一行中,set方法正在检查唯一列表(每个类的softmax向量),然后len方法给您提供示例数,即70。
train_test_split中,y参数是整个ids,因此它沿着行分割(70个示例),同时保留每个示例的softmax向量(71维向量)。
这可以解释为什么模型具有70维输出,而实际上需要71维输出。

相关问题