def eval_define(self,weights_path): n_classes = self.n_classes net = ShelfNet(n_classes=n_classes) net.load_state_dict(torch.load(weights_path)) net.cuda() net.eval() self.evaluator = MscEval(net, dataloader=None, scales=[1.0],flip=False)
def evaluate(respth='./res', dspth='/data2/.encoding/data/cityscapes', checkpoint=None): ## logger logger = logging.getLogger() ## model logger.info('\n') logger.info('====' * 20) logger.info('evaluating the model ...\n') logger.info('setup and restore model') n_classes = 19 net = ShelfNet(n_classes=n_classes) if checkpoint is None: save_pth = osp.join(respth, 'model_final.pth') else: save_pth = checkpoint net.load_state_dict(torch.load(save_pth)) net.cuda() net.eval() ## dataset batchsize = 5 n_workers = 2 dsval = CityScapes(dspth, mode='val') dl = DataLoader(dsval, batch_size=batchsize, shuffle=False, num_workers=n_workers, drop_last=False) ## evaluator logger.info('compute the mIOU') evaluator = MscEval(net, dl, scales=[1.0], flip=False) ## eval mIOU = evaluator.evaluate() logger.info('mIOU is: {:.6f}'.format(mIOU))
def test(): model = ShelfNet(n_classes=19) # official implementation #model = BiSeNet(19,criterion=None,ohem_criterion=None,is_training=False) print(model) # count parameter number pytorch_total_params = sum(p.numel() for p in model.parameters()) print("Total number of parameters: %.3fM" % (pytorch_total_params / 1e6)) model = model.cuda() model.eval() run_time = list() for i in range(0, 100): input = torch.randn(1, 3, 1024, 2048).cuda() # ensure that context initialization and normal_() operations # finish before you start measuring time torch.cuda.synchronize() torch.cuda.synchronize() start = time.perf_counter() with torch.no_grad(): #output = model(input)#, aucx=False) output = model(input, aux=False) torch.cuda.synchronize() # wait for mm to finish end = time.perf_counter() print(end - start) run_time.append(end - start) run_time.pop(0) print('Mean running time is ', np.mean(run_time))