【发布时间】:2020-03-25 07:58:48
【问题描述】:
我使用以下代码读取文件夹中的所有图像并将它们用于图像增强。 load_images() 函数将所有图像读取为 numpy 数组,但是当我在代码的第二部分中将此函数用作图像增强的输入时,我收到错误(使用序列设置数组元素)。任何帮助表示赞赏。
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from matplotlib.pyplot import imread, imshow, subplots, show
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
import os
image_path = '/path/to/images/'
def load_images(image_path):
imagees = []
for filename in os.listdir(image_path):
img = mpimg.imread(os.path.join(image_path, filename))
if img is not None:
imagees.append(img)
return imagees
datagen = ImageDataGenerator(rotation_range=90, width_shift_range=0.3,
height_shift_range=0.3,shear_range=45.0, brightness_range=(0.1, 0.9),
zoom_range=[0.5, 1.5],channel_shift_range = 150.0, horizontal_flip=True, vertical_flip=True)
images = load_images(image_path)
images = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
save_here = '/path/to/images/'
datagen.fit(images)
for x, val in zip(datagen.flow(images,
save_to_dir=save_here,
save_prefix='aug',
save_format='png'),range(36)):
pass
【问题讨论】:
-
总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图)放在有问题的(不是评论)中。还有其他有用的信息。
-
首先您使用
load_images读取所有图像并将它们分配给images,但在一行之后您将image分配给相同的变量images和image可能是单个图像。如果你想重塑所有图像,那么你应该在load_images(使用img.reshape())中进行,或者创建for-loop,它将对每个图像(来自列表images)分别进行。 -
您可以使用
print()和/或print( type() )- 查看变量images-print(images)print( type(images) )中的内容。在images =load_images()之后,您应该拥有带有图像/数组的list。在images = image.reshape()之后,您应该有单个图像/数组。
标签: python numpy tensorflow matplotlib python-os