3 回答
TA贡献1877条经验 获得超6个赞
在这两种情况下,您所说的 CNN 都在谈论同一件事,这是一种神经网络模型。只是预训练的模型已经在其他一些数据上进行了训练,而不是您正在处理并尝试分类的数据集。
这里通常使用的称为迁移学习。与其冻结所有层,不如尝试让最后几层保持打开状态,以便可以使用您自己的数据重新训练它们,以便预训练的模型也可以编辑其权重和偏差以满足您的需求。您尝试分类的数据集可能与预训练模型无关。
这是我自己工作的一个例子,还有一些额外的代码,但你可以用你自己的代码让它工作,逻辑保持不变
#You extract the layer which you want to manipulate, usually the last few.
last_layer = pre_trained_model.get_layer(name_of_layer)
# Flatten the output layer to 1 dimension
x = layers.Flatten()(last_output)
# Add a fully connected layer with 1,024 hidden units and ReLU activation
x = layers.Dense(1024,activation='relu')(x)
# Add a dropout rate of 0.2
x = layers.Dropout(0.2)(x)
# Add a final sigmoid layer for classification
x = layers.Dense(1,activation='sigmoid')(x)
#Here we combine your newly added layers and the pre-trained model.
model = Model( pre_trained_model.input, x)
model.compile(optimizer = RMSprop(lr=0.0001),
loss = 'binary_crossentropy',
metrics = ['accuracy'])
TA贡献1943条经验 获得超7个赞
#You extract the layer which you want to manipulate, usually the last few.
last_layer = pre_trained_model.get_layer(name_of_layer)
# Flatten the output layer to 1 dimension
x = layers.Flatten()(last_output)
# Add a fully connected layer with 1,024 hidden units and ReLU activation
x = layers.Dense(1024,activation='relu')(x)
# Add a dropout rate of 0.2
x = layers.Dropout(0.2)(x)
# Add a final sigmoid layer for classification
x = layers.Dense(1,activation='sigmoid')(x)
#Here we combine your newly added layers and the pre-trained model.
model = Model( pre_trained_model.input, x)
model.compile(optimizer = RMSprop(lr=0.0001),
loss = 'binary_crossentropy',
metrics = ['accuracy'])
TA贡献1785条经验 获得超8个赞
除了@Ilknur Mustafa 提到的内容之外,由于您的数据集可能与用于预训练的图像不同,您可以尝试重新训练预训练模型的最后几层,而不是添加全新的层。下面的示例代码除了输出层之外没有添加任何额外的可训练层。通过这种方式,您可以通过在现有权重上重新训练最后几层来受益,而不是从头开始训练。如果您没有要训练的大型数据集,这可能会有所帮助。
# load model without classifier layers
vgg = VGG16(include_top=False, input_shape=(100, 100, 3), weights='imagenet', pooling='avg')
# make only last 2 conv layers trainable
for layer in vgg.layers[:-4]:
layer.trainable = False
# add output layer
out_layer = Dense(3, activation='softmax')(vgg.layers[-1].output)
model_pre_vgg = Model(vgg.input, out_layer)
# compile model
opt = SGD(lr=1e-5)
model_pre_vgg.compile(optimizer=opt, loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])
添加回答
举报