def main(exp_const, data_const, model_const): io.mkdir_if_not_exists(exp_const.vis_dir) print('Creating network ...') model = Model() model.const = model_const model.net = ResnetModel(model.const.net) if model.const.model_num is not None: model.net.load_state_dict(torch.load(model.const.net_path)) model.net.cuda() if exp_const.feedforward == False: model.embed2class = Embed2Class(model.const.embed2class) if model.const.model_num is not None: model.embed2class.load_state_dict( torch.load(model.const.embed2class_path)) model.embed2class.cuda() model.img_mean = np.array([0.485, 0.456, 0.406]) model.img_std = np.array([0.229, 0.224, 0.225]) print('Creating dataloader ...') dataset = Cifar100Dataset(data_const) dataloader = DataLoader(dataset, batch_size=exp_const.batch_size, shuffle=True, num_workers=exp_const.num_workers) eval_results = eval_model(model, dataloader, exp_const) confmat_npy = os.path.join(exp_const.exp_dir, 'confmat.npy') np.save(confmat_npy, eval_results['Conf Mat']) results = { 'Avg Loss': eval_results['Avg Loss'], 'Acc': eval_results['Acc'] } print(results) results_json = os.path.join(exp_const.exp_dir, 'results.json') io.dump_json_object(results, results_json) embeddings_npy = os.path.join(exp_const.exp_dir, 'embeddings.npy') if exp_const.feedforward == True: np.save(embeddings_npy, model.net.resnet_layers.fc.weight.data.cpu().numpy()) else: np.save(embeddings_npy, model.embed2class.embed.weight.data.cpu().numpy()) labels_npy = os.path.join(exp_const.exp_dir, 'labels.npy') np.save(labels_npy, dataset.labels)
def main(exp_const, data_const, model_const): io.mkdir_if_not_exists(exp_const.exp_dir, recursive=True) io.mkdir_if_not_exists(exp_const.log_dir) io.mkdir_if_not_exists(exp_const.model_dir) io.mkdir_if_not_exists(exp_const.vis_dir) configure(exp_const.log_dir) save_constants({ 'exp': exp_const, 'data': data_const, 'model': model_const }, exp_const.exp_dir) print('Creating network ...') model = Model() model.const = model_const model.net = ResnetModel(model.const.net) model.embed2class = Embed2Class(model.const.embed2class) if model.const.model_num is not None: model.net.load_state_dict(torch.load(model.const.net_path)) model.embed2class.load_state_dict( torch.load(model.const.embed2class_path)) model.net.cuda() model.embed2class.cuda() model.img_mean = np.array([0.485, 0.456, 0.406]) model.img_std = np.array([0.229, 0.224, 0.225]) model.to_file(os.path.join(exp_const.exp_dir, 'model.txt')) print('Creating dataloader ...') dataloaders = {} for mode, subset in exp_const.subset.items(): data_const = copy.deepcopy(data_const) if subset == 'train': data_const.train = True else: data_const.train = False dataset = Cifar100Dataset(data_const) collate_fn = dataset.get_collate_fn() dataloaders[mode] = DataLoader(dataset, batch_size=exp_const.batch_size, shuffle=True, num_workers=exp_const.num_workers, collate_fn=collate_fn) train_model(model, dataloaders, exp_const)