| 120 | #optimizer = optim.SGD(net.parameters(), lr=base_lr, momentum=0.9, weight_decay=0.0001) |
| 121 | |
| 122 | def val(): |
| 123 | logging.info("Validation ===>") |
| 124 | dc_sum=0 |
| 125 | metric_list = 0.0 |
| 126 | net.eval() |
| 127 | for i, val_sampled_batch in enumerate(valloader): |
| 128 | val_image_batch, val_label_batch = val_sampled_batch["image"], val_sampled_batch["label"] |
| 129 | |
| 130 | val_image_batch, val_label_batch = val_image_batch.squeeze(0).cpu().detach().numpy(), val_label_batch.squeeze(0).cpu().detach().numpy() |
| 131 | |
| 132 | x, y = val_image_batch.shape[0], val_image_batch.shape[1] |
| 133 | if x != args.img_size or y != args.img_size: |
| 134 | val_image_batch = zoom(val_image_batch, (args.img_size / x, args.img_size / y), order=3) # not for double_maxvits |
| 135 | val_image_batch = torch.from_numpy(val_image_batch).unsqueeze(0).unsqueeze(0).float().cuda() |
| 136 | |
| 137 | P = net(val_image_batch) |
| 138 | #print(len(P)) |
| 139 | |
| 140 | val_outputs = 0.0 |
| 141 | for idx in range(len(P)): |
| 142 | val_outputs += P[idx] |
| 143 | |
| 144 | val_outputs = torch.softmax(val_outputs, dim=1) |
| 145 | |
| 146 | val_outputs = torch.argmax(val_outputs, dim=1).squeeze(0) |
| 147 | val_outputs = val_outputs.cpu().detach().numpy() |
| 148 | if x != args.img_size or y != args.img_size: |
| 149 | val_outputs = zoom(val_outputs, (x / args.img_size, y / args.img_size), order=0) |
| 150 | else: |
| 151 | val_outputs = val_outputs |
| 152 | |
| 153 | dc_sum+=dc(val_outputs,val_label_batch[:]) |
| 154 | performance = dc_sum / len(valloader) |
| 155 | logging.info('Testing performance in val model: mean_dice : %f, best_dice : %f' % (performance, Best_dcs)) |
| 156 | |
| 157 | print('Testing performance in val model: mean_dice : %f, best_dice : %f' % (performance, Best_dcs)) |
| 158 | #print("val avg_dsc: %f" % (performance)) |
| 159 | return performance |
| 160 | |
| 161 | |
| 162 | l = [0, 1, 2, 3] |