Tensorflow 离线使用预训练模型

puh*_*huk 1 supervisord tensorflow transfer-learning

我正在使用 Tensorflow 的 mobilenet 和以下代码。当我从本地 PC 运行此代码时,它会下载权重文件。但是,当上传到服务器时,我无法在线下载它。

  1. 有什么方法可以将 MobileNetV2 与 tensorflow 2.0.0 一起使用。

  2. 或者,我使用 MobielNetV2 训练了自己的权重文件,然后可以在没有权重选项的情况下使用下面的文件

    mobilenet = tf.keras.applications.mobilenet_v2.MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights='imagenet')

  3. 我想从 linux 服务器使用 supervisord 。看来我需要使用绝对路径而不是相对路径。任何人都可以帮助我在离线环境中使用带有 supervisord 的 MobileNetV2

小智 5

您可以先使用:

mobilenet = tf.keras.applications.mobilenet_v2.MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights='imagenet')
Run Code Online (Sandbox Code Playgroud)

然后将权重保存在某处:

mobilenet.save_weights('somefolder/mobilenetweights.h5')
Run Code Online (Sandbox Code Playgroud)

然后当你离线时,你可以先调用:

mobilenet = tf.keras.applications.mobilenet_v2.MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights=None)
Run Code Online (Sandbox Code Playgroud)

注意Noneinweights参数。之后,您可以从之前保存的文件中加载权重:

mobilenet.load_weights('somefolder/mobilenetweights.h5')
Run Code Online (Sandbox Code Playgroud)

这应该有效。

当您调用 mobilenet 模型时,如果您要求它使用 为您提供 imagenet 权重weights='imagenet',则需要互联网连接才能下载这些权重。所以它不会离线工作。这里解释的方法应该可以让它工作。