augmented images does not store in their own classes directory

Issue

I am new in DCNN. Moreover, I am working on image data augmentation and I have been writing code of augmentation. Moreover, I have 5 classes in the dataset i.e. Grass, Flower, Fruits, Dust, and Leaves Thus, the train set in is also consist of 8 classes. However, after augmentation, all of the augmented data has been store in the train folder but it does not store in their individual class. Besides that, I have been applied the directory manually for example:

directory = ('/content/dataset/train/Grass'),
save_to_dir = ('/content/dataset/train/Grass')

Unfortunately, it does not work, and augmented images, not generated.

Therefore, I want to store augmented data with their raw that is has been already stored in the train folder. In short augmented data of Grass, classes will be stored in the grass classes folder that exists in the train folder that is as similar given bellow:

directory = ('/content/dataset/train/Grass'),
    save_to_dir = ('/content/dataset/train/Grass')

Using platform: Google Colab

My code:

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

Solution

what you want is

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')  

This will create the augmented images and store them in the associated class sub directories that have the original images in them.

Answered By – Gerry P

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published