无预训练权重的迁移学习

huangapple go评论77阅读模式
英文:

Transfer learning without pretrained weights

问题

我想**使用Keras上可用的深度学习架构**([resnet50][1]),**添加一些层,并且在我的数据上训练整个确保模型没有任何预训练权重,同时使所有层都可训练。**

我是转移学习的新手,正在遵循这个[Keras链接][2]以及其他一些资源。

我附上了我在下面代码块中使用的代码。

**我应该怎么做才能确保所有层都是从头开始在我的数据上训练,没有任何预训练权重?**

pre_trained_model = ResNet50(input_shape = (150, 150, 3),
include_top = False, )

使预训练模型中的所有层都可训练(解冻)。

for layer in pre_trained_model.layers:
layer.trainable = True

last_layer = pre_trained_model.get_layer('conv5_block3_3_bn')
last_output = last_layer.output

x = layers.Flatten()(last_output)
x = layers.Dense(1024, activation='relu')(x)
x = layers.Dropout(0.2)(x)
x = layers.Dense(3, activation='relu')(x)

model = Model(pre_trained_model.input, x)

model.compile(optimizer = tf.keras.optimizers.Adam(
learning_rate=0.01),
loss = "mean_squared_error",
metrics = ["mse"])


  [1]: https://keras.io/api/applications/resnet/#resnet50-function
  [2]: https://keras.io/guides/transfer_learning/
英文:

I would like to use a deep learning architecture available on Keras(resnet50), add a few layers, and train the entire ensure model on my data without any pretrained weights while making all layers trainable.

I am new to transfer learning and am following this Keras link and a few other resources.

I am attaching the code that I am using in the below code block.

What should I do differently to make sure that all layers are trained from scratch on my data without any pretrained weights?

pre_trained_model = ResNet50(input_shape = (150, 150, 3), 
                                include_top = False, )

# Make all the layers in the pre-trained model trainable (unfrozen.)
for layer in pre_trained_model.layers:
    layer.trainable = True 


last_layer = pre_trained_model.get_layer('conv5_block3_3_bn')
last_output = last_layer.output 

x = layers.Flatten()(last_output)
x = layers.Dense(1024, activation='relu')(x)
x = layers.Dropout(0.2)(x)                  
x = layers.Dense(3, activation='relu')(x)           

model = Model( pre_trained_model.input, x) 

model.compile(optimizer = tf.keras.optimizers.Adam(
                          learning_rate=0.01), 
              loss = "mean_squared_error",
              metrics = ["mse"])

答案1

得分: 2

默认情况下,keras.applications 模块中的 Keras 模型使用在 ImageNet 数据集上预训练的权重。只需将 weights 参数设置为 None,即可获得一个使用随机权重初始化的模型。

pre_trained_model = ResNet50(input_shape=(150, 150, 3),
                             include_top=False, weights=None)

你可以在文档中详细了解更多信息:Keras Applications / ResNet 和 ResNetV2

英文:

By default, Keras models available in the keras.applications module come with weights pretrained on the imagenet dataset. Simply pass None to the weights argument to get a model initialized with random weights.

pre_trained_model = ResNet50(input_shape = (150, 150, 3), 
                            include_top = False, weights=None)

You can read more in the documentation: Keras Applications / ResNet and ResNetV2 :

> Arguments
>
> - weights: one of None (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded.

huangapple
  • 本文由 发表于 2023年3月9日 16:12:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定