为什么在尝试使用train_generator和fit_generator对图像进行分类时出现形状错误?

时间:2019-03-17 12:04:01

标签: python image tensorflow classification

运行以下代码时。我收到一个错误消息:

ValueError: Error when checking target: expected dense_1 to have shape (1,) but got array with shape (3,).

有人可以给我提示吗?

代码

train_datagen = ImageDataGenerator(rescale=1.0/255)
train_generator = train_datagen.flow_from_directory(
        './marques', 
        target_size=(300, 300),
        color_mode="rgb",
        batch_size=16,
        class_mode='categorical')

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(8, (7,7), activation='relu', input_shape=(300, 300,3)), 
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit_generator(train_generator, steps_per_epoch=16, epochs=1, verbose=1)

0 个答案:

没有答案