def cross_fold_validation(cfg, result_dir, best_model=True): model_file = cfg['model'] + '_best.model' if best_model else cfg[ 'model'] + '_final.model' # run five fold validation model = get_model(cfg['model'], cfg['data']['channel'], 1).to(device) saved_model = torch.load(os.path.join(result_dir, model_file), map_location=device) try: model.load_state_dict( saved_model['model_state_dict']) # maybe need to change except RuntimeError: print('convert model layer name!!!') model_state_dict = convert_state_dict(saved_model['model_state_dict']) model.load_state_dict(model_state_dict) test_result = evaluation(model, data_root=cfg['data']['root'], csv_file=cfg['data']['csv_file']) test_csv = pd.DataFrame({ 'image_name': test_result['image_name'], 'target': test_result['target'] }) test_csv.to_csv(os.path.join(result_dir, 'submission.csv'), index=False)
def load_saved_model(self, fname, train=True): self.print_to_log_file("loading checkpoint", fname, "train=", train) saved_model = torch.load(fname, map_location=self.device) # load model state dict try: self.model.load_state_dict( saved_model['model_state_dict']) # maybe need to change except RuntimeError: model_state_dict = convert_state_dict( saved_model['model_state_dict']) self.model.load_state_dict(model_state_dict) # load optimizer and lr_scheduler if train: optimizer_state_dict = saved_model['optimizer_state_dict'] if optimizer_state_dict is not None: self.optimizer.load_state_dict(optimizer_state_dict) if self.lr_scheduler is not None and isinstance( self.lr_scheduler, optim.lr_scheduler.ReduceLROnPlateau): self.lr_scheduler.load_state_dict( saved_model['lr_scheduler_state_dict']) # load other related training results self.epoch = saved_model['epoch'] self.monitor_MA = saved_model['monitor_MA'] self.val_eval_criterion_MA = saved_model['val_eval_criterion_MA'] self.best_val_eval_criterion_MA = saved_model[ 'best_val_eval_criterion_MA'] self.best_epoch_based_on_monitor_MA = saved_model[ 'best_epoch_based_on_monitor_MA'] self.train_losses, self.train_eval_metrics, self.val_losses, self.val_eval_metrics = saved_model[ 'plot_stuff']
def inference_csv(cfg, result_dir, device, best_model=True): test_aug_parameters = OrderedDict({ 'affine': None, 'hflip': None, 'vflip': None, 'color_trans': None, 'hair_mask': None, 'rotate': None, 'microscope': None, 'normalization': { 'mean': (0.485, 0.456, 0.406), 'std': (0.229, 0.224, 0.225) }, 'size': 384, 'scale': (1.0, 1.0), 'ratio': (1.0, 1.0) }) model_file = cfg['model'] + '_best.model' if best_model else cfg[ 'model'] + '_final.model' # run five fold validation cfg['data']['root'] = '/home/zyi/My_disk/ISIC_2020/data/test' cfg['data']['csv_file'] = '/home/zyi/My_disk/ISIC_2020/test.csv' model = get_model(cfg['model'], cfg['data']['channel'], 1).to(device) saved_model = torch.load(os.path.join(result_dir, model_file), map_location=device) try: model.load_state_dict( saved_model['model_state_dict']) # maybe need to change except RuntimeError: print('convert model layer name!!!') model_state_dict = convert_state_dict(saved_model['model_state_dict']) model.load_state_dict(model_state_dict) test_result = evaluation(model, data_root=cfg['data']['root'], csv_file=cfg['data']['csv_file'], device=device, aug_parameters=test_aug_parameters) test_csv = pd.DataFrame({ 'image_name': list(test_result['image_name']), 'target': list(test_result['target']) }) test_csv.to_csv(os.path.join(result_dir, 'submission.csv'), index=False)