将预训练的 CNN 从二元分类更新为多分类时出现输入形状错误

问题描述 投票:0回答:1

我有一个包含 3 类图像的数据集,细分为训练/验证/测试文件夹:

new_base_dir = '/Users/.../img_dir'
import os
from tensorflow.keras.utils import image_dataset_from_directory
  
train_dataset = image_dataset_from_directory(
    os.path.join(new_base_dir, 'train'),
    image_size=(180, 180),
    batch_size=10)
validation_dataset = image_dataset_from_directory(
    os.path.join(new_base_dir, 'validation'),
    image_size=(180, 180),
    batch_size=10)
test_dataset = image_dataset_from_directory(
    os.path.join(new_base_dir, 'test'),
    image_size=(180, 180),
    batch_size=10)

我确认有 3 类:

train_dataset
Found 3000 files belonging to 3 classes
<BatchDataset element_spec=(TensorSpec(shape=(None, 180, 180, 3), dtype=tf.float32, name=None), TensorSpec(shape=(None,), dtype=tf.int32, name=None))>

我首先将图像加载到预训练的

Xception
模型中:

# load a pre-trained model
import keras
from keras import layers
from keras.applications import VGG16

conv_base = keras.applications.xception.Xception(
    weights="imagenet",
    include_top=False,
    input_shape=(180, 180, 3))

import numpy as np
  
def get_features_and_labels(dataset):
    all_features = []
    all_labels = []
    for images, labels in dataset:
        preprocessed_images = keras.applications.xception.preprocess_input(images)
        features = conv_base.predict(preprocessed_images)
        all_features.append(features)
        all_labels.append(labels)
    return np.concatenate(all_features), np.concatenate(all_labels)
  
train_features, train_labels =  get_features_and_labels(train_dataset)
val_features, val_labels =  get_features_and_labels(validation_dataset)
test_features, test_labels =  get_features_and_labels(test_dataset)

但是,当我在密集连接的分类器上分层时,我收到错误:

inputs = keras.Input(shape=(6, 6, 2048))
x = layers.Flatten()(inputs)             
x = layers.Dense(256)(x)
x = layers.Dropout(0.5)(x)
# the commented lines work fine for a binary classification model
#outputs = layers.Dense(1, activation="sigmoid")(x)
#model.compile(loss="binary_crossentropy",
#              optimizer="rmsprop",
#              metrics=["accuracy"])
outputs = layers.Dense(3, activation="softmax")(x)
model = keras.Model(inputs, outputs)
model.compile(loss="categorical_crossentropy",
              optimizer="rmsprop",
              metrics=["accuracy"])

这是错误:

ValueError: Shapes (None, 1) and (None, 3) are incompatible

python tensorflow keras deep-learning conv-neural-network
1个回答
0
投票

您的标签数据不是分类的,请将 get_features_and_labels 返回输出修改为

return np.concatenate(all_features), np_utils.to_categorical(np.concatenate(all_labels))
© www.soinside.com 2019 - 2024. All rights reserved.