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

Method evaluate

models/fullysupervised/fullysupervised.py:184–210  ·  view source on GitHub ↗
(self, eval_loader=None, args=None)

Source from the content-addressed store, hash-verified

182
183 @torch.no_grad()
184 def evaluate(self, eval_loader=None, args=None):
185 self.model.eval()
186 self.ema.apply_shadow()
187 if eval_loader is None:
188 eval_loader = self.loader_dict['eval']
189 total_loss = 0.0
190 total_num = 0.0
191 y_true = []
192 y_pred = []
193 y_logits = []
194 for _, x, y in eval_loader:
195 x, y = x.cuda(args.gpu), y.cuda(args.gpu)
196 num_batch = x.shape[0]
197 total_num += num_batch
198 logits = self.model(x)
199 loss = F.cross_entropy(logits, y, reduction='mean')
200 y_true.extend(y.cpu().tolist())
201 y_pred.extend(torch.max(logits, dim=-1)[1].cpu().tolist())
202 y_logits.extend(torch.softmax(logits, dim=-1).cpu().tolist())
203 total_loss += loss.detach() * num_batch
204 top1 = accuracy_score(y_true, y_pred)
205 top5 = top_k_accuracy_score(y_true, y_logits, k=5)
206 cf_mat = confusion_matrix(y_true, y_pred, normalize='true')
207 self.print_fn('confusion matrix:\n' + np.array_str(cf_mat))
208 self.ema.restore()
209 self.model.train()
210 return {'eval/loss': total_loss / total_num, 'eval/top-1-acc': top1, 'eval/top-5-acc': top5}
211
212 def save_model(self, save_name, save_path):
213 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