Keras.ImageDataGenerator 结果展示 [flow()]
Keras.ImageDataGenerator result display [flow()]
我正在尝试显示由 Imagedatagenerator.flow() 生成的图像,但我无法这样做。
我正在使用单个图像并将其传递给 .flow(img_path) 以生成增强图像,直到总数符合我们的要求:
total = 0
for image in imageGen:
total += 1
if total == 10:
break
.flow()
imageGen = aug.flow(image_path, batch_size=1,
save_to_dir="/path/to/save_dir",
save_prefix="", save_format='png')
如何接收在循环中生成的图像,以便在运行时显示它?
如果要使用图片路径,可以使用flow_from_directory,并传递包含单张图片的图片文件夹。要从生成器获取图像,请使用 dir_It.next()
并访问第一个元素,因为此函数的 return 是:
A DirectoryIterator yielding tuples of (x, y) where x is a numpy array containing a batch of images with shape (batch_size, *target_size, channels) and y is a numpy array of corresponding labels.
要显示图像,您可以使用 matplotlib
中的 plt.imshow
传递批处理的第一张(也是唯一一张)图像 plt.imshow(img[0])
。
文件夹结构
├── data
│ └── smile
│ └── smile_8.jpg
# smile_8.jpg shape (256,256,3)
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
datagen = keras.preprocessing.image.ImageDataGenerator(
rescale=1./255,
rotation_range=180,
width_shift_range=0.2,
height_shift_range=0.2,
)
dir_It = datagen.flow_from_directory(
"data/",
batch_size=1,
save_to_dir="output/",
save_prefix="",
save_format='png',
)
for _ in range(5):
img, label = dir_It.next()
print(img.shape) # (1,256,256,3)
plt.imshow(img[0])
plt.show()
我正在尝试显示由 Imagedatagenerator.flow() 生成的图像,但我无法这样做。
我正在使用单个图像并将其传递给 .flow(img_path) 以生成增强图像,直到总数符合我们的要求:
total = 0
for image in imageGen:
total += 1
if total == 10:
break
.flow()
imageGen = aug.flow(image_path, batch_size=1,
save_to_dir="/path/to/save_dir",
save_prefix="", save_format='png')
如何接收在循环中生成的图像,以便在运行时显示它?
如果要使用图片路径,可以使用flow_from_directory,并传递包含单张图片的图片文件夹。要从生成器获取图像,请使用 dir_It.next()
并访问第一个元素,因为此函数的 return 是:
A DirectoryIterator yielding tuples of (x, y) where x is a numpy array containing a batch of images with shape (batch_size, *target_size, channels) and y is a numpy array of corresponding labels.
要显示图像,您可以使用 matplotlib
中的 plt.imshow
传递批处理的第一张(也是唯一一张)图像 plt.imshow(img[0])
。
文件夹结构
├── data
│ └── smile
│ └── smile_8.jpg
# smile_8.jpg shape (256,256,3)
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
datagen = keras.preprocessing.image.ImageDataGenerator(
rescale=1./255,
rotation_range=180,
width_shift_range=0.2,
height_shift_range=0.2,
)
dir_It = datagen.flow_from_directory(
"data/",
batch_size=1,
save_to_dir="output/",
save_prefix="",
save_format='png',
)
for _ in range(5):
img, label = dir_It.next()
print(img.shape) # (1,256,256,3)
plt.imshow(img[0])
plt.show()