拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 每批大类的采样和小类的增强

每批大类的采样和小类的增强

白鹭 - 2022-02-18 1947 0 0
这个赏金已经结束回答这个问题有资格获得 50声望奖励。赏金宽限期将在12 小时后结束Michael D引起更多人对这个问题的关注

假设我们有 2 个类,一个是小类,第二个是大类。

我想用于类似于ImageDataGenerator 小班级的资料增强,并从每批中抽样,以这样的方式,使每批都是平衡的(从次要类-增强为主要类-采样)。

另外,我想继续使用image_dataset_from_directory(因为资料集不适合 RAM)。

uj5u.com热心网友回复:

什么 sample_from_datasets 功能?

import tensorflow as tf
from tensorflow.python.data.experimental import sample_from_datasets

def augment(val):
    # Example of augmentation function
    return val - tf.random.uniform(shape=tf.shape(val), maxval=0.1)

big_dataset_size = 1000
small_dataset_size = 10

# Init some datasets
dataset_class_large_positive = tf.data.Dataset.from_tensor_slices(tf.range(100, 100   big_dataset_size, dtype=tf.float32))
dataset_class_small_negative = tf.data.Dataset.from_tensor_slices(-tf.range(1, 1   small_dataset_size, dtype=tf.float32))

# Upsample and augment small dataset
dataset_class_small_negative = dataset_class_small_negative
    .repeat(big_dataset_size // small_dataset_size)
    .map(augment)

dataset = sample_from_datasets(
    datasets=[dataset_class_large_positive, dataset_class_small_negative], 
    weights=[0.5, 0.5]
)

dataset = dataset.shuffle(100)
dataset = dataset.batch(6)

iterator = dataset.as_numpy_iterator()
for i in range(5):
    print(next(iterator))

# [109.        -10.044552  136.        140.         -1.0505208  -5.0829906]
# [122.        108.        141.         -4.0211563 126.        116.       ]
# [ -4.085523  111.         -7.0003924  -7.027302   -8.0362625  -4.0226436]
# [ -9.039093  118.         -1.0695585 110.        128.         -5.0553837]
# [100.        -2.004463  -9.032592  -8.041705 127.       149.      ]

在 的weights自变量中设定类之间所需的平衡sample_from_datasets

uj5u.com热心网友回复:

您可以使用 tf.data.Dataset.from_generator 来更好地控制资料生成,而无需将所有资料加载到 RAM 中。

def generator():
 i=0   
 while True :
   if i%2 == 0:
      elem = large_class_sample()
   else :
      elem =small_class_augmented()

   yield elem
   i=i 1
  

ds= tf.data.Dataset.from_generator(
         generator,
         output_signature=(
             tf.TensorSpec(shape=yourElem_shape , dtype=yourElem_ype))
    

这个生成器会改变两个类之间的样本,你可以添加更多的资料集操作(batch , shuffle ..)

uj5u.com热心网友回复:

我没有完全遵循这个问题。伪代码会起作用吗?也许有一些运营商tf.data.Dataset足以解决您的问题。

ds = image_dataset_from_directory(...)

ds1=ds.filter(lambda image, label: label == MAJORITY)
ds2=ds.filter(lambda image, label: label != MAJORITY)

ds2 = ds2.map(lambda image, label: data_augment(image), label)

ds1.batch(int(10. / MAJORITY_RATIO))
ds2.batch(int(10. / MINORITY_RATIO))

ds3 = ds1.zip(ds2)

ds3 = ds3.map(lambda left, right: tf.concat(left, right, axis=0)

uj5u.com热心网友回复:

您可以使用 tf.data.Dataset.from_tensor_slices 分别加载两个类别的影像并对少数类进行资料增强。现在您有两个资料集,将它们与 tf.data.Dataset.sample_from_datasets 结合起来。

# assume class1 is the minority class
files_class1 = glob('class1\\*.jpg')
files_class2 = glob('class2\\*.jpg')

def augment(filepath):
    class_name = tf.strings.split(filepath, os.sep)[0]
    image = tf.io.read_file(filepath)
    image = tf.expand_dims(image, 0)
    if tf.equal(class_name, 'class1'):
        # do all the data augmentation
        image_flip = tf.image.flip_left_right(image)
    return [[image, class_name],[image_flip, class_name]]

# apply data augmentation for class1
train_class1 = tf.data.Dataset.from_tensor_slices(files_class1).\
map(augment,num_parallel_calls=tf.data.AUTOTUNE)
train_class2 = tf.data.Dataset.from_tensor_slices(files_class2)

dataset = tf.python.data.experimental.sample_from_datasets(
datasets=[train_class1,train_class2], 
weights=[0.5, 0.5])

dataset = dataset.batch(BATCH_SIZE)
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *