MCPcopy Create free account
hub / github.com/SLDGroup/EMCAD / test_single_volume

Function test_single_volume

utils/utils.py:165–237  ·  view source on GitHub ↗
(image, label, net, classes, patch_size=[256, 256], test_save_path=None, case=None, z_spacing=1, class_names=None)

Source from the content-addressed store, hash-verified

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

Callers 1

inferenceFunction · 0.90

Calls 1

calculate_metric_percaseFunction · 0.85

Tested by

no test coverage detected