Machine learning 为什么我的有线电视新闻网(CNN)过度安装,我如何修复它?

Machine learning 为什么我的有线电视新闻网(CNN)过度安装,我如何修复它?,machine-learning,keras,computer-vision,conv-neural-network,Machine Learning,Keras,Computer Vision,Conv Neural Network,我正在微调一个名为C3D的3D-CNN,它最初被训练用于从视频剪辑中对运动进行分类 我正在冻结卷积(特征提取)层,并使用GIPHY中的GIF对完全连接的层进行训练,以对GIF进行分类,以便进行情绪分析(正面或负面) 除最终完全连接的层外,所有层的权重都已预加载 我使用5000张图像(2500张正片,2500张负片)进行训练,使用Keras进行70/30训练/测试分割。我使用的Adam优化器的学习率为0.0001 在训练期间,训练精度增加,训练损失减少,但在验证的早期,随着模型开始过度拟合,训练精

我正在微调一个名为C3D的3D-CNN,它最初被训练用于从视频剪辑中对运动进行分类

我正在冻结卷积(特征提取)层,并使用GIPHY中的GIF对完全连接的层进行训练,以对GIF进行分类,以便进行情绪分析(正面或负面)

除最终完全连接的层外,所有层的权重都已预加载

我使用5000张图像(2500张正片,2500张负片)进行训练,使用Keras进行70/30训练/测试分割。我使用的Adam优化器的学习率为0.0001

在训练期间,训练精度增加,训练损失减少,但在验证的早期,随着模型开始过度拟合,训练精度和损失并没有改善

我相信我有足够的训练数据,并且在两个完全连接的层上都使用了0.5的落差,那么我该如何应对这种过度拟合呢

Keras的模型架构、培训代码和培训绩效可视化可在下面找到

列车c3d.py

from training.c3d_model import create_c3d_sentiment_model
from ImageSentiment import load_gif_data
import numpy as np
import pathlib
from keras.callbacks import ModelCheckpoint
from keras.optimizers import Adam


def image_generator(files, batch_size):
    """
    Generate batches of images for training instead of loading all images into memory
    :param files:
    :param batch_size:
    :return:
    """
    while True:
        # Select files (paths/indices) for the batch
        batch_paths = np.random.choice(a=files,
                                       size=batch_size)
        batch_input = []
        batch_output = []

        # Read in each input, perform preprocessing and get labels
        for input_path in batch_paths:
            input = load_gif_data(input_path)
            if "pos" in input_path:  # if file name contains pos
                output = np.array([1, 0])  # label
            elif "neg" in input_path:  # if file name contains neg
                output = np.array([0, 1])  # label

            batch_input += [input]
            batch_output += [output]
        # Return a tuple of (input,output) to feed the network
        batch_x = np.array(batch_input)
        batch_y = np.array(batch_output)

        yield (batch_x, batch_y)


model = create_c3d_sentiment_model()
print(model.summary())
model.load_weights('models/C3D_Sport1M_weights_keras_2.2.4.h5', by_name=True)

for layer in model.layers[:14]:  # freeze top layers as feature extractor
    layer.trainable = False
for layer in model.layers[14:]:  # fine tune final layers
    layer.trainable = True

train_files = [str(filepath.absolute()) for filepath in pathlib.Path('data/sample_train').glob('**/*')]
val_files = [str(filepath.absolute()) for filepath in pathlib.Path('data/sample_validation').glob('**/*')]

batch_size = 8
train_generator = image_generator(train_files, batch_size)
validation_generator = image_generator(val_files, batch_size)

model.compile(optimizer=Adam(lr=0.0001),
              loss='binary_crossentropy',
              metrics=['accuracy'])

mc = ModelCheckpoint('best_model.h5', monitor='val_loss', mode='min', verbose=1)

history = model.fit_generator(train_generator, validation_data=validation_generator,
                              steps_per_epoch=int(np.ceil(len(train_files) / batch_size)),
                              validation_steps=int(np.ceil(len(val_files) / batch_size)), epochs=5, shuffle=True,
                              callbacks=[mc])
加载\u gif\u数据()

模型架构

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1 (Conv3D)               (None, 16, 112, 112, 64)  5248      
_________________________________________________________________
pool1 (MaxPooling3D)         (None, 16, 56, 56, 64)    0         
_________________________________________________________________
conv2 (Conv3D)               (None, 16, 56, 56, 128)   221312    
_________________________________________________________________
pool2 (MaxPooling3D)         (None, 8, 28, 28, 128)    0         
_________________________________________________________________
conv3a (Conv3D)              (None, 8, 28, 28, 256)    884992    
_________________________________________________________________
conv3b (Conv3D)              (None, 8, 28, 28, 256)    1769728   
_________________________________________________________________
pool3 (MaxPooling3D)         (None, 4, 14, 14, 256)    0         
_________________________________________________________________
conv4a (Conv3D)              (None, 4, 14, 14, 512)    3539456   
_________________________________________________________________
conv4b (Conv3D)              (None, 4, 14, 14, 512)    7078400   
_________________________________________________________________
pool4 (MaxPooling3D)         (None, 2, 7, 7, 512)      0         
_________________________________________________________________
conv5a (Conv3D)              (None, 2, 7, 7, 512)      7078400   
_________________________________________________________________
conv5b (Conv3D)              (None, 2, 7, 7, 512)      7078400   
_________________________________________________________________
zeropad5 (ZeroPadding3D)     (None, 2, 8, 8, 512)      0         
_________________________________________________________________
pool5 (MaxPooling3D)         (None, 1, 4, 4, 512)      0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 8192)              0         
_________________________________________________________________
fc6 (Dense)                  (None, 4096)              33558528  
_________________________________________________________________
dropout_1 (Dropout)          (None, 4096)              0         
_________________________________________________________________
fc7 (Dense)                  (None, 4096)              16781312  
_________________________________________________________________
dropout_2 (Dropout)          (None, 4096)              0         
_________________________________________________________________
nfc8 (Dense)                 (None, 2)                 8194      
=================================================================
Total params: 78,003,970
Trainable params: 78,003,970
Non-trainable params: 0
_________________________________________________________________
None
培训可视化
我认为错误在于损失函数和最后一个密集层。如模型摘要所示,最后一个致密层为

nfc8 (Dense) (None, 2)
输出形状为(无,2),表示层有2个单位。正如你之前所说的,你需要将GIF分为积极的或消极的

GIF分类可以是二元分类问题或多类分类问题(有两类)

二元分类在最后一个致密层中只有1个单元,具有S形激活函数。但是,在这里,模型在最后一个致密层中有2个单元。

因此,该模型是一个多类分类器,但您给出了一个损失函数
binary\u crossentropy
,用于二进制分类器(最后一层中只有一个单元)

因此,用分类交叉熵取代损失应该是可行的。或者编辑最后一个密集层并更改单元数和激活函数


希望这有帮助。

我认为错误在损失函数和最后一个密集层中。如模型摘要所示,最后一个致密层为

nfc8 (Dense) (None, 2)
输出形状为(无,2),表示层有2个单位。正如你之前所说的,你需要将GIF分为积极的或消极的

GIF分类可以是二元分类问题或多类分类问题(有两类)

二元分类在最后一个致密层中只有1个单元,具有S形激活函数。但是,在这里,模型在最后一个致密层中有2个单元。

因此,该模型是一个多类分类器,但您给出了一个损失函数
binary\u crossentropy
,用于二进制分类器(最后一层中只有一个单元)

因此,用分类交叉熵取代损失应该是可行的。或者编辑最后一个密集层并更改单元数和激活函数


希望这有帮助。

可以尝试不同的学习速度,顺便说一句,至少跑8-10个历元。可以尝试不同的学习速度,顺便说一句,至少跑8-10个历元