def main(): #get user options/configurations opt = opts.get_opt() #set cuda visible devices if user specified a value if opt.gpus is not None: os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus #load class to save metrics, iamges and models to disk output = outputs.Outputs(opt) #load class to store metrics and losses values metric = metrics.Metrics() #load synthetic dataset healthy_dataloader_train, anomaly_dataloader_train = init_synth_dataloader_original( opt.folder_toy_dataset, opt.batch_size, mode='train') healthy_dataloader_val, anomaly_dataloader_val = init_synth_dataloader_original( opt.folder_toy_dataset, opt.batch_size, mode='val') net_g, net_r = init_model(opt) optim_g, optim_r = init_optimizer(opt, net_g=net_g, net_r=net_r) net_g = net_g.cuda() net_r = net_r.cuda() train(opt, healthy_dataloader_train, anomaly_dataloader_train, healthy_dataloader_val, anomaly_dataloader_val, net_g=net_g, net_r=net_r, optim_g=optim_g, optim_r=optim_r, output=output, metric=metric)
def main(): """Train model and run inference on coco test set to output metrics""" args = get_opt() os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu) if(args.is_train == 1): if not os.path.exists(args.model_dir): os.makedirs(args.model_dir) train(args) bestmodelfn = os.path.join(args.model_dir, 'model.pth') if(os.path.exists(bestmodelfn)): if(args.beam_size == 1): scores = test(args, 'test', modelfn=bestmodelfn) else: scores = test_beam(args, 'test', modelfn=bestmodelfn) print('TEST set scores') for k, v in list(scores[0].items()): print(('%s: %f' % (k, v))) else: raise Exception('No checkpoint found %s' % bestmodelfn)
if args.caption_model == "transformer": optim_state_dict = optimizer.optimizer.state_dict() else: optim_state_dict = optimizer.state_dict() torch.save( { 'epoch': epoch, 'decoder_state_dict': decoder.state_dict(), 'encoder_state_dict': encoder.state_dict(), 'optimizer': optim_state_dict, 'iteration': iteration, args.score_select: score, }, save_path) if (score > bestscore): bestscore = score print(('[DEBUG] Saving model at epoch %d with %s score of %f'\ % (epoch, args.score_select, score))) bestmodel_path = os.path.join(args.model_path, 'best_model.ckpt') os.system('cp %s %s' % (save_path, bestmodel_path)) # test args.vis = True test(args, "test", encoder=encoder, decoder=decoder) if __name__ == '__main__': args = get_opt() print(args) main(args)