我有一个img文件,每个img都有不同的维度,我把img放入这样的列表中
files = []
for item in images:
img_org = nii_reader(item)
img = sitk.GetArrayFromImage(img_org) # example shape: (9, 40, 43), (6, 46, 45) and more
files.append(img)
train_files, val_files, train_labels, val_labels = train_test_split(files, Label, test_size=0.33, random_state=42)
然后我把标签改成这样的分类
n_classes = 2
train_label = np_utils.to_categorical(train_labels, n_classes)
val_label = np_utils.to_categorical(val_labels, n_classes)
我做cnn的模特
model_cnn = keras.Sequential(
[
layers.Conv2D(32, (3, 3), activation="relu", input_shape=(32, 32, 3)),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Conv2D(64, (3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Conv2D(128, (3, 3), activation="relu"),
layers.Flatten(),
layers.Dense(64, activation="relu"),
layers.Dense(n_classes, activation="softmax"),
]
)
model_cnn.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), optimizer="adam", metrics=["accuracy"])
model_cnn.fit(train_files, val_files, epochs=5, validation_data=(train_label, val_label))
然后我在model_cnn.fit中得到了错误
Data cardinality is ambiguous:
x sizes: 7, 10, 8, 9, 9, 8, 6, 8, 8, 8, 9, 8, 8, 7, 5, 10, 7, 7, 6, 7, 7, 8, 6, 7, 5, 6, 8, 5, 7, 8, 9, 6, 8, 7, 8, 5, 9, 7, 7, 5, 7, 9, 5, 5, 7, 5, 10, 6, 8, 7, 7, 9, 7, 8, 8, 7, 6, 6, 5, 8, 7, 6, 6, 5, 7, 7, 6
y sizes: 6, 7, 6, 9, 7, 8, 6, 6, 9, 9, 7, 7, 6, 10, 6, 4, 9, 9, 7, 6, 5, 7, 7, 7, 7, 7, 8, 6, 8, 6, 7, 7, 5
Make sure all arrays contain the same number of samples.
我可以做什么来修复这个错误?
1条答案
按热度按时间nhn9ugyo1#
在回答你的问题之前,我假设你的示例模型需要像下面这样。(查看如何使用
fit
方法,https://keras.io/api/models/model_training_apis/)然而,模型期望接收RGB(3通道)中32x32像素的图像。如果您将形状为(32,32,3)的NOT图像作为输入,则模型可能会抛出另一个错误消息。