def check_preloaded_paths_correct(preloaded_paths): #ensure that if we have only one detector, that the network was trained with both types of roof #if we have more than one detector, ensure that each was trained on only one type of roof metal_network = None thatch_network = None for i, path in enumerate(preloaded_paths): metal_num = (int(float(utils.get_param_value_from_filename(path, 'metal')))) thatch_num = (int(float(utils.get_param_value_from_filename(path,'thatch')))) nonroof_num = (int(float(utils.get_param_value_from_filename(path,'nonroof')))) assert nonroof_num > 0 if len(preloaded_paths) == 1: assert metal_num > 0 and thatch_num > 0 metal_network = path thatch_network = path elif len(preloaded_paths) == 2: if (metal_num == 0 and thatch_num > 0): thatch_network = path if (metal_num > 0 and thatch_num == 0): metal_network = path else: raise ValueError('You passed in too many network weights to the pipeline.') #if we passed in two neural networks, ensure that we actually #have one for metal and one for thatch assert metal_network is not None and thatch_network is not None return metal_network, thatch_network
def setup_params(parameters, pipe_fname, method=None, decision='decideMean'): ''' Get parameters for the pipeline and the components of the pipeline: the neural network(s) and the viola detector(s) ''' preloaded_paths = parameters['preloaded_path'].split() #there can be more than one network, separated by space single_detector_boolean = False if len(preloaded_paths) == 2 else True #PIPE PARAMS: the grouping for each roof class parameter pipe_params = dict() if 'metal_groupThres' in parameters: pipe_params['metal_groupThres'] = parameters['metal_groupThres'] if 'thatch_groupThres' in parameters: pipe_params['thatch_groupThres'] = parameters['thatch_groupThres'] neural_ensemble = Ensemble(preloaded_paths, scoring_strategy=decision, method=method) from nolearn.lasagne.visualize import plot_conv_weights #plot_conv_weights(neural_ensemble.neural_nets['metal'][0].net.layers_['conv1'], figsize=(4,4)) #plt.show() assert neural_ensemble is not None if method=='viola': #VIOLA PARAMS viola_params = dict() viola_data = neural_ensemble.data_path #a little messy, but we no longer call viola_data that, we call it data_path combo_fname = 'combo{0}'.format(int(utils.get_param_value_from_filename(viola_data,'combo'))) viola_params['detector_names'] = viola_detector_helpers.get_detectors(combo_fname) #get other non-default params possible_parameters = ['min_neighbors','scale', 'group', 'removeOff', 'rotate', 'mergeFalsePos'] for param in possible_parameters: if param in viola_data: viola_params[param]= utils.get_param_value_from_filename(viola_data, param) detector_params = viola_params elif method=='slide': #sliding window detector_params = dict() '''WE KNOW WHAT THESE PARAMS ARE, SO USE THEM DIRECTLY for param in ['scale', 'minSize', 'windowSize', 'stepSize']: if param == 'scale': detector_params[param] = float(parameters[param]) elif param == 'stepSize': detector_params[param] = int(float(parameters[param])) else: detector_params[param] = (int(float(parameters[param])),int(float(parameters[param]))) ''' detector_params['scale'] = 1.3 detector_params['minSize'] = (50,50) detector_params['windowSize'] = (15,15) detector_params['stepSize'] = 4 else: print 'Unknown method of detection {}'.format(method) sys.exit(-1) return neural_ensemble, detector_params, pipe_params, single_detector_boolean
def process_paths_get_nets(self): self.neural_nets = defaultdict(list) self.num_nets = defaultdict(int) for roof_type, net_paths in self.net_paths.iteritems(): for p, path in enumerate(net_paths): self.num_nets[roof_type] += 1 if 'batch' in path: start = len('batch') + path.find('batch') end = path[start:].find('.') starting_batch = int(float(path[start:-len('.pickle')])) else: starting_batch = 0 print 'Starting batch is:{}'.format(starting_batch) neural_params = dict() #one set of parameters per roof type neural_param_num = (utils.get_param_value_from_filename( path, 'params')) if neural_param_num is None: neural_param_num = (utils.get_param_value_from_filename( path, 'violaNet')) #small hack to pick up right param file number in two cases where I foolishly didn't print it out in the name.... if neural_param_num is None: if len(net_paths) == 1: neural_param_num = 20005 if roof_type == 'thatch' else 20006 elif len(net_paths) > 1: neural_param_num = 20003 if roof_type == 'thatch' else 20004 neural_params_fname = 'params{0}.csv'.format( int(neural_param_num)) params_path = '{0}{1}'.format( utils.get_path(params=True, neural=True), neural_params_fname) neural_params = neural_network.get_neural_training_params_from_file( params_path) if 'viola_data' in neural_params.keys(): self.data_path = neural_params['viola_data'] elif 'data_folder' in neural_params.keys(): self.data_path = neural_params['data_folder'] neural_params['preloaded_path'] = path neural_params['net_name'] = path[:-len('.pickle')] neural_params['roof_type'] = roof_type #for each net in the ensemble, we start at a different data batch, that way we get a variety of data for training current_net = Experiment(pipeline=True, method=self.method, starting_batch=starting_batch, **neural_params) self.neural_nets[roof_type].append(current_net)
def process_preloaded_paths(self, preloaded_paths): #ensure that if we have only one detector, that the network was trained with both types of roof #if we have more than one detector, ensure that each was trained on only one type of roof self.net_paths = defaultdict(list) for path in preloaded_paths: metal_num = (int(float(utils.get_param_value_from_filename(path, 'metal')))) thatch_num = (int(float(utils.get_param_value_from_filename(path,'thatch')))) nonroof_num = (int(float(utils.get_param_value_from_filename(path,'nonroof')))) assert nonroof_num > 0 if (metal_num == 0 and thatch_num > 0): self.net_paths['thatch'].append(path) if (metal_num > 0 and thatch_num == 0): self.net_paths['metal'].append(path)
def process_preloaded_paths(self, preloaded_paths): #ensure that if we have only one detector, that the network was trained with both types of roof #if we have more than one detector, ensure that each was trained on only one type of roof self.net_paths = defaultdict(list) for path in preloaded_paths: metal_num = (int( float(utils.get_param_value_from_filename(path, 'metal')))) thatch_num = (int( float(utils.get_param_value_from_filename(path, 'thatch')))) nonroof_num = (int( float(utils.get_param_value_from_filename(path, 'nonroof')))) assert nonroof_num > 0 if (metal_num == 0 and thatch_num > 0): self.net_paths['thatch'].append(path) if (metal_num > 0 and thatch_num == 0): self.net_paths['metal'].append(path)
def process_paths_get_nets(self): self.neural_nets = defaultdict(list) self.num_nets = defaultdict(int) for roof_type, net_paths in self.net_paths.iteritems(): for p, path in enumerate(net_paths): self.num_nets[roof_type] += 1 if 'batch' in path: start = len('batch')+path.find('batch') end = path[start:].find('.') starting_batch = int(float(path[start:-len('.pickle')])) else: starting_batch = 0 print 'Starting batch is:{}'.format(starting_batch) neural_params = dict() #one set of parameters per roof type neural_param_num = (utils.get_param_value_from_filename(path, 'params')) if neural_param_num is None: neural_param_num = (utils.get_param_value_from_filename(path, 'violaNet')) #small hack to pick up right param file number in two cases where I foolishly didn't print it out in the name.... if neural_param_num is None: if len(net_paths) == 1: neural_param_num = 20005 if roof_type=='thatch' else 20006 elif len(net_paths) > 1: neural_param_num = 20003 if roof_type=='thatch' else 20004 neural_params_fname = 'params{0}.csv'.format(int(neural_param_num)) params_path = '{0}{1}'.format(utils.get_path(params=True, neural=True), neural_params_fname) neural_params = neural_network.get_neural_training_params_from_file(params_path) if 'viola_data' in neural_params.keys(): self.data_path = neural_params['viola_data'] elif 'data_folder' in neural_params.keys(): self.data_path = neural_params['data_folder'] neural_params['preloaded_path'] = path neural_params['net_name'] = path[:-len('.pickle')] neural_params['roof_type'] = roof_type #for each net in the ensemble, we start at a different data batch, that way we get a variety of data for training current_net = Experiment(pipeline=True, method=self.method, starting_batch=starting_batch, **neural_params) self.neural_nets[roof_type].append(current_net)
def setup_neural_viola_params(parameters, pipe_fname): ''' Get parameters for the pipeline and the components of the pipeline: the neural network(s) and the viola detector(s) ''' preloaded_paths = parameters['preloaded_path'].split() #there can be more than one network, separated by space single_detector_boolean = False if len(preloaded_paths) == 2 else True #PIPE PARAMS:the step size (probably not needed) and the neural nets to be used metal_net, thatch_net = check_preloaded_paths_correct(preloaded_paths) pipe_params = dict() preloaded_paths_dict = {'metal': metal_net, 'thatch': thatch_net} pipe_params = {'step_size':parameters['step_size'] ,'preloaded_paths': preloaded_paths_dict} neural_params = dict() #one set of parameters per roof type for roof_type, path in preloaded_paths_dict.iteritems(): #NEURAL parameters: there could be two neural networks trained and used neural_param_num = int(utils.get_param_value_from_filename(path, 'params')) neural_params_fname = 'params{0}.csv'.format(neural_param_num) params_path = '{0}{1}'.format(utils.get_path(params=True, neural=True), neural_params_fname) neural_params[roof_type] = neural_network.get_neural_training_params_from_file(params_path) neural_params[roof_type]['preloaded_path'] = path neural_params[roof_type]['net_name'] = path[:-len('.pickle')] if single_detector_boolean: neural_params[roof_type]['roof_type'] = 'Both' #VIOLA PARAMS viola_params = dict() viola_data = neural_params['metal']['viola_data'] combo_fname = 'combo{0}'.format(int(utils.get_param_value_from_filename(viola_data,'combo'))) viola_params['detector_names'] = viola_detector_helpers.get_detectors(combo_fname) #get other non-default params possible_parameters = ['min_neighbors','scale', 'group', 'removeOff', 'rotate', 'mergeFalsePos'] for param in possible_parameters: if param in viola_data: viola_params[param]= utils.get_param_value_from_filename(viola_data, param) return neural_params, viola_params, pipe_params, single_detector_boolean
if 'viola' in score[0]: viola_num += 1 if viola_num > 10: continue if prev_viola_auc == score[1]: prev_viola_auc = score[1] continue prev_viola_auc = score[1] elif 'slide' in score[0]: slide_num += 1 if slide_num > 10: continue if prev_slide_auc == score[1]: prev_slide_auc = score[1] continue prev_slide_auc = score[1] end_name = score[0].find('_') name = score[0][:end_name] group_thres = float( utils.get_param_value_from_filename(score[0], 'group', separator='_')) log.append( '{0}&\({7}\)&\({1}\)&\({2}\)&\({3:.3f}\)&\({4:.3f}\)&\({5:.3f}\)&\({6:.3f}\)\\\\' .format(name, score[1], score[2], score[3], score[4], score[5], score[6], group_thres)) output = '\n'.join(log) with open('results/{}_{}.txt'.format(roof_type, method), 'w') as f: f.write(output) print output
log.append('{}&{}&{}&{}&{}&{}&{}\\\\'.format('Detector','Group Threshold' , 'Auc', 'Detections', 'Recall','Precision','F1','Time (s.)')) for score in scores[roof_type][method]: if 'viola' in score[0]: viola_num += 1 if viola_num > 10: continue if prev_viola_auc == score[1]: prev_viola_auc = score[1] continue prev_viola_auc = score[1] elif 'slide' in score[0]: slide_num += 1 if slide_num > 10: continue if prev_slide_auc == score[1]: prev_slide_auc = score[1] continue prev_slide_auc = score[1] end_name = score[0].find('_') name = score[0][:end_name] group_thres = float(utils.get_param_value_from_filename(score[0], 'group', separator='_')) log.append('{0}&\({7}\)&\({1}\)&\({2}\)&\({3:.3f}\)&\({4:.3f}\)&\({5:.3f}\)&\({6:.3f}\)\\\\'.format(name, score[1], score[2], score[3], score[4], score[5], score[6], group_thres)) output = '\n'.join(log) with open('results/{}_{}.txt'.format(roof_type, method), 'w') as f: f.write(output) print output
def setup_params(parameters, pipe_fname, method=None, decision='decideMean'): ''' Get parameters for the pipeline and the components of the pipeline: the neural network(s) and the viola detector(s) ''' preloaded_paths = parameters['preloaded_path'].split( ) #there can be more than one network, separated by space single_detector_boolean = False if len(preloaded_paths) == 2 else True #PIPE PARAMS: the grouping for each roof class parameter pipe_params = dict() if 'metal_groupThres' in parameters: pipe_params['metal_groupThres'] = parameters['metal_groupThres'] if 'thatch_groupThres' in parameters: pipe_params['thatch_groupThres'] = parameters['thatch_groupThres'] neural_ensemble = Ensemble(preloaded_paths, scoring_strategy=decision, method=method) from nolearn.lasagne.visualize import plot_conv_weights #plot_conv_weights(neural_ensemble.neural_nets['metal'][0].net.layers_['conv1'], figsize=(4,4)) #plt.show() assert neural_ensemble is not None if method == 'viola': #VIOLA PARAMS viola_params = dict() viola_data = neural_ensemble.data_path #a little messy, but we no longer call viola_data that, we call it data_path combo_fname = 'combo{0}'.format( int(utils.get_param_value_from_filename(viola_data, 'combo'))) viola_params['detector_names'] = viola_detector_helpers.get_detectors( combo_fname) #get other non-default params possible_parameters = [ 'min_neighbors', 'scale', 'group', 'removeOff', 'rotate', 'mergeFalsePos' ] for param in possible_parameters: if param in viola_data: viola_params[param] = utils.get_param_value_from_filename( viola_data, param) detector_params = viola_params elif method == 'slide': #sliding window detector_params = dict() '''WE KNOW WHAT THESE PARAMS ARE, SO USE THEM DIRECTLY for param in ['scale', 'minSize', 'windowSize', 'stepSize']: if param == 'scale': detector_params[param] = float(parameters[param]) elif param == 'stepSize': detector_params[param] = int(float(parameters[param])) else: detector_params[param] = (int(float(parameters[param])),int(float(parameters[param]))) ''' detector_params['scale'] = 1.3 detector_params['minSize'] = (50, 50) detector_params['windowSize'] = (15, 15) detector_params['stepSize'] = 4 else: print 'Unknown method of detection {}'.format(method) sys.exit(-1) return neural_ensemble, detector_params, pipe_params, single_detector_boolean