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



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

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

我用下载了/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

@Frightera指出,输入形状存在错误。此外,";尝试2";通过允许所选GPU上的内存增长来解决"尝试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)

相关内容

  • 没有找到相关文章

最新更新