假设有一个预先训练的模型(base_model),该模型已经使用大型数据集进行了训练,以预测7种人类情绪,例如
'Anger', 'Disgust', 'Fear', 'Happiness', 'Sadness', 'Surprise','Neutral'现在,为了建立一个迁移学习模型,我将删除最后一层"base_model",冻结它们的权重,使它们不可训练,然后添加一个我自己的可训练的微调图层。
我想知道如何在一个较小的数据集上训练这个新编译的模型"model_finetuned“,该数据集只包含7种情绪中的3种。
'Anger', 'Sadness', 'Surprise'任何以Python代码形式提供的帮助或建议都将不胜感激。提前感谢!
发布于 2020-08-18 22:05:36
正如您正确解释的那样,您可以冻结预定义的模型宽度,并在模型的末尾添加完全连接的层进行微调。
有两种方法可以利用预先训练的网络:特征提取和微调。
使用预先训练好的vgg16的示例:
#Load pretrained vgg16 network
from torchvision.models import vgg16
num_classes = 3
pretrained_model = vgg16(pretrained=True)
pretrained_model.eval()
pretrained_model.to(device)
#Extracting the first part of the model
feature_extractor = pretrained_model.features
#Define feature classifier
feature_classifier = nn.Sequential(
nn.Linear(4*4*512,256),
nn.ReLU(),
nn.Linear(256, num_classes))
#
model = nn.Sequential(
feature_extractor,
nn.Flatten(),
feature_classifier)正如您所看到的,您必须在最后一个完全连接的层中指定模型的输出。在您的情况下将是(num_classes = 3)。
发布于 2020-08-19 06:09:08
这是我几天前使用Tensorflow Keras处理的代码示例
import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.models import Sequential
from tensorflow.python.keras.layers import Dense
num_classes= 3
# Include the path of the weights for the pretrained model
resnet_weights_path='imagenet'
# Create your model
model= Sequential()
# Include the pre-trained model. In this case, ResNet50
model.add(ResNet50(include_top=False,pooling='avg',weights=resnet_weights_path ))
# Add as many extra layers as you need, according to you problem
# You can also try it directly
# Add the final layer that makes predictions. Suit yourself with the activation function
model.add(Dense(num_classes,activation='softmax'))
# Don't train the pre-trained model
model.layers[0].trainable=False
# Compile your model according to your needs
model.compile(optimizer='sgd',loss='categorical_crossentropy',metrics=['accuracy'])现在你的模型已经准备好接受训练了。
https://stackoverflow.com/questions/63469500
复制相似问题