从存储桶下载时 Cloud Function 的奇怪行为

问题描述

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

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

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

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,3). 我不明白为什么这在我的本地机器上有效,而在云功能上无效。

解决方法

来自评论

在此云中,Eager Execution 似乎默认禁用 环境。所以之前调用 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}