Pytorch - 跳过计算每个时期的预训练模型的特征
Posted
技术标签:
【中文标题】Pytorch - 跳过计算每个时期的预训练模型的特征【英文标题】:Pytorch - skip calculating features of pretrained models for every epoch 【发布时间】:2022-01-11 03:29:21 【问题描述】:我习惯于使用tenserflow - keras,但现在我不得不开始使用Pytorch 来解决灵活性问题。但是,我似乎没有找到只专注于训练模型分类层的 pytorch 代码。这不是一种常见的做法吗?现在我必须等待每个时期对相同数据的特征提取的计算。有没有办法避免这种情况?
# in tensorflow - keras :
from tensorflow.keras.applications import vgg16, MobileNetV2, mobilenet_v2
# Load a pre-trained
pretrained_nn = MobileNetV2(weights='imagenet', include_top=False, input_shape=(Image_size, Image_size, 3))
# Extract features of the training data only once
X = mobilenet_v2.preprocess_input(X)
features_x = pretrained_nn.predict(X)
# Save features for later use
joblib.dump(features_x, "features_x.dat")
# Create a model and add layers
model = Sequential()
model.add(Flatten(input_shape=features_x.shape[1:]))
model.add(Dense(100, activation='relu', use_bias=True))
model.add(Dense(Y.shape[1], activation='softmax', use_bias=False))
# Compile & train only the fully connected model
model.compile( loss="categorical_crossentropy", optimizer=keras.optimizers.Adam(learning_rate=0.001))
history = model.fit( features_x, Y_train, batch_size=16, epochs=Epochs)
【问题讨论】:
有可能,但是如果没有看到你的代码就无法很好地回答这个问题。请提供minimal reproducible example,说明您正在寻找什么。 感谢您的互动@GoodDeeds。我编辑了帖子以添加我想要实现的翻译 第一个model.add
不应该是features_x.shape[1:]
吗?
是的。我错过了重新编辑
【参考方案1】:
假设您已经具备 features_x
的特征,您可以执行以下操作来创建和训练模型:
# create a loader for the data
dataset = torch.utils.data.TensorDataset(features_x, Y_train)
loader = torch.utils.data.DataLoader(dataset, batch_size=16, shuffle=True)
# define the classification model
in_features = features_x.flatten(1).size(1)
model = torch.nn.Sequential(
torch.nn.Flatten(),
torch.nn.Linear(in_features=in_features, out_features=100, bias=True),
torch.nn.ReLU(),
torch.nn.Linear(in_features=100, out_features=Y.shape[1], bias=False) # Softmax is handled by CrossEntropyLoss below
)
model.train()
# define the optimizer and loss function
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
loss_function = torch.nn.CrossEntropyLoss()
# training loop
for e in range(Epochs):
for batch_x, batch_y in enumerate(loader):
optimizer.zero_grad() # clear gradients from previous batch
out = model(batch_x) # forward pass
loss = loss_function(out, batch_y) # compute loss
loss.backward() # backpropagate, get gradients
optimizer.step() # update model weights
【讨论】:
谢谢@GoodDeeds 你能详细说明提取和保存pytorch框架中的特征吗? @RamiHachicha 您可以使用 torch.save 将 Torch 张量保存到磁盘。对于特征提取,您是否已经在 PyTorch 中定义了预训练模型?在您的问题中,您提到您在每个时期都提取特征,您是如何做到的?以上是关于Pytorch - 跳过计算每个时期的预训练模型的特征的主要内容,如果未能解决你的问题,请参考以下文章
有啥方法可以将 PyTorch 中可用的预训练模型下载到特定路径?