python - 检查目标时出错:预期 dense_1 的形状为 (5749,) 但得到的数组的形状为 (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, )
在错误中?
注意 - 我已经看到下面给出的链接,但它们没有解决我的问题。
解决方案
快速浏览一下,我认为您的问题如下:您的网络将形状张量 (batch_size, 250, 250, 1) 作为输入,并将形状张量 (batch_size, 5749) 作为输出
当您开始拟合时,keras 会批量输入数据和相应的标签,因此它会为您的网络输入张量提供形状 (batch_size,) + imgs_array.shape[1:] 和形状标签 (batch_size,) + Y.shape[1 :]
也就是说,对于批次中的每个图像,您的标签是一个标量(或者显然是一个一维向量,我没有仔细阅读)并且您与之比较的输出是一个大小为 5749 的张量,这不是标量。
推荐阅读
- c# - 一段时间内没有收到任何消息时取消注册事件处理器
- ios - 如何在 Xcode 控制台中禁用 Firebase/Analytics 登录?
- python - Python:PYODBC 连接链接错误 08S01
- angular - 如何在 Angular 原理图中使用当前路径
- python - 如何在 pandas 合并期间使用多个 where 条件
- node.js - 如何使用 npm 在本地网络中安装所有节点模块?
- node.js - 将我的 socket.io 从 http 转换为 https 后,我的 javascript 给了我一个奇怪的结果
- angular - 如何删除 Angular 默认警报错误消息
- javascript - Javascript:一个接一个地执行功能
- ios - UITableView.reloadRows 在 Timer.scheduledTimer 中调用时重置行动画类型