def evaluate_train(self, output_dict: dict, targets: torch.Tensor): assert isinstance(output_dict, dict) and KEY_OUTPUT in output_dict.keys() probs = output_dict[KEY_OUTPUT] res = topk_accuracy(probs, targets, topk=self.topk) acc_dict = dict() for i in range(len(self.topk)): acc_dict[f'tok{self.topk[i]}'] = res[i] return acc_dict
def evaluate_test(self, output_dict: dict, targets: torch.Tensor): assert isinstance(output_dict, dict) and KEY_OUTPUT in output_dict.keys() probs = output_dict[KEY_OUTPUT] outputs = probs.to(device=self.device) targets = targets.to(device=self.device) res = topk_accuracy(outputs, targets, topk=self.topk) self.topk_list.append(torch.stack(res)) preds = torch.argmax(outputs, dim=1) for target, pred in zip(targets.numpy(), preds.numpy()): self.cate_num_dict.update( {str(target): self.cate_num_dict.get(str(target), 0) + 1}) self.cate_acc_dict.update({ str(target): self.cate_acc_dict.get(str(target), 0) + int(target == pred) })