检查目标时出错:预期density_1具有形状(5749,),但形状为(1,)的数组

问题描述 投票:0回答:1

我是深度学习的初学者,正在构建一个程序,该程序可以根据其图像确定人。但是我的神经网络显示错误,我不知道如何解决-

model.fit(imgs_array,Y,batch_size = 401, epochs = 2, validation_split = 0.2)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1527, in fit
    x, y, sample_weights = self._standardize_user_data(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 991, in _standardize_user_data
    x, y, sample_weights = self._standardize_weights(x, y, sample_weight,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1149, in _standardize_weights
    y = training_utils.standardize_input_data(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/training_utils.py", line 329, in standardize_input_data
    raise ValueError(
ValueError: Error when checking target: expected dense_1 to have shape (5749,) but got array with shape (1,)

我的完整代码是

import os
import numpy as np
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from sklearn.model_selection import train_test_split
from tensorflow.python import keras
from tensorflow.keras.utils import to_categorical
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropout


folders = os.listdir('lfw/')
# print(len(folders))       #5749
folders_a = np.asarray(folders)

image_files = []
X = []
Y = []
for folder in folders:
    files = os.listdir('lfw/'+folder+'/')       # type of files is a list
    image_files.append(files)
    for file in files:
        X.append(file)
        Y.append(folder)

# print(len(Y))     #13233
img_paths = []
for i in range(0,13233):
    img_paths.append('lfw/'+Y[i]+'/'+X[i])      #img_paths is set now

print(len(img_paths))

imgs = []
for img_path in img_paths:
    img_1 = load_img(img_path, color_mode="grayscale")
    imgs.append(img_to_array(img_1))

    # print(imgs[0].shape)      #(250,250,1)



Y = np.array(Y)
# print(type(Y))
# print(Y.shape)        #(13233,)

#Building Neural Network
imgs_array = np.array(imgs)
imgs_array /= 255


model = Sequential()
model.add(Conv2D(20, kernel_size = 3, activation = 'relu', input_shape =(250,250,1)))
model.add(Conv2D(20, kernel_size =3, activation = 'relu'))
model.add(Flatten())
model.add(Dense(401, activation = 'relu'))
model.add(Dense(5749,activation = 'softmax'))
print("compiling initiated")
model.compile(loss = 'mean_squared_error',optimizer = 'sgd')
print("model compiled")
model.fit(imgs_array,Y,batch_size = 401, epochs = 2, validation_split = 0.2)

错误的原因是什么以及如何解决?上面代码中的print(Y.shape)行为我提供了输出(13233,)。为什么在错误中显示(1, )

注意-我看到了下面给出的链接,但它们没有解决我的问题。

Error when checking target: expected dense_1 to have shape (257, 257) but got array with shape (257, 1)

Error in fitting an RNN LSTM model

Error when checking target: expected dense_1 to have shape (1,) but got array with shape (256,)

python numpy tensorflow machine-learning deep-learning
1个回答
0
投票

快速浏览,我认为您的问题如下:您的网络将形状为(batch_size,250,250,1)的张量作为输入并给出形状为(batch_size,5749)的张量作为输出

[当您开始拟合时,keras会获取成批的输入数据和相应的标签,因此它会为您的形状为(batch_size,)+ imgs_array.shape [1:]的网络输入张量提供数据。和形状标签(batch_size,)+ Y.shape [1:]

即,对于该批次中的每个图像,您的标签都是标量(或者显然是一维矢量,我没有完全读过),并且您与之进行比较的输出是大小为5749的张量。不是标量。

© www.soinside.com 2019 - 2024. All rights reserved.