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

Function validate

engine/engine.py:91–145  ·  view source on GitHub ↗
(val_loader, model, epoch, args)

Source from the content-addressed store, hash-verified

89
90@torch.no_grad()
91def validate(val_loader, model, epoch, args):
92 iou_list = []
93 I = []
94 U = []
95 model.eval()
96 time.sleep(2)
97 for imgs, text, masks, l_mask in val_loader:
98 # data
99 imgs = torch.stack(imgs).cuda(non_blocking=True)
100 text = torch.stack(text).cuda(non_blocking=True)
101 l_mask = torch.stack(l_mask).cuda(non_blocking=True)
102 text = text.squeeze(1)
103 l_mask = l_mask.squeeze(1)
104 # inference
105 preds, maps = model(imgs, text, l_mask)
106 preds = torch.sigmoid(preds)
107 # process one batch
108 for pred, mask in zip(preds, masks):
109 # iou
110 pred = pred.cpu().numpy()
111 mask = mask.cpu().numpy()
112 pred = np.array(pred > 0.5)
113 inter = np.logical_and(pred, mask)
114 union = np.logical_or(pred, mask)
115 iou = np.sum(inter) / (np.sum(union) + 1e-6)
116 iou_list.append(iou)
117 I.append(np.sum(inter))
118 U.append(np.sum(union))
119 iou_list = np.stack(iou_list)
120 iou_list = torch.from_numpy(iou_list).to(imgs.device)
121 iou_list = concat_all_gather(iou_list)
122 I = np.stack(I)
123 I = torch.from_numpy(I).to(imgs.device)
124 I = concat_all_gather(I).sum()
125
126 U = np.stack(U)
127 U = torch.from_numpy(U).to(imgs.device)
128 U = concat_all_gather(U).sum()
129 oIoU = I/U
130 prec_list = []
131 for thres in torch.arange(0.5, 1.0, 0.1):
132 tmp = (iou_list > thres).float().mean()
133 prec_list.append(tmp)
134 iou = iou_list.mean()
135 prec = {}
136 temp = ' '
137 for i, thres in enumerate(range(5, 10)):
138 key = 'Pr@{}'.format(thres * 10)
139 value = prec_list[i].item()
140 prec[key] = value
141 temp += "{}: {:.2f} ".format(key, 100. * value)
142 head = 'Evaluation: Epoch=[{}/{}] mIoU={:.2f} oIoU={:.2f}'.format(
143 epoch, args.epochs, 100. * iou.item(), 100.*(oIoU))
144 logger.info(head + temp)
145 return oIoU, prec
146
147
148@torch.no_grad()

Callers 1

main_workerFunction · 0.90

Calls 2

concat_all_gatherFunction · 0.90
toMethod · 0.80

Tested by

no test coverage detected