【发布时间】:2017-08-30 01:46:12
【问题描述】:
我从文件名队列开始设置我的管道,如下伪代码所示:
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]
【问题讨论】:
-
您是否要多次处理每个文件?如果是,将它们随机分成训练集和测试集可能是个问题。
-
是的,因为我从每张图片中随机抽取一个补丁,
-
在这种情况下,您需要以某种方式记住您指定为测试的文件部分以及指定为训练的部分。甚至在读取文件之前,从一开始就这样做可能会更容易。浏览您的文件名列表,如果您的图像大小/形状不同,则读取元数据,并随机决定要测试哪些位以及要训练哪些位。将此信息与文件名一起存储在字典或其他东西中,然后将字典提供给队列,而不仅仅是文件名
-
根据我的经验,在运行时将数据分成训练集和测试集一直被证明是一个糟糕的主意。
标签: python python-3.x tensorflow dataset tensorflow-datasets