Commit 98ccd279 authored by David Peter's avatar David Peter
Browse files

Create plot_quantization_static_vs_dynamic_text_only.ipynb

For printing the results of learned and fixed bitwidth quant
parent 46c7178c
%% Cell type:code id: tags:
``` python
import os
import torch
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from matplotlib import pyplot
import json
import numpy as np
from glob import glob
from collections import ChainMap
%matplotlib inline
import yaml
import math
from pathlib import Path
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
import pytorch_lightning.utilities.argparse_utils
```
%% Cell type:code id: tags:
``` python
out_path = '/Users/david/Git/mastersthesis/results/plots/'
os.chdir('/Users/david/Git/kws-nas/models/')
os.getcwd()
```
%% Output
'/Users/david/Git/kws-nas/models'
%% Cell type:code id: tags:
``` python
# Return average of values in python list x
def avg(x):
return sum(x) / len(x)
```
%% Cell type:code id: tags:
``` python
# Load results from models
def load_model_results(path):
if not os.path.exists(path):
raise FileNotFoundError
path = os.path.join(path, '**/learned_net/')
path = os.path.normpath(path)
model_results = {}
for p in glob(path, recursive=True):
model_name = p.split(os.sep)[-2]
if model_name[-1] == '_':
continue
# Get net.config
net_config_path = os.path.join(p, 'net.config')
with open(net_config_path, 'r') as net_config:
net_config = json.load(net_config)
# Get run.config
run_config_path = os.path.join(p, 'run.config')
with open(run_config_path, 'r') as run_config:
run_config = json.load(run_config)
# Get net_info.txt
net_info_path = os.path.join(p, 'logs', 'net_info.txt')
with open(net_info_path, 'r') as net_info:
net_info = json.load(net_info)
# Get outputs of different runs
output = {}
run_output_paths = glob(os.path.join(p, 'output_run*'))
for run_output_path in run_output_paths:
with open(run_output_path, 'r') as run_output:
run_output = json.load(run_output)
if not output:
output = {k:[float(v)] for k, v in run_output.items()}
else:
for k, v in run_output.items():
output[k].append(float(v))
# Insert results into dict which is later returned
model_results[model_name] = ChainMap(run_config, net_info, net_config, output)
return model_results
```
%% Cell type:code id: tags:
``` python
# Load results from models
def load_model_results_dyn(path):
if not os.path.exists(path):
raise FileNotFoundError
path = os.path.normpath(path)
model_name = path.split(os.sep)[-1]
# Get act_summary
act_summary_path = os.path.join(path, 'act_summary.json')
with open(act_summary_path, 'r') as f:
act_summary = json.load(f)
# Get weights_summary
weights_summary_path = os.path.join(path, 'weights_summary.json')
with open(weights_summary_path, 'r') as f:
weights_summary = json.load(f)
# Get hparams
hparams_path = os.path.join(path, 'hparams.yaml')
with open(hparams_path, 'r') as f:
hparams = yaml.load(f, Loader=yaml.BaseLoader)
# Insert results into dict which is later returned
model_results = {'act_summary': act_summary, 'weights_summary': weights_summary}
return model_results
```
%% Cell type:code id: tags:
``` python
def load_tf_results(path):
path = Path(path)
exp_data = []
exp_hparams = []
exp_results = []
exp_folder_list = [f for f in path.glob(path.parts[-1] + '*')]