(model, criterion, postprocessors, data_loader, base_ds, device, output_dir, wo_class_error=False, args=None, logger=None)
| 121 | |
| 122 | @torch.no_grad() |
| 123 | def evaluate(model, criterion, postprocessors, data_loader, base_ds, device, output_dir, wo_class_error=False, args=None, logger=None): |
| 124 | try: |
| 125 | need_tgt_for_training = args.use_dn |
| 126 | except: |
| 127 | need_tgt_for_training = False |
| 128 | |
| 129 | model.eval() |
| 130 | criterion.eval() |
| 131 | |
| 132 | metric_logger = utils.MetricLogger(delimiter=" ") |
| 133 | if not wo_class_error: |
| 134 | metric_logger.add_meter('class_error', utils.SmoothedValue(window_size=1, fmt='{value:.2f}')) |
| 135 | header = 'Test:' |
| 136 | |
| 137 | iou_types = tuple(k for k in ('segm', 'bbox') if k in postprocessors.keys()) |
| 138 | useCats = True |
| 139 | try: |
| 140 | useCats = args.useCats |
| 141 | except: |
| 142 | useCats = True |
| 143 | if not useCats: |
| 144 | print("useCats: {} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!".format(useCats)) |
| 145 | coco_evaluator = CocoEvaluator(base_ds, iou_types, useCats=useCats) |
| 146 | # coco_evaluator.coco_eval[iou_types[0]].params.iouThrs = [0, 0.1, 0.5, 0.75] |
| 147 | |
| 148 | panoptic_evaluator = None |
| 149 | if 'panoptic' in postprocessors.keys(): |
| 150 | panoptic_evaluator = PanopticEvaluator( |
| 151 | data_loader.dataset.ann_file, |
| 152 | data_loader.dataset.ann_folder, |
| 153 | output_dir=os.path.join(output_dir, "panoptic_eval"), |
| 154 | ) |
| 155 | |
| 156 | _cnt = 0 |
| 157 | output_state_dict = {} # for debug only |
| 158 | for samples, targets in metric_logger.log_every(data_loader, 10, header, logger=logger): |
| 159 | samples = samples.to(device) |
| 160 | |
| 161 | # targets = [{k: v.to(device) for k, v in t.items()} for t in targets] |
| 162 | targets = [{k: to_device(v, device) for k, v in t.items()} for t in targets] |
| 163 | |
| 164 | with torch.cuda.amp.autocast(enabled=args.amp): |
| 165 | if need_tgt_for_training: |
| 166 | outputs = model(samples, targets) |
| 167 | else: |
| 168 | outputs = model(samples) |
| 169 | # outputs = model(samples) |
| 170 | |
| 171 | loss_dict = criterion(outputs, targets) |
| 172 | weight_dict = criterion.weight_dict |
| 173 | |
| 174 | # reduce losses over all GPUs for logging purposes |
| 175 | loss_dict_reduced = utils.reduce_dict(loss_dict) |
| 176 | loss_dict_reduced_scaled = {k: v * weight_dict[k] |
| 177 | for k, v in loss_dict_reduced.items() if k in weight_dict} |
| 178 | loss_dict_reduced_unscaled = {f'{k}_unscaled': v |
| 179 | for k, v in loss_dict_reduced.items()} |
| 180 | metric_logger.update(loss=sum(loss_dict_reduced_scaled.values()), |
no test coverage detected