我设置了一个流水线,从文件名队列开始,如以下伪代码所示:
filename_queue = tf.train.string_input_producer(["file0.pd", "file1.pd"])
指向包含多个序列化的tf.train.Example
图像的TFRecords
。按照TensorFlow指南,以下是读取一个示例的函数:
def read_my_file_format(filename_queue):
reader = tf.SomeReader()
key, record_string = reader.read(filename_queue)
example, label = tf.some_decoder(record_string)
processed_example = some_processing(example)
return processed_example, label
这是用于批处理队列的:
def input_pipeline(filenames, batch_size):
filename_queue = tf.train.string_input_producer(filenames)
example, label = read_my_file_format(filename_queue)
example_batch, label_batch = tf.train.shuffle_batch(
[example, label], batch_size=batch_size, capacity=100,
min_after_dequeue=10)
return example_batch, label_batch
我正在寻找一种将数据随机分为训练集和测试集的方法。我不想将训练集和测试集保存到不同的文件中,而是希望独立于读取文件的方式将图像随机分配到训练或测试集中。理想情况下,我希望将输入流水线分成一个训练队列和一个测试队列。
以下是我通常在numpy中执行巨大数据集拆分时所做的内容。
import numpy as np
from numpy.random import choice
from numpy.random import RandomState
queue = range(10)
weights = (.8,.2) # create 2 partitions with this weights
def sampler(partition, seed=0):
rng = RandomState(seed)
return lambda x: rng.choice(np.arange(len(weights)), p=weights) == partition
def split(queue, weights):
# filter the queue for each partition
return [filter(sampler(partition), queue) for partition in range(len(weights)) ]
(train, test) = split(queue, weights)
print(list(train)) # [0, 1, 2, 3, 4, 5, 6, 9]
print(list(test)) # [7, 8]
sklearn.model_selection.train_test_split()
函数。 - igrinis