tf.train.slice_input_producer()和tf.train.batch()

1.tf.train.slice_input_producer函數(shù),一種模型數(shù)據(jù)的排隊(duì)輸入方法。

tf.train.slice_input_producer(
    tensor_list,
    num_epochs=None, 
    shuffle=True,
    seed=None,
    capacity=32,
    shared_name=None,
    name=None
)

其參量為:

Args:
tensor_list: A list of Tensor objects. Every Tensor in tensor_list must have the same size in the first dimension.

# 循環(huán)Queue輸入次數(shù)
num_epochs: An integer (optional). If specified, slice_input_producer produces each slice num_epochs times before generating an OutOfRange error. If not specified, slice_input_producer can cycle through the slices an unlimited number of times.

shuffle: Boolean. If true, the integers are randomly shuffled within each epoch.
seed: An integer (optional). Seed used if shuffle == True.

# Queue的容量
capacity: An integer. Sets the queue capacity.

shared_name: (optional). If set, this queue will be shared under the given name across multiple sessions.
name: A name for the operations (optional).

相關(guān)代碼實(shí)例:

    # 生成包含輸入和目標(biāo)圖片地址名的list
    input_files = [os.path.join(dirname, 'input', f) for f in flist]
    output_files = [os.path.join(dirname, 'output', f) for f in flist]

    # 內(nèi)部自動(dòng)轉(zhuǎn)換為Constant String的Tensor,并排隊(duì)進(jìn)入隊(duì)列
    input_queue, output_queue = tf.train.slice_input_producer(
        [input_files, output_files], shuffle=self.shuffle,
        seed=0123, num_epochs=self.num_epochs)

    # tf.train.slice_input_producer()每次取一對(duì)【輸入-目標(biāo)】對(duì),交給ReadFile這
    # 個(gè)Op
    input_file = tf.read_file(input_queue)
    output_file = tf.read_file(output_queue)
    
    # 生成RGB格式的圖像tensor
    im_input = tf.image.decode_jpeg(input_file, channels=3)
    im_output = tf.image.decode_jpeg(output_file, channels=3)

2.tf.train.batch()函數(shù)

tf.train.batch(
    tensors,
    batch_size,
    num_threads=1,
    capacity=32,
    enqueue_many=False,
    shapes=None,
    dynamic_pad=False,
    allow_smaller_final_batch=False,
    shared_name=None,
    name=None
)

其參量為:

Args:
tensors: The list or dictionary of tensors to enqueue.
batch_size: The new batch size pulled from the queue.
num_threads: The number of threads enqueuing tensors. The batching will be nondeterministic if num_threads > 1.
capacity: An integer. The maximum number of elements in the queue.

#進(jìn)行shuffle的輸入是否為單個(gè)tensor
enqueue_many: Whether each tensor in tensors is a single example.

shapes: (Optional) The shapes for each example. Defaults to the inferred shapes for tensors.
dynamic_pad: Boolean. Allow variable dimensions in input shapes. The given dimensions are padded upon dequeue so that tensors within a batch have the same shapes.
allow_smaller_final_batch: (Optional) Boolean. If True, allow the final batch to be smaller if there are insufficient items left in the queue.
shared_name: (Optional). If set, this queue will be shared under the given name across multiple sessions.
name: (Optional) A name for the operations.

相關(guān)代碼實(shí)例

samples = tf.train.batch(
        sample,
        batch_size=self.batch_size,
        num_threads=self.nthreads,
        capacity=self.capacity)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容