tf.train.Coordinator

轉自:https://blog.csdn.net/weixin_42052460/article/details/80714539

tensorflow中協調器 tf.train.Coordinator 和入隊線程啓動器 tf.train.start_queue_runners

TensorFlow的Session對象是支持多線程的,可以在同一個會話(Session)中創建多個線程,並行執行。在Session中的所有線程都必須能被同步終止,異常必須能被正確捕獲並報告,會話終止的時候, 隊列必須能被正確地關閉。

TensorFlow提供了兩個類來實現對Session中多線程的管理:tf.Coordinator和 tf.QueueRunner,這兩個類往往一起使用。
 

Coordinator類用來管理在Session中的多個線程,可以用來同時停止多個工作線程並且向那個在等待所有工作線程終止的程序報告異常,該線程捕獲到這個異常之後就會終止所有線程。使用 tf.train.Coordinator()來創建一個線程管理器(協調器)對象。

QueueRunner類用來啓動tensor的入隊線程,可以用來啓動多個工作線程同時將多個tensor(訓練數據)推送入文件名稱隊列中,具體執行函數是 tf.train.start_queue_runners , 只有調用 tf.train.start_queue_runners 之後,纔會真正把tensor推入內存序列中,供計算單元調用,否則會由於內存序列爲空,數據流圖會處於一直等待狀態。
 

tf中的數據讀取機制如下圖:


 

  1. 調用 tf.train.slice_input_producer,從 本地文件裏抽取tensor,準備放入Filename Queue(文件名隊列)中;
  2. 調用 tf.train.batch,從文件名隊列中提取tensor,使用單個或多個線程,準備放入文件隊列;
  3. 調用 tf.train.Coordinator() 來創建一個線程協調器,用來管理之後在Session中啓動的所有線程;
  4. 調用tf.train.start_queue_runners, 啓動入隊線程,由多個或單個線程,按照設定規則,把文件讀入Filename Queue中。函數返回線程ID的列表,一般情況下,系統有多少個核,就會啓動多少個入隊線程(入隊具體使用多少個線程在tf.train.batch中定義);
  5. 文件從 Filename Queue中讀入內存隊列的操作不用手動執行,由tf自動完成;
  6. 調用sess.run 來啓動數據出列和執行計算;
  7. 使用 coord.should_stop()來查詢是否應該終止所有線程,當文件隊列(queue)中的所有文件都已經讀取出列的時候,會拋出一個 OutofRangeError 的異常,這時候就應該停止Sesson中的所有線程了;
  8. 使用coord.request_stop()來發出終止所有線程的命令,使用coord.join(threads)把線程加入主線程,等待threads結束。

 

以上對列(Queue)和 協調器(Coordinator)操作示例:

[python] view plain copy

 

  1. # -*- coding:utf-8 -*-  
  2. import tensorflow as tf  
  3. import numpy as np  
  4.   
  5. # 樣本個數  
  6. sample_num=5  
  7. # 設置迭代次數  
  8. epoch_num = 2  
  9. # 設置一個批次中包含樣本個數  
  10. batch_size = 3  
  11. # 計算每一輪epoch中含有的batch個數  
  12. batch_total = int(sample_num/batch_size)+1  
  13.   
  14. # 生成4個數據和標籤  
  15. def generate_data(sample_num=sample_num):  
  16.     labels = np.asarray(range(0, sample_num))  
  17.     images = np.random.random([sample_num, 224, 224, 3])  
  18.     print('image size {},label size :{}'.format(images.shape, labels.shape))  
  19.     return images,labels  
  20.   
  21. def get_batch_data(batch_size=batch_size):  
  22.     images, label = generate_data()  
  23.     # 數據類型轉換爲tf.float32  
  24.     images = tf.cast(images, tf.float32)  
  25.     label = tf.cast(label, tf.int32)  
  26.   
  27.     #從tensor列表中按順序或隨機抽取一個tensor準備放入文件名稱隊列  
  28.     input_queue = tf.train.slice_input_producer([images, label], num_epochs=epoch_num, shuffle=False)  
  29.   
  30.     #從文件名稱隊列中讀取文件準備放入文件隊列  
  31.     image_batch, label_batch = tf.train.batch(input_queue, batch_size=batch_size, num_threads=2, capacity=64, allow_smaller_final_batch=False)  
  32.     return image_batch, label_batch  
  33.   
  34. image_batch, label_batch = get_batch_data(batch_size=batch_size)  
  35.   
  36.   
  37. with tf.Session() as sess:  
  38.   
  39.     # 先執行初始化工作  
  40.     sess.run(tf.global_variables_initializer())  
  41.     sess.run(tf.local_variables_initializer())  
  42.   
  43.     # 開啓一個協調器  
  44.     coord = tf.train.Coordinator()  
  45.     # 使用start_queue_runners 啓動隊列填充  
  46.     threads = tf.train.start_queue_runners(sess, coord)  
  47.   
  48.     try:  
  49.         while not coord.should_stop():  
  50.             print '************'  
  51.             # 獲取每一個batch中batch_size個樣本和標籤  
  52.             image_batch_v, label_batch_v = sess.run([image_batch, label_batch])  
  53.             print(image_batch_v.shape, label_batch_v)  
  54.     except tf.errors.OutOfRangeError:  #如果讀取到文件隊列末尾會拋出此異常  
  55.         print("done! now lets kill all the threads……")  
  56.     finally:  
  57.         # 協調器coord發出所有線程終止信號  
  58.         coord.request_stop()  
  59.         print('all threads are asked to stop!')  
  60.     coord.join(threads) #把開啓的線程加入主線程,等待threads結束  
  61.     print('all threads are stopped!')  

 
  1. # -*- coding:utf-8 -*-

  2. import tensorflow as tf

  3. import numpy as np

  4.  
  5. # 樣本個數

  6. sample_num=5

  7. # 設置迭代次數

  8. epoch_num = 2

  9. # 設置一個批次中包含樣本個數

  10. batch_size = 3

  11. # 計算每一輪epoch中含有的batch個數

  12. batch_total = int(sample_num/batch_size)+1

  13.  
  14. # 生成4個數據和標籤

  15. def generate_data(sample_num=sample_num):

  16. labels = np.asarray(range(0, sample_num))

  17. images = np.random.random([sample_num, 224, 224, 3])

  18. print('image size {},label size :{}'.format(images.shape, labels.shape))

  19. return images,labels

  20.  
  21. def get_batch_data(batch_size=batch_size):

  22. images, label = generate_data()

  23. # 數據類型轉換爲tf.float32

  24. images = tf.cast(images, tf.float32)

  25. label = tf.cast(label, tf.int32)

  26.  
  27. #從tensor列表中按順序或隨機抽取一個tensor準備放入文件名稱隊列

  28. input_queue = tf.train.slice_input_producer([images, label], num_epochs=epoch_num, shuffle=False)

  29.  
  30. #從文件名稱隊列中讀取文件準備放入文件隊列

  31. image_batch, label_batch = tf.train.batch(input_queue, batch_size=batch_size, num_threads=2, capacity=64, allow_smaller_final_batch=False)

  32. return image_batch, label_batch

  33.  
  34. image_batch, label_batch = get_batch_data(batch_size=batch_size)

  35.  
  36.  
  37. with tf.Session() as sess:

  38.  
  39. # 先執行初始化工作

  40. sess.run(tf.global_variables_initializer())

  41. sess.run(tf.local_variables_initializer())

  42.  
  43. # 開啓一個協調器

  44. coord = tf.train.Coordinator()

  45. # 使用start_queue_runners 啓動隊列填充

  46. threads = tf.train.start_queue_runners(sess, coord)

  47.  
  48. try:

  49. while not coord.should_stop():

  50. print '************'

  51. # 獲取每一個batch中batch_size個樣本和標籤

  52. image_batch_v, label_batch_v = sess.run([image_batch, label_batch])

  53. print(image_batch_v.shape, label_batch_v)

  54. except tf.errors.OutOfRangeError: #如果讀取到文件隊列末尾會拋出此異常

  55. print("done! now lets kill all the threads……")

  56. finally:

  57. # 協調器coord發出所有線程終止信號

  58. coord.request_stop()

  59. print('all threads are asked to stop!')

  60. coord.join(threads) #把開啓的線程加入主線程,等待threads結束

  61. print('all threads are stopped!')

 

輸出:

[python] view plain copy

 

  1. ************  
  2. ((3, 224, 224, 3), array([0, 1, 2], dtype=int32))  
  3. ************  
  4. ((3, 224, 224, 3), array([3, 4, 0], dtype=int32))  
  5. ************  
  6. ((3, 224, 224, 3), array([1, 2, 3], dtype=int32))  
  7. ************  
  8. done! now lets kill all the threads……  
  9. all threads are asked to stop!  
  10. all threads are stopped!  

 
  1. ************

  2. ((3, 224, 224, 3), array([0, 1, 2], dtype=int32))

  3. ************

  4. ((3, 224, 224, 3), array([3, 4, 0], dtype=int32))

  5. ************

  6. ((3, 224, 224, 3), array([1, 2, 3], dtype=int32))

  7. ************

  8. done! now lets kill all the threads……

  9. all threads are asked to stop!

  10. all threads are stopped!

 

以上程序在 tf.train.slice_input_producer 函數中設置了 num_epochs 的數量, 所以在文件隊列末尾有結束標誌,讀到這個結束標誌的時候拋出 OutofRangeError 異常,就可以結束各個線程了。

如果不設置 num_epochs 的數量,則文件隊列是無限循環的,沒有結束標誌,程序會一直執行下去。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章