增强图像不会将原始数据存储在自己的 类 目录中,这些原始数据会显示在训练文件夹中
augmented images does not store in their own classes directory with raw data that is presented into the train folder
我正在为火车集数据做图像数据扩充,我一直在写扩充代码。我在数据集中有 12 classes,即草、花、水果、灰尘和树叶,图像总数约为 5539。我将数据集拆分为 70% 的火车和 15% 的两个有效并分别进行测试。 Train 文件夹还包含 Grass、Flower、Fruits、Dust 和 Leaves 子文件夹。但是,在扩充之后,所有扩充数据都已正确扩充,但存储在 train 文件夹中的某个位置,而不是它们各自的 class 子文件夹
简而言之,例如在train文件夹中,我有一个子文件夹,即具有325个图像数据的Black-grass文件夹。之后,我想在 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
使用平台:Google 协作
解决代码如下
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')
这将创建增强图像并将它们存储在关联的 class 目录中。
我正在为火车集数据做图像数据扩充,我一直在写扩充代码。我在数据集中有 12 classes,即草、花、水果、灰尘和树叶,图像总数约为 5539。我将数据集拆分为 70% 的火车和 15% 的两个有效并分别进行测试。 Train 文件夹还包含 Grass、Flower、Fruits、Dust 和 Leaves 子文件夹。但是,在扩充之后,所有扩充数据都已正确扩充,但存储在 train 文件夹中的某个位置,而不是它们各自的 class 子文件夹
简而言之,例如在train文件夹中,我有一个子文件夹,即具有325个图像数据的Black-grass文件夹。之后,我想在 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
使用平台:Google 协作
解决代码如下
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')
这将创建增强图像并将它们存储在关联的 class 目录中。