(
dataLoader, epoch_number, model, cfg, criterion, logger, device, rank, distributed, **kwargs
)
| 102 | return rt |
| 103 | |
| 104 | def valid_model( |
| 105 | dataLoader, epoch_number, model, cfg, criterion, logger, device, rank, distributed, **kwargs |
| 106 | ): |
| 107 | model.eval() |
| 108 | |
| 109 | if cfg['loss']['type']=="DiVEKLD": |
| 110 | criterion = criterion.base_loss |
| 111 | with torch.no_grad(): |
| 112 | all_loss = AverageMeter() |
| 113 | acc_avg = AverageMeter() |
| 114 | |
| 115 | labels = [] |
| 116 | preds = [] |
| 117 | pred_scores = [] |
| 118 | label_weights = [] |
| 119 | now_results = [] |
| 120 | |
| 121 | func = torch.nn.Sigmoid() \ |
| 122 | if cfg['loss']['type'] in ['FocalLoss', 'ClassBalanceFocal'] else \ |
| 123 | torch.nn.Softmax(dim=1) |
| 124 | |
| 125 | for i, batch_dic in enumerate(dataLoader): |
| 126 | data = batch_dic['x'] |
| 127 | label = batch_dic['y'] |
| 128 | label_weights += batch_dic['y_weight'] |
| 129 | data, label = data.to(device), label.to(device) |
| 130 | feature = model(data, feature_flag=True) |
| 131 | |
| 132 | output = model(feature, head_flag=True, label=label) |
| 133 | |
| 134 | labels += list(label.cpu().numpy()) |
| 135 | preds += list(torch.argmax(output, 1).cpu().numpy()) |
| 136 | pred_scores += list(func(output).cpu().numpy()) |
| 137 | |
| 138 | loss = criterion(output, label.long(), feature=feature) |
| 139 | |
| 140 | if cfg['setting']['type'] == "LT Regression": |
| 141 | now_result = output[:, 0] |
| 142 | elif cfg['setting']['type'] in ["LT Classification","Open LT"]: |
| 143 | score_result = func(output) |
| 144 | now_result = torch.argmax(score_result, 1) |
| 145 | now_results += list(now_result.cpu().numpy()) |
| 146 | acc, cnt = accuracy(now_result.cpu().numpy(), label.cpu().numpy()) |
| 147 | |
| 148 | if distributed: |
| 149 | world_size = float(os.environ.get("WORLD_SIZE", 1)) |
| 150 | reduced_loss = reduce_tensor(loss.data, world_size) |
| 151 | reduced_acc = reduce_tensor(torch.from_numpy(np.array([acc])).cuda(), world_size) |
| 152 | loss = reduced_loss.cpu().data |
| 153 | acc = reduced_acc.cpu().data |
| 154 | |
| 155 | all_loss.update(loss.data.item(), label.shape[0]) |
| 156 | if distributed: |
| 157 | acc_avg.update(acc.data.item(), cnt*world_size) |
| 158 | else: |
| 159 | acc_avg.update(acc, cnt) |
| 160 | |
| 161 | # statistics for long-tailed validation metrics |
no test coverage detected