如何使用 TensorFlow 2 数据集从 URL 加载图像
How to load images from URL with a TensorFlow 2 dataset
我想使用 TensorFlow 2 数据集对象将图像提供给 CNN。我的图像位于 AWS S3 上,但我将在示例中使用来自维基百科的图像(问题相同)。
image_urls = [
'https://upload.wikimedia.org/wikipedia/commons/6/60/Matterhorn_from_Domh%C3%BCtte_-_2.jpg',
'https://upload.wikimedia.org/wikipedia/commons/6/6e/Matterhorn_from_Klein_Matterhorn.jpg',
]
dataset = tf.data.Dataset.from_tensor_slices(image_urls)
def read_image_from_url(url):
img_array = None
with urlopen(url) as request:
img_array = np.asarray(bytearray(request.read()), dtype=np.uint8)
img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #as RGB image (cv2 is BGR by default)
当我用数据集的一个元素测试我的函数时,它起作用了:
url = next(iter(dataset)).numpy().decode('utf-8')
img = read_image_from_url(url)
plt.imshow(img)
但是当我将我的函数映射到数据集以创建一个服务于图像的新数据集时,它失败了:
dataset_images = dataset.map(lambda x: read_image_from_url(x.numpy().decode('utf-8')))
AttributeError: in converted code:
<ipython-input-6-e8eb89833196>:2 None *
map_func=lambda x: read_image_from_url(x.numpy().decode('utf-8')),
AttributeError: 'Tensor' object has no attribute 'numpy'
显然,当使用 next
或 map
迭代时,数据集提供了不同的 dtype。知道我该如何解决这个问题吗?
好吧,这比需要的要难得多:
import tensorflow as tf
import numpy as np
import cv2
from urllib.request import urlopen
import matplotlib.pyplot as plt
image_urls = [
'https://upload.wikimedia.org/wikipedia/commons/6/60/Matterhorn_from_Domh%C3%BCtte_-_2.jpg',
'https://upload.wikimedia.org/wikipedia/commons/6/6e/Matterhorn_from_Klein_Matterhorn.jpg',
]
dataset = tf.data.Dataset.from_tensor_slices(image_urls)
def get(url):
with urlopen(str(url.numpy().decode("utf-8"))) as request:
img_array = np.asarray(bytearray(request.read()), dtype=np.uint8)
img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
def read_image_from_url(url):
return tf.py_function(get, [url], tf.uint8)
dataset_images = dataset.map(lambda x: read_image_from_url(x))
for d in dataset_images:
print(d)
为什么第一个成功,然后在 tf.Dataset
失败了? tf.Dataset
是在 graph mode
中定义的,而不是像第一个那样在 eager mode
中定义的。图形模式更快,并且 tf.Dataset
针对速度进行了优化,因此它是有道理的。你不能在图形模式下执行 .numpy()
,因为所有内容都应在 tensorflow
操作中定义。 py_func
将 python 函数包装在 tf.Operation
中,该函数在 eager mode
中执行,这正是我们所需要的。
注意:我尝试了 tf.keras.utils.get_file()
,但我 运行 遇到了与您在此处描述的类似问题。希望这对您有所帮助!
我想使用 TensorFlow 2 数据集对象将图像提供给 CNN。我的图像位于 AWS S3 上,但我将在示例中使用来自维基百科的图像(问题相同)。
image_urls = [
'https://upload.wikimedia.org/wikipedia/commons/6/60/Matterhorn_from_Domh%C3%BCtte_-_2.jpg',
'https://upload.wikimedia.org/wikipedia/commons/6/6e/Matterhorn_from_Klein_Matterhorn.jpg',
]
dataset = tf.data.Dataset.from_tensor_slices(image_urls)
def read_image_from_url(url):
img_array = None
with urlopen(url) as request:
img_array = np.asarray(bytearray(request.read()), dtype=np.uint8)
img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #as RGB image (cv2 is BGR by default)
当我用数据集的一个元素测试我的函数时,它起作用了:
url = next(iter(dataset)).numpy().decode('utf-8')
img = read_image_from_url(url)
plt.imshow(img)
但是当我将我的函数映射到数据集以创建一个服务于图像的新数据集时,它失败了:
dataset_images = dataset.map(lambda x: read_image_from_url(x.numpy().decode('utf-8')))
AttributeError: in converted code:
<ipython-input-6-e8eb89833196>:2 None *
map_func=lambda x: read_image_from_url(x.numpy().decode('utf-8')),
AttributeError: 'Tensor' object has no attribute 'numpy'
显然,当使用 next
或 map
迭代时,数据集提供了不同的 dtype。知道我该如何解决这个问题吗?
好吧,这比需要的要难得多:
import tensorflow as tf
import numpy as np
import cv2
from urllib.request import urlopen
import matplotlib.pyplot as plt
image_urls = [
'https://upload.wikimedia.org/wikipedia/commons/6/60/Matterhorn_from_Domh%C3%BCtte_-_2.jpg',
'https://upload.wikimedia.org/wikipedia/commons/6/6e/Matterhorn_from_Klein_Matterhorn.jpg',
]
dataset = tf.data.Dataset.from_tensor_slices(image_urls)
def get(url):
with urlopen(str(url.numpy().decode("utf-8"))) as request:
img_array = np.asarray(bytearray(request.read()), dtype=np.uint8)
img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
def read_image_from_url(url):
return tf.py_function(get, [url], tf.uint8)
dataset_images = dataset.map(lambda x: read_image_from_url(x))
for d in dataset_images:
print(d)
为什么第一个成功,然后在 tf.Dataset
失败了? tf.Dataset
是在 graph mode
中定义的,而不是像第一个那样在 eager mode
中定义的。图形模式更快,并且 tf.Dataset
针对速度进行了优化,因此它是有道理的。你不能在图形模式下执行 .numpy()
,因为所有内容都应在 tensorflow
操作中定义。 py_func
将 python 函数包装在 tf.Operation
中,该函数在 eager mode
中执行,这正是我们所需要的。
注意:我尝试了 tf.keras.utils.get_file()
,但我 运行 遇到了与您在此处描述的类似问题。希望这对您有所帮助!