增强图像不会将原始数据存储在其自己的class目录中,而原始数据会显示在train文件夹中 [英] augmented images does not store in their own classes directory with raw data that is presented into the train folder

查看:44
本文介绍了增强图像不会将原始数据存储在其自己的class目录中,而原始数据会显示在train文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为火车集合数据进行图像数据增强,并且我一直在编写增强代码.我在数据集中有12个类别,即草,花,水果,灰尘和树叶,图像总数约为5539.我将数据集分为有效期和测试期分别为火车的70%和15%.火车"文件夹还包含草",花",水果",灰尘"和叶子"子文件夹.但是,扩充后,所有扩充后的数据均已正确扩充,但存储在火车文件夹中的某个位置,而不是存储在其各自的类子文件夹中.

I am working on image data augmentation for the train set data and I have been writing code of augmentation. I have 12 classes in the dataset i.e. Grass, Flower, Fruits, Dust, and Leaves and the total number of images is about 5539. I have split the dataset as 70% of train and 15% for both of Valid and test respectively. Train folder also consists of Grass, Flower, Fruits, Dust, and Leaves subfolders. However, after augmentation, all of the augmented data has been correctly augmented but stored somewhere in the train folder but not in their respective class subfolder

简而言之,例如在train文件夹中,我有一个子文件夹,即Black-grass文件夹,其中包含325个图像数据.之后,我想在train文件夹中已经存在的grass文件夹中生成100个增强数据.我不想在火车文件夹中生成一个新文件夹.我希望所有扩展数据及其原始数据都存储在它们自己的现有文件夹中

In short, for example in the train folder, I have a sub-folder i.e. Black-grass folder that has 325 image data. Afterward, I want to generate 100 augmented data in the grass folder that is already been exists in the train folder. I do not want to generate a new folder into the train folder. I want that, all augmented data will be stored in their own existed folder with their raw data

我的代码:

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

使用平台:Google合作

Using Platform: Google collaboratory

推荐答案

解决方案代码如下所示

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 directories.

这篇关于增强图像不会将原始数据存储在其自己的class目录中,而原始数据会显示在train文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆