def gen_one_batch(self, genSample, img_fix, lab_fix, img_mv, lab_mv, output_dir): fix_imgs, fix_labs, mv_imgs, mv_labs = genSample.get_batch_data_V2( [img_mv], [img_fix], [lab_mv], [lab_fix]) trainFeed = self.create_feed_dict(fix_imgs, fix_labs, mv_imgs, mv_labs) input_mv_label, \ input_fix_label, \ warp_mv_label, \ input_mv_img, \ input_fix_img, \ warp_mv_img = self.sess.run([self.input_MV_label, self.input_FIX_label, self.warped_MV_label, self.input_MV_image, self.input_FIX_image, self.warped_MV_image], feed_dict=trainFeed) param = sitk.ReadImage(img_fix) sitk_write_image(input_fix_img[0, ...], param, output_dir, get_name_wo_suffix(img_fix)) sitk_write_lab(input_fix_label[0, ...], param, output_dir, get_name_wo_suffix(img_fix).replace('image', 'label')) sitk_write_image(warp_mv_img[0, ...], param, output_dir, get_name_wo_suffix(img_mv)) sitk_write_lab(warp_mv_label[0, ...], param, output_dir, get_name_wo_suffix(img_mv).replace('image', 'label')) ddf_mv_fix = self.sess.run(self.ddf_MV_FIX, feed_dict=trainFeed) _, _, neg2 = neg_jac(ddf_mv_fix[0, ...]) self.logger.debug("neg_jac %d" % (neg2)) ds = calculate_binary_dice(warp_mv_label, input_fix_label) hd = calculate_binary_hd(warp_mv_label, input_fix_label, spacing=param.GetSpacing()) return ds, hd
def validate(self): self.is_train = False init_op = tf.global_variables_initializer() self.saver = tf.train.Saver() self.sess.run(init_op) if self.load(self.args.checkpoint_dir): print(" [*] Load SUCCESS") else: print(" [!] Load failed...") genSample = AbdomenSampler(self.args, 'validate') # self.generator_sample_targetwise(genSample, ) outputdir = self.args.sample_dir + "/atlas_target/" ds_all = [] bf_ds_all = [] jt_all = [] for img_fix, lab_fix in zip(genSample.img_fix, genSample.lab_fix): output_dir = outputdir + get_name_wo_suffix(img_fix) mk_or_cleardir(output_dir) print(output_dir) for img_mv, lab_mv in zip(genSample.img_mv, genSample.lab_mv): bfds, ds = self.gen_one_batch(genSample, img_fix, lab_fix, img_mv, lab_mv, output_dir) # print("sim= %f"%ds) ds_all.append(ds) bf_ds_all.append(bfds) # print(ds_all) self.logger.debug("%s total_sim %s->%s " % (Get_Name_By_Index( self.args.component), self.args.Tatlas, self.args.Ttarget)) print_mean_and_std(bf_ds_all) print_mean_and_std(ds_all)
def generator_sample_atlaswise(self, genSample, outputdir): for img_mv, lab_mv in zip(genSample.img_mv, genSample.lab_mv): output_dir = outputdir + get_name_wo_suffix(img_mv) mk_or_cleardir(output_dir) print(output_dir) for img_fix, lab_fix in zip(genSample.img_fix, genSample.lab_fix): self.gen_test(genSample, img_fix, lab_fix, img_mv, lab_mv, output_dir)
def generator_sample_targetwise(self, genSample, outputdir): all_ds = [] all_hd = [] for img_fix, lab_fix in zip(genSample.img_fix, genSample.lab_fix): output_dir = outputdir + get_name_wo_suffix(img_fix) mk_or_cleardir(output_dir) print(output_dir) for img_mv, lab_mv in zip(genSample.img_mv, genSample.lab_mv): ds, hd = self.gen_one_batch(genSample, img_fix, lab_fix, img_mv, lab_mv, output_dir) all_ds.append(ds) all_hd.append(hd) outpu2excel(self.args.res_excel, self.args.MOLD_ID + "_DS", all_ds) outpu2excel(self.args.res_excel, self.args.MOLD_ID + "_HD", all_hd)
def gen_one_batch(self, genSample, img_fix, lab_fix, img_mv, lab_mv, output_dir): fix_imgs, fix_labs, mv_imgs, mv_labs = genSample.get_batch_data_V2( [img_mv], [img_fix], [lab_mv], [lab_fix]) feed = self.create_feed_dict(fix_imgs, fix_labs, mv_imgs, mv_labs) input_mv_label, \ input_fix_label, \ warp_mv_label, \ input_mv_img, \ input_fix_img, \ warp_mv_img,\ fw,\ bw= self.sess.run([self.input_MV_label, self.input_FIX_label, self.warped_MV_label, self.input_MV_image, self.input_FIX_image, self.warped_MV_image, self.theta_bw, self.theta_fw], feed_dict=feed) # param=sitk.ReadImage(img_fix) param = None sitk_write_image( input_fix_img[0, ...], param, output_dir, get_name_wo_suffix(img_fix).replace('image', 'target_image')) sitk_write_lab( input_fix_label[0, ...], param, output_dir, get_name_wo_suffix(lab_fix).replace('label', 'target_label')) sitk_write_image( warp_mv_img[0, ...], param, output_dir, get_name_wo_suffix(img_mv).replace('image', 'atlas_image')) sitk_write_lab( warp_mv_label[0, ...], param, output_dir, get_name_wo_suffix(lab_mv).replace('label', 'atlas_label')) resotre_mv, restore_fix = self.sess.run( [self.restore_MV_image, self.restore_FIX_image], feed_dict=feed) sitk_write_image(input_mv_img[0, ...], param, output_dir, get_name_wo_suffix(img_mv)) sitk_write_image( resotre_mv[0, ...], param, output_dir, get_name_wo_suffix(img_mv).replace('image', 'restore')) sitk_write_image( restore_fix[0, ...], param, output_dir, get_name_wo_suffix(img_fix).replace('image', 'restore')) print(fw) # contour= np.where(warp_mv_label> 0.5, 1, 0) # contour= contour.astype(np.uint16) # contour=sitk.GetImageFromArray(np.squeeze(contour)) # contour=sitk.LabelContour(contour,True) # sitk_write_lab(sitk.GetArrayFromImage(contour),param , output_dir,get_name_wo_suffix(img_mv).replace('image','contour')) # ddf_fix_mv, ddf_mv_fix = self.sess.run([self.ddf_FIX_MV, self.ddf_MV_FIX], feed_dict=feed) # _, _, neg1 = neg_jac(ddf_fix_mv[0, ...]) # _, _, neg2 = neg_jac(ddf_mv_fix[0, ...]) ds = calculate_binary_dice(warp_mv_label, input_fix_label) bf_ds = calculate_binary_dice(input_mv_label, input_fix_label) return bf_ds, ds
def fusion_one_target(self, itr): target_img_batch, target_lab_batch, atlas_img_batch, atlas_lab_batch, sim_batch, p_fix_img, p_fix_lab = self.validate_sampler.next_sample_4_fusion( ) sims = [] param = sitk.ReadImage(p_fix_img[0]) sitk_write_lab(np.squeeze(target_lab_batch.astype(np.uint8)), param, dir=self.args.validate_dir, name=get_name_wo_suffix(p_fix_lab[0])) sitk_write_image(np.squeeze(target_img_batch), param, dir=self.args.validate_dir, name=get_name_wo_suffix(p_fix_img[0])) for i in range(atlas_lab_batch.shape[-2]): trainFeed = { self.ph_target_image: target_img_batch, self.ph_target_label: target_lab_batch, self.ph_atlas_label: atlas_lab_batch[..., i, :], self.ph_gt_dicesim: sim_batch[..., i, :] } target_img, atlas_label, gt, target_lab, pred_lab, pred_sim = self.sess.run( [ self.ph_target_image, self.ph_atlas_label, self.ph_gt_dicesim, self.ph_target_label, self.predict_label, self.predict_sim ], feed_dict=trainFeed) sims.append(pred_sim) sitk_write_lab(np.squeeze(atlas_lab_batch[..., i, :]), param, dir=self.args.validate_dir, name=get_name_wo_suffix(p_fix_lab[0].replace( 'label', 'label_' + str(i)))) sitk_write_image(np.squeeze(pred_sim), param, dir=self.args.validate_dir, name=get_name_wo_suffix(p_fix_lab[0]).replace( 'label', 'sim_' + str(i))) sims = np.stack(sims, -1) u_lab = np.unique(target_lab.astype(np.uint8)) LabelStats = np.zeros((len(u_lab), ) + np.squeeze(target_lab).shape) for i, lab in enumerate(u_lab): LabelStats[i] = np.sum( (np.squeeze(atlas_lab_batch) == lab).astype(np.int16) * np.squeeze(sims), axis=-1) fusion_label = u_lab[np.argmax(LabelStats, axis=0)] ds = calculate_binary_dice(fusion_label, target_lab_batch) hd = calculate_binary_hd(fusion_label, target_lab_batch, spacing=param.GetSpacing()) # sitk_write_image(np.squeeze(target_img_batch),param,dir=os.path.dirname(p_fix_lab[0]),name=get_name_wo_suffix(p_fix_img[0])) sitk_write_lab(np.squeeze(fusion_label).astype(np.uint8), param, dir=os.path.dirname(p_fix_lab[0]), name=get_name_wo_suffix(p_fix_lab[0]).replace( 'label', 'net_fusion_label')) return ds, hd
def generator_ROI_mask(args): labs=sort_glob("../../dataset/MMWHS/" + "/%s-test-label/*.nii.gz" % (args.Ttarget)) for p_lab in labs: lab = sitk.ReadImage(p_lab) bbox = get_bounding_box_by_id(sitk.GetArrayFromImage(lab), padding=10,id=None) # sitk.RegionOfInterest(lab,) # crop_lab = crop_by_bbox(lab, bbox) array_lab=sitk.GetArrayFromImage(lab) array_lab[0:,0:,0:]=0 array_lab[bbox[0].start:bbox[0].stop + 1, bbox[1].start:bbox[1].stop + 1,bbox[2].start:bbox[2].stop + 1 ]=1 sitk_write_lab(array_lab,parameter_img=lab,dir='../../dataset/MMWHS'+"/%s-label-test_ROI/"%(args.Ttarget),name=get_name_wo_suffix(p_lab))
def gen_warp_atlas(self, dir, genSample, i, img_fix, lab_fix, is_aug=True): output_dir = self.args.gen_dir + "/" + dir + "/target_" + str( i) + "_" + get_name_wo_suffix(img_fix) mk_or_cleardir(output_dir) params = [] input_fix_imgs = [] input_fix_labels = [] warp_mv_imgs = [] warp_mv_labels = [] losses = [] sims = [] for img_mv, lab_mv in zip(genSample.img_mv, genSample.lab_mv): fix_imgs, fix_labs, mv_imgs, mv_labs = genSample.get_batch_data_V2( [img_mv], [img_fix], [lab_mv], [lab_fix]) trainFeed = self.create_feed_dict(fix_imgs, fix_labs, mv_imgs, mv_labs, is_aug) input_mv_label, \ input_fix_label, \ warp_mv_label, \ input_mv_img, \ input_fix_img, \ warp_mv_img = self.sess.run([self.input_MV_label, self.input_FIX_label, self.warped_MV_label, self.input_MV_image, self.input_FIX_image, self.warped_MV_image], feed_dict=trainFeed) input_fix_label = np.where(input_fix_label > 0.5, 1, 0) warp_mv_label = np.where(warp_mv_label > 0.5, 1, 0) sims.append( calculate_binary_dice(input_fix_label[0, ...], warp_mv_label[0, ...])) param = sitk.ReadImage(img_fix) params.append(param) input_fix_imgs.append(input_fix_img[0, ...]) input_fix_labels.append(input_fix_label[0, ...]) warp_mv_imgs.append(warp_mv_img[0, ...]) warp_mv_labels.append(warp_mv_label[0, ...]) losses.append( conditional_entropy_label_over_image( np.squeeze(input_fix_img[0, ...]), np.squeeze(warp_mv_label[0, ...]))) indexs = np.argsort(losses) for ind in indexs: sitk_write_image(warp_mv_imgs[ind], params[ind], dir=output_dir, name=str(ind) + "_mv_img") sitk_write_lab(warp_mv_labels[ind], params[ind], dir=output_dir, name=str(ind) + "_mv_lab") sitk_write_image(input_fix_imgs[0], params[0], dir=output_dir, name=str(0) + "_fix_img") sitk_write_lab(input_fix_labels[0], params[0], dir=output_dir, name=str(0) + "_fix_lab") self.logger.debug(sims) self.logger.debug("%s %f -> %f" % (output_dir, np.mean(sims), np.mean([sims[ind] for ind in indexs[:5]])))