作为上下文,我对机器学习的世界还比较陌生,我正在尝试一个项目,目标是对 NBA 比赛中的比赛进行分类。我的输入是 NBA 比赛中每场比赛的 40 帧序列,我的标签是给定比赛的 11 个包罗万象的分类。
计划是获取每个帧序列并将每个帧传递到 CNN 中以提取一组特征。然后,来自给定视频的每个特征序列都将传递到 RNN。
我目前在大部分实现中使用 Keras,我选择为我的 CNN 使用 VGG16 模型。下面是一些相关代码:
video = keras.Input(shape = (None, 255, 255, 3), name = 'video')
cnn = keras.applications.VGG16(include_top=False, weights = None, input_shape=
(255,255,3), pooling = 'avg', classes=11)
cnn.trainable = True
我的问题是 - 如果我的目标是对 NBA 比赛的视频剪辑进行分类,那么将 VGG16 ConvNet 的权重初始化为“imagenet”是否仍然有益?如果是这样,为什么?如果没有,我如何训练 VGG16 ConvNet 以获得我自己的一组权重,然后如何将它们插入此函数?我几乎没有找到任何教程,其中有人在使用 VGG16 模型时包含了他们自己的一组权重。
如果我的问题看起来很幼稚,我深表歉意,但如果您能帮助我解决这个问题,我将不胜感激。
最佳答案
您是否应该为您的特定任务重新训练 VGG16? 绝对不是!重新训练如此庞大的网络非常困难,并且需要大量的直觉和训练深度网络的知识。让我们分析一下为什么您可以使用在 ImageNet 上预训练的权重来完成您的任务:
ImageNet 是一个庞大的数据集,包含数百万张图像。 VGG16 本身已经在强大的 GPU 上训练了 3-4 天左右。在 CPU 上(假设您没有像 NVIDIA GeForce Titan X 这样强大的 GPU)将需要数周时间。
ImageNet 包含来自真实世界场景的图像。 NBA比赛也可以被认为是真实世界的场景。因此,在 ImageNet 上预训练的特征很可能也可以用于 NBA 比赛。
实际上,您不需要使用预训练 VGG16 的所有卷积层。我们来看看visualization of internal VGG16 layers并查看它们检测到的内容(取自 this article ;图像太大,所以为了紧凑我只放了一个链接):
- 第一个和第二个卷积 block 着眼于低级特征,例如角、边等。
- 第三个和第四个卷积 block 着眼于表面特征、曲线、圆等。
- 第五层着眼于高级特征
因此,您可以决定哪种功能对您的特定任务有益。您需要第 5 个街区的高级功能吗?或者您可能想使用 3rd block 的中级功能?也许您想在 VGG 的底层之上堆叠另一个神经网络?有关更多说明,请查看我编写的以下教程;它曾经出现在 SO 文档中。
使用 VGG 和 Keras 进行迁移学习和微调
在此示例中,提供了三个简短而全面的子示例:
- 从可用的预训练模型加载权重,包含在 Keras 库中
- 在 VGG 的任何层之上堆叠另一个网络进行训练
- 在其他层中间插入一个层
- 使用 VGG 进行微调和迁移学习的技巧和一般经验法则
加载预训练权重
ImageNet 模型(包括 VGG-16 和 VGG-19)的预训练可在 Keras。在本示例中,此处和之后将使用 VGG-16。更多信息,请访问Keras Applications documentation .
from keras import applications
# This will load the whole VGG16 network, including the top Dense layers.
# Note: by specifying the shape of top layers, input tensor shape is forced
# to be (224, 224, 3), therefore you can use it only on 224x224 images.
vgg_model = applications.VGG16(weights='imagenet', include_top=True)
# If you are only interested in convolution filters. Note that by not
# specifying the shape of top layers, the input tensor shape is (None, None, 3),
# so you can use them for any size of images.
vgg_model = applications.VGG16(weights='imagenet', include_top=False)
# If you want to specify input tensor
from keras.layers import Input
input_tensor = Input(shape=(160, 160, 3))
vgg_model = applications.VGG16(weights='imagenet',
include_top=False,
input_tensor=input_tensor)
# To see the models' architecture and layer names, run the following
vgg_model.summary()
使用取自 VGG 的底层创建一个新网络
假设对于尺寸为 (160, 160, 3)
的图像的某些特定任务,您想要使用 VGG 的预训练底层,直到名称为 的层block2_pool
.
vgg_model = applications.VGG16(weights='imagenet',
include_top=False,
input_shape=(160, 160, 3))
# Creating dictionary that maps layer names to the layers
layer_dict = dict([(layer.name, layer) for layer in vgg_model.layers])
# Getting output tensor of the last VGG layer that we want to include
x = layer_dict['block2_pool'].output
# Stacking a new simple convolutional network on top of it
x = Conv2D(filters=64, kernel_size=(3, 3), activation='relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(10, activation='softmax')(x)
# Creating new model. Please note that this is NOT a Sequential() model.
from keras.models import Model
custom_model = Model(input=vgg_model.input, output=x)
# Make sure that the pre-trained bottom layers are not trainable
for layer in custom_model.layers[:7]:
layer.trainable = False
# Do not forget to compile it
custom_model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
删除多个图层并在中间插入一个新图层
假设您需要通过用单个卷积层替换 block1_conv1
和 block2_conv2
来加速 VGG16,这样可以保存预训练的权重。
这个想法是将整个网络分解成不同的层,然后再组装回去。这是专门针对您的任务的代码:
vgg_model = applications.VGG16(include_top=True, weights='imagenet')
# Disassemble layers
layers = [l for l in vgg_model.layers]
# Defining new convolutional layer.
# Important: the number of filters should be the same!
# Note: the receiptive field of two 3x3 convolutions is 5x5.
new_conv = Conv2D(filters=64,
kernel_size=(5, 5),
name='new_conv',
padding='same')(layers[0].output)
# Now stack everything back
# Note: If you are going to fine tune the model, do not forget to
# mark other layers as un-trainable
x = new_conv
for i in range(3, len(layers)):
layers[i].trainable = False
x = layers[i](x)
# Final touch
result_model = Model(input=layer[0].input, output=x)
关于python - 我需要 Keras VGG16 的预训练权重吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45383706/