ini = ini_temp() #np.save(weights_path+r'result_test/avg_direct.npy', ini) #import SimpleITK as sitk #avg = sitk.ReadImage(r'/home/dkuang/LPBA40/avg152T1_brain.nii') #avg_data = sitk.GetArrayViewFromImage(avg) #ini = avg_data/np.max(avg_data) ini = np.expand_dims(np.expand_dims(ini, 3),0) #print(ini.shape) ''' test transform ''' ini1 = transform(np.zeros([1,res1, res2, res3, 3]), ini, (res1, res2, res3)) print(np.all(ini1==ini)) print([np.min(ini1-ini), np.max(ini1-ini)]) #alpha = 0.5 #for i in range(20): # disp_sum = 0 # for sample in samples: # tgt = np.load(datapath + sample) # tgt = np.expand_dims(np.expand_dims(tgt, 3),0) # pair = np.stack([ini, tgt], 4)[...,0] # print(np.all(pair[0,...,1] == tgt[0,...,0])) #print(tgt.shape) # disp_sum+=sdn.predict(pair) # disp_sum = disp_sum/len(samples)
label_list = list(np.unique(label1_data))[1:] dice_before, dice_after = [], [] ''' Different ways to warp the label mask makes huge differences... ''' brain_test1 = np.expand_dims(brain_test1, 0) warped_brain, deformation = sdn.predict(brain_test1) from Utils import transform for i in label_list: dice_before.append(Dice(label2_data == i, label1_data == i)) seg = transform(deformation, np.expand_dims(np.expand_dims(label1_data == i, 3), 0), (res1, res2, res3)) dice_after.append(Dice(label2_data == i, np.rint(seg) > 0)) #print("ROI {} result appended..".format(i)) count_worse = 0 count_equal = 0 count_better = 0 for i in range(56): if dice_after[i] < dice_before[i]: count_worse += 1
] + [101, 102, 121, 122] + [i for i in range(161, 167, 1)] + [181, 182] dice_before = np.zeros([len(label_test), 56]) dice_after = np.zeros([len(label_test), 56]) for test_ind, test_label in enumerate(label_test): brain_test = np.load(datapath + test_label) brain_test_label1 = np.load(labelpath + test_label[:2] + '.npy') brain_test_label2 = np.load(labelpath + test_label[2:4] + '.npy') brain_test = np.expand_dims(brain_test, 0) deformation = sdn.predict(brain_test) for label_ind, i in enumerate(label_list): dice_before[test_ind, label_ind] = Dice(brain_test_label2 == i, brain_test_label1 == i) seg = transform( deformation, np.expand_dims(np.expand_dims(brain_test_label1 == i, 3), 0), (res1, res2, res3)) dice_after[test_ind, label_ind] = Dice(brain_test_label2 == i, np.rint(seg) > 0) #print("ROI {} result appended..".format(i)) print('Test sample {}\'s evaluation completed.'.format(test_ind + 1)) #print("writing the first to log....") #test_log = open('result_test/SDN3dTest.txt', 'w') #test_log.write("Brain pair: {}\n".format(label_test[0])) #test_log.write("*"*40+"\n") #test_log.write("Before Registration"+"\t" + "After Registration\n") #for before, after in zip(dice_before[0], dice_after[0]): #test_log.write("{0:.6f}".format(before)+"\t" + "{0:.6f}".format(after)+'\n') #test_log.close()
dice_before = np.zeros([len(train_list), 56]) for test_ind, test_label in enumerate(train_list): brain_test_mov = np.load(datapath+test_label[:2]+'.npy') brain_test_fix = np.load(datapath+test_label[2:]+'.npy') brain_test = np.stack([brain_test_mov, brain_test_fix], 3) brain_test_label1 = np.load(labelpath+test_label[:2]+'.npy') brain_test_label2 = np.load(labelpath+test_label[2:4]+'.npy') brain_test = np.expand_dims(brain_test, 0) warped_vol, deformation = sdn_pred.predict(brain_test) np.save('/home/dkuang/{}.npy'.format(test_label), warped_vol[0,...,0]) for label_ind, i in enumerate(label_list): seg = transform(deformation, np.expand_dims(np.expand_dims(brain_test_label1==i, 3),0), (res1, res2, res3)) dice_before[test_ind, label_ind] = Dice(brain_test_label2 == i, np.rint(seg)>0) print(dice_before) print(np.mean(dice_before, 1)) sdn_refine.compile(loss = ['mse', total_variation_loss], loss_weights = par['loss_weights'], optimizer = Adam(lr = par['lr'], decay = 1e-5) ) print(sdn_refine.summary()) gen_train = vol_gen_refine(datapath, predpath,train_list, par['batch_size']) history = sdn_refine.fit_generator(gen_train, steps_per_epoch = len(train_list)/par['batch_size'], epochs = par['epochs'], use_multiprocessing = True, verbose=1)
brain_utr_data = sitk.GetArrayViewFromImage(brain_utr) utr_warped_img = brain_utr_data / np.max(brain_utr_data) label_utr = sitk.ReadImage( utillabelpath + 'S{}toS{}.fsl152.structure.label.nii'.format(mov, fix)) utr_warped_label = sitk.GetArrayViewFromImage(label_utr) brain_test = np.load(datapath + test_label) brain_test_label1 = np.load(labelpath + '{}.npy'.format(mov)) brain_test_label2 = np.load(labelpath + '{}.npy'.format(fix)) brain_test = np.expand_dims(brain_test, 0) deformation = sdn.predict(brain_test) sdn_warped_img = transform(deformation, np.expand_dims(brain_test[..., 0], 4), (res1, res2, res3)) sse_before[ind] = np.sum((brain_test[..., 0] - brain_test[..., 1])**2) sse_sdn[ind] = np.sum( (brain_test[0, ..., 1] - sdn_warped_img[0, ..., 0])**2) sse_utr[ind] = np.sum((brain_test[0, ..., 1] - utr_warped_img)**2) #print("sse_before: {}".format(sse_before)) #print("sse_sdn: {}".format(sse_sdn)) #print("sse_utr: {}".format(sse_utr)) for label_ind, i in enumerate(label_list): dice_before[ind, label_ind] = Dice(brain_test_label2 == i, brain_test_label1 == i) seg = transform(