【问题标题】:Implementing lambda preprocess function for VGGFace为 VGGFace 实现 lambda 预处理函数
【发布时间】:2021-03-01 01:54:20
【问题描述】:

您好,我正在尝试使用迁移学习制作网络,同时微调 VGGFace 实现:

img_height, img_width = 224,224
module=VGGFace(model = 'resnet50',include_top = False,weights = 'vggface',input_shape = (img_height, img_width, 3))

model = tf.keras.Sequential([
    tf.keras.layers.InputLayer(input_shape=(img_height, img_width, 3)),             
    tf.keras.layers.Lambda(lambda x: utils.preprocess_input(np.expand_dims(x, axis=0), version=2)),
    module,
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(256, activation="relu"),
    tf.keras.layers.Dense(num, "sigmoid"), 
])

但是出现以下错误:

Cannot convert a symbolic Tensor (Placeholder:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

我想这是因为 utils.preprocess_input 函数适用于 numpy 数组而不是张量,但我需要它在网络架构中,因为我要处理很多图像并且我无法存储它们立刻。有什么建议可以让它发挥作用吗?

【问题讨论】:

  • 错误说明了一切:您正在尝试将张量传递给 NumPy 调用。试试这个:utils.preprocess_input(np.expand_dims(x.numpy(), axis=0), version=2)
  • @MertKöklü 谢谢,我同意你的看法!我尝试了不同的方法将张量转换为数组,但似乎没有任何效果,这个说'Tensor' object has no attribute 'numpy'

标签: python tensorflow keras keras-vggface


【解决方案1】:

您正在尝试使用 numpy expand_dims 扩展张量的维度。 试试这个:

utils.preprocess_input(tf.expand_dims(x, axis=0), version=2)

【讨论】:

    猜你喜欢
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    相关资源
    最近更新 更多