gpt4 book ai didi

python - 如何选择策略来减少过拟合?

转载 作者:太空狗 更新时间:2023-10-30 00:18:23 25 4
gpt4 key购买 nike

我正在使用 keras 在预训练网络上应用迁移学习。我有带有二进制类标签的图像 block ,并且想使用 CNN 来预测 [0; 范围内的类标签。 1] 对于看不见的图像 block 。

  • 网络:使用 imageNet 预训练的 ResNet50,我向其添加了 3 层
  • 数据:70305 个训练样本,8000 个验证样本,66823 个测试样本,所有样本的两个类标签数量均等
  • 图片:3 波段 (RGB) 和 224x224 像素
  • 设置:32 个批处理,conv 的大小。层数:16

  • 结果:几个 epoch 之后,我的准确率已经接近 1,loss 接近 0,而在验证数据上,准确率保持在 0.5,每个 epoch 的损失都不同.最后,CNN 只为所有看不见的补丁预测一个类别。

  • 问题:我的网络似乎过拟合了。

results screenshot enter image description here

以下策略可以减少过度拟合:

  • 增加批量
  • 减小全连接层的尺寸
  • 添加drop-out层
  • 添加数据扩充
  • 通过修改损失函数应用正则化
  • 解冻更多预训练层
  • 使用不同的网络架构

我已经尝试了最大 512 的批量大小并更改了全连接层的大小但没有取得太大成功。在随机测试其余部分之前,我想问一下如何调查出现问题的原因,以便找出上述哪些策略最有潜力

在我的代码下面:

def generate_data(imagePathTraining, imagesize, nBatches):
datagen = ImageDataGenerator(rescale=1./255)
generator = datagen.flow_from_directory\
(directory=imagePathTraining, # path to the target directory
target_size=(imagesize,imagesize), # dimensions to which all images found will be resize
color_mode='rgb', # whether the images will be converted to have 1, 3, or 4 channels
classes=None, # optional list of class subdirectories
class_mode='categorical', # type of label arrays that are returned
batch_size=nBatches, # size of the batches of data
shuffle=True) # whether to shuffle the data
return generator

def create_model(imagesize, nBands, nClasses):
print("%s: Creating the model..." % datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
# Create pre-trained base model
basemodel = ResNet50(include_top=False, # exclude final pooling and fully connected layer in the original model
weights='imagenet', # pre-training on ImageNet
input_tensor=None, # optional tensor to use as image input for the model
input_shape=(imagesize, # shape tuple
imagesize,
nBands),
pooling=None, # output of the model will be the 4D tensor output of the last convolutional layer
classes=nClasses) # number of classes to classify images into
print("%s: Base model created with %i layers and %i parameters." %
(datetime.now().strftime('%Y-%m-%d_%H-%M-%S'),
len(basemodel.layers),
basemodel.count_params()))

# Create new untrained layers
x = basemodel.output
x = GlobalAveragePooling2D()(x) # global spatial average pooling layer
x = Dense(16, activation='relu')(x) # fully-connected layer
y = Dense(nClasses, activation='softmax')(x) # logistic layer making sure that probabilities sum up to 1

# Create model combining pre-trained base model and new untrained layers
model = Model(inputs=basemodel.input,
outputs=y)
print("%s: New model created with %i layers and %i parameters." %
(datetime.now().strftime('%Y-%m-%d_%H-%M-%S'),
len(model.layers),
model.count_params()))

# Freeze weights on pre-trained layers
for layer in basemodel.layers:
layer.trainable = False

# Define learning optimizer
optimizerSGD = optimizers.SGD(lr=0.01, # learning rate.
momentum=0.0, # parameter that accelerates SGD in the relevant direction and dampens oscillations
decay=0.0, # learning rate decay over each update
nesterov=False) # whether to apply Nesterov momentum

# Compile model
model.compile(optimizer=optimizerSGD, # stochastic gradient descent optimizer
loss='categorical_crossentropy', # objective function
metrics=['accuracy'], # metrics to be evaluated by the model during training and testing
loss_weights=None, # scalar coefficients to weight the loss contributions of different model outputs
sample_weight_mode=None, # sample-wise weights
weighted_metrics=None, # metrics to be evaluated and weighted by sample_weight or class_weight during training and testing
target_tensors=None) # tensor model's target, which will be fed with the target data during training
print("%s: Model compiled." % datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
return model

def train_model(model, nBatches, nEpochs, imagePathTraining, imagesize, nSamples, valX,valY, resultPath):
history = model.fit_generator(generator=generate_data(imagePathTraining, imagesize, nBatches),
steps_per_epoch=nSamples//nBatches, # total number of steps (batches of samples)
epochs=nEpochs, # number of epochs to train the model
verbose=2, # verbosity mode. 0 = silent, 1 = progress bar, 2 = one line per epoch
callbacks=None, # keras.callbacks.Callback instances to apply during training
validation_data=(valX,valY), # generator or tuple on which to evaluate the loss and any model metrics at the end of each epoch
class_weight=None, # optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function
max_queue_size=10, # maximum size for the generator queue
workers=32, # maximum number of processes to spin up when using process-based threading
use_multiprocessing=True, # whether to use process-based threading
shuffle=True, # whether to shuffle the order of the batches at the beginning of each epoch
initial_epoch=0) # epoch at which to start training
print("%s: Model trained." % datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
return history

最佳答案

这些结果看起来太糟糕了,不可能是过度拟合的情况。相反,我怀疑用于训练和验证的数据存在差异。

我注意到,对于训练数据,您正在使用 ImageDataGenerator(rescale=1./255),但是对于 valX,我没有看到任何此类处理。我建议对验证数据使用具有相同缩放配置的单独 ImageDataGenerator。这样差异就尽可能小。

关于python - 如何选择策略来减少过拟合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55533413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com