【问题标题】:how do I append this two arrays without crashing my RAM?如何在不破坏 RAM 的情况下附加这两个数组?
【发布时间】:2021-04-21 12:05:43
【问题描述】:

如果我尝试附加 train_images 和 new_imgs 我的 RAM 崩溃。我试图将 train_images 转换为常规列表,但同样的问题。此外,当我尝试附加图像 (batch[0]) 时,一个完整的批次大约需要两秒钟,这是不可接受的。没有错误信息。没有错误信息,程序只是没有响应然后就关闭了,我需要再次运行它,但每次都会出现问题。

%tensorflow_version 2.x  
import tensorflow as tf

from tensorflow.keras import datasets, layers, models
import numpy as np
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator

我已经安装了数据集并预先处理了数据:

(train_images, train_labels), (test_images, test_labels) = 
datasets.cifar10.load_data()

train_images = train_images / 255.0;

创建了一个图像数据生成器:

datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')

我创建了一个数组来存储所有修改后的图像,然后开始循环遍历我的所有数据集并重塑我的数据以准备修改:

new_images = []
for num, test_img in enumerate(train_images):
  img = image.img_to_array(test_img)
  img = img.reshape((1,) + img.shape)

我已经通过图像数据生成器运行了重新整形的图像,然后存储在数组中:

  i = 0
  print(str(num) + ' out of ' + str(len(train_images)) + ' left')
  for batch in datagen.flow(img):
      new_images.append(batch[0])
      i += 1
      if i > 4:
          break

现在我无法以任何方式追加数组,而不会使我的 12 GB RAM 崩溃...

【问题讨论】:

  • "我的 RAM 崩溃" 你能说得更具体点吗?您可以添加错误消息吗?此外,“12 RAM”是否意味着为应用程序分配/可用的 12 GB ram?

标签: python numpy tensorflow machine-learning neural-network


【解决方案1】:

我创建了一个数组来存储所有修改后的图像

生成的图片不需要存储,使用.fit(...).flow(...)方法即可。

datagen.fit(train_images)

model.fit(datagen.flow(train_images, train_labels, batch_size=32),
          steps_per_epoch=len(train_images) / 32, epochs=epochs)

ImageDataGenerator

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 2019-02-28
    • 2017-07-17
    • 2013-04-13
    • 2013-08-29
    • 2011-08-19
    • 1970-01-01
    相关资源
    最近更新 更多