【发布时间】: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:]吗? -
是的。我错过了重新编辑
标签: keras pytorch feature-extraction pre-trained-model