[英]Tensorflow: loss and accuracy stay flat training CNN on image classification
I copied / pasted this Tensorflow tutorial into a Jupyter notebook.我将这个 Tensorflow 教程复制/粘贴到 Jupyter 笔记本中。 (As of this writting they changed the tutorial to the flower data set instead of the dog one, but the question still applies).
(在撰写本文时,他们将教程更改为花数据集而不是狗数据集,但问题仍然存在)。 https://www.tensorflow.org/tutorials/images/classification
https://www.tensorflow.org/tutorials/images/classification
The first part (without augmentation) runs fine and I get similar results.第一部分(没有增强)运行良好,我得到了类似的结果。
But with data augmentation, my Loss and Accuracy stay flat across all epoch.但是通过数据增强,我的损失和准确性在所有时期都保持不变。 I've checked this posts already on SO: Keras accuracy does not change How to fix flatlined accuracy and NaN loss in tensorflow image classification Tensorflow: loss decreasing, but accuracy stable
我已经在 SO 上检查过这些帖子: Keras 精度没有改变如何修复 tensorflow 图像分类中的扁平精度和 NaN 损失Tensorflow:损失减少,但精度稳定
None of this applied, since the dataset is a standard one, I don't have the problem of corrupted data, plus I printed a couple of images augmented and it works fine (see below).这些都没有应用,因为数据集是标准数据集,所以我没有数据损坏的问题,而且我打印了几张增强的图像并且工作正常(见下文)。
I've tried adding more fully connected layers to increase the model capacity, dropout to limit over fitting,... nothing change here are the curve:我已经尝试添加更多完全连接的层以增加 model 容量,dropout 以限制过度拟合,...这里没有任何变化是曲线:
Any ideas as to why?关于为什么的任何想法? Have I missed something in the code?
我错过了代码中的某些内容吗? I know training a DL model is a lot of trial and error, but I'm sure there must be some logic or intuition beyond randomly turning the knobs until something happens.
我知道训练 DL model 需要大量的试验和错误,但我确信除了随机转动旋钮直到发生某些事情之外,一定有一些逻辑或直觉。
Thanks !谢谢 !
Source Data: _URL = 'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'源数据:_URL = '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')
Params:参数:
batch_size = 128
epochs = 15
IMG_HEIGHT = 150
IMG_WIDTH = 150
Preprocessing stage:预处理阶段:
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')
Model: Model:
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
)
As suggested by @today, class_method= 'binary' was missing from the training data generator Now the model is able to train properly.正如@today 所建议的,训练数据生成器中缺少 class_method= 'binary' 现在 model 能够正确训练。
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.