如何分别增加和减少模型精度和批量大小

How to increse and decreses the model accuracy and batch size respectively

İ 正在研究由 12 类 组成的图像数据集的多类分类的迁移学习。因此,我正在使用 VGG19。然而,模型的准确性远低于预期。此外,训练和有效准确度不会增加。除此之外,İ ma 试图减小仍然是 383

的批量大小

我的代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import shutil
import os
import glob as gb
import tensorflow as tf
from tensorflow.keras.models import Sequential
from zipfile import ZipFile
import cv2
from tensorflow.keras.layers import Flatten,Dense,BatchNormalization,Activation,Dropout
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras import optimizers
IMAGE_SHAPE = (256, 256)
BATCH_SIZE = 32
#--------------------------------------------------Train--------------------------------
train = ImageDataGenerator()
train_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale= 1./255, fill_mode= 'nearest')
train_data = train_generator.flow_from_directory(directory="/content/dataset_base/train",target_size=IMAGE_SHAPE , color_mode="rgb" , class_mode='categorical', batch_size=BATCH_SIZE , shuffle = True )

#--------------------------------------------------valid-------------------------------
valid = ImageDataGenerator()
validation_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
valid_data = validation_generator.flow_from_directory(directory="/content/dataset_base/valid", target_size=IMAGE_SHAPE , color_mode="rgb" , class_mode='categorical' , batch_size=BATCH_SIZE , shuffle = True )

#--------------------------------------------------Test---------------------------------------------------------------------------------------------------
test = ImageDataGenerator()
test_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
test_data = test_generator.flow_from_directory(directory="/content/dataset_base/valid",target_size=IMAGE_SHAPE , color_mode="rgb" , class_mode='categorical' , batch_size=1 , shuffle = False )
test_data.reset()

for image_batch, labels_batch in train_data:
  print(image_batch.shape)
  print(labels_batch.shape)
  break

#Defining the VGG Convolutional Neural Net
base_model = VGG19(weights='imagenet', input_shape=(256, 256, 3), include_top=False)

from tensorflow.keras.layers import Flatten,Dense,BatchNormalization,Activation,Dropout
from tensorflow.keras import optimizers

inputs = tf.keras.Input(shape=(256, 256, 3))
x = base_model(inputs, training=False)
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
x = Dense(256, activation='relu')(x)


outputs = Dense(12,activation='softmax')(x)
model = tf.keras.Model(inputs, outputs)

model.summary()

Model: "model_8"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_10 (InputLayer)        [(None, 256, 256, 3)]     0         
_________________________________________________________________
vgg19 (Functional)           (None, 8, 8, 512)         20024384  
_________________________________________________________________
flatten_8 (Flatten)          (None, 32768)             0         
_________________________________________________________________
dense_32 (Dense)             (None, 256)               8388864   
_________________________________________________________________
dense_33 (Dense)             (None, 256)               65792     
_________________________________________________________________
dense_34 (Dense)             (None, 12)                3084      
=================================================================
Total params: 28,482,124
Trainable params: 28,482,124
Non-trainable params: 0

model.compile(optimizer = optimizers.Adam(learning_rate=0.05), loss='categorical_crossentropy', metrics=["accuracy"])

from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
checkpoint = ModelCheckpoint("vgg16_1.h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, period=1)
history = model.fit(
    train_data,
    validation_data=valid_data,
    batch_size = 256,
    epochs=10,
    callbacks=[
        tf.keras.callbacks.EarlyStopping(
            monitor='val_loss',
            patience=55,
            restore_best_weights=True
        )
    ]
)
model_final.save_weights("vgg16_1.h5")

10个epoch后的结果:

Epoch 1/10
383/383 [==============================] - 214s 557ms/step - loss: 2.4934 - accuracy: 0.0781 - val_loss: 2.4919 - val_accuracy: 0.0833
Epoch 2/10
383/383 [==============================] - 219s 572ms/step - loss: 2.4918 - accuracy: 0.0847 - val_loss: 2.4888 - val_accuracy: 0.0833
Epoch 3/10
383/383 [==============================] - 220s 573ms/step - loss: 2.4930 - accuracy: 0.0840 - val_loss: 2.4918 - val_accuracy: 0.0833
Epoch 4/10
383/383 [==============================] - 220s 574ms/step - loss: 2.4919 - accuracy: 0.0842 - val_loss: 2.4934 - val_accuracy: 0.0833
Epoch 5/10
383/383 [==============================] - 220s 573ms/step - loss: 2.4928 - accuracy: 0.0820 - val_loss: 2.4893 - val_accuracy: 0.0833
Epoch 6/10
383/383 [==============================] - 220s 573ms/step - loss: 2.4921 - accuracy: 0.0842 - val_loss: 2.4920 - val_accuracy: 0.0833
Epoch 7/10
383/383 [==============================] - 220s 573ms/step - loss: 2.4922 - accuracy: 0.0858 - val_loss: 2.4910 - val_accuracy: 0.0833
Epoch 8/10
383/383 [==============================] - 219s 573ms/step - loss: 2.4920 - accuracy: 0.0862 - val_loss: 2.4912 - val_accuracy: 0.0833
Epoch 9/10
383/383 [==============================] - 220s 573ms/step - loss: 2.4926 - accuracy: 0.0813 - val_loss: 2.4943 - val_accuracy: 0.0833
Epoch 10/10
383/383 [==============================] - 220s 573ms/step - loss: 2.4920 - accuracy: 0.0829 - val_loss: 2.4948 - val_accuracy: 0.0833

İ 更新了我的代码。现在,训练集的准确度很高,而有效集的准确度较低。显然是过度拟合。现在 İ 如何获得 valid 的最佳准确度?

更新代码:

# Separate in train and test data
train_df, test_df = train_test_split(image_df, train_size=0.8, shuffle=True, random_state=1)

# Create the generators
train_generator,test_generator,train_images,val_images,test_images = create_gen()

Found 3545 validated image filenames belonging to 12 classes.
Found 886 validated image filenames belonging to 12 classes.
Found 1108 validated image filenames belonging to 12 classes.

from tensorflow.keras.applications.vgg16 import VGG16
vggmodel = VGG16(weights='imagenet', include_top=True)

vggmodel.trainable = False

for layers in (vggmodel.layers)[:19]:
    print(layers)
    layers.trainable = False

<tensorflow.python.keras.engine.input_layer.InputLayer object at 0x7ff3dc0e10d0>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7ff3dc0e8910>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7ff3ca0f6910>
<tensorflow.python.keras.layers.pooling.MaxPooling2D object at 0x7ff3ca0f65d0>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7ff3ca08d810>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7ff3ca08d150>
<tensorflow.python.keras.layers.pooling.MaxPooling2D object at 0x7ff3ca089f90>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7ff3ca090c10>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7ff3ca09b8d0>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7ff4291d8290>
<tensorflow.python.keras.layers.pooling.MaxPooling2D object at 0x7ff3ca0825d0>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7ff3ca0aae10>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7ff3cb71b7d0>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7ff3ca0a7a10>
<tensorflow.python.keras.layers.pooling.MaxPooling2D object at 0x7ff3ca03c090>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7ff3ca03c250>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7ff3ca0a7810>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7ff3ca0aa410>
<tensorflow.python.keras.layers.pooling.MaxPooling2D object at 0x7ff3ca0434d0>

X= vggmodel.layers[-17].output
X=tf.keras.layers.Dropout(0.09)(X)
X = tf.keras.layers.Flatten()(X)
predictions = Dense(12, activation="softmax")(X)

model_final = Model(vggmodel.input, predictions)

model_final.compile(optimizer = optimizers.Adam(learning_rate=0.005), loss='categorical_crossentropy', metrics=["accuracy"])

from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
checkpoint = ModelCheckpoint("vgg16_1.h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, period=1)

rlronp=tf.keras.callbacks.ReduceLROnPlateau( monitor="val_loss", factor=0.005,patience=1, verbose=1)

History = model_final.fit(train_images,validation_data=val_images,batch_size = 64,epochs=60,  
callbacks=[tf.keras.callbacks.EarlyStopping(monitor='val_loss',patience=60,restore_best_weights=True), ])
tf.keras.callbacks.ModelCheckpoint(filepaths, monitor='val_loss', verbose=0, save_best_only=False,save_weights_only=False, mode='auto', save_freq='epoch')
model_final.save_weights("vgg16_1.h5")

Epoch 1/60
111/111 [==============================] - 49s 436ms/step - loss: 2.3832 - accuracy: 0.5396 - val_loss: 1.7135 - val_accuracy: 0.6095
Epoch 2/60
111/111 [==============================] - 39s 348ms/step - loss: 1.0699 - accuracy: 0.7385 - val_loss: 1.6680 - val_accuracy: 0.6208
Epoch 3/60
111/111 [==============================] - 38s 338ms/step - loss: 0.8366 - accuracy: 0.7853 - val_loss: 1.3777 - val_accuracy: 0.6975
Epoch 4/60
111/111 [==============================] - 37s 337ms/step - loss: 0.6213 - accuracy: 0.8299 - val_loss: 1.3766 - val_accuracy: 0.7269
Epoch 5/60
111/111 [==============================] - 38s 339ms/step - loss: 0.6173 - accuracy: 0.8446 - val_loss: 1.9170 - val_accuracy: 0.7133
Epoch 6/60
111/111 [==============================] - 38s 338ms/step - loss: 0.5782 - accuracy: 0.8511 - val_loss: 1.9968 - val_accuracy: 0.6501
Epoch 7/60
111/111 [==============================] - 37s 337ms/step - loss: 0.5672 - accuracy: 0.8564 - val_loss: 1.6436 - val_accuracy: 0.7088
Epoch 8/60
111/111 [==============================] - 38s 340ms/step - loss: 0.3971 - accuracy: 0.8894 - val_loss: 1.6819 - val_accuracy: 0.7314
Epoch 9/60
111/111 [==============================] - 38s 342ms/step - loss: 0.3657 - accuracy: 0.9038 - val_loss: 1.9244 - val_accuracy: 0.7133
Epoch 10/60
111/111 [==============================] - 37s 337ms/step - loss: 0.4003 - accuracy: 0.9016 - val_loss: 1.8337 - val_accuracy: 0.7246
Epoch 11/60
111/111 [==============================] - 38s 341ms/step - loss: 0.6439 - accuracy: 0.8731 - val_loss: 1.8070 - val_accuracy: 0.7460
Epoch 12/60
111/111 [==============================] - 37s 338ms/step - loss: 0.2917 - accuracy: 0.9190 - val_loss: 1.7533 - val_accuracy: 0.7494
Epoch 13/60
111/111 [==============================] - 37s 336ms/step - loss: 0.4685 - accuracy: 0.9032 - val_loss: 1.9534 - val_accuracy: 0.7393
Epoch 14/60
111/111 [==============================] - 38s 339ms/step - loss: 0.3936 - accuracy: 0.9061 - val_loss: 1.8643 - val_accuracy: 0.7280
Epoch 15/60
111/111 [==============================] - 37s 336ms/step - loss: 0.2598 - accuracy: 0.9368 - val_loss: 1.7242 - val_accuracy: 0.7856
Epoch 16/60
111/111 [==============================] - 37s 336ms/step - loss: 0.2884 - accuracy: 0.9360 - val_loss: 1.7374 - val_accuracy: 0.7517
Epoch 17/60
111/111 [==============================] - 38s 341ms/step - loss: 0.2487 - accuracy: 0.9362 - val_loss: 1.6373 - val_accuracy: 0.7889
Epoch 18/60
111/111 [==============================] - 37s 337ms/step - loss: 0.1683 - accuracy: 0.9532 - val_loss: 1.6612 - val_accuracy: 0.7698
Epoch 19/60
111/111 [==============================] - 37s 337ms/step - loss: 0.1354 - accuracy: 0.9591 - val_loss: 1.7372 - val_accuracy: 0.7889
Epoch 20/60
111/111 [==============================] - 38s 339ms/step - loss: 0.2793 - accuracy: 0.9329 - val_loss: 2.0405 - val_accuracy: 0.7596
Epoch 21/60
111/111 [==============================] - 38s 338ms/step - loss: 0.3049 - accuracy: 0.9306 - val_loss: 1.8485 - val_accuracy: 0.7912
Epoch 22/60
111/111 [==============================] - 37s 338ms/step - loss: 0.2724 - accuracy: 0.9399 - val_loss: 1.8225 - val_accuracy: 0.7856
Epoch 23/60
111/111 [==============================] - 37s 337ms/step - loss: 0.2088 - accuracy: 0.9475 - val_loss: 2.1015 - val_accuracy: 0.7675
Epoch 24/60
111/111 [==============================] - 38s 341ms/step - loss: 0.2112 - accuracy: 0.9470 - val_loss: 2.2647 - val_accuracy: 0.7404
Epoch 25/60
111/111 [==============================] - 37s 336ms/step - loss: 0.2172 - accuracy: 0.9467 - val_loss: 2.4213 - val_accuracy: 0.7675
Epoch 26/60
111/111 [==============================] - 37s 336ms/step - loss: 0.3093 - accuracy: 0.9300 - val_loss: 2.3260 - val_accuracy: 0.7630
Epoch 27/60
111/111 [==============================] - 37s 338ms/step - loss: 0.3036 - accuracy: 0.9427 - val_loss: 2.4329 - val_accuracy: 0.7460
Epoch 28/60
111/111 [==============================] - 37s 338ms/step - loss: 0.2641 - accuracy: 0.9436 - val_loss: 2.6936 - val_accuracy: 0.7472
Epoch 29/60
111/111 [==============================] - 37s 333ms/step - loss: 0.2258 - accuracy: 0.9509 - val_loss: 2.3055 - val_accuracy: 0.7788
Epoch 30/60
111/111 [==============================] - 37s 334ms/step - loss: 0.2921 - accuracy: 0.9436 - val_loss: 2.3668 - val_accuracy: 0.7517
Epoch 31/60
111/111 [==============================] - 37s 334ms/step - loss: 0.2830 - accuracy: 0.9447 - val_loss: 2.1422 - val_accuracy: 0.7720
Epoch 32/60
111/111 [==============================] - 37s 337ms/step - loss: 0.3584 - accuracy: 0.9312 - val_loss: 3.2875 - val_accuracy: 0.7122
Epoch 33/60
111/111 [==============================] - 37s 337ms/step - loss: 0.3279 - accuracy: 0.9413 - val_loss: 2.3641 - val_accuracy: 0.7686
Epoch 34/60
111/111 [==============================] - 37s 336ms/step - loss: 0.2326 - accuracy: 0.9526 - val_loss: 2.8010 - val_accuracy: 0.7754
Epoch 35/60
111/111 [==============================] - 38s 337ms/step - loss: 0.3131 - accuracy: 0.9388 - val_loss: 2.6276 - val_accuracy: 0.7698
Epoch 36/60
111/111 [==============================] - 37s 335ms/step - loss: 0.1961 - accuracy: 0.9585 - val_loss: 2.4269 - val_accuracy: 0.7912
Epoch 37/60
111/111 [==============================] - 37s 336ms/step - loss: 0.1915 - accuracy: 0.9599 - val_loss: 2.9607 - val_accuracy: 0.7630
Epoch 38/60
111/111 [==============================] - 38s 339ms/step - loss: 0.2457 - accuracy: 0.9512 - val_loss: 3.2177 - val_accuracy: 0.7438
Epoch 39/60
111/111 [==============================] - 38s 346ms/step - loss: 0.1575 - accuracy: 0.9670 - val_loss: 2.7473 - val_accuracy: 0.7675
Epoch 40/60
111/111 [==============================] - 38s 343ms/step - loss: 0.1841 - accuracy: 0.9591 - val_loss: 3.1237 - val_accuracy: 0.7415
Epoch 41/60
111/111 [==============================] - 38s 339ms/step - loss: 0.2344 - accuracy: 0.9498 - val_loss: 2.7585 - val_accuracy: 0.7630
Epoch 42/60
111/111 [==============================] - 37s 337ms/step - loss: 0.2115 - accuracy: 0.9588 - val_loss: 3.0896 - val_accuracy: 0.7314
Epoch 43/60
111/111 [==============================] - 37s 337ms/step - loss: 0.1407 - accuracy: 0.9721 - val_loss: 2.9159 - val_accuracy: 0.7381
Epoch 44/60
111/111 [==============================] - 38s 336ms/step - loss: 0.2045 - accuracy: 0.9616 - val_loss: 2.7607 - val_accuracy: 0.7630
Epoch 45/60
111/111 [==============================] - 38s 338ms/step - loss: 0.1343 - accuracy: 0.9698 - val_loss: 2.8174 - val_accuracy: 0.7709
Epoch 46/60
111/111 [==============================] - 37s 335ms/step - loss: 0.2094 - accuracy: 0.9605 - val_loss: 3.3286 - val_accuracy: 0.7630
Epoch 47/60
111/111 [==============================] - 38s 340ms/step - loss: 0.3178 - accuracy: 0.9433 - val_loss: 4.0310 - val_accuracy: 0.7201
Epoch 48/60
111/111 [==============================] - 39s 350ms/step - loss: 0.2973 - accuracy: 0.9515 - val_loss: 3.3076 - val_accuracy: 0.7698
Epoch 49/60
111/111 [==============================] - 38s 339ms/step - loss: 0.2969 - accuracy: 0.9535 - val_loss: 3.3232 - val_accuracy: 0.7630
Epoch 50/60
111/111 [==============================] - 38s 339ms/step - loss: 0.1693 - accuracy: 0.9678 - val_loss: 3.3474 - val_accuracy: 0.7528
Epoch 51/60
111/111 [==============================] - 38s 338ms/step - loss: 0.2769 - accuracy: 0.9484 - val_loss: 3.5787 - val_accuracy: 0.7573
Epoch 52/60
111/111 [==============================] - 37s 336ms/step - loss: 0.2483 - accuracy: 0.9611 - val_loss: 3.3097 - val_accuracy: 0.7822
Epoch 53/60
111/111 [==============================] - 37s 337ms/step - loss: 0.1674 - accuracy: 0.9670 - val_loss: 3.9156 - val_accuracy: 0.7494
Epoch 54/60
111/111 [==============================] - 38s 340ms/step - loss: 0.1372 - accuracy: 0.9701 - val_loss: 3.4078 - val_accuracy: 0.7810
Epoch 55/60
111/111 [==============================] - 39s 350ms/step - loss: 0.1391 - accuracy: 0.9726 - val_loss: 3.4077 - val_accuracy: 0.7709
Epoch 56/60
111/111 [==============================] - 38s 342ms/step - loss: 0.2218 - accuracy: 0.9628 - val_loss: 3.5559 - val_accuracy: 0.7517
Epoch 57/60
111/111 [==============================] - 38s 340ms/step - loss: 0.1355 - accuracy: 0.9749 - val_loss: 3.4460 - val_accuracy: 0.7709
Epoch 58/60
111/111 [==============================] - 38s 344ms/step - loss: 0.2096 - accuracy: 0.9650 - val_loss: 3.7931 - val_accuracy: 0.7460
Epoch 59/60
111/111 [==============================] - 37s 337ms/step - loss: 0.1943 - accuracy: 0.9701 - val_loss: 3.6928 - val_accuracy: 0.7573
Epoch 60/60
111/111 [==============================] - 37s 336ms/step - loss: 0.1337 - accuracy: 0.9743 - val_loss: 3.5595 - val_accuracy: 0.7641

结果: 测试损失:3.83508 在测试集上的准确率:78.61%

log上的383不是batch size。这是步数 data_size / batch_size.

训练不正常的问题可能是因为学习率太低或太高。尝试调整学习率。

由于您使用生成器向 model.fit 提供输入,因此您不应在 model.fit 中指定批处理大小,因为它已在生成器中设置为 32。 我会将初始学习率降低到 .001。同样在回调中,我建议使用具有如下所示设置的 ReduceLROnPlateau 回调

rlronp=tf.keras.callbacks.ReduceLROnPlateau( monitor="val_loss", factor=0.5,
                                            patience=1, verbose=1)

将 rlronp 添加到回调列表。同样在您的模型中,我会添加一个丢失层 在第二个密集层之后,代码如下。这将有助于防止过度拟合

x=tf.keras.layers.Dropout(.3)(x)