def test_all_nets(): data = 2 data_path = '/exports/lkeb-hpc/syousefi/Data/invivo_decomposed/1/' # parent_path = '/exports/lkeb-hpc/syousefi/2-lkeb-17-dl01/syousefi/TestCode/EsophagusProject/sythesize_code/ASL_LOG/normal_SSIM_p_a_newdb/experiment-1/' parent_path = '/exports/lkeb-hpc/syousefi/2-lkeb-17-dl01/syousefi/TestCode/EsophagusProject/sythesize_code/ASL_LOG/Workshop_log/MSE/experiment-2/' # data_path = '/srv/2-lkeb-17-dl01/syousefi/TestCode/EsophagusProject/Data-01/BrainWeb_permutation00_low/' img_name = '' label_name = '' result_dir = 'results_mse/' _rd = _invivo_read_data(data=data, reverse=False, img_name=img_name, label_name=label_name, dataset_path=data_path) '''read path of the images for train, test, and validation''' test_data = _rd.read_data_path() chckpnt_dir = parent_path + 'unet_checkpoints/' result_path = parent_path + result_dir batch_no = 1 batch_no_validation = batch_no # label_patchs_size = 87#39 # 63 # patch_window = 103#53 # 77#89 img_row1 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row2 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row3 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row4 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row5 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row6 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row7 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row8 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) mri_ph = tf.placeholder(tf.float32, shape=[None, None, None, None, 1], name='mri') segmentation = tf.placeholder(tf.float32, shape=[None, None, None, None, 1], name='segmentation') label1 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label2 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label3 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label4 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label5 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label6 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label7 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label8 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label9 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label10 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label11 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label12 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label13 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label14 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) all_loss = tf.placeholder(tf.float32, name='loss') is_training = tf.placeholder(tf.bool, name='is_training') input_dim = tf.placeholder(tf.int32, name='input_dim') # ave_huber = tf.placeholder(tf.float32, name='huber') densenet = _densenet() y, degree = densenet.densenet(img_row1=img_row1, img_row2=img_row2, img_row3=img_row3, img_row4=img_row4, img_row5=img_row5, img_row6=img_row6, img_row7=img_row7, img_row8=img_row8, input_dim=input_dim, mri=mri_ph, is_training=is_training) loss_instance = _loss_func() labels = [] labels.append(label1) labels.append(label2) labels.append(label3) labels.append(label4) labels.append(label5) labels.append(label6) labels.append(label7) labels.append(label8) labels.append(label9) labels.append(label10) labels.append(label11) labels.append(label12) labels.append(label13) labels.append(label14) logits = [] logits.append(y[:, :, :, :, 0, np.newaxis]) logits.append(y[:, :, :, :, 1, np.newaxis]) logits.append(y[:, :, :, :, 2, np.newaxis]) logits.append(y[:, :, :, :, 3, np.newaxis]) logits.append(y[:, :, :, :, 4, np.newaxis]) logits.append(y[:, :, :, :, 5, np.newaxis]) logits.append(y[:, :, :, :, 6, np.newaxis]) logits.append(y[:, :, :, :, 7, np.newaxis]) logits.append(y[:, :, :, :, 8, np.newaxis]) logits.append(y[:, :, :, :, 9, np.newaxis]) logits.append(y[:, :, :, :, 10, np.newaxis]) logits.append(y[:, :, :, :, 11, np.newaxis]) logits.append(y[:, :, :, :, 12, np.newaxis]) logits.append(y[:, :, :, :, 13, np.newaxis]) with tf.name_scope('Loss'): loss_dic = loss_instance.loss_selector('SSIM_perf_angio', labels=labels, logits=logits, angio_SSIM_weight=1) cost = tf.reduce_mean(loss_dic["loss"], name="cost") # cost_angio = tf.reduce_mean(loss_dic["angio_SSIM"], name="angio_SSIM") # cost_perf = tf.reduce_mean(loss_dic["perf_SSIM"], name="perf_SSIM") # ======================================================================== # ave_loss = tf.placeholder(tf.float32, name='loss') # ave_loss_perf = tf.placeholder(tf.float32, name='loss_perf') # ave_loss_angio = tf.placeholder(tf.float32, name='loss_angio') # # average_gradient_perf = tf.placeholder(tf.float32, name='grad_ave_perf') # average_gradient_angio = tf.placeholder(tf.float32, name='grad_ave_angio') # # ave_huber = tf.placeholder(tf.float32, name='huber') # restore the model sess = tf.Session() saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(chckpnt_dir) saver.restore(sess, ckpt.model_checkpoint_path) # copyfile('./test_synthesize_ssim_perf_angio.py', result_path + '/test_synthesize_ssim_perf_angio.py') _image_class = invivo_image_class(test_data, bunch_of_images_no=1, is_training=1, patch_window=patch_window, sample_no_per_bunch=1, label_patch_size=label_patchs_size, validation_total_sample=0) learning_rate = 1E-5 extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(extra_update_ops): optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost) # init = tf.global_variables_initializer() dic_perf0 = [] dic_perf1 = [] dic_perf2 = [] dic_perf3 = [] dic_perf4 = [] dic_perf5 = [] dic_perf6 = [] dic_angio0 = [] dic_angio1 = [] dic_angio2 = [] dic_angio3 = [] dic_angio4 = [] dic_angio5 = [] dic_angio6 = [] loss = 0 #min ASL crush : 0.499999, max ASL crush : 0.500000 #min ASL noncrush : 0.499984, max ASL noncrush : 0.500000 #min perfusion : 2.192158, max perfusion : 11.602790 #min angio : 0.000000, max angio : 1309.346558 max_angio = 1309.346558 max_perf = 11.602790 for img_indx in range(len(test_data)): crush, noncrush, perf, angio, mri, segmentation_, spacing, direction, origin = _image_class.read_image_for_test( test_set=test_data, img_indx=img_indx, input_size=in_dim, final_layer=final_layer, invivo=1) for i in range(8): crush[i][np.where(crush[i] < 0)] = 0 # crush[i]=np.flipud(crush[i]) crush = crush / np.max(crush) crush = crush * (0.500000) for i in range(8): noncrush[i][np.where(noncrush[i] < 0)] = 0 # noncrush[i] = np.flipud(noncrush[i]) noncrush = noncrush / np.max(noncrush) noncrush = noncrush * (0.500000) for i in range(7): perf[i][np.where(perf[i] < 0)] = 0 # perf[i] = np.flipud(perf[i]) perf = perf / np.max(perf) * max_perf for i in range(7): angio[i][np.where(angio[i] < 0)] = 0 # angio[i] = np.flipud(angio[i]) angio = angio / np.max(angio) * max_angio [out] = sess.run( [y], feed_dict={ img_row1: np.expand_dims(np.expand_dims(crush[0], 0), -1), img_row2: np.expand_dims(np.expand_dims(noncrush[1], 0), -1), img_row3: np.expand_dims(np.expand_dims(crush[2], 0), -1), img_row4: np.expand_dims(np.expand_dims(noncrush[3], 0), -1), img_row5: np.expand_dims(np.expand_dims(crush[4], 0), -1), img_row6: np.expand_dims(np.expand_dims(noncrush[5], 0), -1), img_row7: np.expand_dims(np.expand_dims(crush[6], 0), -1), img_row8: np.expand_dims(np.expand_dims(noncrush[7], 0), -1), mri_ph: np.expand_dims(np.expand_dims(crush[0], 0), -1), #useless input segmentation: np.expand_dims(np.expand_dims(crush[0], 0), -1), #useless input label1: np.expand_dims(np.expand_dims(perf[0], 0), -1), label2: np.expand_dims(np.expand_dims(perf[1], 0), -1), label3: np.expand_dims(np.expand_dims(perf[2], 0), -1), label4: np.expand_dims(np.expand_dims(perf[3], 0), -1), label5: np.expand_dims(np.expand_dims(perf[4], 0), -1), label6: np.expand_dims(np.expand_dims(perf[5], 0), -1), label7: np.expand_dims(np.expand_dims(perf[6], 0), -1), label8: np.expand_dims(np.expand_dims(angio[0], 0), -1), label9: np.expand_dims(np.expand_dims(angio[1], 0), -1), label10: np.expand_dims(np.expand_dims(angio[2], 0), -1), label11: np.expand_dims(np.expand_dims(angio[3], 0), -1), label12: np.expand_dims(np.expand_dims(angio[4], 0), -1), label13: np.expand_dims(np.expand_dims(angio[5], 0), -1), label14: np.expand_dims(np.expand_dims(angio[6], 0), -1), is_training: False, input_dim: patch_window, all_loss: -1., }) parent_path = '/exports/lkeb-hpc/syousefi/Data/invivo_decomposed/1/00_PP0_0_cinema1/' # result_dir='normal_SSIM/' result_dir = 'results_mse/' for i in range(np.shape(out)[-1]): image = out[0, :, :, :, i] sitk_image = sitk.GetImageFromArray(image) res_dir = test_data[img_indx][0][0].split('/')[-2] if i == 0: os.mkdir(parent_path + result_dir + res_dir) if i < 7: nm = 'perf' else: nm = 'angi' sitk_image.SetDirection(direction=direction) sitk_image.SetOrigin(origin=origin) sitk_image.SetSpacing(spacing=spacing) sitk.WriteImage( sitk_image, parent_path + result_dir + res_dir + '/' + nm + '_' + str(i % 7) + '.nii') print(parent_path + result_dir + res_dir + '/' + nm + '_' + str(i % 7) + '.nii done!') for i in range(7): if i == 0: os.mkdir(parent_path + result_dir + res_dir + '/GT/') sitk_angio = sitk.GetImageFromArray(angio[i]) sitk_angio.SetDirection(direction=direction) sitk_angio.SetOrigin(origin=origin) sitk_angio.SetSpacing(spacing=spacing) sitk.WriteImage( sitk_angio, parent_path + result_dir + res_dir + '/GT/angio_' + str(i) + '.nii') sitk_perf = sitk.GetImageFromArray(perf[i]) sitk_perf.SetDirection(direction=direction) sitk_perf.SetOrigin(origin=origin) sitk_perf.SetSpacing(spacing=spacing) sitk.WriteImage( sitk_perf, parent_path + result_dir + res_dir + '/GT/perf_' + str(i) + '.nii') dic_perf0.append( anly.analysis(out[0, :, :, :, 0], perf[i], 0, max_perf)) dic_perf1.append( anly.analysis(out[0, :, :, :, 1], perf[i], 0, max_perf)) dic_perf2.append( anly.analysis(out[0, :, :, :, 2], perf[i], 0, max_perf)) dic_perf3.append( anly.analysis(out[0, :, :, :, 3], perf[i], 0, max_perf)) dic_perf4.append( anly.analysis(out[0, :, :, :, 4], perf[i], 0, max_perf)) dic_perf5.append( anly.analysis(out[0, :, :, :, 5], perf[i], 0, max_perf)) dic_perf6.append( anly.analysis(out[0, :, :, :, 6], perf[i], 0, max_perf)) dic_angio0.append( anly.analysis(out[0, :, :, :, 7], angio[i], 0, max_angio)) dic_angio1.append( anly.analysis(out[0, :, :, :, 8], angio[i], 0, max_angio)) dic_angio2.append( anly.analysis(out[0, :, :, :, 9], angio[i], 0, max_angio)) dic_angio3.append( anly.analysis(out[0, :, :, :, 10], angio[i], 0, max_angio)) dic_angio4.append( anly.analysis(out[0, :, :, :, 11], angio[i], 0, max_angio)) dic_angio5.append( anly.analysis(out[0, :, :, :, 12], angio[i], 0, max_angio)) dic_angio6.append( anly.analysis(out[0, :, :, :, 13], angio[i], 0, max_angio)) if img_indx == 0: headers = dic_perf0[0].keys() dics = [ dic_perf0, dic_perf1, dic_perf2, dic_perf3, dic_perf4, dic_perf5, dic_perf6, dic_angio0, dic_angio1, dic_angio2, dic_angio3, dic_angio4, dic_angio5, dic_angio6 ] save_in_xlsx(parent_path, headers, dics=dics) print('Total loss: ', loss / len(test_data))
def test_all_nets(): data = 2 Server = 'DL' if Server == 'DL': parent_path = '/srv/2-lkeb-17-dl01/syousefi/TestCode/EsophagusProject/sythesize_code/ASL_LOG/multi_stage/experiment-2/' data_path = '/srv/2-lkeb-17-dl01/syousefi/TestCode/EsophagusProject/Data-01/BrainWeb_permutation00_low/' else: parent_path = '/exports/lkeb-hpc/syousefi/Code/' data_path = '/srv/2-lkeb-17-dl01/syousefi/TestCode/EsophagusProject/Data-01/BrainWeb_permutation00_low/' img_name = '' label_name = '' _rd = _read_data(data=data, reverse=False, img_name=img_name, label_name=label_name, dataset_path=data_path) '''read path of the images for train, test, and validation''' train_data, validation_data, test_data = _rd.read_data_path() chckpnt_dir = parent_path + 'unet_checkpoints/' result_path = parent_path + 'results/' batch_no = 1 batch_no_validation = batch_no # label_patchs_size = 87#39 # 63 # patch_window = 103#53 # 77#89 if test_vali == 1: test_set = validation_data else: test_set = test_data # =================================================================================== # img_row1 = tf.placeholder(tf.float32, # shape=[batch_no, patch_window, patch_window, patch_window, 1], # name='img_row1') # img_row2 = tf.placeholder(tf.float32, # shape=[batch_no, patch_window, patch_window, patch_window, 1], # name='img_row2') # img_row3 = tf.placeholder(tf.float32, # shape=[batch_no, patch_window, patch_window, patch_window, 1], # name='img_row3') # img_row4 = tf.placeholder(tf.float32, # shape=[batch_no, patch_window, patch_window, patch_window, 1], # name='img_row4') # img_row5 = tf.placeholder(tf.float32, # shape=[batch_no, patch_window, patch_window, patch_window, 1], # name='img_row5') # img_row6 = tf.placeholder(tf.float32, # shape=[batch_no, patch_window, patch_window, patch_window, 1], # name='img_row6') # img_row7 = tf.placeholder(tf.float32, # shape=[batch_no, patch_window, patch_window, patch_window, 1], # name='img_row7') # img_row8 = tf.placeholder(tf.float32, # shape=[batch_no, patch_window, patch_window, patch_window, 1], # name='img_row8') # # mri_ph = tf.placeholder(tf.float32, # shape=[batch_no, patch_window, patch_window, patch_window, 1], # name='mri') # # segmentation = tf.placeholder(tf.float32, # shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='segments') # # label1 = tf.placeholder(tf.float32, shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='label1') # label2 = tf.placeholder(tf.float32, shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='label2') # label3 = tf.placeholder(tf.float32, shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='label3') # label4 = tf.placeholder(tf.float32, shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='label4') # label5 = tf.placeholder(tf.float32, shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='label5') # label6 = tf.placeholder(tf.float32, shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='label6') # label7 = tf.placeholder(tf.float32, shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='label7') # label8 = tf.placeholder(tf.float32, shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='label8') # label9 = tf.placeholder(tf.float32, shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='label9') # label10 = tf.placeholder(tf.float32, shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='label10') # label11 = tf.placeholder(tf.float32, shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='label11') # label12 = tf.placeholder(tf.float32, shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='label12') # label13 = tf.placeholder(tf.float32, shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='label13') # label14 = tf.placeholder(tf.float32, shape=[batch_no, label_patchs_size, label_patchs_size, # label_patchs_size, 1], name='label14') img_row1 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row2 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row3 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row4 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row5 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row6 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row7 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row8 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) mri_ph = tf.placeholder(tf.float32, shape=[None, None, None, None, 1], name='mri') # segmentation = tf.placeholder(tf.float32, shape=[None, None, None, None, 1], name='segmentation') label1 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label2 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label3 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label4 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label5 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label6 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label7 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label8 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label9 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label10 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label11 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label12 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label13 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label14 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) all_loss = tf.placeholder(tf.float32, name='loss') is_training = tf.placeholder(tf.bool, name='is_training') input_dim = tf.placeholder(tf.int32, name='input_dim') # ave_huber = tf.placeholder(tf.float32, name='huber') multi_stage_densenet = _multi_stage_densenet() y, loss_upsampling11, loss_upsampling22 = multi_stage_densenet.multi_stage_densenet( img_row1=img_row1, img_row2=img_row2, img_row3=img_row3, img_row4=img_row4, img_row5=img_row5, img_row6=img_row6, img_row7=img_row7, img_row8=img_row8, input_dim=input_dim, mri=mri_ph, is_training=is_training) loss_instance = _loss_func() labels = [] labels.append(label1) labels.append(label2) labels.append(label3) labels.append(label4) labels.append(label5) labels.append(label6) labels.append(label7) labels.append(label8) labels.append(label9) labels.append(label10) labels.append(label11) labels.append(label12) labels.append(label13) labels.append(label14) logits = [] logits.append(y[:, :, :, :, 0, np.newaxis]) logits.append(y[:, :, :, :, 1, np.newaxis]) logits.append(y[:, :, :, :, 2, np.newaxis]) logits.append(y[:, :, :, :, 3, np.newaxis]) logits.append(y[:, :, :, :, 4, np.newaxis]) logits.append(y[:, :, :, :, 5, np.newaxis]) logits.append(y[:, :, :, :, 6, np.newaxis]) logits.append(y[:, :, :, :, 7, np.newaxis]) logits.append(y[:, :, :, :, 8, np.newaxis]) logits.append(y[:, :, :, :, 9, np.newaxis]) logits.append(y[:, :, :, :, 10, np.newaxis]) logits.append(y[:, :, :, :, 11, np.newaxis]) logits.append(y[:, :, :, :, 12, np.newaxis]) logits.append(y[:, :, :, :, 13, np.newaxis]) stage1 = [] stage1.append(loss_upsampling11[:, :, :, :, 0, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 1, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 2, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 3, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 4, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 5, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 6, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 7, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 8, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 9, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 10, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 11, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 12, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 13, np.newaxis]) stage2 = [] stage2.append(loss_upsampling22[:, :, :, :, 0, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 1, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 2, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 3, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 4, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 5, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 6, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 7, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 8, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 9, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 10, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 11, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 12, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 13, np.newaxis]) with tf.name_scope('Loss'): loss_dic = loss_instance.loss_selector( 'Multistage_ssim_perf_angio_loss', labels=labels, logits=logits, stage1=stage1, stage2=stage2) cost = tf.reduce_mean(loss_dic["loss"], name="cost") # cost_angio = tf.reduce_mean(loss_dic["angio_SSIM"], name="angio_SSIM") # cost_perf = tf.reduce_mean(loss_dic["perf_SSIM"], name="perf_SSIM") # ======================================================================== # ave_loss = tf.placeholder(tf.float32, name='loss') # ave_loss_perf = tf.placeholder(tf.float32, name='loss_perf') # ave_loss_angio = tf.placeholder(tf.float32, name='loss_angio') # # average_gradient_perf = tf.placeholder(tf.float32, name='grad_ave_perf') # average_gradient_angio = tf.placeholder(tf.float32, name='grad_ave_angio') # # ave_huber = tf.placeholder(tf.float32, name='huber') # restore the model sess = tf.Session() saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(chckpnt_dir) saver.restore(sess, ckpt.model_checkpoint_path) copyfile('./test_synthesize_multistage_perf_angio.py', result_path + '/test_synthesize_multistage_perf_angio.py') _image_class = image_class(train_data, bunch_of_images_no=1, is_training=1, patch_window=patch_window, sample_no_per_bunch=1, label_patch_size=label_patchs_size, validation_total_sample=0) learning_rate = 1E-5 extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(extra_update_ops): optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost) # init = tf.global_variables_initializer() dic_perf0 = [] dic_perf1 = [] dic_perf2 = [] dic_perf3 = [] dic_perf4 = [] dic_perf5 = [] dic_perf6 = [] dic_angio0 = [] dic_angio1 = [] dic_angio2 = [] dic_angio3 = [] dic_angio4 = [] dic_angio5 = [] dic_angio6 = [] loss = 0 Elapsed = [] for img_indx in range(2): #len(test_set)): crush, noncrush, perf, angio, mri, segmentation_, spacing, direction, origin = _image_class.read_image_for_test( test_set=test_set, img_indx=img_indx, input_size=in_dim, final_layer=final_layer) t = time.time() [out] = sess.run( [y], feed_dict={ img_row1: np.expand_dims(np.expand_dims(crush[0], 0), -1), img_row2: np.expand_dims(np.expand_dims(noncrush[1], 0), -1), img_row3: np.expand_dims(np.expand_dims(crush[2], 0), -1), img_row4: np.expand_dims(np.expand_dims(noncrush[3], 0), -1), img_row5: np.expand_dims(np.expand_dims(crush[4], 0), -1), img_row6: np.expand_dims(np.expand_dims(noncrush[5], 0), -1), img_row7: np.expand_dims(np.expand_dims(crush[6], 0), -1), img_row8: np.expand_dims(np.expand_dims(noncrush[7], 0), -1), mri_ph: np.expand_dims(np.expand_dims(mri, 0), -1), label1: np.expand_dims(np.expand_dims(perf[0], 0), -1), label2: np.expand_dims(np.expand_dims(perf[1], 0), -1), label3: np.expand_dims(np.expand_dims(perf[2], 0), -1), label4: np.expand_dims(np.expand_dims(perf[3], 0), -1), label5: np.expand_dims(np.expand_dims(perf[4], 0), -1), label6: np.expand_dims(np.expand_dims(perf[5], 0), -1), label7: np.expand_dims(np.expand_dims(perf[6], 0), -1), label8: np.expand_dims(np.expand_dims(angio[0], 0), -1), label9: np.expand_dims(np.expand_dims(angio[1], 0), -1), label10: np.expand_dims(np.expand_dims(angio[2], 0), -1), label11: np.expand_dims(np.expand_dims(angio[3], 0), -1), label12: np.expand_dims(np.expand_dims(angio[4], 0), -1), label13: np.expand_dims(np.expand_dims(angio[5], 0), -1), label14: np.expand_dims(np.expand_dims(angio[6], 0), -1), is_training: False, input_dim: patch_window, all_loss: -1., }) elapsed = time.time() - t Elapsed.append(elapsed) print(elapsed) for i in range(np.shape(out)[-1]): image = out[0, :, :, :, i] sitk_image = sitk.GetImageFromArray(image) res_dir = test_set[img_indx][0][0].split('/')[-2] if i == 0: os.mkdir(parent_path + 'results/' + res_dir) if i < 7: nm = 'perf' else: nm = 'angi' sitk_image.SetDirection(direction=direction) sitk_image.SetOrigin(origin=origin) sitk_image.SetSpacing(spacing=spacing) sitk.WriteImage( sitk_image, parent_path + 'results/' + res_dir + '/' + nm + '_' + str(i % 7) + '.mha') print(parent_path + 'results/' + res_dir + '/' + nm + '_' + str(i % 7) + '.mha done!') for i in range(7): if i == 0: os.mkdir(parent_path + 'results/' + res_dir + '/GT/') sitk_angio = sitk.GetImageFromArray(angio[i]) sitk_angio.SetDirection(direction=direction) sitk_angio.SetOrigin(origin=origin) sitk_angio.SetSpacing(spacing=spacing) sitk.WriteImage( sitk_angio, parent_path + 'results/' + res_dir + '/GT/angio_' + str(i) + '.mha') sitk_perf = sitk.GetImageFromArray(perf[i]) sitk_perf.SetDirection(direction=direction) sitk_perf.SetOrigin(origin=origin) sitk_perf.SetSpacing(spacing=spacing) sitk.WriteImage( sitk_perf, parent_path + 'results/' + res_dir + '/GT/perf_' + str(i) + '.mha') dic_perf0.append( anly.analysis(out[0, :, :, :, 0], perf[i], 0, max_perf)) dic_perf1.append( anly.analysis(out[0, :, :, :, 1], perf[i], 0, max_perf)) dic_perf2.append( anly.analysis(out[0, :, :, :, 2], perf[i], 0, max_perf)) dic_perf3.append( anly.analysis(out[0, :, :, :, 3], perf[i], 0, max_perf)) dic_perf4.append( anly.analysis(out[0, :, :, :, 4], perf[i], 0, max_perf)) dic_perf5.append( anly.analysis(out[0, :, :, :, 5], perf[i], 0, max_perf)) dic_perf6.append( anly.analysis(out[0, :, :, :, 6], perf[i], 0, max_perf)) dic_angio0.append( anly.analysis(out[0, :, :, :, 7], angio[i], 0, max_angio)) dic_angio1.append( anly.analysis(out[0, :, :, :, 8], angio[i], 0, max_angio)) dic_angio2.append( anly.analysis(out[0, :, :, :, 9], angio[i], 0, max_angio)) dic_angio3.append( anly.analysis(out[0, :, :, :, 10], angio[i], 0, max_angio)) dic_angio4.append( anly.analysis(out[0, :, :, :, 11], angio[i], 0, max_angio)) dic_angio5.append( anly.analysis(out[0, :, :, :, 12], angio[i], 0, max_angio)) dic_angio6.append( anly.analysis(out[0, :, :, :, 13], angio[i], 0, max_angio)) if img_indx == 0: headers = dic_perf0[0].keys() dics = [ dic_perf0, dic_perf1, dic_perf2, dic_perf3, dic_perf4, dic_perf5, dic_perf6, dic_angio0, dic_angio1, dic_angio2, dic_angio3, dic_angio4, dic_angio5, dic_angio6 ] # print(np.mean(Elapsed)) # print(np.std(Elapsed)) save_in_xlsx(parent_path, headers, dics=dics) print('Total loss: ', loss / len(test_set))
dic_angio3 = [] dic_angio4 = [] dic_angio5 = [] dic_angio6 = [] max_angio = 2.99771428108 max_perf = 17.0151833445 type=["/angi_","/perf_"] gt_type=["/angio_","/perf_"] for i in range(2,len(data_dir)): for j in range(7): for k in range(2): output= sitk.GetArrayFromImage(sitk.ReadImage(data_dir[i] + type[k] +str(j)+ ".mha" )) GT= sitk.GetArrayFromImage(sitk.ReadImage(data_dir[i] +"/GT"+ gt_type[k] +str(j)+ ".mha" )) if k==1: if j==0: dic_perf0.append(anly.analysis(output,GT, 0, max_perf)) elif j==1: dic_perf1.append(anly.analysis(output, GT, 0, max_perf)) elif j == 2: dic_perf2.append(anly.analysis(output, GT, 0, max_perf)) elif j==3: dic_perf3.append(anly.analysis(output, GT, 0, max_perf)) elif j==4: dic_perf4.append(anly.analysis(output, GT, 0, max_perf)) elif j==5: dic_perf5.append(anly.analysis(output, GT, 0, max_perf)) elif j==6: dic_perf6.append(anly.analysis(output, GT, 0, max_perf)) else: if j==0: dic_angio0.append(anly.analysis(output,GT, 0, max_angio))
import functions.analysis as anly import SimpleITK as sitk if __name__ == '__main__': path = '/srv/2-lkeb-17-dl01/syousefi/TestCode/EsophagusProject/sythesize_code/ASL_LOG/multi_stage/' experiment = 'experiment-2/' results = 'results/' subject = '18_PP5_53_cinema2/' type = 'perf_0.mha' gt_pth = path + experiment + results + subject + 'GT/' + type img_pth = path + experiment + results + subject + type gt = sitk.GetArrayFromImage(sitk.ReadImage(gt_pth)) img = sitk.GetArrayFromImage(sitk.ReadImage(img_pth)) max_angio = 2.99771428108 max_perf = 17.0151833445 print(anly.analysis(img, gt, 0, max_perf))
def test_all_nets(num, test_set): chckpnt_dir = parent_path + 'unet_checkpoints/' img_row1 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row2 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row3 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row4 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row5 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row6 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row7 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) img_row8 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) mri_ph = tf.placeholder(tf.float32, shape=[None, None, None, None, 1], name='mri') label1 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label2 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label3 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label4 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label5 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label6 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label7 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label8 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label9 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label10 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label11 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label12 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label13 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) label14 = tf.placeholder(tf.float32, shape=[None, None, None, None, 1]) all_loss = tf.placeholder(tf.float32, name='loss') is_training = tf.placeholder(tf.bool, name='is_training') input_dim = tf.placeholder(tf.int32, name='input_dim') multi_stage_densenet = _multi_stage_densenet() y, loss_upsampling11, loss_upsampling22 = multi_stage_densenet.multi_stage_densenet( img_row1=img_row1, img_row2=img_row2, img_row3=img_row3, img_row4=img_row4, img_row5=img_row5, img_row6=img_row6, img_row7=img_row7, img_row8=img_row8, input_dim=input_dim, mri=mri_ph, is_training=is_training) loss_instance = _loss_func() labels = [] labels.append(label1) labels.append(label2) labels.append(label3) labels.append(label4) labels.append(label5) labels.append(label6) labels.append(label7) labels.append(label8) labels.append(label9) labels.append(label10) labels.append(label11) labels.append(label12) labels.append(label13) labels.append(label14) logits = [] logits.append(y[:, :, :, :, 0, np.newaxis]) logits.append(y[:, :, :, :, 1, np.newaxis]) logits.append(y[:, :, :, :, 2, np.newaxis]) logits.append(y[:, :, :, :, 3, np.newaxis]) logits.append(y[:, :, :, :, 4, np.newaxis]) logits.append(y[:, :, :, :, 5, np.newaxis]) logits.append(y[:, :, :, :, 6, np.newaxis]) logits.append(y[:, :, :, :, 7, np.newaxis]) logits.append(y[:, :, :, :, 8, np.newaxis]) logits.append(y[:, :, :, :, 9, np.newaxis]) logits.append(y[:, :, :, :, 10, np.newaxis]) logits.append(y[:, :, :, :, 11, np.newaxis]) logits.append(y[:, :, :, :, 12, np.newaxis]) logits.append(y[:, :, :, :, 13, np.newaxis]) stage1 = [] stage1.append(loss_upsampling11[:, :, :, :, 0, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 1, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 2, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 3, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 4, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 5, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 6, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 7, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 8, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 9, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 10, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 11, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 12, np.newaxis]) stage1.append(loss_upsampling11[:, :, :, :, 13, np.newaxis]) stage2 = [] stage2.append(loss_upsampling22[:, :, :, :, 0, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 1, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 2, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 3, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 4, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 5, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 6, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 7, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 8, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 9, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 10, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 11, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 12, np.newaxis]) stage2.append(loss_upsampling22[:, :, :, :, 13, np.newaxis]) with tf.name_scope('Loss'): loss_dic = loss_instance.loss_selector( 'Multistage_ssim_perf_angio_loss', labels=labels, logits=logits, stage1=stage1, stage2=stage2) cost = tf.reduce_mean(loss_dic["loss"], name="cost") sess = tf.Session() saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(chckpnt_dir) model_path = ckpt.model_checkpoint_path.rsplit( '/', 1)[0] + '/unet_inter_epoch0_point' + str(num) + '.ckpt-' + str(num) saver.restore(sess, model_path) # copyfile('./test_synthesize_multistage_perf_angio.py', result_path + '/test_synthesize_multistage_perf_angio.py') _image_class = image_class(train_data, bunch_of_images_no=1, is_training=1, patch_window=patch_window, sample_no_per_bunch=1, label_patch_size=label_patchs_size, validation_total_sample=0) learning_rate = 1E-5 extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(extra_update_ops): optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost) # init = tf.global_variables_initializer() loss = 0 for img_indx in range(len(test_set[0:1])): crush, noncrush, perf, angio, mri, segmentation_, spacing, direction, origin = _image_class.read_image_for_test( test_set=test_set, img_indx=img_indx, input_size=in_dim, final_layer=final_layer) [out] = sess.run( [y], feed_dict={ img_row1: np.expand_dims(np.expand_dims(crush[0], 0), -1), img_row2: np.expand_dims(np.expand_dims(noncrush[1], 0), -1), img_row3: np.expand_dims(np.expand_dims(crush[2], 0), -1), img_row4: np.expand_dims(np.expand_dims(noncrush[3], 0), -1), img_row5: np.expand_dims(np.expand_dims(crush[4], 0), -1), img_row6: np.expand_dims(np.expand_dims(noncrush[5], 0), -1), img_row7: np.expand_dims(np.expand_dims(crush[6], 0), -1), img_row8: np.expand_dims(np.expand_dims(noncrush[7], 0), -1), mri_ph: np.expand_dims(np.expand_dims(mri, 0), -1), label1: np.expand_dims(np.expand_dims(perf[0], 0), -1), label2: np.expand_dims(np.expand_dims(perf[1], 0), -1), label3: np.expand_dims(np.expand_dims(perf[2], 0), -1), label4: np.expand_dims(np.expand_dims(perf[3], 0), -1), label5: np.expand_dims(np.expand_dims(perf[4], 0), -1), label6: np.expand_dims(np.expand_dims(perf[5], 0), -1), label7: np.expand_dims(np.expand_dims(perf[6], 0), -1), label8: np.expand_dims(np.expand_dims(angio[0], 0), -1), label9: np.expand_dims(np.expand_dims(angio[1], 0), -1), label10: np.expand_dims(np.expand_dims(angio[2], 0), -1), label11: np.expand_dims(np.expand_dims(angio[3], 0), -1), label12: np.expand_dims(np.expand_dims(angio[4], 0), -1), label13: np.expand_dims(np.expand_dims(angio[5], 0), -1), label14: np.expand_dims(np.expand_dims(angio[6], 0), -1), is_training: False, input_dim: patch_window, all_loss: -1., }) for i in range(np.shape(out)[-1]): image = out[0, :, :, :, i] gt = sitk.GetArrayFromImage(sitk.GetImageFromArray(perf[i])) dic = analysis.analysis(result=image, gt=gt, min=0, max=max_perf) print(dic) # for i in range(np.shape(out)[-1]): # image = out[0, :, :, :, i] # sitk_image = sitk.GetImageFromArray(image) # res_dir = test_set[img_indx][0][0].split('/')[-2] # if i == 0: # os.mkdir(parent_path + 'results/' + res_dir) # if i < 7: # nm = 'perf' # else: # nm = 'angi' # sitk_image.SetDirection(direction=direction) # sitk_image.SetOrigin(origin=origin) # sitk_image.SetSpacing(spacing=spacing) # sitk.WriteImage(sitk_image, parent_path + 'results/' + res_dir + '/' + nm + '_' + str(i % 7) + '.mha') # print(parent_path + 'results/' + res_dir + '/' + nm + '_' + str(i % 7) + '.mha done!') # for i in range(7): # if i == 0: # os.mkdir(parent_path + 'results/' + res_dir + '/GT/') # sitk_angio = sitk.GetImageFromArray(angio[i]) # sitk_angio.SetDirection(direction=direction) # sitk_angio.SetOrigin(origin=origin) # sitk_angio.SetSpacing(spacing=spacing) # sitk.WriteImage(sitk_angio, parent_path + 'results/' + res_dir + '/GT/angio_' + str(i) + '.mha') # # sitk_perf = sitk.GetImageFromArray(perf[i]) # sitk_perf.SetDirection(direction=direction) # sitk_perf.SetOrigin(origin=origin) # sitk_perf.SetSpacing(spacing=spacing) # sitk.WriteImage(sitk_perf, parent_path + 'results/' + res_dir + '/GT/perf_' + str(i) + '.mha') a = 1 # plt.imshow(out[0, int(gt_cube_size / 2), :, :, 0]) # plt.figure() # loss += loss_train1 # print('Loss_train: ', loss_train1) print('Total loss: ', loss / len(test_set))
def test_all_nets(): data = 2 Server = 'DL' if Server == 'DL': parent_path = '/srv/2-lkeb-17-dl01/syousefi/TestCode/EsophagusProject/sythesize_code/ASL_LOG/Log_perceptual/regularization/perceptual-0/' data_path = '/srv/2-lkeb-17-dl01/syousefi/TestCode/EsophagusProject/Data-01/BrainWeb_permutation00_low/' else: parent_path = '/exports/lkeb-hpc/syousefi/Code/' data_path = '/srv/2-lkeb-17-dl01/syousefi/TestCode/EsophagusProject/Data-01/BrainWeb_permutation00_low/' img_name = '' label_name = '' _rd = _read_data(data=data, reverse=False, img_name=img_name, label_name=label_name, dataset_path=data_path) '''read path of the images for train, test, and validation''' train_data, validation_data, test_data = _rd.read_data_path() chckpnt_dir = parent_path + 'unet_checkpoints/' result_path = parent_path + 'results/' batch_no = 1 batch_no_validation = batch_no # label_patchs_size = 87#39 # 63 # patch_window = 103#53 # 77#89 if test_vali == 1: test_set = validation_data else: test_set = test_data # =================================================================================== img_row1 = tf.placeholder( tf.float32, shape=[batch_no, patch_window, patch_window, patch_window, 1], name='img_row1') img_row2 = tf.placeholder( tf.float32, shape=[batch_no, patch_window, patch_window, patch_window, 1], name='img_row2') img_row3 = tf.placeholder( tf.float32, shape=[batch_no, patch_window, patch_window, patch_window, 1], name='img_row3') img_row4 = tf.placeholder( tf.float32, shape=[batch_no, patch_window, patch_window, patch_window, 1], name='img_row4') img_row5 = tf.placeholder( tf.float32, shape=[batch_no, patch_window, patch_window, patch_window, 1], name='img_row5') img_row6 = tf.placeholder( tf.float32, shape=[batch_no, patch_window, patch_window, patch_window, 1], name='img_row6') img_row7 = tf.placeholder( tf.float32, shape=[batch_no, patch_window, patch_window, patch_window, 1], name='img_row7') img_row8 = tf.placeholder( tf.float32, shape=[batch_no, patch_window, patch_window, patch_window, 1], name='img_row8') mri_ph = tf.placeholder( tf.float32, shape=[batch_no, patch_window, patch_window, patch_window, 1], name='mri') label1 = tf.placeholder(tf.float32, shape=[ batch_no, label_patchs_size, label_patchs_size, label_patchs_size, 1 ], name='label1') label2 = tf.placeholder(tf.float32, shape=[ batch_no, label_patchs_size, label_patchs_size, label_patchs_size, 1 ], name='label2') label3 = tf.placeholder(tf.float32, shape=[ batch_no, label_patchs_size, label_patchs_size, label_patchs_size, 1 ], name='label3') label4 = tf.placeholder(tf.float32, shape=[ batch_no, label_patchs_size, label_patchs_size, label_patchs_size, 1 ], name='label4') label5 = tf.placeholder(tf.float32, shape=[ batch_no, label_patchs_size, label_patchs_size, label_patchs_size, 1 ], name='label5') label6 = tf.placeholder(tf.float32, shape=[ batch_no, label_patchs_size, label_patchs_size, label_patchs_size, 1 ], name='label6') label7 = tf.placeholder(tf.float32, shape=[ batch_no, label_patchs_size, label_patchs_size, label_patchs_size, 1 ], name='label7') label8 = tf.placeholder(tf.float32, shape=[ batch_no, label_patchs_size, label_patchs_size, label_patchs_size, 1 ], name='label8') label9 = tf.placeholder(tf.float32, shape=[ batch_no, label_patchs_size, label_patchs_size, label_patchs_size, 1 ], name='label9') label10 = tf.placeholder(tf.float32, shape=[ batch_no, label_patchs_size, label_patchs_size, label_patchs_size, 1 ], name='label10') label11 = tf.placeholder(tf.float32, shape=[ batch_no, label_patchs_size, label_patchs_size, label_patchs_size, 1 ], name='label11') label12 = tf.placeholder(tf.float32, shape=[ batch_no, label_patchs_size, label_patchs_size, label_patchs_size, 1 ], name='label12') label13 = tf.placeholder(tf.float32, shape=[ batch_no, label_patchs_size, label_patchs_size, label_patchs_size, 1 ], name='label13') label14 = tf.placeholder(tf.float32, shape=[ batch_no, label_patchs_size, label_patchs_size, label_patchs_size, 1 ], name='label14') is_training = tf.placeholder(tf.bool, name='is_training') input_dim = tf.placeholder(tf.int32, name='input_dim') perf_vgg_loss_tens = tf.placeholder(tf.float32, name='VGG_perf') angio_vgg_loss_tens = tf.placeholder(tf.float32, name='VGG_angio') perf_vgg_tens0 = tf.placeholder(tf.float32, name='vgg_perf0') perf_vgg_tens1 = tf.placeholder(tf.float32, name='vgg_perf1') perf_vgg_tens2 = tf.placeholder(tf.float32, name='vgg_perf2') perf_vgg_tens3 = tf.placeholder(tf.float32, name='vgg_perf3') perf_vgg_tens4 = tf.placeholder(tf.float32, name='vgg_perf4') perf_vgg_tens5 = tf.placeholder(tf.float32, name='vgg_perf5') perf_vgg_tens6 = tf.placeholder(tf.float32, name='vgg_perf6') angio_vgg_tens0 = tf.placeholder(tf.float32, name='vgg_angio0') angio_vgg_tens1 = tf.placeholder(tf.float32, name='vgg_angio1') angio_vgg_tens2 = tf.placeholder(tf.float32, name='vgg_angio2') angio_vgg_tens3 = tf.placeholder(tf.float32, name='vgg_angio3') angio_vgg_tens4 = tf.placeholder(tf.float32, name='vgg_angio4') angio_vgg_tens5 = tf.placeholder(tf.float32, name='vgg_angio5') angio_vgg_tens6 = tf.placeholder(tf.float32, name='vgg_angio6') perf_huber_loss_tens = tf.placeholder(tf.float32, name='huber_perf') angio_huber_loss_tens = tf.placeholder(tf.float32, name='huber_angio') perf_huber_tens0 = tf.placeholder(tf.float32, name='huber_perf0') perf_huber_tens1 = tf.placeholder(tf.float32, name='huber_perf1') perf_huber_tens2 = tf.placeholder(tf.float32, name='huber_perf2') perf_huber_tens3 = tf.placeholder(tf.float32, name='huber_perf3') perf_huber_tens4 = tf.placeholder(tf.float32, name='huber_perf4') perf_huber_tens5 = tf.placeholder(tf.float32, name='huber_perf5') perf_huber_tens6 = tf.placeholder(tf.float32, name='huber_perf6') angio_huber_tens0 = tf.placeholder(tf.float32, name='huber_angio0') angio_huber_tens1 = tf.placeholder(tf.float32, name='huber_angio1') angio_huber_tens2 = tf.placeholder(tf.float32, name='huber_angio2') angio_huber_tens3 = tf.placeholder(tf.float32, name='huber_angio3') angio_huber_tens4 = tf.placeholder(tf.float32, name='huber_angio4') angio_huber_tens5 = tf.placeholder(tf.float32, name='huber_angio5') angio_huber_tens6 = tf.placeholder(tf.float32, name='huber_angio6') # =================================================================================== densenet = _densenet() [y, _] = densenet.densenet(img_row1=img_row1, img_row2=img_row2, img_row3=img_row3, img_row4=img_row4, img_row5=img_row5, img_row6=img_row6, img_row7=img_row7, img_row8=img_row8, input_dim=input_dim, is_training=is_training) vgg = vgg_feature_maker(test=1) feature_type = 'huber' vgg_y0 = vgg.feed_img(y[:, :, :, :, 0], feature_type=feature_type).copy() vgg_y1 = vgg.feed_img(y[:, :, :, :, 1], feature_type=feature_type).copy() vgg_y2 = vgg.feed_img(y[:, :, :, :, 2], feature_type=feature_type).copy() vgg_y3 = vgg.feed_img(y[:, :, :, :, 3], feature_type=feature_type).copy() vgg_y4 = vgg.feed_img(y[:, :, :, :, 4], feature_type=feature_type).copy() vgg_y5 = vgg.feed_img(y[:, :, :, :, 5], feature_type=feature_type).copy() vgg_y6 = vgg.feed_img(y[:, :, :, :, 6], feature_type=feature_type).copy() vgg_y7 = vgg.feed_img(y[:, :, :, :, 7], feature_type=feature_type) vgg_y8 = vgg.feed_img(y[:, :, :, :, 8], feature_type=feature_type) vgg_y9 = vgg.feed_img(y[:, :, :, :, 9], feature_type=feature_type) vgg_y10 = vgg.feed_img(y[:, :, :, :, 10], feature_type=feature_type) vgg_y11 = vgg.feed_img(y[:, :, :, :, 11], feature_type=feature_type) vgg_y12 = vgg.feed_img(y[:, :, :, :, 12], feature_type=feature_type) vgg_y13 = vgg.feed_img(y[:, :, :, :, 13], feature_type=feature_type) vgg_label0 = vgg.feed_img(label1[:, :, :, :, 0], feature_type=feature_type).copy() vgg_label1 = vgg.feed_img(label2[:, :, :, :, 0], feature_type=feature_type).copy() vgg_label2 = vgg.feed_img(label3[:, :, :, :, 0], feature_type=feature_type).copy() vgg_label3 = vgg.feed_img(label4[:, :, :, :, 0], feature_type=feature_type).copy() vgg_label4 = vgg.feed_img(label5[:, :, :, :, 0], feature_type=feature_type).copy() vgg_label5 = vgg.feed_img(label6[:, :, :, :, 0], feature_type=feature_type).copy() vgg_label6 = vgg.feed_img(label7[:, :, :, :, 0], feature_type=feature_type).copy() vgg_label7 = vgg.feed_img(label8[:, :, :, :, 0], feature_type=feature_type) vgg_label8 = vgg.feed_img(label9[:, :, :, :, 0], feature_type=feature_type) vgg_label9 = vgg.feed_img(label10[:, :, :, :, 0], feature_type=feature_type) vgg_label10 = vgg.feed_img(label11[:, :, :, :, 0], feature_type=feature_type) vgg_label11 = vgg.feed_img(label12[:, :, :, :, 0], feature_type=feature_type) vgg_label12 = vgg.feed_img(label13[:, :, :, :, 0], feature_type=feature_type) vgg_label13 = vgg.feed_img(label14[:, :, :, :, 0], feature_type=feature_type) all_loss = tf.placeholder(tf.float32, name='loss') # is_training = tf.placeholder(tf.bool, name='is_training') # input_dim = tf.placeholder(tf.int32, name='input_dim') # ave_huber = tf.placeholder(tf.float32, name='huber') labels = [] labels.append(label1) labels.append(label2) labels.append(label3) labels.append(label4) labels.append(label5) labels.append(label6) labels.append(label7) labels.append(label8) labels.append(label9) labels.append(label10) labels.append(label11) labels.append(label12) labels.append(label13) labels.append(label14) logits = [] logits.append(y[:, :, :, :, 0, np.newaxis]) logits.append(y[:, :, :, :, 1, np.newaxis]) logits.append(y[:, :, :, :, 2, np.newaxis]) logits.append(y[:, :, :, :, 3, np.newaxis]) logits.append(y[:, :, :, :, 4, np.newaxis]) logits.append(y[:, :, :, :, 5, np.newaxis]) logits.append(y[:, :, :, :, 6, np.newaxis]) logits.append(y[:, :, :, :, 7, np.newaxis]) logits.append(y[:, :, :, :, 8, np.newaxis]) logits.append(y[:, :, :, :, 9, np.newaxis]) logits.append(y[:, :, :, :, 10, np.newaxis]) logits.append(y[:, :, :, :, 11, np.newaxis]) logits.append(y[:, :, :, :, 12, np.newaxis]) logits.append(y[:, :, :, :, 13, np.newaxis]) loss_instance = _loss_func() vgg_in_feature = [] vgg_in_feature.append(vgg_y0) vgg_in_feature.append(vgg_y1) vgg_in_feature.append(vgg_y2) vgg_in_feature.append(vgg_y3) vgg_in_feature.append(vgg_y4) vgg_in_feature.append(vgg_y5) vgg_in_feature.append(vgg_y6) vgg_in_feature.append(vgg_y7) vgg_in_feature.append(vgg_y8) vgg_in_feature.append(vgg_y9) vgg_in_feature.append(vgg_y10) vgg_in_feature.append(vgg_y11) vgg_in_feature.append(vgg_y12) vgg_in_feature.append(vgg_y13) vgg_label_feature = [] vgg_label_feature.append(vgg_label0) vgg_label_feature.append(vgg_label1) vgg_label_feature.append(vgg_label2) vgg_label_feature.append(vgg_label3) vgg_label_feature.append(vgg_label4) vgg_label_feature.append(vgg_label5) vgg_label_feature.append(vgg_label6) vgg_label_feature.append(vgg_label7) vgg_label_feature.append(vgg_label8) vgg_label_feature.append(vgg_label9) vgg_label_feature.append(vgg_label10) vgg_label_feature.append(vgg_label11) vgg_label_feature.append(vgg_label12) vgg_label_feature.append(vgg_label13) with tf.name_scope('Loss'): loss_dic = loss_instance.loss_selector( 'content_vgg_pairwise_loss_huber', labels=vgg_label_feature, logits=vgg_in_feature, vgg=vgg, h_labels=labels, h_logits=logits) cost = tf.reduce_mean(loss_dic["loss"], name="cost") # cost_angio = tf.reduce_mean(loss_dic["angio_SSIM"], name="angio_SSIM") # cost_perf = tf.reduce_mean(loss_dic["perf_SSIM"], name="perf_SSIM") # ======================================================================== # ave_loss = tf.placeholder(tf.float32, name='loss') # ave_loss_perf = tf.placeholder(tf.float32, name='loss_perf') # ave_loss_angio = tf.placeholder(tf.float32, name='loss_angio') # # average_gradient_perf = tf.placeholder(tf.float32, name='grad_ave_perf') # average_gradient_angio = tf.placeholder(tf.float32, name='grad_ave_angio') # # ave_huber = tf.placeholder(tf.float32, name='huber') # restore the model sess = tf.Session() saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(chckpnt_dir) saver.restore(sess, ckpt.model_checkpoint_path) copyfile('./test_synthesize_ssim_perf_angio.py', result_path + '/test_synthesize_ssim_perf_angio.py') _image_class = image_class(train_data, bunch_of_images_no=1, is_training=1, patch_window=patch_window, sample_no_per_bunch=1, label_patch_size=label_patchs_size, validation_total_sample=0) learning_rate = 1E-5 extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(extra_update_ops): optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost) # init = tf.global_variables_initializer() dic_perf0 = [] dic_perf1 = [] dic_perf2 = [] dic_perf3 = [] dic_perf4 = [] dic_perf5 = [] dic_perf6 = [] dic_angio0 = [] dic_angio1 = [] dic_angio2 = [] dic_angio3 = [] dic_angio4 = [] dic_angio5 = [] dic_angio6 = [] loss = 0 for img_indx in range(len(test_set)): crush, noncrush, perf, angio, mri, segmentation_, spacing, direction, origin = _image_class.read_image_for_test( test_set=test_set, img_indx=img_indx, input_size=in_dim, final_layer=final_layer) [out] = sess.run( [y], feed_dict={ img_row1: np.expand_dims(np.expand_dims(crush[0], 0), -1), img_row2: np.expand_dims(np.expand_dims(noncrush[1], 0), -1), img_row3: np.expand_dims(np.expand_dims(crush[2], 0), -1), img_row4: np.expand_dims(np.expand_dims(noncrush[3], 0), -1), img_row5: np.expand_dims(np.expand_dims(crush[4], 0), -1), img_row6: np.expand_dims(np.expand_dims(noncrush[5], 0), -1), img_row7: np.expand_dims(np.expand_dims(crush[6], 0), -1), img_row8: np.expand_dims(np.expand_dims(noncrush[7], 0), -1), mri_ph: np.expand_dims(np.expand_dims(mri, 0), -1), label1: np.expand_dims(np.expand_dims(perf[0], 0), -1), label2: np.expand_dims(np.expand_dims(perf[1], 0), -1), label3: np.expand_dims(np.expand_dims(perf[2], 0), -1), label4: np.expand_dims(np.expand_dims(perf[3], 0), -1), label5: np.expand_dims(np.expand_dims(perf[4], 0), -1), label6: np.expand_dims(np.expand_dims(perf[5], 0), -1), label7: np.expand_dims(np.expand_dims(perf[6], 0), -1), label8: np.expand_dims(np.expand_dims(angio[0], 0), -1), label9: np.expand_dims(np.expand_dims(angio[1], 0), -1), label10: np.expand_dims(np.expand_dims(angio[2], 0), -1), label11: np.expand_dims(np.expand_dims(angio[3], 0), -1), label12: np.expand_dims(np.expand_dims(angio[4], 0), -1), label13: np.expand_dims(np.expand_dims(angio[5], 0), -1), label14: np.expand_dims(np.expand_dims(angio[6], 0), -1), is_training: False, input_dim: patch_window, all_loss: -1., angio_vgg_loss_tens: -1, # vgg angio perf_vgg_loss_tens: -1, perf_vgg_tens0: -1, perf_vgg_tens1: -1, perf_vgg_tens2: -1, perf_vgg_tens3: -1, perf_vgg_tens4: -1, perf_vgg_tens5: -1, perf_vgg_tens6: -1, angio_vgg_tens0: -1, angio_vgg_tens1: -1, angio_vgg_tens2: -1, angio_vgg_tens3: -1, angio_vgg_tens4: -1, angio_vgg_tens5: -1, angio_vgg_tens6: -1, perf_huber_loss_tens: -1, angio_huber_loss_tens: -1, perf_huber_tens0: -1, perf_huber_tens1: -1, perf_huber_tens2: -1, perf_huber_tens3: -1, perf_huber_tens4: -1, perf_huber_tens5: -1, perf_huber_tens6: -1, angio_huber_tens0: -1, angio_huber_tens1: -1, angio_huber_tens2: -1, angio_huber_tens3: -1, angio_huber_tens4: -1, angio_huber_tens5: -1, angio_huber_tens6: -1, }) for i in range(np.shape(out)[-1]): image = out[0, :, :, :, i] sitk_image = sitk.GetImageFromArray(image) res_dir = test_set[img_indx][0][0].split('/')[-2] if i == 0: os.mkdir(parent_path + 'results/' + res_dir) if i < 7: nm = 'perf' else: nm = 'angi' sitk_image.SetDirection(direction=direction) sitk_image.SetOrigin(origin=origin) sitk_image.SetSpacing(spacing=spacing) sitk.WriteImage( sitk_image, parent_path + 'results/' + res_dir + '/' + nm + '_' + str(i % 7) + '.mha') print(parent_path + 'results/' + res_dir + '/' + nm + '_' + str(i % 7) + '.mha done!') for i in range(7): if i == 0: os.mkdir(parent_path + 'results/' + res_dir + '/GT/') sitk_angio = sitk.GetImageFromArray(angio[i]) sitk_angio.SetDirection(direction=direction) sitk_angio.SetOrigin(origin=origin) sitk_angio.SetSpacing(spacing=spacing) sitk.WriteImage( sitk_angio, parent_path + 'results/' + res_dir + '/GT/angio_' + str(i) + '.mha') sitk_perf = sitk.GetImageFromArray(perf[i]) sitk_perf.SetDirection(direction=direction) sitk_perf.SetOrigin(origin=origin) sitk_perf.SetSpacing(spacing=spacing) sitk.WriteImage( sitk_perf, parent_path + 'results/' + res_dir + '/GT/perf_' + str(i) + '.mha') a = 1 dic_perf0.append( anly.analysis(out[0, :, :, :, 0], perf[i], 0, max_perf)) dic_perf1.append( anly.analysis(out[0, :, :, :, 1], perf[i], 0, max_perf)) dic_perf2.append( anly.analysis(out[0, :, :, :, 2], perf[i], 0, max_perf)) dic_perf3.append( anly.analysis(out[0, :, :, :, 3], perf[i], 0, max_perf)) dic_perf4.append( anly.analysis(out[0, :, :, :, 4], perf[i], 0, max_perf)) dic_perf5.append( anly.analysis(out[0, :, :, :, 5], perf[i], 0, max_perf)) dic_perf6.append( anly.analysis(out[0, :, :, :, 6], perf[i], 0, max_perf)) dic_angio0.append( anly.analysis(out[0, :, :, :, 7], angio[i], 0, max_angio)) dic_angio1.append( anly.analysis(out[0, :, :, :, 8], angio[i], 0, max_angio)) dic_angio2.append( anly.analysis(out[0, :, :, :, 9], angio[i], 0, max_angio)) dic_angio3.append( anly.analysis(out[0, :, :, :, 10], angio[i], 0, max_angio)) dic_angio4.append( anly.analysis(out[0, :, :, :, 11], angio[i], 0, max_angio)) dic_angio5.append( anly.analysis(out[0, :, :, :, 12], angio[i], 0, max_angio)) dic_angio6.append( anly.analysis(out[0, :, :, :, 13], angio[i], 0, max_angio)) if img_indx == 0: headers = dic_perf0[0].keys() dics = [ dic_perf0, dic_perf1, dic_perf2, dic_perf3, dic_perf4, dic_perf5, dic_perf6, dic_angio0, dic_angio1, dic_angio2, dic_angio3, dic_angio4, dic_angio5, dic_angio6 ] save_in_xlsx(parent_path, headers, dics=dics) # plt.imshow(out[0, int(gt_cube_size / 2), :, :, 0]) # plt.figure() # loss += loss_train1 # print('Loss_train: ', loss_train1) print('Total loss: ', loss / len(test_set))