如何使用 Tensorflow Hub 加载模型并进行预测?

原文标题How to load a model using Tensorflow Hub and make a prediction?

这应该是一个简单的任务:下载一个以 tensorflow_hub 格式保存的模型,使用 tensorflow_hub 加载,然后使用..

这是我正在尝试使用的模型(simCLR 存储在 Google Cloud 中):https://console.cloud.google.com/storage/browser/simclr-checkpoints/simclrv2/pretrained/r50_1x_sk0;tab=objects?pageState=( %22StorageObjectListTable%22:(%22f%22:%22%255B%255D%22))&prefix=&forceOnObjectsSortingFiltering=false

我下载了 /hub 文件夹,正如他们所说,使用

gsutil -m cp -r \
"gs://simclr-checkpoints/simclrv2/pretrained/r50_1x_sk0/hub" \

.

/hub 文件夹包含以下文件:

/saved_model.pb
/tfhub_module.pb
/variables/variables.index
/variables/variables.data-00000-of-00001

到目前为止一切顺利。现在在 python3、tensorflow2、tensorflow_hub 0.12 中,我运行以下代码:

import numpy as np
import tensorflow as tf
import tensorflow_hub as hub

path_to_hub = '/home/my_name/my_path/simclr/hub'

# Attempt 1
m = tf.keras.models.Sequential([hub.KerasLayer(path_to_hub, input_shape=(224,224,3))])

# Attempt 2

m = tf.keras.models.Sequential(hub.KerasLayer(hubmod))
m.build(input_shape=[None,224,224,3])


# Attempt 3

m = hub.KerasLayer(hub.load(hubmod))

# Toy Data Test

X = np.random.random((1,244,244,3)).astype(np.float32)

y = m.predict(X)

加载集线器模型的这 3 个选项均不起作用,并出现以下错误:

Attempt 1 :

ValueError: Error when checking input: expected keras_layer_2_input to have shape (224, 224, 3) but got array with shape (244, 244, 3)

Attempt 2:

tensorflow.python.framework.errors_impl.UnknownError:  Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
     [[{{node sequential_3/keras_layer_3/StatefulPartitionedCall/base_model/conv2d/Conv2D}}]] [Op:__inference_keras_scratch_graph_46402]

Function call stack:
keras_scratch_graph

Attempt 3:

ValueError: Expected a string, got <tensorflow.python.training.tracking.tracking.AutoTrackable object at 0x7fa71c7a2dd0>

这 3 次尝试都是取自 tensorflow_hub 教程的代码,并在 stackoverflow 的其他答案中重复,但没有一个有效,我不知道如何从这些错误消息中继续。

感谢任何帮助,谢谢。

更新 1:如果我尝试使用此 ResNet50 集线器/https://storage.cloud.google.com/simclr-gcs/checkpoints/ResNet50_1x.zip,也会出现同样的问题

原文链接:https://stackoverflow.com//questions/71664909/how-to-load-a-model-using-tensorflow-hub-and-make-a-prediction

回复

我来回复
  • hirschme的头像
    hirschme 评论

    正如@Frightera 指出的那样,输入形状存在错误。通过允许所选 GPU 上的内存增长也解决了“尝试 2”上的错误。 “尝试3”仍然不起作用,但至少有两种方法可以加载和使用以/hub格式保存的模型:

    import numpy as np
    import tensorflow as tf
    import tensorflow_hub as hub   
    
    gpus = tf.config.experimental.list_physical_devices('GPU')
    tf.config.experimental.set_visible_devices(gpus[0], 'GPU')
    tf.config.experimental.set_memory_growth(gpus[0], True)
    
    
    hubmod = 'https://tfhub.dev/google/imagenet/mobilenet_v2_035_96/feature_vector/5'
    
    # Alternative 1 - Works!
    
    m = tf.keras.models.Sequential([hub.KerasLayer(hubmod, input_shape=(96,96,3))])
    print(m.summary())
    
    # Alternative 2 - Works!
    
    m = tf.keras.models.Sequential(hub.KerasLayer(hubmod))
    m.build(input_shape=[None, 96,96,3])
    print(m.summary())
    
    # Alternative 3 - Doesnt work
    
    #m = hub.KerasLayer(hub.load(hubmod))
    #m.build(input_shape=[None, 96,96,3])
    #print(m.summary())
    
    # Test
    
    X = np.random.random((1,96,96,3)).astype(np.float32)
    y = m.predict(X)
    
    print(y.shape)
    
    2年前 0条评论