]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - jacinto-ai/pytorch-jacinto-ai-devkit.git/blob - modules/pytorch_jacinto_ai/xnn/utils/utils_hist.py
release commit
[jacinto-ai/pytorch-jacinto-ai-devkit.git] / modules / pytorch_jacinto_ai / xnn / utils / utils_hist.py
1 import sys
2 import numpy as np
3 import cv2
4 import torch
5 import os
6 import matplotlib.pyplot as plt
8 #from .. import layers as xtensor_layers
10 #study histogram for 3D tensor. The 1st dim belongs to ch
11 #study 2D histogram of each channel
12 def comp_hist_tensor3d(x=[], name='tensor', en=True, dir = 'dir_name', log = False, ch_dim=2):
13     if en == False:
14         return
15     root = os.getcwd()
16     path = root + '/checkpoints/debug/'+ dir
17     if not os.path.exists(path):
18         os.makedirs(path)
20     if ch_dim == 2:
21         for ch in range(x.shape[ch_dim]):
22             x_ch = x[:,:,ch]
23             print('min={}, max={}, std={}, mean={}'.format(x_ch.min(), x_ch.max(), x_ch.std(), x_ch.mean()))
24             plt.hist(x_ch.flatten(), bins=256, log=log)  # arguments are passed to np.histogram
25             #plt.title("Histogram with 'auto' bins")
26             #plt.show()
27             plt.savefig('{}/{}_{:03d}.jpg'.format(path, name, ch))
28             plt.close()
29     elif ch_dim == 0:
30         for ch in range(x.shape[ch_dim]):
31             x_ch = x[ch,:,:]
32             print('min={}, max={}, std={}, mean={}'.format(x_ch.min(), x_ch.max(), x_ch.std(), x_ch.mean()))
33             plt.hist(x_ch.flatten(), bins=256, log=log)  # arguments are passed to np.histogram
34             #plt.title("Histogram with 'auto' bins")
35             #plt.show()
36             plt.savefig('{}/{}_{:03d}.jpg'.format(path, name, ch))
37             plt.close()
39 def hist_tensor2D(x_ch=[], dir = 'tensor_dir', name='tensor', en=True, log=False, ch=0):
40     if en == False:
41         return
42     root = os.getcwd()
43     path = root + '/checkpoints/debug/'+ dir
44     if not os.path.exists(path):
45         os.makedirs(path)
46     
47     print('min={:.3f}, max={:.3f}, std={:.3f}, mean={:.3f}'.format(x_ch.min(), x_ch.max(), x_ch.std(), x_ch.mean()))
48     hist_ary = plt.hist(x_ch.flatten(), bins=256, log=log)  # arguments are passed to np.histogram
49     
51     #plt.title("Histogram with 'auto' bins")
52     #plt.show()
53     plt.savefig('{}/{}_{:03d}.jpg'.format(path, name,ch))
54     plt.close()
55     return hist_ary
57 def analyze_model(model):
58     num_dead_ch = 0
59     for n, m in model.named_modules():
60         if isinstance(m, torch.nn.Conv2d):
61             if m.weight.shape[1] == 1:
62                 for ch in range(m.weight.shape[0]):
63                     cur_ch_wt = m.weight[ch][0][...]
64                     mn = cur_ch_wt.min()
65                     mx = cur_ch_wt.max()
66                     mn = mn.cpu().detach().numpy()
67                     mx = mx.cpu().detach().numpy()
68                     print(n, 'dws weight ch mn mx', ch, mn, mx)
69                     #print(n, 'dws weight ch', ch, cur_ch_wt)
70                     if max(abs(mn), abs(mx)) <= 1E-40:
71                         num_dead_ch += 1
72             else:
73                 print(n, 'weight', 'shape', m.weight.shape, m.weight.min(), m.weight.max())
74                 if m.bias is not None:
75                     print(n, 'bias', m.bias.min(), m.bias.max())        
77     print("num_dead_ch: ", num_dead_ch)                
79 def study_wts(self, modules):
80     for key, value in modules.items():
81         print(key, value)
82         for key2, value2 in value._modules.items():
83             print(key2, value2)
84             print(value2.weight.shape)
87 def comp_hist(self, x=[], ch_idx=0, name='tensor'):
88     #hist_pred = torch.histc(x.cpu(), bins=256)
89     root = os.getcwd()
90     path = root + '/checkpoints/debug/'+ name
91     if not os.path.exists(path):
92         os.makedirs(path)
94     #print(hist_pred)
95     for ch in range(x.shape[1]):
96         x_ch = x[0][ch]
97         plt.hist(x_ch.view(-1).cpu().numpy(), bins=256)  # arguments are passed to np.histogram
98         print('min={}, max={}, std={}, mean={}'.format(x_ch.min(), x_ch.max(), x_ch.std(), x_ch.mean()))
99         #plt.title("Histogram with 'auto' bins")
100         #plt.show()
101         plt.savefig('{}/{}_{:03d}.jpg'.format(path, name, ch))
102         plt.close()
105 def store_layer_op(en=False, tensor= [], name='tensor_name'):
106     if en == False:
107         return
109     # write tensor
110     tensor = tensor.astype(np.int16)
111     print("writing tensor {} : {} : {} : {} : {}".format(name, tensor.shape, tensor.dtype, tensor.min(), tensor.max()))
113     root = os.getcwd()
114     tensor_dir = root + '/checkpoints/debug/' + name
116     if not os.path.exists(tensor_dir):
117         os.makedirs(tensor_dir)
119     tensor_name = tensor_dir + "{}.npy".format(name)
120     np.save(tensor_name, tensor)
121     comp_hist_tensor3d(x=tensor, name=name, en=True, dir=name, log=True, ch_dim=0)