def load_model_from_config(pretrained_root): model_path = os.path.join(pretrained_root, 'checkpoints', 'best', 'best_model.pt') json_path = os.path.join(pretrained_root, 'logs', 'config.json') with open(json_path, 'r') as f: model_config = Munch.fromDict(json.load(f)) # Weird required hack to fix groupings (None is added to start during model training). if 'groupings' in model_config.model and model_config.model.groupings[0] is -1: model_config.model.groupings = model_config.model.groupings[1:] model = get_model(model_config) model.load_state_dict(torch.load(model_path)) return model, model_config
def main(opt): exp_dir = opt['model']['exp_path'] model_path = os.path.join(exp_dir, 'checkpoints', 'best', 'best_model.pt') with open(os.path.join(exp_dir, 'logs', 'config.json'), 'r') as f: model_config = Munch.fromDict(json.load(f)) # Weird required hack to fix groupings (None is added to start during model training) if 'groupings' in model_config.model and model_config.model.groupings[0] is -1: model_config.model.groupings = model_config.model.groupings[1:] model_config.cuda = opt['cuda'] model_config.data.cuda = opt['cuda'] model = get_model(model_config) model.load_state_dict(torch.load(model_path)) if opt['cuda']: print('Using CUDA') model.cuda() def on_sample(state): if opt['cuda']: state['sample'] = [x.cuda() for x in state['sample']] def on_forward(state): state['model'].add_to_meters(state) data = load_data(model_config) # Change the model to use ortho layers by copying the base weights model = convert_model_from_bjorck(model, model_config) # model.model.project_network_weights(Munch.fromDict({'type': 'l_inf_projected'})) # Instantiate the trainer. trainer = Trainer() trainer.hooks['on_sample'] = on_sample trainer.hooks['on_forward'] = on_forward print('TESTING') state = trainer.test(model, data['test']) for tag, meter in state['model'].meters.items(): print(tag, meter.value()) logit_margins = check_logit_margins(model, data['test']) print(logit_margins.min().item(), logit_margins.max().item(), logit_margins.mean().item()) plt.hist(logit_margins.detach().cpu().numpy()) plt.show()
def load_model_hack(pretrained_root): model_path = os.path.join(pretrained_root, 'checkpoints', 'best', 'best_model.pt') json_path = os.path.join(pretrained_root, 'logs', 'config.json') with open(json_path, 'r') as f: model_config = Munch.fromDict(json.load(f)) if 'groupings' in model_config.model and model_config.model.groupings[ 0] is -1: model_config.model.groupings = model_config.model.groupings[1:] from lnets.models import get_model model = get_model(model_config) state_dict = torch.load(model_path)['state_dict'][0] for (a, b) in zip(model.model.model.state_dict(), state_dict): model.model.model.state_dict()[a].copy_(state_dict[b]) return model, model_config
def main(opt): # if not os.path.isdir(opt['output_root']): # os.makedirs(opt['output_root']) exp_dir = opt['model']['exp_path'] model_path = os.path.join(exp_dir, 'checkpoints', 'best', 'best_model.pt') with open(os.path.join(exp_dir, 'logs', 'config.json'), 'r') as f: model_config = Munch.fromDict(json.load(f)) # Weird required hack to fix groupings (None is added to start during model training) if 'groupings' in model_config.model and model_config.model.groupings[ 0] is -1: model_config.model.groupings = model_config.model.groupings[1:] model_config.model.linear.bjorck_iters = 20 model_config.cuda = opt['cuda'] model = get_model(model_config) model.load_state_dict(torch.load(model_path)) if opt['cuda']: print('Using CUDA') model.cuda() data = load_data(model_config) # Change the model to use ortho layers by copying the base weights model = convert_model_from_bjorck(model, model_config) model.eval() rates = [] thresholds = np.linspace(0.0, 1.0, 50, endpoint=True) for t in thresholds: undead_rate = get_undead_rate(model, data['test'], threshold=t, cuda=opt['cuda']) rates.append(undead_rate) plt.plot(thresholds, np.array(rates)) plt.show() np.save('undead_rates', rates) print(undead_rate)
def main(opt): if not os.path.isdir(opt['output_root']): os.makedirs(opt['output_root']) exp_dir = opt['model']['exp_path'] model_path = os.path.join(exp_dir, 'checkpoints', 'best', 'best_model.pt') with open(os.path.join(exp_dir, 'logs', 'config.json'), 'r') as f: model_config = Munch.fromDict(json.load(f)) # Weird required hack to fix groupings (None is added to start during model training) if 'groupings' in model_config.model and model_config.model.groupings[ 0] is -1: model_config.model.groupings = model_config.model.groupings[1:] model = get_model(model_config) model.load_state_dict(torch.load(model_path)) if opt['data']['cuda']: print('Using CUDA') model.cuda() model_config.data.cuda = opt['data']['cuda'] data = load_data(model_config) # Change the model to use ortho layers by copying the base weights bjorck_iters = 50 model = convert_model_to_bjorck(model, model_config) for m in model.modules(): if isinstance(m, BjorckLinear): m.config.model.linear.bjorck_iter = bjorck_iters model_config.output_root = opt['output_root'] model_config.optim.lr_schedule.lr_init = 1e-5 model_config.optim.epochs = 5 model = train(model, data, model_config)