我正在研究一个新冠肺炎图片分类问题.我有新冠肺炎和正常的CXRS图像.现在我想建立一个前馈神经网络.我写的这段代码一直给我一个错误,我不确定我到底做错了什么.

我的x_train.shape160,256,256,3,x_test40,256,256,3

这就是我收到的错误

ValueError:
Input 0 of layer "sequential_4" is incompatible with the layer:
expected shape=(None, 196608), found shape=(None, 256, 256, 3)

附言:我是机器学习/深度学习的新手.

这是我的代码片段

import numpy as np
from keras.models import Sequential
from keras.layers import Flatten, Dense

# Define the architecture of the FNN model
model = Sequential()
model.add(Flatten(input_shape=(256,256,3)))
model.add(Dense(64, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# Compile the FNN model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Train the FNN model
model.fit(x_train, y_train, epochs=10, batch_size=32)

推荐答案

展平层似乎期望输入具有196608个值的一维数组,等于256X256X3=196608.

使用numpy.reshape()应该可以使图像数组变平. 您可以按如下方式进行操作:

# Flatten the image array values in x_train
flattened_x_train = np.reshape(x_train, (x_train.shape[0], -1))
flattened_x_test = np.reshape(x_test, (x_test.shape[0],-1))

您可以使用这些展平的数组作为FNN的输入.

Python相关问答推荐

在Python中添加期货之间的延迟

取相框中一列的第二位数字

脚注在Python中使用regex导致错误匹配

Python:MultiIndex Dataframe到类似json的字典列表

为什么基于条件的过滤会导致pandas中的空数据框架?

Twilio:CallInstance对象没有来自_的属性'

遵循轮廓中对象方向的计算线

Python -根据另一个数据框中的列编辑和替换数据框中的列值

跟踪我已从数组中 Select 的样本的最有效方法

Python会扔掉未使用的表情吗?

Python 3.12中的通用[T]类方法隐式类型检索

Pandas 有条件轮班操作

使可滚动框架在tkinter环境中看起来自然

如何在Django基于类的视图中有效地使用UTE和RST HTIP方法?

给定高度约束的旋转角解析求解

如何使Matplotlib标题以图形为中心,而图例框则以图形为中心

如何从列表框中 Select 而不出错?

处理具有多个独立头的CSV文件

Flask Jinja2如果语句总是计算为false&

Odoo16:模板中使用的docs变量在哪里定义?