| 79 | |
| 80 | @torch.no_grad() |
| 81 | def evaluate(model, dataloader, config, device, engine, save_dir=None, sliding=False): |
| 82 | print("Evaluating...") |
| 83 | model.eval() |
| 84 | n_classes = config.num_classes |
| 85 | metrics = Metrics(n_classes, config.background, device) |
| 86 | |
| 87 | for idx, minibatch in enumerate(dataloader): |
| 88 | if ((idx + 1) % int(len(dataloader) * 0.5) == 0 or idx == 0) and ( |
| 89 | (engine.distributed and (engine.local_rank == 0)) or (not engine.distributed) |
| 90 | ): |
| 91 | print(f"Validation Iter: {idx + 1} / {len(dataloader)}") |
| 92 | images = minibatch["data"] |
| 93 | labels = minibatch["label"] |
| 94 | modal_xs = minibatch["modal_x"] |
| 95 | if len(images.shape) == 3: |
| 96 | images = images.unsqueeze(0) |
| 97 | if len(modal_xs.shape) == 3: |
| 98 | modal_xs = modal_xs.unsqueeze(0) |
| 99 | if len(labels.shape) == 2: |
| 100 | labels = labels.unsqueeze(0) |
| 101 | # print(images.shape,labels.shape) |
| 102 | images = [images.to(device), modal_xs.to(device)] |
| 103 | labels = labels.to(device) |
| 104 | if sliding: |
| 105 | preds = slide_inference(model, images, modal_xs, config).softmax(dim=1) |
| 106 | else: |
| 107 | preds = model(images[0], images[1]).softmax(dim=1) |
| 108 | # print(preds.shape,labels.shape) |
| 109 | B, H, W = labels.shape |
| 110 | metrics.update(preds, labels) |
| 111 | # for i in range(B): |
| 112 | # metrics.update(preds[i].unsqueeze(0), labels[i].unsqueeze(0)) |
| 113 | # metrics.update(preds, labels) |
| 114 | |
| 115 | if save_dir is not None: |
| 116 | palette = [ |
| 117 | [128, 64, 128], |
| 118 | [244, 35, 232], |
| 119 | [70, 70, 70], |
| 120 | [102, 102, 156], |
| 121 | [190, 153, 153], |
| 122 | [153, 153, 153], |
| 123 | [250, 170, 30], |
| 124 | [220, 220, 0], |
| 125 | [107, 142, 35], |
| 126 | [152, 251, 152], |
| 127 | [70, 130, 180], |
| 128 | [220, 20, 60], |
| 129 | [255, 0, 0], |
| 130 | [0, 0, 142], |
| 131 | [0, 0, 70], |
| 132 | [0, 60, 100], |
| 133 | [0, 80, 100], |
| 134 | [0, 0, 230], |
| 135 | [119, 11, 32], |
| 136 | ] |
| 137 | palette = np.array(palette, dtype=np.uint8) |
| 138 | cmap = ListedColormap(palette) |