如何改善我的CNN?高且持续的验证错误 [英] How to improve my CNN ? high and constant validation error
问题描述
我正在研究基于母牛图像来预测肥牛状况的分数的问题。
我应用了CNN来估计介于0-5之间的值(我拥有的数据集仅包含2.25和4之间的值)
我正在使用4个CNN层和3个隐藏层。
I am working on a problem for predicting a score of how fat cows are, based on images of cows. I applied a CNN to estimate the value which is between 0-5 ( the dataset i have, contains only values between 2.25 and 4 ) I am using 4 CNN layers and 3 Hidden layers.
我实际上有2个问题:
1 /我有0.05的训练误差,但是在3-5个时期之后,验证误差仍然约为0.33。
2 /我的神经网络预测的值在2.9到3.3之间,与数据集范围相比太窄了。正常吗?
I actualy have 2 problems : 1/ I got 0.05 training error, but after 3-5 epochs the validation error remains at about 0.33. 2/ The value predicted by my NN are between 2.9 and 3.3 which is too narrow compared with the dataset range. Is it normal ?
如何改进我的模型?
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(512, 424,1)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(input_shape=(512, 424)),
tf.keras.layers.Dense(256, activation=tf.nn.relu),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(64, activation=tf.nn.relu),
tf.keras.layers.Dense(1, activation='linear')
])
学习曲线:
预测:
推荐答案
这似乎是过度拟合的情况。您可以
This seems to be the case of Overfitting. You can
-
随机播放
数据
,方法是在cnn_model.fit
中使用shuffle = True
。代码如下所示:
Shuffle
theData
, by usingshuffle=True
incnn_model.fit
. Code is shown below:
history = cnn_model.fit(x = X_train_reshaped,
y = y_train,
batch_size = 512 ,
epochs = epochs,回调= [callback],
verbose = 1,validation_data =(X_test_reshaped,y_test),
validation_steps = 10,steps_per_epoch = steps_per_epoch,shuffle = True)
使用提前停止
。代码如下所示
callback = tf.keras.callbacks.EarlyStopping(monitor ='val_loss',patience = 15)
使用正则化。正则化代码如下所示(您也可以尝试l1正则化或l1_l2正则化):
Use Regularization. Code for Regularization is shown below (You can try l1 Regularization or l1_l2 Regularization as well):
从tensorflow.keras.regularizers导入l2
Regularizer = l2(0.001)
cnn_model.add(Conv2D(64,3,3,input_shape =(28,28,1),activation ='relu', data_format ='channels_last',
activity_regularizer =正则化,kernel_regularizer =正则化))
cnn_model.add (密集(单位= 10,激活='sigmoid',
activity_regularizer =正则化,kernel_regularizer =正则化))
-
您可以尝试使用
BatchNormalization
。
使用 ImageDataGenerator
执行图像数据增强。请参阅此链接以了解更多信息
Perform Image Data Augmentation using ImageDataGenerator
. Refer this link for more info about that.
如果像素未进行归一化
,则将像素值除以 255
也有帮助。
If the Pixels are not Normalized
, Dividing the Pixel Values with 255
also helps.
最后,如果仍然没有更改,则可以尝试使用 Pre-训练有素的模型
,例如 ResNet
或 VGG Net
等。
Finally, if there still no change, you can try using Pre-Trained Models
like ResNet
or VGG Net
, etc..
这篇关于如何改善我的CNN?高且持续的验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!