如何从特定班级中抽样批次? [英] How to sample batch from a specific class?
问题描述
我想在一个 ImageNet 数据集上训练分类器(1000 个类,每个类有大约 1300 张图像).出于某种原因,我需要每个批次包含来自特定类(以 int
或占位符形式提供)的 64 张图像.如何使用最新的 TensorFlow 高效地做到这一点?
I'd like to train a classifier on one ImageNet dataset (1000 classes each with around 1300 images). For some reason, I need each batch to contain 64 images from a specific class (provided as int
or placeholder). How to do it efficiently with the latest TensorFlow?
我目前的想法是使用tf.data.Dataset.filter
:
specific_class = 2 # as an example
dataset = tf.data.TFRecordDataset(filenames)
# __parser_fun__ produces datum tuple (x, y)
dataset = dataset.map(__parser_fun__, num_parallel_calls=num_threads)
dataset = dataset.shuffle(20000)
# print(dataset) gives <ShuffleDataset shapes: ((3, 128, 128), (1,)),
# types: (tf.float32, tf.int64)>
dataset = dataset.filter(lambda x, y: tf.equal(y[0], specific_class))
dataset = dataset.batch(64)
dataset = dataset.repeat()
iterator = dataset.make_one_shot_iterator()
x_batch, y_batch = iterator.get_next()
filter
的一个小问题是,每次我想从一个新类中采样时,我都需要构造一个迭代器.
A minor problem with filter
is that I need to construct an iterator every time I want to sample from a new class.
另一个想法是使用 tf.contrib.data.rejection_resample
但它在计算上似乎令人望而却步(或者是吗?).
Another idea is to use tf.contrib.data.rejection_resample
but it seems prohibitive computationally (or is it?).
我想知道是否有其他有效的方法可以从特定类中抽样批次?
I wonder if there is other efficient way to sample batches from a particular class?
推荐答案
从概念上讲,您的数据集是由一个变量(要采样的标签)参数化的.这是完全可行的!
Conceptually your Dataset is parameterized by a variable (the label to sample). This is totally doable!
急切地执行:
import numpy as np
import tensorflow as tf
tf.enable_eager_execution()
data = dict(
x=tf.constant([1., 2., 3., 4.]),
y=tf.constant([1, 2, 1, 2])
)
requested_label = tf.Variable(1)
dataset = (
tf.data.Dataset.from_tensor_slices(data)
.repeat()
.filter(lambda d: tf.equal(d["y"], requested_label)))
it = dataset.make_one_shot_iterator()
for i, datum in enumerate(it):
assert int(datum["y"]) == 1
assert float(datum["x"]) in [1., 3.]
if i > 5:
break
requested_label.assign(2)
for i, datum in enumerate(it):
assert int(datum["y"]) == 2
assert float(datum["x"]) in [2., 4.]
if i > 5:
break
图构建:
import tensorflow as tf
graph = tf.Graph()
with graph.as_default():
data = dict(
x=tf.constant([1., 2., 3., 4.]),
y=tf.constant([1, 2, 1, 2])
)
requested_label = tf.Variable(1)
dataset = (
tf.data.Dataset.from_tensor_slices(data)
.repeat()
.filter(lambda d: tf.equal(d["y"], requested_label)))
it = dataset.make_initializable_iterator()
datum_tensors = it.get_next()
switch_label_op = requested_label.assign(2)
graph.finalize()
with tf.Session() as session:
session.run(requested_label.initializer) # label=1
session.run(it.initializer)
for _ in range(5):
datum = session.run(datum_tensors)
assert int(datum["y"]) == 1
assert float(datum["x"]) in [1., 3.]
session.run(switch_label_op) # label=2
for _ in range(5):
datum = session.run(datum_tensors)
assert int(datum["y"]) == 2
assert float(datum["x"]) in [2., 4.]
这篇关于如何从特定班级中抽样批次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!