簡體   English   中英

如何在 keras 中使用 ImageDataGenerator 和 flow_from_directory 保存增強圖像

[英]How to save augmented images using ImageDataGenerator and flow_from_directory in keras

我想使用 Keras 中的 ImageDataGenerator 增強兩個不同目錄(文件夾:良性/惡性)中的圖像。

然后我想將每個類的增強圖像保存在一個單獨的文件夾中。

我的目錄結構如下:

dataset
|   
|-- original_images
|   |                        
|   |-- benign                  
|   |    |-- benign_image1.png
|   |    |-- benign_image2.png
|   |    |-- ...
|   |
|   |-- malignant                   
|        |-- malignant_image1.png
|        |-- malignant_image2.png
|        |-- ...  
|   
|-- augmented_images
    |                        
    |-- augmented_benign                <-- Here i want to save augmented images of benign folder   
    |    |-- augmented_img1.png
    |    |-- augmented_img2.png
    |    |-- ...
    |
    |-- augmented_malignant             <-- Here i want to save augmented images of malignant folder
         |-- augmented_img1.png
         |-- augmented_img2.png
         |-- ...  

我的問題是我無法區分這兩個類的增強圖像,因為它們都將存儲在同一個文件夾中。

實際上,我只能為“save_to_dir”參數設置一個文件夾路徑,以便在那里存儲圖像。

因此,正如我提到的,所有增強圖像都將保存在一個文件夾中( augmented_images )。

你們能告訴我如何將每個類的增強圖像保存在一個單獨的文件夾中( augmented_benign _ augmented_benignaugmented_malignant _ augmented_malignant )?

我寫的代碼是這樣的:

from keras.preprocessing.image import ImageDataGenerator

img_dir_path = "D:/dataset/original_images"
save_dir_path = "D:/dataset/augmented_images"

datagen = ImageDataGenerator(rotation_range=90)

data_generator = datagen.flow_from_directory(
    img_dir_path, 
    target_size=(128, 128), 
    color_mode="rgb", 
    batch_size=20, 
    save_to_dir="save_dir_path", 
    class_mode="binary", 
    save_prefix="augmented", 
    save_format="png")

for i in range(10):
    data_generator.next()

下面的代碼應該給你你想要的。 我測試了它,它完成了工作。 我注意到的一件事是,當您設置 rotation_range=90 時,圖像似乎在 -90 度到 + 90 度之間隨機旋轉。 這有點意外。

sdir=r'c:\temp\dataset'
aug_dir=os.path.join(sdir,'augmented_images')
if os.path.isdir(aug_dir): # see if aug_dir exists if so remove it to get a clean slate
    shutil.rmtree(aug_dir)
os.mkdir(aug_dir) # make a new empty aug_dir
filepaths=[]
labels=[]
# iterate through original_images and create a dataframe of the form filepaths, labels
original_images_dir=os.path.join(sdir, 'original_images')
for klass in ['benign', 'malignant']:
    os.mkdir(os.path.join(aug_dir,klass)) # make the class subdirectories in the aug_dir
    classpath=os.path.join(original_images_dir, klass) # get the path to the classes (benign and maligant)
    flist=os.listdir(classpath)# for each class the the list of files in the class    
    for f in flist:        
        fpath=os.path.join(classpath, f) # get the path to the file
        filepaths.append(fpath)
        labels.append(klass)
    Fseries=pd.Series(filepaths, name='filepaths')
    Lseries=pd.Series(labels, name='labels')
df=pd.concat([Fseries, Lseries], axis=1) # create the dataframe
gen=ImageDataGenerator( rotation_range=90)
groups=df.groupby('labels') # group by class
for label in df['labels'].unique():  # for every class               
    group=groups.get_group(label)  # a dataframe holding only rows with the specified label 
    sample_count=len(group)   # determine how many samples there are in this class  
    aug_img_count=0
    target_dir=os.path.join(aug_dir, label)  # define where to write the images    
    aug_gen=gen.flow_from_dataframe( group,  x_col='filepaths', y_col=None, target_size=(128,128), class_mode=None,
                                        batch_size=1, shuffle=False, save_to_dir=target_dir, save_prefix='aug-',
                                        save_format='jpg')
    while aug_img_count<len(group):
        images=next(aug_gen)            
        aug_img_count += len(images) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM