MCPcopy Index your code
hub / github.com/SooLab/CGFormer / inference

Function inference

engine/engine.py:149–199  ·  view source on GitHub ↗
(test_loader, model, args)

Source from the content-addressed store, hash-verified

147
148@torch.no_grad()
149def inference(test_loader, model, args):
150 iou_list = []
151 I = 0.
152 U = 0.
153 tbar = tqdm(test_loader, desc='Inference:', ncols=100)
154 model.eval()
155 time.sleep(2)
156 for ori_img, img, texts, mask, l_masks, seg_id, sents in tbar:
157 img = img.cuda(non_blocking=True)
158 mask = mask.cpu().numpy()
159 for text, l_mask, sent in zip(texts, l_masks, sents):
160 text = text.cuda(non_blocking=True)
161 l_mask = l_mask.cuda(non_blocking=True)
162
163 text = text.squeeze(1)
164 l_mask = l_mask.squeeze(1)
165
166 # inference
167 pred, maps = model(img, text, l_mask)
168 pred = torch.sigmoid(pred)
169 if pred.shape[-2:] != ori_img.shape[:-1]:
170 pred = F.interpolate(pred, size=ori_img.shape[1:-1], mode='bicubic', align_corners=True)
171 # # process one sentence
172 pred = pred.cpu().numpy()
173 pred_ = np.array(pred > 0.5)
174 inter = np.logical_and(pred_, mask)
175 union = np.logical_or(pred_, mask)
176 I += np.sum(inter)
177 U += np.sum(union)
178 iou = np.sum(inter) / (np.sum(union) + 1e-6)
179 iou_list.append(iou)
180
181 logger.info('=> Metric Calculation <=')
182 iou_list = np.stack(iou_list)
183 iou_list = torch.from_numpy(iou_list).to(img.device)
184 prec_list = []
185 for thres in torch.arange(0.5, 1.0, 0.1):
186 tmp = (iou_list > thres).float().mean()
187 prec_list.append(tmp)
188 iou = iou_list.mean()
189 prec = {}
190 for i, thres in enumerate(range(5, 10)):
191 key = 'Pr@{}'.format(thres*10)
192 value = prec_list[i].item()
193 prec[key] = value
194 logger.info('oIoU={:.2f}'.format(100.*(I/U)))
195 logger.info('mIoU={:.2f}'.format(100.*iou.item()))
196 for k, v in prec.items():
197 logger.info('{}: {:.2f}.'.format(k, 100.*v))
198
199 return iou.item(), prec

Callers 1

mainFunction · 0.90

Calls 2

toMethod · 0.80
itemsMethod · 0.80

Tested by 1

mainFunction · 0.72