下面代码在64G内存会溢出,请设计不会溢出的方法 for i in range(LEN): if (i+1) % 50 == 0: print("load {}-images in memory!".format(i+1)) lr_image, hr_image = imageio.imread(self.lr_filenames[i], pilmode="RGB"), imageio.imread(self.hr_filenames[i], pilmode="RGB") if self.colors == 1: lr_image, hr_image = sc.rgb2ycbcr(lr_image)[:, :, 0:1], sc.rgb2ycbcr(hr_image)[:, :, 0:1] self.hr_images.append(hr_image) self.lr_images.append(lr_image)
时间: 2024-02-14 08:05:20 浏览: 68
可以将代码中的 `self.hr_images` 和 `self.lr_images` 改为生成器函数,这样就不需要将所有图片都加载到内存中,从而避免了内存溢出的问题。具体实现可以参考以下代码:
```
def images_generator(lr_filenames, hr_filenames, colors):
for i in range(len(lr_filenames)):
if (i+1) % 50 == 0:
print("load {}-images in memory!".format(i+1))
lr_image, hr_image = imageio.imread(lr_filenames[i], pilmode="RGB"), imageio.imread(hr_filenames[i], pilmode="RGB")
if colors == 1:
lr_image, hr_image = sc.rgb2ycbcr(lr_image)[:, :, 0:1], sc.rgb2ycbcr(hr_image)[:, :, 0:1]
yield lr_image, hr_image
```
调用该生成器函数时,可以按需加载每一张图片:
```
generator = images_generator(self.lr_filenames, self.hr_filenames, self.colors)
for lr_image, hr_image in generator:
# do something with lr_image and hr_image
```
阅读全文