TensorFlow 筆記03-TensoeFlow 和 TensorRT 調試的一些方法


▶ TensoeFlow 和 TensorRT 調試的一些方法,用於輸出中間層的情況方便觀察

● Tensorflow 中的方法

1 sess = tf.Session()                             # 新開會話用於調試
2 sess.run(tf.global_variables_initializer())
3 temp = sess.run(h1,feed_dict = {'input:0':X})   # 獲取指定節點,並給輸入節點喂進數據
4 16 print(i, "-shape: ", np.shape(temp))
5 17 print(temp)
6 18 sess.close()

● Keras 中的兩種方法

 1 from keras.models import Model
 2 from keras import backend as K
 3 
 4 ...                                     # 建圖
 5 model = Model(inputs=x, outputs=y)
 6 
 7 i = 1                                   # 指定需要輸出的中間層序號(0 為 輸入層)
 8 kFun = K.function([model.layers[0].input],[model.layers[i].output]) # 建立 K.function 用來提取中間層輸出,后面喂上輸入數據
 9 temp = kFun(X)                          # 喂進指定層輸入數據,獲取指定層輸出
10 print(i, "-shape: ", np.shape(temp))    # NHWC 格式
11 print(temp)
12 
13 sess = tf.Session()                                                 # 另方法,用 tensorflow 的接口
14 sess.run(tf.global_variables_initializer())
15 temp = sess.run(model.layers[i].output,feed_dict = {'input:0':X})   # 獲取指定層輸出
16 print(i, "-shape: ", np.shape(temp))
17 print(temp)
18 sess.close()

● TensorRT 中的調試方法

 1 h1 = network.add_ ...
 2 print(h1.get_output(0).shape)           # 查看該節點的一些屬性
 3 
 4 # 舉栗,tensorrt.tensorrt.IConvolutionLayer 對象(add_convolution 層的返回值)的屬性:
 5 bias                # numpy.ndarray,偏置值(尺寸等於輸出特征數)
 6 dilation            # numpy.ndarray,擴張量?
 7 get_input 
 8 get_output 
 9 get_output_type 
10 kernel              # numpy.ndarray,卷積窗口權重
11 kernel_size         # tensorrt.tensorrt.DimsHW,卷積窗口尺寸
12 name                # str,節點名
13 num_groups 
14 num_inputs 
15 num_output_maps 
16 num_outputs 
17 output_type_is_set
18 padding             # numpy.ndarray,被卷積對象光環厚度,左右統一設置,上下統一設置
19 padding_mode        # tensorrt.tensorrt.PaddingMode,
20 post_padding        # numpy.ndarray,被卷積對象右下角光環厚度
21 pre_padding         # numpy.ndarray,被卷積對象左上角光環厚度
22 precision           # tensorrt.tensorrt.DataType,數據類型  
23 precision_is_set    # bool,是否改變了默認數據類型?
24 reset_output_type   # bound method PyCapsule.reset_output_type of <tensorrt.tensorrt.IConvolutionLayer>
25 reset_precision         
26 set_output_type
27 stride              # tensorrt.tensorrt.DimsHW,卷積跨步
28 type                # tensorrt.tensorrt.LayerType,節點類型
29 
30 
31 network.mark_output(h1.get_output(0))    # 另方法,調整模型的輸出節點,使得模型輸出就是想要調試的節點

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM