def pre_segmentation(options): """ Infer segmentation given the input options passed as parameters """ # define the training backend define_backend(options) from CNN.base import test_cascaded_model from CNN.build_model import cascade_model # -------------------------------------------------- # net configuration # take into account if the pretrained models have to be used # all images share the same network model # -------------------------------------------------- options['full_train'] = True options['load_weights'] = True options['weight_paths'] = os.path.join(CURRENT_PATH, 'nets') options['net_verbose'] = 0 model = cascade_model(options) # -------------------------------------------------- # process each of the scans # - image identification # - image registration # - skull-stripping # - WM segmentation # -------------------------------------------------- # scan_list = os.listdir(options['test_folder']) # scan_list.sort() # # check and remove the folder which dose not contain the necessary modalities before prepossessing step # for check in scan_list: # check_oututs(check, options) # update scan list after removing the unnecessary folders before prepossessing step options['task'] = 'testing' scan_list = os.listdir(options['test_folder']) scan_list.sort() for scan in scan_list: total_time = time.time() options['tmp_scan'] = scan # -------------------------------------------------- # move things to a tmp folder before starting # -------------------------------------------------- current_folder = os.path.join(options['test_folder'], scan) options['tmp_folder'] = os.path.normpath( os.path.join(current_folder, 'tmp')) # -------------------------------------------------- # preprocess scans # -------------------------------------------------- preprocess_scan(current_folder, options)
"> ERROR: The OS system", host_os, "is not currently supported" from CNN.base import test_cascaded_model from CNN.build_model import cascade_model # -------------------------------------------------- # net configuration # take into account if the pretrained models have to be used # all images share the same network model # -------------------------------------------------- options['full_train'] = True options['load_weights'] = True options['weight_paths'] = os.path.join(CURRENT_PATH, 'nets') options['net_verbose'] = 0 model = cascade_model(options) # -------------------------------------------------- # process each of the scans # - image identification # - image registration # - skull-stripping # - WM segmentation # -------------------------------------------------- if container: options['test_folder'] = os.path.normpath('/data' + options['test_folder']) else: options['test_folder'] = os.path.normpath(options['test_folder']) # set task to train
def train_network(options): """ Train the CNN network given the options passed as parameter """ # set GPU mode from the configuration file. Trying to update # the backend automatically from here in order to use either theano # or tensorflow backends from CNN.base import train_cascaded_model from CNN.build_model import cascade_model # define the training backend define_backend(options) scan_list = os.listdir(options['train_folder']) scan_list.sort() options['task'] = 'training' options['train_folder'] = os.path.normpath(options['train_folder']) for scan in scan_list: total_time = time.time() # -------------------------------------------------- # move things to a tmp folder before starting # -------------------------------------------------- options['tmp_scan'] = scan current_folder = os.path.join(options['train_folder'], scan) options['tmp_folder'] = os.path.normpath( os.path.join(current_folder, 'tmp')) # preprocess scan preprocess_scan(current_folder, options) # -------------------------------------------------- # WM MS lesion training # - configure net and train # -------------------------------------------------- seg_time = time.time() print "> CNN: Starting training session" # select training scans train_x_data = { f: { m: os.path.join(options['train_folder'], f, 'tmp', n) for m, n in zip(options['modalities'], options['x_names']) } for f in scan_list } train_y_data = { f: os.path.join(options['train_folder'], f, 'tmp', 'lesion.nii.gz') for f in scan_list } options['weight_paths'] = os.path.join(CURRENT_PATH, 'nets') options['load_weights'] = False # train the model for the current scan print "> CNN: training net with %d subjects" % (len(train_x_data.keys())) # -------------------------------------------------- # initialize the CNN and train the classifier # -------------------------------------------------- model = cascade_model(options) model = train_cascaded_model(model, train_x_data, train_y_data, options) print "> INFO: training time:", round(time.time() - seg_time), "sec" print "> INFO: total pipeline time: ", round(time.time() - total_time), "sec" print "> INFO: All processes have been finished. Have a good day!"
def infer_segmentation(options): """ Infer segmentation given the input options passed as parameters """ # define the training backend define_backend(options) from CNN.base import test_cascaded_model from CNN.build_model import cascade_model # -------------------------------------------------- # net configuration # take into account if the pretrained models have to be used # all images share the same network model # -------------------------------------------------- options['full_train'] = True options['load_weights'] = True options['weight_paths'] = os.path.join(CURRENT_PATH, 'nets') options['net_verbose'] = 0 model = cascade_model(options) # -------------------------------------------------- # process each of the scans # - image identification # - image registration # - skull-stripping # - WM segmentation # -------------------------------------------------- options['task'] = 'testing' scan_list = os.listdir(options['test_folder']) scan_list.sort() for scan in scan_list: total_time = time.time() options['tmp_scan'] = scan # -------------------------------------------------- # move things to a tmp folder before starting # -------------------------------------------------- current_folder = os.path.join(options['test_folder'], scan) options['tmp_folder'] = os.path.normpath( os.path.join(current_folder, 'tmp')) # -------------------------------------------------- # preprocess scans # -------------------------------------------------- preprocess_scan(current_folder, options) # -------------------------------------------------- # WM MS lesion inference # -------------------------------------------------- seg_time = time.time() "> CNN:", scan, "running WM lesion segmentation" sys.stdout.flush() options['test_scan'] = scan test_x_data = { scan: { m: os.path.join(options['tmp_folder'], n) for m, n in zip(options['modalities'], options['x_names']) } } test_cascaded_model(model, test_x_data, options) print "> INFO:", scan, "CNN Segmentation time: ", round( time.time() - seg_time), "sec" print "> INFO:", scan, "total pipeline time: ", round( time.time() - total_time), "sec" # remove tmps if not set if options['save_tmp'] is False: try: os.rmdir(options['tmp_folder']) os.rmdir( os.path.join(options['current_folder'], options['experiment'])) except: pass print "> INFO: All processes have been finished. Have a good day!"
def train_network(options): """ Train the CNN network given the options passed as parameter """ # set GPU mode from the configuration file. # So, this has to be updated before calling # the CNN libraries if the default config "~/.theanorc" has to be replaced. if options['mode'].find('cuda') == -1 and options['mode'].find( 'gpu') == -1: os.environ[ 'THEANO_FLAGS'] = 'mode=FAST_RUN,device=cpu,floatX=float32,optimizer=fast_compile' else: os.environ['THEANO_FLAGS'] = 'mode=FAST_RUN,device=' + options[ 'mode'] + ',floatX=float32,optimizer=fast_compile' from CNN.base import train_cascaded_model from CNN.build_model import cascade_model scan_list = os.listdir(options['train_folder']) scan_list.sort() options['task'] = 'training' options['train_folder'] = os.path.normpath(options['train_folder']) for scan in scan_list: total_time = time.time() # -------------------------------------------------- # move things to a tmp folder before starting # -------------------------------------------------- options['tmp_scan'] = scan current_folder = os.path.join(options['train_folder'], scan) options['tmp_folder'] = os.path.normpath( os.path.join(current_folder, 'tmp')) # preprocess scan preprocess_scan(current_folder, options) # -------------------------------------------------- # WM MS lesion training # - configure net and train # -------------------------------------------------- seg_time = time.time() print "> CNN: Starting training session" # select training scans train_x_data = { f: { m: os.path.join(options['train_folder'], f, 'tmp', n) for m, n in zip(options['modalities'], options['x_names']) } for f in scan_list } train_y_data = { f: os.path.join(options['train_folder'], f, 'tmp', 'lesion.nii.gz') for f in scan_list } options['weight_paths'] = os.path.join(CURRENT_PATH, 'nets') options['load_weights'] = False # train the model for the current scan print "> CNN: training net with %d subjects" % (len(train_x_data.keys())) # -------------------------------------------------- # initialize the CNN and train the classifier # -------------------------------------------------- model = cascade_model(options) model = train_cascaded_model(model, train_x_data, train_y_data, options) print "> INFO: training time:", round(time.time() - seg_time), "sec" print "> INFO: total pipeline time: ", round(time.time() - total_time), "sec" print "> INFO: All processes have been finished. Have a good day!"
def infer_segmentation(options): """ Infer segmentation given the input options passed as parameters """ # set GPU mode from the configuration file. So, this has to be updated before calling # the CNN libraries if the default config "~/.theanorc" has to be replaced. if options['mode'].find('cuda') == -1 and options['mode'].find( 'gpu') == -1: os.environ[ 'THEANO_FLAGS'] = 'mode=FAST_RUN,device=cpu,floatX=float32,optimizer=fast_compile' else: os.environ['THEANO_FLAGS'] = 'mode=FAST_RUN,device=' + options[ 'mode'] + ',floatX=float32,optimizer=fast_compile' from CNN.base import test_cascaded_model from CNN.build_model import cascade_model # -------------------------------------------------- # net configuration # take into account if the pretrained models have to be used # all images share the same network model # -------------------------------------------------- options['full_train'] = True options['load_weights'] = True options['weight_paths'] = os.path.join(CURRENT_PATH, 'nets') options['net_verbose'] = 0 model = cascade_model(options) # -------------------------------------------------- # process each of the scans # - image identification # - image registration # - skull-stripping # - WM segmentation # -------------------------------------------------- options['task'] = 'testing' scan_list = os.listdir(options['test_folder']) scan_list.sort() for scan in scan_list: total_time = time.time() options['tmp_scan'] = scan # -------------------------------------------------- # move things to a tmp folder before starting # -------------------------------------------------- current_folder = os.path.join(options['test_folder'], scan) options['tmp_folder'] = os.path.normpath( os.path.join(current_folder, 'tmp')) # -------------------------------------------------- # preprocess scans # -------------------------------------------------- preprocess_scan(current_folder, options) # -------------------------------------------------- # WM MS lesion inference # -------------------------------------------------- seg_time = time.time() "> CNN:", scan, "running WM lesion segmentation" sys.stdout.flush() options['test_scan'] = scan test_x_data = { scan: { m: os.path.join(options['tmp_folder'], n) for m, n in zip(options['modalities'], options['x_names']) } } test_cascaded_model(model, test_x_data, options) print "> INFO:", scan, "CNN Segmentation time: ", round( time.time() - seg_time), "sec" print "> INFO:", scan, "total pipeline time: ", round( time.time() - total_time), "sec" # remove tmps if not set if options['save_tmp'] is False: try: os.rmdir(options['tmp_folder']) os.rmdir( os.path.join(options['current_folder'], options['experiment'])) except: pass print "> INFO: All processes have been finished. Have a good day!"
def infer_segmentation(options): """ Infer segmentation given the input options passed as parameters """ # define the training backend define_backend(options) from CNN.base import test_cascaded_model from CNN.build_model import cascade_model # -------------------------------------------------- # net configuration # take into account if the pretrained models have to be used # all images share the same network model # -------------------------------------------------- options['full_train'] = True options['load_weights'] = True options['weight_paths'] = os.path.join(CURRENT_PATH, 'nets') options['net_verbose'] = 0 model = cascade_model(options) # -------------------------------------------------- # process each of the scans # - image identification # - image registration # - skull-stripping # - WM segmentation # -------------------------------------------------- # scan_list = os.listdir(options['test_folder']) # scan_list.sort() # # check and remove the folder which dose not contain the necessary modalities before prepossessing step # for check in scan_list: # check_oututs(check, options) # update scan list after removing the unnecessary folders before prepossessing step options['task'] = 'testing' scan_list = os.listdir(options['test_folder']) scan_list.sort() for scan in scan_list: total_time = time.time() options['tmp_scan'] = scan # -------------------------------------------------- # move things to a tmp folder before starting # -------------------------------------------------- current_folder = os.path.join(options['test_folder'], scan) options['tmp_folder'] = os.path.normpath( os.path.join(current_folder, 'tmp')) # -------------------------------------------------- # preprocess scans # -------------------------------------------------- # preprocess_scan(current_folder, options) # -------------------------------------------------- # WM MS lesion inference # -------------------------------------------------- seg_time = time.time() print("> CNN:", scan, "running WM lesion segmentation") sys.stdout.flush() options['test_scan'] = scan test_x_data = { scan: { m: os.path.join(options['tmp_folder'], n) for m, n in zip(options['modalities'], options['x_names']) } } test_cascaded_model(model, test_x_data, options) if options['register_modalities']: # print("> INFO:", scan, "Inverting lesion segmentation masks") print(CYELLOW + "Inverting lesion segmentation masks:", CRED + scan + CEND, ".....started!" + CEND) invert_registration(current_folder, options) print("> INFO:", scan, "CNN Segmentation time: ", round(time.time() - seg_time), "sec") print("> INFO:", scan, "total pipeline time: ", round(time.time() - total_time), "sec") # remove tmps if not set if options['save_tmp'] is False: try: os.rmdir(options['tmp_folder']) os.rmdir( os.path.join(options['current_folder'], options['experiment'])) except: pass print("> INFO: All processes have been finished.")