def predict_all_xmls(args, dirs, wsi, iteration): # reshape regions calc downsample = int(args.downsampleRateLR**.5) region_size = int(args.boxSizeLR * (downsample)) step = int(region_size * (1 - args.overlap_percentLR)) # figure out the number of classes annotatedXMLs = glob(args.base_dir + '/' + args.project + dirs['training_data_dir'] + str(iteration - 1) + '/*.xml') classes = [] for xml in annotatedXMLs: classes.append(get_num_classes(xml)) classNum = max(classes) # chop wsi fileID, test_num_steps, slide = chop_suey(wsi, dirs, downsample, region_size, step, args) dirs['fileID'] = fileID print('Chop SUEY!\n') # call DeepLab for prediction (Low resolution) print('finding Glom locations ...\n') make_folder(dirs['outDir'] + fileID + dirs['img_save_dir'] + 'prediction') modeldir = args.base_dir + '/' + args.project + dirs['modeldir'] + str( iteration) + '/LR' test_steps_LR = get_all_test_steps(modeldir) for idx, test_step in enumerate(test_steps_LR): # test all saved models in current iteration modeldir = args.base_dir + '/' + args.project + dirs['modeldir'] + str( iteration) + '/LR' test_data_list = fileID + '_images' + '.txt' print("\033[1;32;40m" + "starting prediction using model: \n\t" + modeldir + str(test_step) + "\033[0;37;40m" + "\n\n") call([ 'python3', args.base_dir + '/Codes/Deeplab_network/main.py', '--option', 'predict', '--test_data_list', dirs['outDir'] + fileID + dirs['txt_save_dir'] + test_data_list, '--out_dir', dirs['outDir'] + fileID + dirs['img_save_dir'], '--test_step', str(test_step), '--test_num_steps', str(test_num_steps), '--modeldir', modeldir, '--data_dir', dirs['outDir'] + fileID + dirs['img_save_dir'], '--num_classes', str(classNum), '--gpu', '0' ]) # un chop print('\nreconstructing wsi map ...\n') wsiMask = un_suey(dirs=dirs, args=args) # save hotspots if dirs['save_outputs'] == True: make_folder(dirs['outDir'] + fileID + dirs['mask_dir']) print('saving to: ' + dirs['outDir'] + fileID + dirs['mask_dir'] + fileID + '.png') with warnings.catch_warnings(): warnings.simplefilter("ignore") imsave( dirs['outDir'] + fileID + dirs['mask_dir'] + fileID + '.png', wsiMask) # find glom locations in reconstructed map print('\ninterpreting prediction map ...') test_num_steps, labeledArray, label_offsets = find_suey( wsiMask, dirs, downsample, args, wsi) print('\n\nthe cropped regions have been saved to: ' + dirs['outDir'] + fileID + dirs['img_save_dir'] + fileID + dirs['crop_dir']) # call DeepLab to predict Glom boundaries (High resolution) print('\ngetting Glom boundaries ...\n') make_folder(dirs['outDir'] + fileID + dirs['final_output_dir'] + 'prediction') test_data_list = fileID + '_crops.txt' modeldir = args.base_dir + '/' + args.project + dirs['modeldir'] + str( iteration) + '/HR' test_steps_HR = get_all_test_steps(modeldir) if idx > len(test_steps_HR): test_step = test_steps_HR[-1] else: test_step = test_steps_HR[idx] print("\033[1;32;40m" + "starting prediction using model: \n\t" + modeldir + str(test_step) + "\033[0;37;40m" + "\n\n") call([ 'python3', args.base_dir + '/Codes/Deeplab_network/main.py', '--option', 'predict', '--test_data_list', dirs['outDir'] + fileID + dirs['txt_save_dir'] + test_data_list, '--out_dir', dirs['outDir'] + fileID + dirs['final_output_dir'], '--test_step', str(test_step), '--test_num_steps', str(test_num_steps), '--modeldir', modeldir, '--data_dir', dirs['outDir'] + fileID + dirs['img_save_dir'], '--num_classes', str(classNum), '--gpu', '0' ]) print('\nsaving final glom images ...') print('\nworking on:') crop_suey(label_offsets, dirs, args, classNum, iteration, idx) # clean up if dirs['save_outputs'] == False: print('cleaning up') rmtree(dirs['outDir'] + fileID)
def predict_xml(args, dirs, wsi, iteration): # reshape regions calc downsample = int(args.downsampleRateLR**.5) downsample_HR = int(args.downsampleRateHR**.5) region_size = int(args.boxSizeLR*(downsample)) step = int(region_size*(1-args.overlap_percentLR)) # figure out the number of classes if args.classNum == 0: annotatedXMLs=glob(args.base_dir + '/' + args.project + dirs['training_data_dir'] + str(iteration-1) + '/*.xml') classes = [] for xml in annotatedXMLs: classes.append(get_num_classes(xml)) print(classes) print(args.classNum) classNum_LR = max(classes) classNum_HR = max(classes) else: classNum_LR = args.classNum if args.classNum_HR != 0: classNum_HR = args.classNum_HR else: classNum_HR = classNum_LR # chop wsi fileID, test_num_steps, slide = chop_suey(wsi, dirs, downsample, region_size, step, args) dirs['fileID'] = fileID print('Chop SUEY!\n') # call DeepLab for prediction (Low resolution) print('finding Glom locations ...\n') make_folder(dirs['outDir'] + fileID + dirs['img_save_dir'] + 'prediction') test_data_list = fileID + '_images' + '.txt' modeldir = args.base_dir + '/' + args.project + dirs['modeldir'] + str(iteration) + '/LR' test_step = get_test_step(modeldir) print("\033[1;32;40m"+"starting prediction using model: \n\t" + modeldir + str(test_step) + "\033[0;37;40m"+"\n\n") call(['python3', args.base_dir+'/Codes/Deeplab_network/main.py', '--option', 'predict', '--test_data_list', dirs['outDir']+fileID+dirs['txt_save_dir']+test_data_list, '--out_dir', dirs['outDir']+fileID+dirs['img_save_dir'], '--test_step', str(test_step), '--test_num_steps', str(test_num_steps), '--modeldir', modeldir, '--data_dir', dirs['outDir']+fileID+dirs['img_save_dir'], '--num_classes', str(classNum_LR), '--gpu', str(args.gpu)]) # un chop print('\nreconstructing wsi map ...\n') wsiMask = un_suey(dirs=dirs, args=args) # save hotspots if dirs['save_outputs'] == True: make_folder(dirs['outDir'] + fileID + dirs['mask_dir']) print('saving to: ' + dirs['outDir'] + fileID + dirs['mask_dir'] + fileID + '.png') with warnings.catch_warnings(): warnings.simplefilter("ignore") imsave(dirs['outDir'] + fileID + dirs['mask_dir'] + fileID + '.png', wsiMask/np.max(wsiMask)) # find glom locations in reconstructed map print('\ninterpreting prediction map ...') test_num_steps, labeledArray, label_offsets = find_suey(wsiMask, dirs, downsample, args, wsi) print('\n\nthe cropped regions have been saved to: ' + dirs['outDir'] + fileID + dirs['img_save_dir'] + fileID + dirs['crop_dir']) # call DeepLab to predict Glom boundaries (High resolution) print('\ngetting Glom boundaries ...\n') make_folder(dirs['outDir'] + fileID + dirs['final_output_dir'] + 'prediction') test_data_list = fileID + '_crops.txt' modeldir = args.base_dir + '/' + args.project + dirs['modeldir'] + str(iteration) + '/HR' test_step = get_test_step(modeldir) print("\033[1;32;40m"+"starting prediction using model: \n\t" + modeldir + str(test_step) + "\033[0;37;40m"+"\n\n") call(['python3', args.base_dir+'/Codes/Deeplab_network/main.py', '--option', 'predict', '--test_data_list', dirs['outDir']+fileID+dirs['txt_save_dir']+test_data_list, '--out_dir', dirs['outDir']+fileID+dirs['final_output_dir'], '--test_step', str(test_step), '--test_num_steps', str(test_num_steps), '--modeldir', modeldir, '--data_dir', dirs['outDir']+fileID+dirs['img_save_dir'], '--num_classes', str(classNum_HR), '--gpu', str(args.gpu)]) print('\nsaving final glom images ...') print('\nstitching high resolution WSI mask:') wsiMask_HR=unstitch_HR(dirs=dirs,args=args,wsi=wsi) wsidims=wsiMask_HR.shape d1=int(wsidims[0]*(1/args.approx_downsample)) d2=int(wsidims[1]*(1/args.approx_downsample)) if args.approx_downsample!=1: print('\nDownsampling high resolution mask for prediction smoothing...') wsiMask_HR=resize(wsiMask_HR,(d1,d2),order=0,anti_aliasing=False,preserve_range=True) #mask_display=resize(wsiMask_HR,(d1,d2)) #plt.imshow(mask_display/np.max(wsiMask)) #plt.show() print('\nGenerating XML annotations from WSI mask...') make_folder(dirs['outDir'] + dirs['fileID'] + dirs['final_output_dir'] + dirs['final_boundary_image_dir']) crop_suey(wsiMask_HR,label_offsets, dirs, args, classNum_HR, downsample_HR) # clean up if dirs['save_outputs'] == False: print('\ncleaning up') rmtree(dirs['outDir']+fileID)
def predict_xml(args, dirs, wsi, iteration): # reshape regions calc downsample = int(args.downsampleRateHR**.5) region_size = int(args.boxSizeHR * (downsample)) step = int(region_size * (1 - args.overlap_percentHR)) # figure out the number of classes if args.classNum == 0: annotatedXMLs = glob(args.base_dir + '/' + args.project + dirs['training_data_dir'] + str(iteration - 1) + '/*.xml') classes = [] for xml in annotatedXMLs: classes.append(get_num_classes(xml)) classNum = max(classes) else: classNum = args.classNum # chop wsi fileID, test_num_steps = chop_suey(wsi, dirs, downsample, region_size, step, args) dirs['fileID'] = fileID print('Chop SUEY!\n') # call DeepLab for prediction print('Segmenting tissue ...\n') make_folder(dirs['outDir'] + fileID + dirs['img_save_dir'] + 'prediction') test_data_list = fileID + '_images' + '.txt' modeldir = args.base_dir + '/' + args.project + dirs['modeldir'] + str( iteration) + '/HR' test_step = get_test_step(modeldir) print("\033[1;32;40m" + "starting prediction using model: \n\t" + modeldir + '/' + str(test_step) + "\033[0;37;40m" + "\n\n") call([ 'python3', args.base_dir + '/Codes/Deeplab_network/main.py', '--option', 'predict', '--test_data_list', dirs['outDir'] + fileID + dirs['txt_save_dir'] + test_data_list, '--out_dir', dirs['outDir'] + fileID + dirs['img_save_dir'], '--test_step', str(test_step), '--test_num_steps', str(test_num_steps), '--modeldir', modeldir, '--data_dir', dirs['outDir'] + fileID + dirs['img_save_dir'], '--num_classes', str(classNum), '--gpu', str(args.gpu) ]) # un chop print('\nreconstructing wsi map ...\n') wsiMask = un_suey(dirs=dirs, args=args) # save hotspots if dirs['save_outputs'] == True: make_folder(dirs['outDir'] + fileID + dirs['mask_dir']) print('saving to: ' + dirs['outDir'] + fileID + dirs['mask_dir'] + fileID + '.png') with warnings.catch_warnings(): warnings.simplefilter("ignore") imsave( dirs['outDir'] + fileID + dirs['mask_dir'] + fileID + '.png', wsiMask) print('\n\nStarting XML construction: ') xml_suey(wsiMask=wsiMask, dirs=dirs, args=args, classNum=classNum, downsample=downsample) # clean up if dirs['save_outputs'] == False: print('cleaning up') rmtree(dirs['outDir'] + fileID)
def predict_xml(args, dirs, wsi, iteration): # reshape regions calc downsample = int(args.downsampleRateHR**.5) region_size = int(args.boxSizeHR*(downsample)) step = int(region_size*(1-args.overlap_percentHR)) # figure out the number of classes if args.classNum == 0: annotatedXMLs=glob(args.base_dir + '/' + args.project + dirs['training_data_dir'] + str(iteration-1) + '/*.xml') classes = [] for xml in annotatedXMLs: classes.append(get_num_classes(xml)) classNum = max(classes) else: classNum = args.classNum # chop wsi if args.chop_data == 'True': # chop wsi fileID, test_num_steps = chop_suey(wsi, dirs, downsample, region_size, step, args) dirs['fileID'] = fileID print('Chop SUEY!\n') else: basename = os.path.splitext(wsi)[0] if wsi.split('.')[-1] != 'tif': slide=getWsi(wsi) # get image dimensions dim_x, dim_y=slide.dimensions else: im = Image.open(wsi) dim_x, dim_y=im.size fileID=basename.split('/') dirs['fileID'] = fileID=fileID[len(fileID)-1] test_num_steps = file_len(dirs['outDir'] + fileID + dirs['txt_save_dir'] + fileID + '_images' + ".txt") # call DeepLab for prediction print('Segmenting tissue ...\n') make_folder(dirs['outDir'] + fileID + dirs['img_save_dir'] + 'prediction') test_data_list = fileID + '_images' + '.txt' modeldir = args.base_dir + '/' + args.project + dirs['modeldir'] + str(iteration) + '/HR' test_step = get_test_step(modeldir) print("\033[1;32;40m"+"starting prediction using model: \n\t" + modeldir + '/' + str(test_step) + "\033[0;37;40m"+"\n\n") call(['python3.5', args.base_dir+'/Codes/Deeplab_network/main.py', '--option', 'predict', '--test_data_list', dirs['outDir']+fileID+dirs['txt_save_dir']+test_data_list, '--out_dir', dirs['outDir']+fileID+dirs['img_save_dir'], '--test_step', str(test_step), '--test_num_steps', str(test_num_steps), '--modeldir', modeldir, '--data_dir', dirs['outDir']+fileID+dirs['img_save_dir'], '--num_classes', str(classNum), '--gpu', str(args.gpu), '--encoder_name',args.encoder_name]) # un chop print('\nreconstructing wsi map ...\n') wsiMask = un_suey(dirs=dirs, args=args) # save hotspots if dirs['save_outputs'] == True: #reduce the resolution of the image wsidims=wsiMask.shape wsiMask_save=resize(wsiMask,(int(wsidims[0]/4),int(wsidims[1]/4)),order=0,preserve_range=True) make_folder(dirs['outDir'] + fileID + dirs['mask_dir']) print('saving to: ' + dirs['outDir'] + fileID + dirs['mask_dir'] + fileID + '.png') with warnings.catch_warnings(): warnings.simplefilter("ignore") imsave(dirs['outDir'] + fileID + dirs['mask_dir'] + fileID + '.png', wsiMask_save) print('\n\nStarting XML construction: ') xml_suey(wsiMask=wsiMask, dirs=dirs, args=args, classNum=classNum, downsample=downsample) # clean up if dirs['save_outputs'] == False: print('cleaning up') rmtree(dirs['outDir']+fileID)