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')

在此代码中,有 "wrapper"(这就是它所指的)ResNet50。我可以为此使用哪些其他类型的权重?我试着环顾四周,但我什至不了解源代码;也没有定论

您可以在 keras doc 上找到它 或者 github 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)