简体   繁体   English

层“dense3”的输入 0 与层不兼容:预期 min_ndim=2,发现 ndim=1。 收到的完整形状:(4096,)

[英]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) 

Please find the sample code here .请在此处找到示例代码。 Thank you谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “密集”层的输入 0 与该层不兼容:预期 min_ndim=2,发现 ndim=1。 完整形状收到:(32,) - Input 0 of layer "dense" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (32,) ValueError:层密集的输入 0 与层不兼容::预期 min_ndim=2,发现 ndim=1。 收到的完整形状:[无] - ValueError: Input 0 of layer dense is incompatible with the layer: : expected min_ndim=2, found ndim=1. Full shape received: [None] 层序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=2。 收到的完整形状:(无,1) - Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (None, 1) 层“conv1d_5”的输入 0 与层不兼容:预期 min_ndim=3,发现 ndim=1。 收到完整形状:(无,)预测新值时 - Input 0 of layer "conv1d_5" is incompatible with the layer: expected min_ndim=3, found ndim=1. Full shape received: (None,) when predicting new values ValueError:conv2d 层的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(2240、70、3) - ValueError: Input 0 of layer conv2d is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (2240, 70, 3) ValueError:layersequential_5 的输入 0 与 layer 不兼容::预期 min_ndim=4,发现 ndim=2。 收到的完整形状:[无,953] - ValueError: Input 0 of layer sequential_5 is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 953] ValueError:顺序层的输入 0 与层不兼容::预计 min_ndim=4,发现 ndim=3。 收到完整形状:[8, 28, 28] - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28] ValueError: 层顺序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=2。 收到完整形状:[无,1] - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 1] ValueError: 层序列 9 的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[无,无,无] - ValueError: Input 0 of layer sequential_9 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, None, None] ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2。 收到的完整形状:(10, 24) - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (10, 24)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM