【发布时间】:2019-09-13 06:03:49
【问题描述】:
我想制作一个生成器,从 url 生成批量图像以训练 keras 模型。我有另一个生成器,可以为我提供图像 url。
我目前所做的是将图像下载到磁盘,然后从磁盘加载图像。
def loadImage(URL):
with urllib.request.urlopen(URL) as url:
with open('temp.jpg', 'wb') as f:
f.write(url.read())
img_path = 'temp.jpg'
img = image.load_img(img_path, target_size=(125, 125))
os.remove(img_path)
x = image.img_to_array(img)
return x
def imageGenerator(batch_size):
i = 0
batch = []
for URL in imageUrlGenerator():
if i>batch_size:
yield batch
batch = []
i=0
batch.append(loadImage(URL))
i+=1
这可行,但我想知道是否没有更快的方法从网络加载图像而无需在磁盘中写入和读取。
【问题讨论】: