def get_stats(network_name): # get weights netparams = load.get_netparams( './nn_quant_and_run_code_train/rlbitwidth.tfmodels/caffe2tf/tfmodels/' + network_name + '/' + network_name + '.ckpt') weights = netparams['weights'] # get layers layers_sorted = load.get_layers( './nn_quant_and_run_code_train/rlbitwidth.tfmodels/caffe2tf/tfmodels/' + network_name + '/' + network_name + '.py') tot_num_layers = len(layers_sorted) cols = ['layer_idx_norm', 'n', 'c', 'k', 'std'] tmp_lst = [] std_lst = [] for i, layer in enumerate(layers_sorted, start=1): layer_shape = weights[layer].shape if len(layer_shape) == 2: k = 0 n, c = layer_shape else: k, _, n, c = layer_shape weights_layer = weights[layer].ravel() idx_norm = i / tot_num_layers std = np.var(weights_layer) std_lst.append(std) tmp_lst.append([idx_norm, n, c, k, std]) df = pd.DataFrame(tmp_lst, columns=cols) return df, std_lst # to access --> df.loc[i, 'std']
def plot_dist(path_params, path_errors, save_name): #path_params = '/home/behnam/rlbitwidth.tfmodels/caffe2tf/tfmodels/lenet/lenet.ckpt' #path_errors = '/home/behnam/results/lenet/' weights = load.get_netparams(path_params) errors = load.get_error(path_errors) n_row = len(weights[0]) n_col = 3 count = 1 n_bins = 200 f = plt.figure(figsize=(n_row*3, n_row*3)) for layer in weights[0]: print(layer) sub_weights = load.get_subparams(weights, [layer]) sub_errors = load.get_suberror(errors, 'w', [layer]) y, x = np.histogram(sub_weights, bins=n_bins, normed=True) ax = f.add_subplot(n_row, n_col, count) ax.set_xlim([-0.5, 0.5]) x = x[:-1] scale = 1 / max(y) ax.hist(x, len(x), weights=scale*y, color='skyblue') best_dist, best_params, best_name = get_best_fit(sub_weights, x, y) label = layer + '[' for n in best_params: label = label + '%.3f ' % n label = label + ']' ax.plot(x, scale * best_dist.pdf(x, *best_params), color='black', label=label) plt.legend(loc='upper left') y, x = np.histogram(sub_errors, bins=n_bins, normed=True) ax = f.add_subplot(n_row, n_col, count+1) ax.set_xlim([-0.5, 0.5]) x = x[:-1] scale = 1 / max(y) ax.hist(x, len(x), weights=scale*y, color='salmon') best_dist, best_params, best_name = get_best_fit(sub_errors, x, y) label = '[' for n in best_params: label = label + '%.3f ' % n label = label + ']' ax.plot(x, scale * best_dist.pdf(x, *best_params), color='black', label=label) plt.legend(loc='upper left') ax = f.add_subplot(n_row, n_col, count+2) ax.set_xlim([-0.1, 0.1]) ax.hist(x, len(x), weights=scale*y, color='salmon') ax.plot(x, scale * best_dist.pdf(x, *best_params), color='black') count = count + 3 for name in save_name: plt.savefig(save_name)
import os import pickle import math from math import exp, expm1 from mu_law_quantize import mu_law_quantize from mu_law_quantize import snr import load path_net = '/home/behnam/rlbitwidth.tfmodels/caffe2tf/tfmodels/alexnet/alexnet.py' path_params = '/home/behnam/rlbitwidth.tfmodels/caffe2tf/tfmodels/alexnet/alexnet.ckpt' path_save = '/home/behnam/results/weights_retrained/alexnet_W_muLAW_uniform.pickle' #path_save = '/home/behnam/results/weights_retrained/alexnet_W_muLAW_norm.pickle' # reading weights from files if '.ckpt' in path_params: params = load.get_netparams(path_params) else: with open(path_params, 'r') as f: params = pickle.load(f) # loading the weights into variables if type(params) is dict: weights_, biases_ = params['weights'], params['biases'] if len(params) > 2: mean_, variance_, scale_, offset_ = params['mean'], params[ 'variance'], params['scale'], params['offset'] else: mean_, variance_, scale_, offset_ = {}, {}, {}, {} else: weights_, biases_ = params[0:2] if len(params) > 2: