這個賞金已經結束。回答這個問題有資格獲得 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/388846.html
上一篇:使用ImageMagick在畫布上疊加多個不同大小的PNG影像
下一篇:c++模板類的使用,編譯的問題
