Tensorflow:图像分类的损失和精度保持不变

2024-04-19 20:08:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我把这个Tensorflow教程复制/粘贴到一个Jupyter笔记本上。(在撰写本文时,他们将教程改为花卉数据集,而不是狗数据集,但问题仍然存在)。 https://www.tensorflow.org/tutorials/images/classification

第一部分(没有增加)运行良好,我得到了类似的结果

但随着数据的增加,我的损失和准确性在所有时代都保持不变。我已经检查了这些帖子,所以: Keras accuracy does not changeHow to fix flatlined accuracy and NaN loss in tensorflow image classificationTensorflow: loss decreasing, but accuracy stable

所有这些都不适用,因为数据集是标准的,所以我没有数据损坏的问题,加上我打印了两张增强的图像,效果很好(见下文)

我已经尝试添加更多完全连接的层以增加模型容量,退出以限制过度拟合,。。。这条曲线没有任何变化:

你知道为什么吗?我在代码中遗漏了什么吗? 我知道训练DL模型需要大量的尝试和错误,但我确信除了随机地转动旋钮直到发生什么事情之外,还必须有一些逻辑或直觉

谢谢

enter image description here

来源数据: _URL='1〕https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip"

path_to_zip = tf.keras.utils.get_file('cats_and_dogs.zip', origin=_URL, extract=True)

PATH = os.path.join(os.path.dirname(path_to_zip), 'cats_and_dogs_filtered')

参数:

batch_size = 128
epochs = 15
IMG_HEIGHT = 150
IMG_WIDTH = 150

预处理阶段:

image_gen = ImageDataGenerator(rescale=1./255,
    rotation_range=20,
    width_shift_range=0.15,
    height_shift_range=0.15,
    horizontal_flip=True,
    zoom_range=0.2)

train_data_gen = image_gen.flow_from_directory(batch_size=batch_size,
                                               directory=train_dir,
                                               shuffle=True,
                                               target_size=(IMG_HEIGHT, IMG_WIDTH))

augmented_images = [train_data_gen[0][0][i] for i in range(5)]
plotImages(augmented_images)

image_gen_val = ImageDataGenerator(rescale=1./255)

val_data_gen = image_gen_val.flow_from_directory(batch_size=batch_size,
                                                 directory=validation_dir,
                                                 target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                 class_mode='binary')

enter image description here

型号:

model_new = Sequential([
    Conv2D(16, 2, padding='same', activation='relu', 
           input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),
    MaxPooling2D(),
    Conv2D(32, 2, padding='same', activation='relu'),
    MaxPooling2D(),
    Conv2D(64, 2, padding='same', activation='relu'),
    MaxPooling2D(),
    Dropout(0.2),
    Flatten(),
    Dense(512, activation='relu'),
    Dense(1)
])

model_new.compile(optimizer='adam',
                  loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
                  metrics=['accuracy'])

model_new.summary()

history = model_new.fit(
    train_data_gen,
    steps_per_epoch= total_train // batch_size,
    epochs=epochs,
    validation_data=val_data_gen,
    validation_steps= total_val // batch_size
)

Tags: and数据pathimagetrueimgdatasize
1条回答
网友
1楼 · 发布于 2024-04-19 20:08:01

正如@today所建议的,训练数据生成器中缺少class_method='binary' 现在,模型能够正常训练

train_data_gen = image_gen.flow_from_directory(batch_size=batch_size,
                                               directory=train_dir,
                                               shuffle=True,
                                               target_size=(IMG_HEIGHT, IMG_WIDTH),
                                               class_method = 'binary')

相关问题 更多 >