【问题标题】:Tensorflow model validation accuracy not increasingTensorflow 模型验证准确度没有增加
【发布时间】:2020-09-16 20:04:41
【问题描述】:

我已经建立了一个 tensorflow 模型,并且在不同时期的验证准确性没有任何变化,这让我相信我的设置有问题。下面是我的代码。

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import regularizers
import tensorflow as tf

model = Sequential()
model.add(Conv2D(16, (3, 3), input_shape=(299, 299,3),padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))

model.add(Conv2D(32, (3, 3),padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))

model.add(Conv2D(64, (3, 3),padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))

model.add(Conv2D(64, (3, 3),padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))

# this converts our 3D feature maps to 1D feature vectors
model.add(Flatten())  

model.add(Dense(512))
model.add(Activation('relu'))

model.add(Dropout(0.5))

model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

batch_size=32

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
        rescale=1./255,
        # shear_range=0.2,
        # zoom_range=0.2,
        horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1./255)

# this is a generator that will read pictures found in
# subfolers of 'data/train', and indefinitely generate
# batches of augmented image data
train_generator = train_datagen.flow_from_directory(
        'Documents/Training',  # this is the target directory
         target_size=(299, 299),  #all images will be resized to 299
        batch_size=batch_size,
        class_mode='binary')  # since we use binary_crossentropy loss, we need binary labels

# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
        'Documents/Dev',
        target_size=(299, 299),
        batch_size=batch_size,
        class_mode='binary')
#w1 =  tf.Variable(tf.truncated_normal([784, 30], stddev=0.1))
model.fit_generator(
        train_generator,
        steps_per_epoch=50 // batch_size,
        verbose = 1,
        epochs=10,
        validation_data=validation_generator,
        validation_steps=8 // batch_size)

当我运行时会产生以下输出。就我的架构或数据生成步骤而言,我在这里缺少什么?我已经引用了Tensorflow model accuracy not increasingaccuracy not increasing in tensorflow model 还没有成功。

Epoch 1/10
3/3 [==============================] - 2s 593ms/step - loss: 0.6719 - accuracy: 0.6250 - val_loss: 0.8198 - val_accuracy: 0.5000
Epoch 2/10
3/3 [==============================] - 2s 607ms/step - loss: 0.6521 - accuracy: 0.6667 - val_loss: 0.8518 - val_accuracy: 0.5000
Epoch 3/10
3/3 [==============================] - 2s 609ms/step - loss: 0.6752 - accuracy: 0.6250 - val_loss: 0.7129 - val_accuracy: 0.5000
Epoch 4/10
3/3 [==============================] - 2s 611ms/step - loss: 0.6841 - accuracy: 0.6250 - val_loss: 0.7010 - val_accuracy: 0.5000
Epoch 5/10
3/3 [==============================] - 2s 608ms/step - loss: 0.6977 - accuracy: 0.5417 - val_loss: 0.6551 - val_accuracy: 0.5000
Epoch 6/10
3/3 [==============================] - 2s 607ms/step - loss: 0.6508 - accuracy: 0.7083 - val_loss: 0.5752 - val_accuracy: 0.5000
Epoch 7/10
3/3 [==============================] - 2s 615ms/step - loss: 0.6596 - accuracy: 0.6875 - val_loss: 0.9326 - val_accuracy: 0.5000
Epoch 8/10
3/3 [==============================] - 2s 604ms/step - loss: 0.7022 - accuracy: 0.6458 - val_loss: 0.6976 - val_accuracy: 0.5000
Epoch 9/10
3/3 [==============================] - 2s 591ms/step - loss: 0.6331 - accuracy: 0.7292 - val_loss: 0.9571 - val_accuracy: 0.5000
Epoch 10/10
3/3 [==============================] - 2s 595ms/step - loss: 0.6085 - accuracy: 0.7292 - val_loss: 0.6029 - val_accuracy: 0.5000
Out[24]: <keras.callbacks.callbacks.History at 0x1ee4e3a8f08>

【问题讨论】:

    标签: python tensorflow image-processing conv-neural-network


    【解决方案1】:

    您正在设置每个 epoch =50//32=1 的训练步数。那么你只有 50 个训练图像吗?同样对于验证,您有步骤 = 8//32=0。你只有 8 个验证图像吗?当您执行程序时,训练和验证生成器会打印出他们找到的多少张图像?您将需要更多的图像。尝试将批量大小设置为 =1

    【讨论】:

    • 我有 8500 张训练图像和 500 张验证图像。步骤的值应该是什么作为起点?我不确定我从哪里得到了我一直在玩弄很多东西试图弄清楚发生了什么的逻辑。
    • 尝试 batch_size=50 和 step per epoch = 170 这样 170 X 50 =8500 这样你就可以在每个 epoch 完成一次训练集。设置验证批量大小 = 50 和步骤 =10,这样您每个时期都会检查一次验证集
    猜你喜欢
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2021-07-27
    • 2020-03-29
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 2021-09-28
    相关资源
    最近更新 更多