def evaluate(): setup = inversefed.utils.system_startup() defs = inversefed.training_strategy('conservative') defs.epochs = opt.epochs loss_fn, trainloader, validloader = preprocess(opt, defs, valid=False) model = create_model(opt) model.to(**setup) root = create_save_dir() filename = os.path.join(root, '{}_{}.pth'.format(opt.arch, opt.epochs)) print(filename) if not os.path.exists(filename): assert False print(filename) model.load_state_dict(torch.load(filename)) model.eval() stats = {'valid_losses': list(), 'valid_Accuracy': list()} inversefed.training.training_routine.validate(model, loss_fn, validloader, defs, setup=setup, stats=stats) print(stats)
def main(): setup = inversefed.utils.system_startup() defs = inversefed.training_strategy('conservative') defs.epochs = opt.epochs loss_fn, trainloader, validloader = preprocess(opt, defs) # init model model = create_model(opt) model.to(**setup) save_dir = create_save_dir() if not os.path.exists(save_dir): os.makedirs(save_dir) file = f'{save_dir}/{arch}_{defs.epochs}.pth' inversefed.train(model, loss_fn, trainloader, validloader, defs, setup=setup, save_dir=save_dir) torch.save(model.state_dict(), f'{file}') model.eval()
import numpy as np from PIL import Image import inversefed torch.backends.cudnn.benchmark = inversefed.consts.BENCHMARK from collections import defaultdict import datetime import time import os # Parse input arguments args = inversefed.options().parse_args() # Parse training strategy defs = inversefed.training_strategy('conservative') defs.epochs = args.epochs # 100% reproducibility? if args.deterministic: image2graph2vec.utils.set_deterministic() def boost_salient_filters(input_gradient, boost_const, salient_filters): for idx in range(len(input_gradient)): if len(input_gradient[idx].size()) == 4: salient_filters -= input_gradient[idx].size()[0] for ind in range(len(salient_filters)): if salient_filters[ind] < 0: input_gradient[idx][salient_filters[ind]] = input_gradient[ idx][salient_filters[ind]] * boost_const
parser.add_argument('--aug_list', default=None, required=True, type=str, help='Vision model.') parser.add_argument('--optim', default=None, required=True, type=str, help='Vision model.') parser.add_argument('--mode', default=None, required=True, type=str, help='Mode.') parser.add_argument('--rlabel', default=False, type=bool, help='rlabel') parser.add_argument('--arch', default=None, required=True, type=str, help='Vision model.') parser.add_argument('--data', default=None, required=True, type=str, help='Vision dataset.') parser.add_argument('--epochs', default=None, required=True, type=int, help='Vision epoch.') parser.add_argument('--resume', default=0, type=int, help='rlabel') opt = parser.parse_args() num_images = 1 # init env setup = inversefed.utils.system_startup() defs = inversefed.training_strategy('conservative'); defs.epochs = opt.epochs # init training arch = opt.arch trained_model = True mode = opt.mode assert mode in ['normal', 'aug', 'crop'] config = create_config(opt) def create_save_dir(): return 'benchmark/images/data_{}_arch_{}_epoch_{}_optim_{}_mode_{}_auglist_{}_rlabel_{}'.format(opt.data, opt.arch, opt.epochs, opt.optim, opt.mode, \ opt.aug_list, opt.rlabel)