(image, label, net, classes, patch_size=[256, 256], test_save_path=None, case=None, z_spacing=1, class_names=None)
| 170 | |
| 171 | |
| 172 | def test_single_volume(image, label, net, classes, patch_size=[256, 256], test_save_path=None, case=None, z_spacing=1, class_names=None): |
| 173 | image, label = image.squeeze(0).cpu().detach().numpy(), label.squeeze(0).cpu().detach().numpy() |
| 174 | if class_names==None: |
| 175 | mask_labels = np.arange(1,classes) |
| 176 | else: |
| 177 | mask_labels = class_names |
| 178 | cmaps = mcolors.CSS4_COLORS |
| 179 | |
| 180 | my_colors=['red','darkorange','yellow','forestgreen','blue','purple','magenta','cyan','deeppink', 'chocolate', 'olive','deepskyblue','darkviolet'] |
| 181 | cmap = {k: cmaps[k] for k in sorted(cmaps.keys()) if k in my_colors[:classes-1]} |
| 182 | |
| 183 | if len(image.shape) == 3: |
| 184 | prediction = np.zeros_like(label) |
| 185 | for ind in range(image.shape[0]): |
| 186 | slice = image[ind, :, :] |
| 187 | x, y = slice.shape[0], slice.shape[1] |
| 188 | if x != patch_size[0] or y != patch_size[1]: |
| 189 | slice = zoom(slice, (patch_size[0] / x, patch_size[1] / y), order=3) # previous using 0 |
| 190 | input = torch.from_numpy(slice).unsqueeze(0).unsqueeze(0).float().cuda() |
| 191 | net.eval() |
| 192 | with torch.no_grad(): |
| 193 | P = net(input) |
| 194 | #print(len(P)) |
| 195 | outputs = 0.0 |
| 196 | for idx in range(len(P)): |
| 197 | outputs += P[idx] |
| 198 | out = torch.argmax(torch.softmax(outputs, dim=1), dim=1).squeeze(0) |
| 199 | out = out.cpu().detach().numpy() |
| 200 | if x != patch_size[0] or y != patch_size[1]: |
| 201 | pred = zoom(out, (x / patch_size[0], y / patch_size[1]), order=0) |
| 202 | else: |
| 203 | pred = out |
| 204 | |
| 205 | lbl = label[ind, :, :] |
| 206 | masks = [] |
| 207 | for i in range(1, classes): |
| 208 | masks.append(lbl==i) |
| 209 | preds_o = [] |
| 210 | for i in range(1, classes): |
| 211 | preds_o.append(pred==i) |
| 212 | |
| 213 | # saving the groundtruth lables and output prediction maps for each frame |
| 214 | fig_gt = overlay_masks(image[ind, :, :], masks, labels=mask_labels, colors=cmap, mask_alpha=0.5) |
| 215 | fig_pred = overlay_masks(image[ind, :, :], preds_o, labels=mask_labels, colors=cmap, mask_alpha=0.5) |
| 216 | # Do with that image whatever you want to do. |
| 217 | fig_gt.savefig(test_save_path + '/'+case + '_' +str(ind)+'_gt.png', bbox_inches="tight", dpi=300) |
| 218 | fig_pred.savefig(test_save_path + '/'+case + '_' +str(ind)+'_pred.png', bbox_inches="tight", dpi=300) |
| 219 | prediction[ind] = pred |
| 220 | else: |
| 221 | input = torch.from_numpy(image).unsqueeze( |
| 222 | 0).unsqueeze(0).float().cuda() |
| 223 | net.eval() |
| 224 | with torch.no_grad(): |
| 225 | P = net(input) |
| 226 | outputs = 0.0 |
| 227 | for idx in range(len(P)): |
| 228 | outputs += P[idx] |
| 229 | out = torch.argmax(torch.softmax(outputs, dim=1), dim=1).squeeze(0) |
no test coverage detected