首页 > 解决方案 > 增强图像不存储在它们自己的类目录中,原始数据呈现在 train 文件夹中

问题描述

我正在为训练集数据进行图像数据增强,并且一直在编写增强代码。我在数据集中有 12 个类,即草、花、水果、灰尘和树叶,图像总数约为 5539。我将数据集拆分为 70% 的训练和 15% 的有效和测试。Train 文件夹还包含 Grass、Flower、Fruits、Dust 和 Leaves 子文件夹。但是,在增强之后,所有增强数据都已正确增强,但存储在 train 文件夹中的某个位置,而不是它们各自的类子文件夹中

简而言之,例如在 train 文件夹中,我有一个子文件夹,即 Black-grass 文件夹,其中包含 325 个图像数据。之后,我想在train文件夹中已经存在的grass文件夹中生成100个增强数据。我不想在 train 文件夹中生成一个新文件夹。我想要这样,所有增强的数据都将与原始数据一起存储在他们自己的现有文件夹中

我的代码:

from keras.preprocessing.image import ImageDataGenerator

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

i = 0

for batch in datagen.flow_from_directory(directory = ('/content/dataset/train'),
                                         batch_size = 32,
                                         target_size = (256, 256),
                                         color_mode = ('rgb'),
                                         save_to_dir = ('/content/dataset/train'),
                                         save_prefix = ('aug'),
                                         save_format = ('png')):
  i += 1
  if i > 5:
    break

使用平台:谷歌合作 目录图片

标签: pythontensorflowkerasdeep-learningdata-augmentation

解决方案


解决方案代码如下所示

import tensorflow as tf
import cv2
import os
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator
sdir= r'c:\temp\people\dtest' # set this to the directory holding the images
ext='jpg' # specify the extension foor the aufmented images
prefix='aug' #set the prefix for the augmented images
batch_size=32 # set the batch size
passes=5  # set the number of time to cycle the generator
datagen = ImageDataGenerator( rotation_range=45, width_shift_range=0.2, height_shift_range=0.2, shear_range = 0.2,
                                zoom_range = 0.2,  horizontal_flip=True, fill_mode = 'nearest')
data=datagen.flow_from_directory(directory = sdir, batch_size = batch_size,  target_size = (256, 256),
                                 color_mode = 'rgb', shuffle=True)
for i in range (passes):
    images, labels=next(data)
    class_dict=data.class_indices
    new_dict={}
    # make a new dictionary with keys and values reversed
    for key, value in class_dict.items(): # dictionary is now {numeric class label: string of class_name}
        new_dict[value]=key    
    for j in range (len(labels)):                
        class_name = new_dict[np.argmax(labels[j])]         
        dir_path=os.path.join(sdir,class_name )         
        new_file=prefix + '-' +str(i*batch_size +j) + '.'  + ext       
        img_path=os.path.join(dir_path, new_file)        
        img=cv2.cvtColor(images[j], cv2.COLOR_BGR2RGB)
        cv2.imwrite(img_path, img)
print ('*** process complete')  

这将创建增强图像并将它们存储在相关的类目录中。


推荐阅读