Tensorflow 队列和多线程:如何使用数据 API

Tbe*_*tin 5 queue parallel-processing multithreading timeline tensorflow

我目前正在尝试实施Tensorflow 管道。事实上,我想用我的CPU加载数据,并使用我的GPU来运行图在同一时间。为了更好地理解正在发生的事情,我创建了一个非常简单的卷积网络:

import os
import h5py
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
sess= tf.InteractiveSession()
from tensorflow.python.client import timeline
import time



t1 = time.time()

class generator:
    def __init__(self, file):
        self.file = file

    def __call__(self):
        with h5py.File(self.file, 'r') as hf:
            for im in hf["data"]:
                yield tuple(im)


dataset = tf.data.Dataset().from_generator(generator('file.h5'),
                                           output_types= tf.float32,
                                           output_shapes=(tf.TensorShape([None,4,128,128,3])))



dataset = dataset.batch(batch_size=1000)
dataset = dataset.prefetch(10)


iter = dataset.make_initializable_iterator()
e1 = iter.get_next()
e1 = tf.reshape(e1, (-1, 128, 128, 3))

with tf.device('gpu'):
    output = tf.layers.conv2d(e1[:150],200,(5,5))
    output = tf.layers.conv2d(output,50,(5,5))
    output = tf.layers.conv2d(output, 50, (5, 5))
    output = tf.layers.conv2d(output, 25, (5, 5))



with tf.Session() as sess:
    config = tf.ConfigProto()
    config.intra_op_parallelism_threads = 2
    tf.Session(config=config)
    options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
    run_metadata = tf.RunMetadata()
    sess.run(tf.global_variables_initializer())
    sess.run(iter.initializer)
    for i in range(10):
        a = sess.run(output, options=options, run_metadata=run_metadata)
        print('done')
        fetched_timeline = timeline.Timeline(run_metadata.step_stats)
        chrome_trace = fetched_timeline.generate_chrome_trace_format()

    with open('timeline_01.json', 'w') as f:
        f.write(chrome_trace)

t2= time.time()
print('TIME', t2-t1)
Run Code Online (Sandbox Code Playgroud)

我不明白结果:

时间线

  • 首先,线程数似乎与我花在运行整个代码上的时间无关。(68 秒)确实当我评论以下几行时:

    config = tf.ConfigProto()
    config.intra_op_parallelism_threads = 2
    tf.Session(config=config)
    
    Run Code Online (Sandbox Code Playgroud)

还是一样...

  • 第二,为什么GPU和CPU不能同时使用?难道我做错了什么 ?

如果有人可以帮助我,那对他来说会非常好,因为我已经在这个问题上花了两天时间。

非常感谢你的帮助