<jupyter_start><jupyter_text>Seperate interface Part to avoid retraining the model if interface was hanged<jupyter_code>%%time
%matplotlib inline

import matplotlib.cm

import matplotlib.pyplot as plt
import numpy as np
from medpy.io import load
from inference.UNetInferenceAgent import UNetInferenceAgent
from utils.volume_stats import Dice3d,Jaccard3d
import torch
path="/media/ahmed000/Personal/Learning/DeepLearning/projects/AI_for_HC_ND/Third_course/final_project/section1/out/TrainingSet"
data = LoadHippocampusData(path, y_shape = 64, z_shape = 64)
images_t=data[230:]

out_dict = {}
out_dict["volume_stats"] = []
dc_list = []

jc_list = []
inference_agent = UNetInferenceAgent("/media/ahmed000/Personal/Learning/DeepLearning/projects/AI_for_HC_ND/Third_course/final_project/section2/out/2020-05-30_1018_Basic_unet/model.pth")
with torch.no_grad():
    for i, x in enumerate(images_t):
        pred_label = inference_agent.single_volume_inference(x["image"])
        dc = Dice3d(pred_label, x["seg"])
        jc = Jaccard3d(pred_label, x["seg"])
        dc_list.append(dc)
        jc_list.append(jc)
        out_dict["volume_stats"].append({
            "filename": x['filename'],
            "dice": dc,
            "jaccard": jc
            })
        print(f"{x['filename']} Dice {dc:.4f}. {100*(i+1)/len(images_t):.2f}% complete")
out_dict["overall"] = {
    "mean_dice": np.mean(dc_list),
    "mean_jaccard": np.mean(jc_list)}

print("\nTesting complete.")
out_dir= "/media/ahmed000/Personal/Learning/DeepLearning/projects/AI_for_HC_ND/Third_course/final_project/section2/out"
with open(os.path.join(out_dir, "results.json"), 'w') as out_file:
    json.dump(out_dict, out_file, indent=2, separators=(',', ': '))


%matplotlib inline

figure,axes=plt.subplots(20,3,figsize=(10,40))
axes=axes.flatten()
for ind in range(20):
    axes[ind*3].imshow(pred_label[ind,0,:,:],cmap='gray')
    axes[ind*3+1].imshow(pred_label[ind,1,:,:],cmap='gray')
    axes[ind*3+2].imshow(pred_label[ind,2,:,:],cmap='gray')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup