keras 数据基数不明确,请确保所有数组包含相同数量的样本

9rygscc1  于 2023-04-06  发布在  其他
关注(0)|答案(1)|浏览(88)

我有一个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.

我可以做什么来修复这个错误?

nhn9ugyo

nhn9ugyo1#

在回答你的问题之前,我假设你的示例模型需要像下面这样。(查看如何使用fit方法,https://keras.io/api/models/model_training_apis/

# Model construction
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(2, activation="softmax"), # n_classes
    ]
)

# Model compile
model_cnn.compile(
    # outputs are softmax results from one-hot encoded labels, thus not sparse one
    loss=tf.keras.losses.CategoricalCrossentropy(from_logits=False),
    optimizer="adam",
    metrics=["accuracy"]
)

# (Optional) Visualize model structure and parameters
model_cnn.summary()

# Model train & val
model_cnn.fit(
    # swap `val_files` and `train_label`
    x=train_files,    # `Input data` with shape (train_size,32,32,3)
    y=train_label,    # `Input data labels` with shape (train_size,2)
    epochs=5, 
    batch_size=32,    # If unspecified, batch_size will default to 32
    validation_data=(
        val_files,    # `Valid data` with shape (val_size,32,32,3)
        val_label     # `Valid data labels` with shape (val_size,2)
    )
)

然而,模型期望接收RGB(3通道)中32x32像素的图像。如果您将形状为(32,32,3)的NOT图像作为输入,则模型可能会抛出另一个错误消息。

相关问题