[英]Input 0 of layer "dense3" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (4096,)
I am pretty new to keras and machine learning, but my code is not working and I am pretty sure it is because of my model.我对 keras 和机器学习很陌生,但是我的代码不起作用,我很确定这是因为我的 model。 I am trying to train my model with a csv file that has my images all flattened into one row.
我正在尝试使用 csv 文件训练我的 model 文件,该文件将我的图像全部展平为一行。 Essentially, each row of data is one image.
本质上,每一行数据就是一个图像。 However, this error message keeps popping up
但是,此错误消息不断弹出
ValueError: Exception encountered when calling layer "sequential" (type Sequential). ValueError:调用层“sequential”(类型 Sequential)时遇到异常。
Input 0 of layer "dense3" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (4096,)
Call arguments received by layer "sequential" (type Sequential):
• inputs=tf.Tensor(shape=(4096,), dtype=uint8)
• training=False
• mask=None
I have looked online and tried a lot of things, but I am new, so I do not completely understand everything.我在网上看了很多东西,但我是新手,所以我并不完全了解所有内容。 The image is 64x64, so I assumed that the columns were the input size.... so set the input size equal to 64 64 and also changed the predict batch size to be 64 64 that way it matches (since I am getting the real-time flattened image to be predicted).
图像是 64x64,所以我假设列是输入大小....所以将输入大小设置为 64 64 并将预测批量大小更改为 64 64 以匹配它(因为我得到了真实的-time flattened image to be predict)。 It solved the problem for the first layer (the input layer), but now the dense layers after are not working.
它解决了第一层(输入层)的问题,但现在之后的密集层不起作用。 I also have the image label at the first index of every row on the csv file.
我还在 csv 文件的每一行的第一个索引处有图像 label。
def modelTraining():
model = keras.models.Sequential()
model.add(keras.Input(shape=(4096,)))
model.add(keras.layers.Dense(4096, activation='relu', name = "dense2"), )
model.add(keras.layers.Dense(1, activation='softmax', name = "dense3", ))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
#Get our training image data
trainingImageMatrix = []
trainingLabelsArray = []
temp = 0
with open(r'C:\Users\Jason\Desktop\signLanguage\testOpenCV\training.csv', 'r', newline='') as myFile:
reader = csv.reader(myFile, delimiter=',')
for row in reader:
temp+=1
imageDataToAdd = np.zeros(4096)
for i in range (1, 4097):
imageDataToAdd[i - 1] = row[i]
trainingLabelsArray.append(int(row[0])) #need to get the first index of every row
trainingImageMatrix.append(imageDataToAdd)
print("reading row: "+str(temp))
#if temp >=10:
#break
trainingImageMatrix = np.array(trainingImageMatrix)
trainingLabelsArray = np.array(trainingLabelsArray)
model.fit(trainingImageMatrix, trainingLabelsArray, epochs = 20, batch_size = 4096)
return model
model.predict(grayFrame.flatten(), batch_size = 4096)
this line is in another part of my code, but this is the predict I am using atm.这一行在我的代码的另一部分,但这是我使用 atm 的预测。
Would appreciate some help and clarification on how to specify the size and how the layers should be customized for my task.希望对如何指定大小以及如何为我的任务自定义图层提供一些帮助和说明。
each row has the 0th index as a label and the rest is 64*64 worth of pixel brightness ---- CSV FILE IMAGE每行的第 0 个索引为 label 和 rest 的像素亮度为 64*64 ---- CSV 文件图像
I have used this dataset and converted the npy file to csv and replicated the issue.我已使用此数据集并将 npy 文件转换为 csv 并复制了该问题。 grayFrame.flatten() gives the output of 1 dimension.
grayFrame.flatten() 给出一维的 output。 Adding an extra dimension will solve the error.
添加额外的维度将解决错误。 Replace the following line
替换以下行
model.predict(grayFrame.flatten(), batch_size = 4096)
with the line below与下面的行
model.predict(tf.expand_dims(grayFrame.flatten(),axis=0), batch_size = 4096)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.