作者:Martin Görner
创建日期 2023-12-13
上次修改 2023-12-13
说明:在共享您的深度学习模型时,请使用函数式子类化模式对其进行打包。
Keras 是共享您尖端深度学习模型的理想框架,可以将其放在预训练(或未训练)模型库中。数百万名 ML 工程师精通熟悉的 Keras API,使您的模型可供全球社区访问,无论他们首选哪个后端(Jax、PyTorch 或 TensorFlow)。
Keras API 的优点之一是,它允许用户以编程方式检查或编辑模型,这是在基于预训练模型创建新架构或工作流时必不可少的功能。
在分发模型时,Keras 团队建议使用函数式子类化模式对其进行打包。以这种方式实现的模型结合了两个优势
model = model_collection_xyz.AmazingModel()
本指南介绍了如何使用函数式子类化模式,并展示了它在编程模型自省和模型手术方面的优势。它还展示了可共享 Keras 模型的另外两个最佳实践:配置模型以支持最广泛的输入范围,例如各种大小的图像,以及使用字典输入以提高更复杂模型的可读性和可用性。
import keras
import tensorflow as tf # only for tf.data
print("Keras version", keras.version())
print("Keras is running on", keras.config.backend())
Keras version 3.0.1
Keras is running on tensorflow
让我们加载一个 MNIST 数据集,这样我们就可以用它进行训练。
# tf.data is a great API for putting together a data stream.
# It works whether you use the TensorFlow, PyTorch or Jax backend,
# as long as you use it in the data stream only and not inside of a model.
BATCH_SIZE = 256
(x_train, train_labels), (x_test, test_labels) = keras.datasets.mnist.load_data()
train_data = tf.data.Dataset.from_tensor_slices((x_train, train_labels))
train_data = train_data.map(
lambda x, y: (tf.expand_dims(x, axis=-1), y)
) # 1-channel monochrome
train_data = train_data.batch(BATCH_SIZE)
train_data = train_data.cache()
train_data = train_data.shuffle(5000, reshuffle_each_iteration=True)
train_data = train_data.repeat()
test_data = tf.data.Dataset.from_tensor_slices((x_test, test_labels))
test_data = test_data.map(
lambda x, y: (tf.expand_dims(x, axis=-1), y)
) # 1-channel monochrome
test_data = test_data.batch(10000)
test_data = test_data.cache()
STEPS_PER_EPOCH = len(train_labels) // BATCH_SIZE
EPOCHS = 5
模型被包装在一个类中,以便最终用户可以通过调用构造函数 MnistModel()
来正常实例化它,而不是调用工厂函数。
class MnistModel(keras.Model):
def __init__(self, **kwargs):
# Keras Functional model definition. This could have used Sequential as
# well. Sequential is just syntactic sugar for simple functional models.
# 1-channel monochrome input
inputs = keras.layers.Input(shape=(None, None, 1), dtype="uint8")
# pixel format conversion from uint8 to float32
y = keras.layers.Rescaling(1 / 255.0)(inputs)
# 3 convolutional layers
y = keras.layers.Conv2D(
filters=16, kernel_size=3, padding="same", activation="relu"
)(y)
y = keras.layers.Conv2D(
filters=32, kernel_size=6, padding="same", activation="relu", strides=2
)(y)
y = keras.layers.Conv2D(
filters=48, kernel_size=6, padding="same", activation="relu", strides=2
)(y)
# 2 dense layers
y = keras.layers.GlobalAveragePooling2D()(y)
y = keras.layers.Dense(48, activation="relu")(y)
y = keras.layers.Dropout(0.4)(y)
outputs = keras.layers.Dense(
10, activation="softmax", name="classification_head" # 10 classes
)(y)
# A Keras Functional model is created by calling keras.Model(inputs, outputs)
super().__init__(inputs=inputs, outputs=outputs, **kwargs)
让我们实例化并训练这个模型。
model = MnistModel()
model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["sparse_categorical_accuracy"],
)
history = model.fit(
train_data,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
validation_data=test_data,
)
Epoch 1/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 9s 33ms/step - loss: 1.8916 - sparse_categorical_accuracy: 0.2933 - val_loss: 0.4278 - val_sparse_categorical_accuracy: 0.8864
Epoch 2/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 7s 31ms/step - loss: 0.5723 - sparse_categorical_accuracy: 0.8201 - val_loss: 0.2703 - val_sparse_categorical_accuracy: 0.9248
Epoch 3/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 7s 31ms/step - loss: 0.4063 - sparse_categorical_accuracy: 0.8772 - val_loss: 0.2010 - val_sparse_categorical_accuracy: 0.9400
Epoch 4/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 7s 31ms/step - loss: 0.3391 - sparse_categorical_accuracy: 0.8996 - val_loss: 0.1869 - val_sparse_categorical_accuracy: 0.9427
Epoch 5/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 7s 31ms/step - loss: 0.2989 - sparse_categorical_accuracy: 0.9120 - val_loss: 0.1513 - val_sparse_categorical_accuracy: 0.9557
请注意,在上面的模型定义中,输入是用未定义的维度指定的:Input(shape=(None, None, 1)
这允许模型接受任何大小的图像作为输入。但是,这只有在可以将松散定义的形状传播到所有层,并且仍然可以确定所有权重的尺寸时才有效。
model = MnistModel()
model = ModelXYZ(input_size=...)
Keras 为每个模型维护一个可编程访问的层图。它可用于自省,并通过 model.layers
或 layer.layers
属性进行访问。实用程序函数 model.summary()
也是在内部使用这种机制。
model = MnistModel()
# Model summary works
model.summary()
# Recursively walking the layer graph works as well
def walk_layers(layer):
if hasattr(layer, "layers"):
for layer in layer.layers:
walk_layers(layer)
else:
print(layer.name)
print("\nWalking model layers:\n")
walk_layers(model)
Model: "mnist_model_1"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩ │ input_layer_1 (InputLayer) │ (None, None, None, 1) │ 0 │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ rescaling_1 (Rescaling) │ (None, None, None, 1) │ 0 │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ conv2d_3 (Conv2D) │ (None, None, None, 16) │ 160 │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ conv2d_4 (Conv2D) │ (None, None, None, 32) │ 18,464 │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ conv2d_5 (Conv2D) │ (None, None, None, 48) │ 55,344 │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ global_average_pooling2d_1 │ (None, 48) │ 0 │ │ (GlobalAveragePooling2D) │ │ │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ dense_1 (Dense) │ (None, 48) │ 2,352 │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ dropout_1 (Dropout) │ (None, 48) │ 0 │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ classification_head (Dense) │ (None, 10) │ 490 │ └─────────────────────────────────┴───────────────────────────┴────────────┘
Total params: 76,810 (300.04 KB)
Trainable params: 76,810 (300.04 KB)
Non-trainable params: 0 (0.00 B)
Walking model layers:
input_layer_1
rescaling_1
conv2d_3
conv2d_4
conv2d_5
global_average_pooling2d_1
dense_1
dropout_1
classification_head
最终用户可能希望从您的库中实例化模型,但在使用前对其进行修改。函数式模型具有可编程访问的层图。可以通过切片和拼接图并创建一个新的函数式模型来进行编辑。
另一种方法是派生模型代码并进行修改,但这会迫使用户无限期地维护其派生版本。
示例:实例化模型,但将分类头更改为进行二元分类,“0”或“非 0”,而不是原始的 10 路数字分类。
model = MnistModel()
input = model.input
# cut before the classification head
y = model.get_layer("classification_head").input
# add a new classification head
output = keras.layers.Dense(
1, # single class for binary classification
activation="sigmoid",
name="binary_classification_head",
)(y)
# create a new functional model
binary_model = keras.Model(input, output)
binary_model.summary()
Model: "functional_1"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩ │ input_layer_2 (InputLayer) │ (None, None, None, 1) │ 0 │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ rescaling_2 (Rescaling) │ (None, None, None, 1) │ 0 │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ conv2d_6 (Conv2D) │ (None, None, None, 16) │ 160 │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ conv2d_7 (Conv2D) │ (None, None, None, 32) │ 18,464 │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ conv2d_8 (Conv2D) │ (None, None, None, 48) │ 55,344 │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ global_average_pooling2d_2 │ (None, 48) │ 0 │ │ (GlobalAveragePooling2D) │ │ │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ dense_2 (Dense) │ (None, 48) │ 2,352 │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ dropout_2 (Dropout) │ (None, 48) │ 0 │ ├─────────────────────────────────┼───────────────────────────┼────────────┤ │ binary_classification_head │ (None, 1) │ 49 │ │ (Dense) │ │ │ └─────────────────────────────────┴───────────────────────────┴────────────┘
Total params: 76,369 (298.32 KB)
Trainable params: 76,369 (298.32 KB)
Non-trainable params: 0 (0.00 B)
我们现在可以训练新的模型作为二元分类器。
# new dataset with 0 / 1 labels (1 = digit '0', 0 = all other digits)
bin_train_data = train_data.map(
lambda x, y: (x, tf.cast(tf.math.equal(y, tf.zeros_like(y)), dtype=tf.uint8))
)
bin_test_data = test_data.map(
lambda x, y: (x, tf.cast(tf.math.equal(y, tf.zeros_like(y)), dtype=tf.uint8))
)
# appropriate loss and metric for binary classification
binary_model.compile(
optimizer="adam", loss="binary_crossentropy", metrics=["binary_accuracy"]
)
history = binary_model.fit(
bin_train_data,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
validation_data=bin_test_data,
)
Epoch 1/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 9s 33ms/step - binary_accuracy: 0.8926 - loss: 0.3635 - val_binary_accuracy: 0.9235 - val_loss: 0.1777
Epoch 2/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 7s 31ms/step - binary_accuracy: 0.9411 - loss: 0.1620 - val_binary_accuracy: 0.9766 - val_loss: 0.0748
Epoch 3/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 7s 31ms/step - binary_accuracy: 0.9751 - loss: 0.0794 - val_binary_accuracy: 0.9884 - val_loss: 0.0414
Epoch 4/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 7s 31ms/step - binary_accuracy: 0.9848 - loss: 0.0480 - val_binary_accuracy: 0.9915 - val_loss: 0.0292
Epoch 5/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 7s 31ms/step - binary_accuracy: 0.9910 - loss: 0.0326 - val_binary_accuracy: 0.9917 - val_loss: 0.0286
在具有多个输入的更复杂模型中,将输入结构化为字典可以提高可读性和可用性。使用函数式模型可以轻松做到这一点
class MnistDictModel(keras.Model):
def __init__(self, **kwargs):
#
# The input is a dictionary
#
inputs = {
"image": keras.layers.Input(
shape=(None, None, 1), # 1-channel monochrome
dtype="uint8",
name="image",
)
}
# pixel format conversion from uint8 to float32
y = keras.layers.Rescaling(1 / 255.0)(inputs["image"])
# 3 conv layers
y = keras.layers.Conv2D(
filters=16, kernel_size=3, padding="same", activation="relu"
)(y)
y = keras.layers.Conv2D(
filters=32, kernel_size=6, padding="same", activation="relu", strides=2
)(y)
y = keras.layers.Conv2D(
filters=48, kernel_size=6, padding="same", activation="relu", strides=2
)(y)
# 2 dense layers
y = keras.layers.GlobalAveragePooling2D()(y)
y = keras.layers.Dense(48, activation="relu")(y)
y = keras.layers.Dropout(0.4)(y)
outputs = keras.layers.Dense(
10, activation="softmax", name="classification_head" # 10 classes
)(y)
# A Keras Functional model is created by calling keras.Model(inputs, outputs)
super().__init__(inputs=inputs, outputs=outputs, **kwargs)
我们现在可以使用结构化为字典的输入来训练模型。
model = MnistDictModel()
# reformat the dataset as a dictionary
dict_train_data = train_data.map(lambda x, y: ({"image": x}, y))
dict_test_data = test_data.map(lambda x, y: ({"image": x}, y))
model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["sparse_categorical_accuracy"],
)
history = model.fit(
dict_train_data,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
validation_data=dict_test_data,
)
Epoch 1/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 9s 34ms/step - loss: 1.8702 - sparse_categorical_accuracy: 0.3175 - val_loss: 0.4505 - val_sparse_categorical_accuracy: 0.8779
Epoch 2/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 8s 32ms/step - loss: 0.5991 - sparse_categorical_accuracy: 0.8131 - val_loss: 0.2582 - val_sparse_categorical_accuracy: 0.9245
Epoch 3/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 7s 32ms/step - loss: 0.3916 - sparse_categorical_accuracy: 0.8846 - val_loss: 0.1938 - val_sparse_categorical_accuracy: 0.9422
Epoch 4/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 8s 33ms/step - loss: 0.3109 - sparse_categorical_accuracy: 0.9089 - val_loss: 0.1450 - val_sparse_categorical_accuracy: 0.9566
Epoch 5/5
234/234 ━━━━━━━━━━━━━━━━━━━━ 8s 32ms/step - loss: 0.2775 - sparse_categorical_accuracy: 0.9197 - val_loss: 0.1316 - val_sparse_categorical_accuracy: 0.9608