从冻结层获取输出张量



我正在使用迁移学习训练一个网络。我目前冻结了前 70 层,并且只在最后 10 层进行训练。由于数据集的大小,每个时期需要 45 分钟来训练。如果有办法,我想在整个网络上训练 1 个 epoch,得到最后一个冻结层的输出张量,并将其输入到训练层中,了解需要多少个 epoch。

我希望这将减少训练网络所需的时间,因为常量层基本上被跳过了。

from tensorflow.keras.applications import MobileNet
from tensorflow.keras.models import Model
base_model = MobileNet(weights='imagenet', include_top=False, input_shape=(224,224,3), dropout=.2)
x = base_model.output
x = Dense(10, activation='softmax')(x)
model = Model(inputs=base_model.input,outputs=x)
for layer in model.layers[:70]:
layer.trainable=False
for layer in model.layers[70:]:
layer.trainable=True
...

我使用ImageDataGenerator将数据导入网络。

这是我遵循的教程:https://towardsdatascience.com/transfer-learning-using-mobilenet-and-keras-c75daf7ff299

trainable = False设置为层时,模型不会花时间训练这些层。默认情况下会跳过不可训练的层。

如果要提高速度,请使更多层不可训练。

此外,您有可能在 CPU 上训练模型,这比在 GPU 上训练要慢得多。

相关内容

最新更新