MCPcopy Create free account
hub / github.com/TorchSSL/TorchSSL / evaluate

Method evaluate

models/mixmatch/mixmatch.py:242–268  ·  view source on GitHub ↗
(self, eval_loader=None, args=None)

Source from the content-addressed store, hash-verified

240
241 @torch.no_grad()
242 def evaluate(self, eval_loader=None, args=None):
243 self.model.eval()
244 self.ema.apply_shadow()
245 if eval_loader is None:
246 eval_loader = self.loader_dict['eval']
247 total_loss = 0.0
248 total_num = 0.0
249 y_true = []
250 y_pred = []
251 y_logits = []
252 for _, x, y in eval_loader:
253 x, y = x.cuda(args.gpu), y.cuda(args.gpu)
254 num_batch = x.shape[0]
255 total_num += num_batch
256 logits = self.model(x)
257 loss = F.cross_entropy(logits, y, reduction='mean')
258 y_true.extend(y.cpu().tolist())
259 y_pred.extend(torch.max(logits, dim=-1)[1].cpu().tolist())
260 y_logits.extend(torch.softmax(logits, dim=-1).cpu().tolist())
261 total_loss += loss.detach() * num_batch
262 top1 = accuracy_score(y_true, y_pred)
263 top5 = top_k_accuracy_score(y_true, y_logits, k=5)
264 cf_mat = confusion_matrix(y_true, y_pred, normalize='true')
265 self.print_fn('confusion matrix:\n' + np.array_str(cf_mat))
266 self.ema.restore()
267 self.model.train()
268 return {'eval/loss': total_loss / total_num, 'eval/top-1-acc': top1, 'eval/top-5-acc': top5}
269
270 def save_model(self, save_name, save_path):
271 if self.it < 1000000:

Callers 1

trainMethod · 0.95

Calls 3

apply_shadowMethod · 0.80
restoreMethod · 0.80
trainMethod · 0.45

Tested by

no test coverage detected