简体   繁体   English

Model 精度高但 Val_Accuracy 低

[英]Model Accuracy is High but Val_Accuracy is low

I'm trying to improve my val accuracy as it is very low.我正在尝试提高我的 val 准确性,因为它非常低。 I have tried changing the batch_size, the number of images being used for validation and training.我尝试更改 batch_size,即用于验证和训练的图像数量。 Added in extra dense levels but none of them have worked.添加了额外的密集级别,但没有一个有效。 The dataset I'm using has not been split up yet into Training and Validation which is what I have done using partitioning.我正在使用的数据集尚未拆分为训练和验证,这是我使用分区所做的。 I have given the values for the samples as you can see below and have tried to increase the VALIDATION_SAMPLES but when I do, my cluster keeps crashing.我已经给出了示例的值,如下所示,并尝试增加 VALIDATION_SAMPLES,但是当我这样做时,我的集群不断崩溃。


    TRAINING_SAMPLES = 10000
    VALIDATION_SAMPLES = 2000
    TEST_SAMPLES = 2000
    IMG_WIDTH = 178
    IMG_HEIGHT = 218
    BATCH_SIZE = 32
    NUM_EPOCHS = 20


    def generate_df(partition, attr, num_samples):
            df_ = df_par_attr[(df_par_attr['partition'] == partition) 
                                   & (df_par_attr[attr] == 0)].sample(int(num_samples/2))
            df_ = pd.concat([df_,
                              df_par_attr[(df_par_attr['partition'] == partition) 
                                          & (df_par_attr[attr] == 1)].sample(int(num_samples/2))])
        
            # for Training and Validation
            if partition != 2:
                x_ = np.array([load_reshape_img(images_folder + fname) for fname in df_.index])
                x_ = x_.reshape(x_.shape[0], 218, 178, 3)
                y_ = np_utils.to_categorical(df_[attr],2)
            # for Test
            else:
                x_ = []
                y_ = []
        
                for index, target in df_.iterrows():
                    im = cv2.imread(images_folder + index)
                    im = cv2.resize(cv2.cvtColor(im, cv2.COLOR_BGR2RGB), (IMG_WIDTH, IMG_HEIGHT)).astype(np.float32) / 255.0
                    im = np.expand_dims(im, axis =0)
                    x_.append(im)
                    y_.append(target[attr])
        
            return x_, y_

My training model is build after the partitioning which you can see below我的训练 model 是在分区之后构建的,如下所示


    # Train data
    x_train, y_train = generate_df(0, 'Male', TRAINING_SAMPLES)
    
    # Train - Data Preparation - Data Augmentation with generators
    train_datagen =  ImageDataGenerator(
      preprocessing_function=preprocess_input,
      rotation_range=30,
      width_shift_range=0.2,
      height_shift_range=0.2,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
    )
    
    train_datagen.fit(x_train)
    
    train_generator = train_datagen.flow(
    x_train, y_train,
    batch_size=BATCH_SIZE,
    )

The same also goes for the validation验证也一样


    # Validation Data
    x_valid, y_valid = generate_df(1, 'Male', VALIDATION_SAMPLES)
    
    
    # Validation - Data Preparation - Data Augmentation with generators
    valid_datagen = ImageDataGenerator(
      preprocessing_function=preprocess_input,
    )
    
    valid_datagen.fit(x_valid)
    
    validation_generator = valid_datagen.flow(
    x_valid, y_valid,
    )

I tried playing around with the layers but got told that it wouldn't really affect your val_accuracy我试着玩弄图层,但被告知它不会真正影响你的 val_accuracy


    x = inc_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation="relu")(x)
    x = Dropout(0.5)(x)
    x = Dense(256, activation="relu")(x)
    predictions = Dense(2, activation="softmax")(x)

I tried using the 'adam' optimizer but it made no difference when compared to sgd我尝试使用“adam”优化器,但与 sgd 相比没有任何区别


    model_.compile(optimizer=SGD(lr=0.0001, momentum=0.9)
                        , loss='categorical_crossentropy'
                        , metrics=['accuracy'])


    hist = model_.fit_generator(train_generator
                         , validation_data = (x_valid, y_valid)
                          , steps_per_epoch= TRAINING_SAMPLES/BATCH_SIZE
                          , epochs= NUM_EPOCHS
                          , callbacks=[checkpointer]
                          , verbose=1
                        )

Who ever told you modifying the model won't effect validation accuracy in most cases is dead wrong.谁告诉你修改 model 在大多数情况下不会影响验证准确性是完全错误的。 The problem you have in your model is it is not deep enough to extract the features of the images.您在 model 中遇到的问题是它不够深,无法提取图像的特征。 Below is the code I have used on hundreds of models and has proved to be very accurate with respect to achieving low training and validation loss and avoid over fitting下面是我在数百个模型上使用过的代码,经证明在实现低训练和验证损失以及避免过度拟合方面非常准确

from tensorflow import keras
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Dense, Activation,Dropout,Conv2D, MaxPooling2D,BatchNormalization, Flatten
from tensorflow.keras.optimizers import Adam, Adamax
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras import regularizers
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model, load_model
def make_model(img_img_size, class_count,lr=.001, trainable=True):
    img_shape=(img_size[0], img_size[1], 3)
    model_name='EfficientNetB3'
    base_model=tf.keras.applications.efficientnet.EfficientNetB3(include_top=False, weights="imagenet",input_shape=img_shape, pooling='max') 
    base_model.trainable=trainable
    x=base_model.output
    x=keras.layers.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001 )(x)
    x = Dense(256, kernel_regularizer = regularizers.l2(l = 0.016),activity_regularizer=regularizers.l1(0.006),
                    bias_regularizer=regularizers.l1(0.006) ,activation='relu')(x)
    x=Dropout(rate=.45, seed=123)(x)        
    output=Dense(class_count, activation='softmax')(x)
    model=Model(inputs=base_model.input, outputs=output)
    model.compile(Adamax(learning_rate=lr), loss='categorical_crossentropy', metrics=['accuracy']) 
    return model, base_model # return the base_model so the callback can control its training state

TRAINING_SAMPLES = 10000
VALIDATION_SAMPLES = 2000
TEST_SAMPLES = 2000
IMG_WIDTH = 178
IMG_HEIGHT = 218
BATCH_SIZE = 32
NUM_EPOCHS = 20
img_size=(IMG_HEIGHT,IMG_WIDTH)
class_count=2
model, base_model=make_model(img_size, class_count, lr=.001, trainable=True)

I also recommend that you use two keras callbacks.我还建议您使用两个 keras 回调。 One is to control the learning rate.一是控制学习率。 Documentation for that is here.文档在这里。 The other controls early stopping and saves the model with the lowest validation loss.另一个控制提前停止并保存具有最低验证损失的 model。 Documentation for that is here.文档在这里。 My recommended code for these callbacks is shown below我推荐的这些回调代码如下所示

rlronp=tf.keras.callbacks.ReduceLROnPlateau(monitor="val_loss", factor=0.5, patience=2,verbose=1)
estop=tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=4, verbose=1,restore_best_weights=True)
callbacks=[rlronp, estop]

put the above code prior to using model.fit.在使用 model.fit 之前放置上面的代码。 In model.fit set the parameter在model.fit设置参数

callbacks=callbacks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 训练 ResNet50 时高 val_loss 和低 val_accuracy model - High val_loss and low val_accuracy when training ResNet50 model 我的模型具有较高的准确性和val_accuracy,但在测试数据上给出了错误的结果 - My model has high accuracy and val_accuracy but giving wrong result on test data Val_accuracy 没有增加 - Val_accuracy not increasing model 历史记录 Tensorflow (YOLO) 中缺少“val_accuracy”和“accuracy” - 'val_accuracy' and 'accuracy' missing in fitted model history Tensorflow (YOLO) val_accuracy 不增加 - val_accuracy does not increase cnn model 用于二进制分类,86% val_accuracy 总是返回 1 - cnn model for binary classification with 86% val_accuracy always returning 1 model.predict 的 Tensorflow 精度与 model.fit 的最终时期 val_accuracy 不匹配 - Tensorflow accuracy from model.predict does not match final epoch val_accuracy of model.fit Keras CNN 模型精度保持相对不变,而 val_accuracy 没有提高 - Keras CNN Model accuracy remaining relatively the same and val_accuracy not improving 没有 dropout 正则化的模型 val_accuracy 高于测试精度 - Model val_accuracy higher than test accuracy without dropout regularization Accuracy 和 val_accuracy 在训练时不会改变 - Accuracy and val_accuracy don't change while training
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM