想象一下我有模型(tf.keras.Model):
class ContextExtractor(tf.keras.Model):
def __init__(self):
super().__init__()
self.model = self.__get_model()
def call(self, x, training=False, **kwargs):
features = self.model(x, training=training)
return features
def __get_model(self):
return self.__get_small_conv()
def __get_small_conv(self):
model = tf.keras.Sequential()
model.add(layers.Conv2D(32, (3, 3), strides=(2, 2), padding='same'))
model.add(layers.LeakyReLU(alpha=0.2))
model.add(layers.Conv2D(32, (3, 3), strides=(2, 2), padding='same'))
model.add(layers.LeakyReLU(alpha=0.2))
model.add(layers.Conv2D(64, (3, 3), strides=(2, 2), padding='same'))
model.add(layers.LeakyReLU(alpha=0.2))
model.add(layers.Conv2D(128, (3, 3), strides=(2, 2), padding='same'))
model.add(layers.LeakyReLU(alpha=0.2))
model.add(layers.Conv2D(256, (3, 3), strides=(2, 2), padding='same'))
model.add(layers.LeakyReLU(alpha=0.2))
model.add(layers.GlobalAveragePooling2D())
return model
我对其进行了训练并使用以下方法保存了它:
checkpoint = tf.train.Checkpoint(
model=self.model,
global_step=tf.train.get_or_create_global_step())
checkpoint.save(weights_path / f'epoch_{epoch}')
这意味着我有两个保存的文件:epoch_10-2.index
和epoch_10-2.data-00000-of-00001
现在我想部署我的模型。我想获取 .pb 文件。我怎么才能得到它?我想我需要以图形模式打开我的模型,加载我的权重并将其保存在 pb.file 中。实际上怎么做呢?