验证精度达到一定值并且无论我更改哪个层都不会增加

时间:2019-10-26 15:28:18

标签: python tensorflow keras neural-network deep-learning

我一直试图基于Kaggle数据集中的花朵图像数据集创建一个模型来预测花朵的类型。我已经尝试了层和优化器的各种选项,例如增加转换层的数量或更改过滤器。但是,无论我做了什么更改,验证精度都将达到相同的值,并且不会改变。我认为我在其他地方遇到问题,但我不知道确切的位置。我正在使用的代码如下。

from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing.image import ImageDataGenerator

data_generator_with_aug = ImageDataGenerator(preprocessing_function=preprocess_input,rescale=1./255,
                                              horizontal_flip = True,
                                              width_shift_range = 0.1,
                                              height_shift_range = 0.1,validation_split=0.2)

data_generator_no_aug = ImageDataGenerator(preprocessing_function=preprocess_input,validation_split=0.2)

image_size=240
train_generator = data_generator_with_aug.flow_from_directory(
    '../content/flowers',target_size=(image_size,image_size), batch_size=32, class_mode='categorical',subset='training',color_mode='rgb')

validation_generator = data_generator_with_aug.flow_from_directory(
    '../content/flowers', # same directory as training data
    target_size=(image_size, image_size),
    batch_size=32,
    class_mode='categorical',
    subset='validation')

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Conv2D, MaxPooling2D, Activation

model=Sequential()
model.add(Conv2D(128,input_shape=(image_size,image_size,3), kernel_size=(3,3), activation='relu'))
model.add(Conv2D(128,kernel_size=(3,3),activation='relu'))
model.add(Conv2D(64,kernel_size=(3,3),activation='relu'))
model.add(GlobalAveragePooling2D())
model.add(Dense(128,activation='relu'))
model.add(Dense(6,activation='softmax'))

import keras
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer='adam',
              metrics=['accuracy'])

model.fit_generator(
        train_generator,
        epochs = 20,
        steps_per_epoch=10,
        validation_data=validation_generator)

如下所示的输出

Epoch 1/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.8361 - acc: 0.3426Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 28s 193ms/step - loss: 1.7156 - acc: 0.5009
10/10 [==============================] - 30s 3s/step - loss: 1.7965 - acc: 0.3750 - val_loss: 1.6750 - val_acc: 0.5009
Epoch 2/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.6037 - acc: 0.5648Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 188ms/step - loss: 1.7528 - acc: 0.5009
10/10 [==============================] - 28s 3s/step - loss: 1.6229 - acc: 0.5417 - val_loss: 1.5765 - val_acc: 0.5009
Epoch 3/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.4273 - acc: 0.5278Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 189ms/step - loss: 1.7291 - acc: 0.5009
10/10 [==============================] - 28s 3s/step - loss: 1.4150 - acc: 0.5333 - val_loss: 1.5868 - val_acc: 0.5009
Epoch 4/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.5868 - acc: 0.4630Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 188ms/step - loss: 1.5234 - acc: 0.5009
10/10 [==============================] - 28s 3s/step - loss: 1.5748 - acc: 0.4667 - val_loss: 1.5052 - val_acc: 0.5009
Epoch 5/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.4770 - acc: 0.4815Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 189ms/step - loss: 1.5737 - acc: 0.5009
10/10 [==============================] - 28s 3s/step - loss: 1.4732 - acc: 0.4833 - val_loss: 1.5131 - val_acc: 0.5009
Epoch 6/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.3751 - acc: 0.5741Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 189ms/step - loss: 1.5204 - acc: 0.5009
10/10 [==============================] - 28s 3s/step - loss: 1.3782 - acc: 0.5750 - val_loss: 1.4613 - val_acc: 0.5009
Epoch 7/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.4134 - acc: 0.5093Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 187ms/step - loss: 1.4864 - acc: 0.5084
10/10 [==============================] - 28s 3s/step - loss: 1.4061 - acc: 0.5167 - val_loss: 1.5240 - val_acc: 0.5084
Epoch 8/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.4955 - acc: 0.5648Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 28s 192ms/step - loss: 1.4305 - acc: 0.5009
10/10 [==============================] - 28s 3s/step - loss: 1.4524 - acc: 0.5833 - val_loss: 1.5050 - val_acc: 0.5009
Epoch 9/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.5669 - acc: 0.4352Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 189ms/step - loss: 1.3848 - acc: 0.5020
10/10 [==============================] - 28s 3s/step - loss: 1.5675 - acc: 0.4333 - val_loss: 1.4219 - val_acc: 0.5020
Epoch 10/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.2631 - acc: 0.5556Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 189ms/step - loss: 1.4723 - acc: 0.5014
10/10 [==============================] - 28s 3s/step - loss: 1.2575 - acc: 0.5583 - val_loss: 1.5531 - val_acc: 0.5014
Epoch 11/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.5603 - acc: 0.4074Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 189ms/step - loss: 1.3506 - acc: 0.5345
10/10 [==============================] - 28s 3s/step - loss: 1.5461 - acc: 0.4167 - val_loss: 1.4463 - val_acc: 0.5345
Epoch 12/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.3654 - acc: 0.5278Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 188ms/step - loss: 1.2601 - acc: 0.5339
10/10 [==============================] - 28s 3s/step - loss: 1.3844 - acc: 0.5083 - val_loss: 1.3356 - val_acc: 0.5339
Epoch 13/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.4722 - acc: 0.3889Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 188ms/step - loss: 1.5981 - acc: 0.5113
10/10 [==============================] - 28s 3s/step - loss: 1.4693 - acc: 0.3833 - val_loss: 1.4915 - val_acc: 0.5113
Epoch 14/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.3568 - acc: 0.5648Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 189ms/step - loss: 1.3523 - acc: 0.5009
10/10 [==============================] - 28s 3s/step - loss: 1.3668 - acc: 0.5500 - val_loss: 1.4202 - val_acc: 0.5009
Epoch 15/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.3153 - acc: 0.5556Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 187ms/step - loss: 1.4327 - acc: 0.5009
10/10 [==============================] - 28s 3s/step - loss: 1.3479 - acc: 0.5333 - val_loss: 1.5144 - val_acc: 0.5009
Epoch 16/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.3491 - acc: 0.5278Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 28s 192ms/step - loss: 1.4084 - acc: 0.5009
10/10 [==============================] - 28s 3s/step - loss: 1.3641 - acc: 0.5250 - val_loss: 1.4756 - val_acc: 0.5009
Epoch 17/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.4532 - acc: 0.4444Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 189ms/step - loss: 1.3895 - acc: 0.5009
10/10 [==============================] - 28s 3s/step - loss: 1.4501 - acc: 0.4500 - val_loss: 1.4436 - val_acc: 0.5009
Epoch 18/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.4398 - acc: 0.4815Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 188ms/step - loss: 1.3809 - acc: 0.5009
10/10 [==============================] - 28s 3s/step - loss: 1.4199 - acc: 0.4833 - val_loss: 1.4092 - val_acc: 0.5009
Epoch 19/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.3896 - acc: 0.4630Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 189ms/step - loss: 1.4327 - acc: 0.5032
10/10 [==============================] - 28s 3s/step - loss: 1.3540 - acc: 0.4917 - val_loss: 1.3641 - val_acc: 0.5032
Epoch 20/20
 9/10 [==========================>...] - ETA: 0s - loss: 1.3058 - acc: 0.5833Epoch 1/20
144/10 [================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 27s 188ms/step - loss: 1.4829 - acc: 0.5009
10/10 [==============================] - 28s 3s/step - loss: 1.3294 - acc: 0.5583 - val_loss: 1.3708 - val_acc: 0.5009
<tensorflow.python.keras.callbacks.History at 0x7f4e50dfa7b8>

1 个答案:

答案 0 :(得分:0)

您有多少张图片?如果您的数据集中有超过320张图像,则应将steps_per_epoch=10中的10张更改为批次数量(超过批次大小的数据集中的图像数量),因为每个时期使用10个批次,每个批次仅包含32个批次图片。

您可以将fit_generator更改为:

model.fit_generator(
  train_generator,
  epochs = 20,
  steps_per_epoch=len(train_generator),
  validation_data=validation_generator)