12

我正在使用以下代码调整存储在文件夹(两个类)中的 RGB 图像的大小:

from keras.preprocessing.image import ImageDataGenerator
dataset=ImageDataGenerator()
dataset.flow_from_directory('/home/1',target_size=(50,50),save_to_dir='/home/resized',class_mode='binary',save_prefix='N',save_format='jpeg',batch_size=10)

我的数据树如下:

1/
 1_1/
     img1.jpg
     img2.jpg
     ........
 1_2/
     IMG1.jpg
     IMG2.jpg
     ........
resized/
        1_1/ (here i want to save resized images of 1_1)
        2_2/ (here i want to save resized images of 1_2)

运行代码后,我得到以下输出,但没有图像:

Found 271 images belonging to 2 classes.
Out[12]: <keras.preprocessing.image.DirectoryIterator at 0x7f22a3569400>

如何保存图像?

4

6 回答 6

20

这是一个非常简单的版本,可以将一张图像的增强图像保存在您想要的任何地方:

步骤 1. 初始化图像数据生成器

在这里,我们确定我们想要对原始图像进行哪些更改并生成增强图像
您可以在此处阅读有关差异效果的信息 - https://keras.io/preprocessing/image/

datagen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1, 
height_shift_range=0.1,shear_range=0.15, 
zoom_range=0.1,channel_shift_range = 10, horizontal_flip=True)

第 2 步:在这里我们选择原始图像来执行增强

在图像中读取

image_path = 'C:/Users/Darshil/gitly/Deep-Learning/My 
Projects/CNN_Keras/test_augment/caty.jpg'

image = np.expand_dims(ndimage.imread(image_path), 0)

第 3 步:选择要保存增强图像的位置

save_here = 'C:/Users/Darshil/gitly/Deep-Learning/My 
Projects/CNN_Keras/test_augment'

Step 4. 我们拟合原始图像

datagen.fit(image)

第 5 步:遍历图像并使用“save_to_dir”参数保存

for x, val in zip(datagen.flow(image,                    #image we chose
        save_to_dir=save_here,     #this is where we figure out where to save
         save_prefix='aug',        # it will save the images as 'aug_0912' some number for every new augmented image
        save_format='png'),range(10)) :     # here we define a range because we want 10 augmented images otherwise it will keep looping forever I think
pass
于 2018-11-02T19:03:50.300 回答
5

如您的输出中所述,该flow_from_directory方法为您提供了一个“迭代器”。迭代器本身并没有真正做任何事情。它正在等待迭代,然后才会读取并生成实际数据。

Keras 中用于拟合的迭代器将像这样使用:

generator = dataset.flow_from_directory('/home/1',target_size=(50,50),save_to_dir='/home/resized',class_mode='binary',save_prefix='N',save_format='jpeg',batch_size=10)

for inputs,outputs in generator:

    #do things with each batch of inputs and ouptus

通常,您只需将生成器传递给一个fit_generator方法,而不是执行上面的循环。没有真正需要做一个 for 循环:

model.fit_generator(generator, ......)

Keras 只会在通过迭代生成器加载和增强图像后保存图像。

于 2017-12-15T11:25:26.483 回答
5

它只是一个声明,您必须使用该生成器,例如,.next()

from keras.preprocessing.image import ImageDataGenerator
dataset=ImageDataGenerator()
image = dataset.flow_from_directory('/home/1',target_size=(50,50),save_to_dir='/home/resized',class_mode='binary',save_prefix='N',save_format='jpeg',batch_size=10)
image.next()

然后你会看到图像/home/resized

于 2020-05-12T12:06:31.830 回答
1

您可以尝试这个简单的代码示例并根据需要对其进行修改:

(它从您的数据生成增强图像,然后将它们保存到不同的文件夹中)

from keras.preprocessing.image import ImageDataGenerator


data_dir = 'data/train' #Due to the structure of ImageDataGenerator, you need to have another folder under train contains your data, for example: data/train/faces
save_dir = 'data/resized'


datagen = ImageDataGenerator(rescale=1./255)


resized = datagen.flow_from_directory(data_dir, target_size=(224, 224),
                                save_to_dir=save_dir,
                                color_mode="rgb", # Choose color mode
                                class_mode='categorical',
                                shuffle=True,
                                save_prefix='N',
                                save_format='jpg', # Formate
                                batch_size=1)


for in in range(len(resized)):
    resized.next()
于 2021-06-11T10:21:26.500 回答
0

如果要将图像保存在与标签同名的文件夹下,则可以遍历标签列表并在循环中调用增强代码。

from tensorflow.keras.preprocessing.image import ImageDataGenerator  

# Augmentation + save augmented images under augmented folder

IMAGE_SIZE = 224
BATCH_SIZE = 500
LABELS = ['lbl_a','lbl_b','lbl_c']

for label in LABELS:
  datagen_kwargs = dict(rescale=1./255)  
  dataflow_kwargs = dict(target_size=(IMAGE_SIZE, IMAGE_SIZE), 
                        batch_size=BATCH_SIZE, interpolation="bilinear")

  train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
    rotation_range=40,
    horizontal_flip=True,
    width_shift_range=0.1, height_shift_range=0.1,
    shear_range=0.1, zoom_range=0.1,
    **datagen_kwargs)

  train_generator = train_datagen.flow_from_directory(
      'original_images', subset="training", shuffle=True, save_to_dir='aug_images/'+label, save_prefix='aug', classes=[label], **dataflow_kwargs)
  
  # Following line triggers execution of train_generator
  batch = next(train_generator) 

那么当生成器可以直接传递给模型时,为什么要这样做呢?如果您想使用tflite-model-makerwhich 不接受生成器并接受每个标签的文件夹下的标记数据:

from tflite_model_maker import ImageClassifierDataLoader
data = ImageClassifierDataLoader.from_folder('aug_images')

结果

aug_images
| 
|__ lbl_a
|   |
|   |_____aug_img_a.png
|
|__ lbl_b
|   |
|   |_____aug_img_b.png
| 
|__ lbl_c
|   |
|   |_____aug_img_c.png

注意:您需要确保文件夹已经存在。

于 2020-12-03T00:35:49.073 回答
0
datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
                     rotation_range =15, 
                     width_shift_range = 0.2, 
                     height_shift_range = 0.2, 
                     shear_range=0.2, 
                     zoom_range=0.2, 
                     horizontal_flip = True, 
                     fill_mode = 'nearest', 
                     brightness_range=[0.5, 1.5])
DATA_DIR = 'splited/train/'
save_here = 'aug dataset/train/normal2/'

cancer = os.listdir(DATA_DIR + 'cancer/')
for i, image_name in enumerate(cancer):
    try:
        if (image_name.split('.')[1] == 'png'):
            image = np.expand_dims(cv2.imread(DATA_DIR +'classs 1/' + image_name), 0)
            for x, val in zip(datagen.flow(image, #image we chose save_to_dir=save_here,     #this is where we figure out where to save
                save_prefix='aug',        # it will save the images as 'aug_0912' some number for every new augmented image
                save_format='png'),range(10)) :     # here we define a range because we want 10 augmented images otherwise it will keep looping forever I think
                    pass
    except Exception:
        print("Could not read image {} with name {}".format(i, image_name))
于 2021-02-22T10:25:59.467 回答