【问题标题】:Cloud Function strange behaviour when downloading from storage bucket从存储桶下载时云函数的奇怪行为
【发布时间】:2021-07-12 18:46:52
【问题描述】:

我使用 (python) 从我的云存储桶下载我的图像:

storage_client = storage.Client()
bucket = storage_client.get_bucket('mybucket')
blob = bucket.blob('myimage.png')
img = blob.download_as_bytes(raw_download=True)

然后使用张量流,我将图像转换为张量,并返回图像的形状。

image = tf.io.decode_image(img, dtype = tf.dtypes.float32)
image = tf.expand_dims(image, axis=0)

shape = image.shape
return {"1":shape}

当我测试函数时,我收到 {1": ""},就像我在本地机器上运行代码一样,我将收到张量/图像的实际形状 (1, 224 , 224, 3)。我不明白为什么这适用于我的本地机器,但不适用于云功能。

【问题讨论】:

  • 你没有问题/错误?这就像如果有一个错误。可能是因为有依赖没有安装或者不兼容
  • 我没有任何问题/错误——只返回这个应该是张量形状的“未知”。由于我已经发布了虽然我已经进行了更多调查并且我认为这是因为函数 tf.io.decode- 我索引到 img 字节并正确返回这些,例如 return img[0] 我收到了字节值,但是如果我改为返回 image[0,0,0],我会收到 'stridedslice0'
  • 我不确定为什么这个函数不起作用——我指定了包含 tensorflow 2.4 的运行时 2.4。这应该包括 tf.io
  • 澄清一下,我收到 { "predictions": "Tensor(\"strided_slice:0\", shape=(), dtype=float32)" }
  • 我已经弄清楚了,在这个云环境中,Eager Execution 似乎默认被禁用。所以在解决问题之前调用 tf.compat.v1.enable_eager_execution() 。奇怪的是为什么默认情况下禁用它...

标签: tensorflow google-cloud-platform google-cloud-functions google-cloud-storage google-ai-platform


【解决方案1】:

来自 cmets

在此云中默认情况下似乎禁用了急切执行 环境。所以之前打电话给tf.compat.v1.enable_eager_execution() 解决问题(转述自waltwhite)

import tensorflow as tf
tf.compat.v1.enable_eager_execution()

image = tf.io.decode_image(img, dtype = tf.dtypes.float32)
image = tf.expand_dims(image, axis=0)

shape = image.shape
return {"1":shape}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    相关资源
    最近更新 更多