|
|
@@ -82,16 +82,34 @@ placeholder = np.zeros(
|
|
|
dtype=np.float32,
|
|
|
) # Placeholder for results
|
|
|
|
|
|
+# Get the total list of image_ids
|
|
|
+img_ids = [img_id for _, _, _, img_id in combined_loader.dataset]
|
|
|
+
|
|
|
+
|
|
|
placeholder[:] = np.nan # Fill with NaNs for easier identification of missing data
|
|
|
-dimensions = ["model", "batch", "img_class"]
|
|
|
+dimensions = ["model", "img_id", "img_class"]
|
|
|
coords = {
|
|
|
"model": [int(mf.stem.split("_")[2]) for mf in model_files],
|
|
|
- "batch": list(range(len(combined_loader))),
|
|
|
+ "img_id": img_ids,
|
|
|
"img_class": list(range(config["data"]["num_classes"])),
|
|
|
}
|
|
|
-
|
|
|
results = xr.DataArray(placeholder, coords=coords, dims=dimensions)
|
|
|
|
|
|
+# Now initialize an additional dataarray to hold the labels per image
|
|
|
+labels_placeholder = np.zeros(
|
|
|
+ (len(combined_loader), config["data"]["num_classes"]), dtype=np.float32
|
|
|
+)
|
|
|
+labels_placeholder[:] = np.nan
|
|
|
+labels_coords = {
|
|
|
+ "img_id": img_ids,
|
|
|
+ "label": list(range(config["data"]["num_classes"])),
|
|
|
+} # type: ignore
|
|
|
+
|
|
|
+labels = xr.DataArray(
|
|
|
+ labels_placeholder, coords=labels_coords, dims=["img_id", "label"]
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
for model_file in model_files:
|
|
|
model_num = int(model_file.stem.split("_")[2])
|
|
|
print(f"Evaluating model {model_num}...")
|
|
|
@@ -114,15 +132,17 @@ for model_file in model_files:
|
|
|
model.eval()
|
|
|
|
|
|
with torch.no_grad():
|
|
|
- for batch_idx, (mri_batch, xls_batch, labels_batch) in enumerate(
|
|
|
- combined_loader
|
|
|
- ):
|
|
|
- outputs = model((mri_batch.float(), xls_batch.float()))
|
|
|
+ for batch_idx, (mri, xls, label, img_id) in enumerate(combined_loader):
|
|
|
+ outputs = model((mri.float(), xls.float()))
|
|
|
probabilities = outputs.cpu().numpy()[0, :] # type: ignore
|
|
|
|
|
|
- results.loc[model_num, batch_idx, :] = probabilities # type: ignore
|
|
|
+ results.loc[model_num, img_id, :] = probabilities # type: ignore
|
|
|
+ labels.loc[int(img_id.cpu()), :] = label.cpu().numpy()[0, :] # type: ignore
|
|
|
+
|
|
|
+# Combine results and labels into a single Dataset
|
|
|
+output_set = xr.Dataset({"predictions": results, "labels": labels})
|
|
|
|
|
|
# Save results to netcdf file
|
|
|
output_path = pl.Path(config["output"]["path"]) / "model_evaluation_results.nc"
|
|
|
-results.to_netcdf(output_path, mode="w") # type: ignore
|
|
|
+output_set.to_netcdf(output_path, mode="w") # type: ignore
|
|
|
print(f"Results saved to {output_path}")
|