繁体   English   中英

Keras 中模型包装器的其他权重类型是什么?

[英]What are other weight types for model wrappers in Keras?

from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

model = ResNet50(weights='imagenet')

在这段代码中,有一个“包装器”(这就是它所指的)ResNet50。 我可以为此使用哪些其他类型的权重? 我试着环顾四周,但我什至不明白源代码; 也没有定论

可以在keras docgithub code 上找到

只有两个选择,要么None如果你只想架构没有权重,或imagenet负载imagenet权重。

编辑:如何使用我们自己的权重:

# Take a DenseNET201
backbone = tf.keras.applications.DenseNet201(input_shape=input_shape, weights=None, include_top=False)

# Change the model a little bit, because why not
input_image = tf.keras.layers.Input(shape=input_shape)
x = backcone(input_image)
x = tf.keras.layers.Conv2D(classes, (3, 3), padding='same', name='final_conv')(input)
x = tf.keras.layers.Activation(activation, name=activation)(x)
model = tf.keras.Model(input, x)

#... some additional code
# training part
optimizer = tf.keras.optimizers.Adam(lr=FLAGS.learning_rate)
model.compile(loss=loss,
              optimizer=optimizer,
              metrics=['accuracy', f1_m, recall_m, precision_m])

callbacks = [tf.keras.callbacks.ModelCheckpoint(filepath=ckpt_name)]

model.fit(train_generator, validation_data=validation_generator, validation_freq=1, epochs=10, callbacks=callbacks)

# using the callback there will weights saved in cktp_name each epoch
# Inference part, just need to reinstance the model (lines after #Change part comment)

model.load_weights(ckpt_name)

results = model.predict(test_generator, verbose=1)

您显然不需要更改模型,您可以使用x = backbone(x)然后model = tf.keras.Model(input, x)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM