options['weight_paths'] = os.path.join(options['train_folder'], scan, 'nets') options['load_weights'] = False options['test_scan'] = scan # train the model for the current scan print("> CNN: training net with %d subjects" % (len(list(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, CURRENT_PATH) print("> INFO: training time:", round(time.time() - seg_time), "sec") print("> INFO: total pipeline time: ", \ round(time.time() - total_time), "sec") # -------------------------------------------------- # test the current scan # -------------------------------------------------- test_x_data = { scan: { m: os.path.join(options['train_folder'], scan, 'tmp', n) for m, n in zip(options['modalities'], options['x_names']) } } print("test_x_data : ", test_x_data)
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!"
if not os.path.exists(os.path.join(options['train_folder'], scan, 'nets')): os.mkdir(os.path.join(options['train_folder'], scan, 'nets')) options['weight_paths'] = os.path.join(options['train_folder'], scan, 'nets') options['load_weights'] = False options['test_scan'] = scan # 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" # -------------------------------------------------- # test the current scan # -------------------------------------------------- test_x_data = { scan: { m: os.path.join(options['train_folder'], scan, 'tmp', n) for m, n in zip(options['modalities'], options['x_names']) } }
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!"