def __init__(self, is_site): #TODO load classifier sess = None if is_site: sess, _ = load_graph('models/real_model.pb') else: sess, _ = load_graph('models/sim_model.pb') self.sess = sess self.sess_graph = self.sess.graph # Definite input and output Tensors for sess self.image_tensor = self.sess_graph.get_tensor_by_name( 'image_tensor:0') # Each box represents a part of the image where a particular object was detected. self.detection_boxes = self.sess_graph.get_tensor_by_name( 'detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. self.detection_scores = self.sess_graph.get_tensor_by_name( 'detection_scores:0') self.detection_classes = self.sess_graph.get_tensor_by_name( 'detection_classes:0') self.num_classes = 3 self.category_index = { 1: { 'id': 1, 'name': u'red' }, 2: { 'id': 2, 'name': u'yellow' }, 3: { 'id': 3, 'name': u'green' } } self.image_count = 0 self.last_pred = TrafficLight.UNKNOWN self.pub_tl_clssifier_monitor = None self.bridge = None if SHOW_MONITOR_IMAGE: self.pub_tl_clssifier_monitor = rospy.Publisher( '/clssifier_monitor_image', Image_msg, queue_size=2) self.bridge = CvBridge()
def __init__(self, sim): K.set_image_dim_ordering('tf') self.ready = False if sim: model_name = 'squeezeNet_sim' else: model_name = 'squeezeNet_real' self.graph, ops = load_graph( 'light_classification/trained_model/{}.frozen.pb'.format( model_name)) self.sess = tf.Session(graph=self.graph) self.learning_phase_tensor = self.graph.get_tensor_by_name( 'fire9_dropout/keras_learning_phase:0') self.op_tensor = self.graph.get_tensor_by_name('softmax/Softmax:0') self.input_tensor = self.graph.get_tensor_by_name('input_1:0') self.ready = True self.pred_dict = { 0: TrafficLight.UNKNOWN, 1: TrafficLight.RED, 2: TrafficLight.GREEN }
def find_same_level_regions(region): # Load graph graph = load_graph() if not graph: logger.error( "Can't trace multiple regions: Region call graph not available.\n\ Run cere profile.\n\ Tracing region {0}".format(region)) return # Find region node id region_node = get_region_id(region, graph) #Find roots roots = [n for n, d in graph.in_degree().items() if d == 0] #Compute for every nodes, the max distance from himself to each root max_path_len = {} for root in roots: for n, d in graph.nodes(data=True): if n not in max_path_len: max_path_len[n] = 0 #Get every paths from the current root to the node paths = list(nx.all_simple_paths(graph, root, n)) #Keep the max length path for path in paths: if len(path) - 1 > max_path_len[n]: max_path_len[n] = len(path) - 1 #Keep region which have the same depth than the requested region for n, p in max_path_len.iteritems(): if p == max_path_len[region_node]: yield graph.node[n]['_name']
def __init__(self): #TODO load classifier sess, _ = load_graph('models/sim_model.pb') self.sess = sess self.sess_graph = self.sess.graph # Definite input and output Tensors for sess self.image_tensor = self.sess_graph.get_tensor_by_name( 'image_tensor:0') # Each box represents a part of the image where a particular object was detected. self.detection_boxes = self.sess_graph.get_tensor_by_name( 'detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. self.detection_scores = self.sess_graph.get_tensor_by_name( 'detection_scores:0') self.detection_classes = self.sess_graph.get_tensor_by_name( 'detection_classes:0') self.num_classes = 3 self.label_map = label_map_util.load_labelmap("./labelmap.pbtxt") self.categories = label_map_util.convert_label_map_to_categories( self.label_map, max_num_classes=self.num_classes, use_display_name=True) self.category_index = label_map_util.create_category_index( self.categories) self.pub_tl_clssifier_monitor = None self.bridge = None if SHOW_MONITOR_IMAGE: self.pub_tl_clssifier_monitor = rospy.Publisher( '/clssifier_monitor_image', Image_msg, queue_size=2) self.bridge = CvBridge()
def solve(gym_environment, cis): # python has dynamic typing, the line below can help IDEs with autocompletion assert isinstance(cis, ChallengeInterfaceSolution) # after this cis. will provide you with some autocompletion in some IDEs (e.g.: pycharm) cis.info('Creating model.') # you can have logging capabilities through the solution interface (cis). # the info you log can be retrieved from your submission files. # We get environment from the Evaluation Engine cis.info('Making environment') env = gym.make(gym_environment) # Then we make sure we have a connection with the environment and it is ready to go cis.info('Reset environment') observation = env.reset() # While there are no signal of completion (simulation done) # we run the predictions for a number of episodes, don't worry, we have the control on this part frozen_model_filename = "frozen_graph.pb" # We use our "load_graph" function graph = load_graph(frozen_model_filename) # We can verify that we can access the list of operations in the graph for op in graph.get_operations(): print(op.name) # prefix/Placeholder/inputs_placeholder # ... # prefix/Accuracy/predictions # We access the input and output nodes x = graph.get_tensor_by_name('prefix/x:0') y = graph.get_tensor_by_name('prefix/ConvNet/fc_layer_2/BiasAdd:0') # We launch a Session with tf.Session(graph=graph) as sess: while True: # we passe the observation to our model, and we get an action in return # 48x96 is the image size the model expects # Additionally img is converted to greyscale observation = fun_img_preprocessing(observation, 48, 96) # this outputs omega, the desired angular velocity action = sess.run(y, feed_dict={x: observation}) action = [action[0, 1], action[0, 0]] action = simulation_scaling(action) # we tell the environment to perform this action and we get some info back in OpenAI Gym style observation, reward, done, info = env.step(action) # here you may want to compute some stats, like how much reward are you getting # notice, this reward may no be associated with the challenge score. # it is important to check for this flag, the Evaluation Engine will let us know when should we finish # if we are not careful with this the Evaluation Engine will kill our container and we will get no score # from this submission if 'simulation_done' in info: break if done: env.reset()
def run_(graph_name): num_classes = 2 image_shape = (160, 576) data_dir = './data' runs_dir = './runs' # Load graph sess, ops = graph_utils.load_graph(graph_name) graph = sess.graph # print("operations ", len(ops)) print("graph names: \n", [n.name for n in graph.as_graph_def().node]) input_image = graph.get_tensor_by_name('image_input:0') keep_prob = graph.get_tensor_by_name('keep_prob:0') logits = graph.get_tensor_by_name('adam_logit:0') helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, input_image)
def __init__(self, model_name): self.ready_for_classification = False K.set_image_dim_ordering('tf') self.graph, ops = load_graph('light_classification/Models/{}.pb'.format(model_name)) self.sess = tf.Session(graph = self.graph) self.learning_phase_tensor = self.graph.get_tensor_by_name('fire9_dropout/keras_learning_phase:0') self.op_tensor = self.graph.get_tensor_by_name('softmax/Softmax:0') self.input_tensor = self.graph.get_tensor_by_name('input_1:0') self.pred_dict = {0: TrafficLight.UNKNOWN, 1: TrafficLight.RED, 2: TrafficLight.GREEN} # This is used to prevent the classification is done before the classifier initialized self.ready_for_classification = True
def run_altered(runs_dir): data_dir = './data' image_shape = (160, 576) model_location = os.path.join(runs_dir, 'model.pb') sess = graph_utils.load_graph(model_location, True) config = tf.ConfigProto() config.gpu_options.allocator_type = 'BFC' # JIT level, this can be set to ON_1 or ON_2 jit_level = tf.OptimizerOptions.ON_1 config.graph_options.optimizer_options.global_jit_level = jit_level with tf.Session(config=config, graph=sess.graph) as sess: image_input = sess.graph.get_tensor_by_name('image_input:0') keep_prob = sess.graph.get_tensor_by_name('keep_prob:0') my_logits = sess.graph.get_tensor_by_name('my_logits:0') helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, my_logits, keep_prob, image_input) run_video(sess, image_input, keep_prob, my_logits, data_dir)
def run(): num_classes = 2 image_shape = (160, 576) data_dir = './data' runs_dir = './runs' # Create a TensorFlow configuration object. This will be # passed as an argument to the session. config = tf.ConfigProto() # JIT level, this can be set to ON_1 or ON_2 jit_level = tf.OptimizerOptions.ON_2 config.graph_options.optimizer_options.global_jit_level = jit_level config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 1.0 #with tf.Session(config=config) as sess: with tf.Session(config=config, graph=tf.Graph()) as sess: #saver = tf.train.import_meta_graph('./runs/semantic_segmentation_model.ckpt.meta') #saver.restore(sess,tf.train.latest_checkpoint('./runs/semantic_segmentation_model.ckpt')) #sess, _ = load_graph('./runs/frozen_graph.pb') sess, _ = load_graph('./runs/optimized_graph.pb') graph = sess.graph adam_angst = graph.get_tensor_by_name('adam_logit:0') image_input = graph.get_tensor_by_name('image_input:0') keep_prob = graph.get_tensor_by_name('keep_prob:0') print('hallo') print('Adam Angst = ', adam_angst) logits = graph.get_tensor_by_name('adam_logit:0') #sess.run(tf.global_variables_initializer()) #sess.run(tf.local_variables_initializer()) # TODO: Save inference data using helper.save_inference_samples helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, image_input) print("%d ops in the final graph." % len(output_graph_def.node))
def compute_action(self, observation): frozen_model_filename = "frozen_graph.pb" # We use our "load_graph" function graph = load_graph(frozen_model_filename) # We can verify that we can access the list of operations in the graph for op in graph.get_operations(): print(op.name) # We access the input and output nodes x = graph.get_tensor_by_name('prefix/x:0') y = graph.get_tensor_by_name('prefix/ConvNet/fc_layer_2/BiasAdd:0') # We launch a Session with tf.Session(graph=graph) as sess: # Additionally img is converted to greyscale observation = fun_img_preprocessing(observation, 48, 96) # this outputs omega, the desired angular velocity action = sess.run(y, feed_dict={ x: observation }) action = [action[0, 1], action[0, 0]] return action
from graph_utils import load_graph sess, base_ops = load_graph('base_graph.pb') print(len(base_ops)) # 2165 sess, frozen_ops = load_graph('frozen_graph.pb') print(len(frozen_ops)) # 245 sess, optimized_ops = load_graph('optimized_graph.pb') print(len(optimized_ops)) # 200 sess, eightbit_ops = load_graph('eightbit_graph.pb') print(len(eightbit_ops)) # 425
def main(): args = parser.parse_args() output_tensor_dir = os.path.join(args.output_dir, args.output_node.replace("/", '_')) makedir_if_needed(output_tensor_dir) print("Loading graph\n") graph = graph_utils.load_graph(args.graph) print("Processing images tensors\n") graph_utils.process_image_tensors(graph=graph, input_node_name=args.input_node, output_node_name=args.output_node, dataset_dir=os.path.join( args.dataset_dir, "images"), save_dir=output_tensor_dir) print("Calculating percentiles for each filter\n") percentile_path = os.path.join( args.output_dir, "_".join([ "percentile", str(args.percentile), args.output_node.replace('/', '_'), ".npy" ])) tensor_utils.calculate_percentiles(tensor_dir=output_tensor_dir, save_path=percentile_path, percentile=args.percentile) print("Creating result dataframe\n") labels_pd = pd.read_csv(os.path.join(args.dataset_dir, "labels.csv"), sep=',') labels_pd.index = labels_pd['number'] result_pd = segmentation_scoring.create_result_dataframe( args.n_maps, labels_pd) source_filenames = os.listdir(os.path.join(args.dataset_dir, "images")) percentiles = np.load(percentile_path) for source_filename in tqdm(source_filenames): source_image_path = os.path.join(args.dataset_dir, 'images', source_filename) label_image_path = os.path.join(args.dataset_dir, 'labeling', source_filename) labeled_image = segmentation_scoring.imread_int_labels( label_image_path) img_labels_set = set(labeled_image.flatten()).intersection( labels_pd.index) tensor_path = os.path.join(output_tensor_dir, source_filename + ".npy") tensor = np.load(tensor_path) for label in img_labels_set: bin_mask_labels = labeled_image == label for i in range(args.n_maps): bin_preds = segmentation_scoring.preprocess_map( tensor[:, :, i], percentiles[i]) intersection, union = segmentation_scoring.compute_bin_iou( bin_preds, bin_mask_labels) result_pd.at[(label, i), 'intersection'] += intersection result_pd.at[(label, i), 'union'] += union result_pd.at[(label, i), 'num_images'] += 1 iou_score = intersection * 1. / union if iou_score > result_pd.at[(label, i), 'best_score']: result_pd.at[(label, i), 'best_score'] = iou_score result_pd.at[(label, i), 'best_image'] = source_image_path result_pd.at[(label, i), 'best_tensor'] = tensor_path result_pd.at[(label, i), 'best_label_img'] = label_image_path result_pd = result_pd[result_pd['union'] != 0] result_pd['iou'] = result_pd['intersection'] / result_pd['union'] result_pd = result_pd.sort_values('iou', ascending=False) output_name = "output_scores" result_pd.to_csv(os.path.join(args.output_dir, output_name + ".csv")) makedir_if_needed(os.path.join(args.output_dir, "demo")) print("Making demo: " + os.path.join(args.output_dir, "demo")) demo_utils.make_demo(result_pd, os.path.join(args.output_dir, "demo"), percentiles, top_k=10)
def run(args): if not cere_configure.init(): return False #Need to save this because args.force will be changed by the replay pass force_flags = args.force #Array to store best flags for every regions best_flags = [] graph = graph_utils.load_graph() if graph == None: logger.error("CERE graph is missing. Please run cere profile") return False #Load previous best results best_history = {} if os.path.isfile("{0}/regions_flags.csv".format(var.CERE_FLAGS_PATH)) and not force_flags: flags_file = read_csv(open("{0}/regions_flags.csv".format(var.CERE_FLAGS_PATH), 'rb')) for row in flags_file: best_history[row["Region"]] = {"best_id":row["Id"], "best_cycles":row["Cycles"], "original_cycles":row["Original_cycles"]} #Use llc as backend os.environ["CERE_LLC"]="llc" #Read flags file flags = read_csv(args.FLAGSFILE) #Run the requested region if args.region: found = False #Load region information for n, d in graph.nodes(data=True): if d['_name'] == args.region: found = True break if not found: logger.error("{0} does not exist. Valid region name can be found in {1}`".format(args.region, cere_configure.cere_config["regions_infos"])) return False if not d['_tested']: logger.error("{0} not tested. Please run `cere check-matching --region={0}`".format(args.region)) return False if not d['_matching']: logger.warning("{0} replay is marked as not matching. Results might not be accurate.".format(args.region)) #Load any previous measures for this region flags_history = get_region_history(args.region, force_flags) #Replay region with flags to test best_id, best_cycles = run_replay(flags, args, flags_history) best_flags.append(get_best_flags(args.region, best_history, d['_invivo'], best_id, best_cycles)) #or run selected regions from ilp selector elif not args.region: graph = graph_utils.load_graph() if graph == None or graph.graph['selector'] != "ILP": logger.error("Selected regions missing. First run cere select-ilp or choose a region manually with cere flag --region.") return False #Run selected regions for n, d in graph.nodes(data=True): if d["_selected"]: args.region=d['_name'] #Load any previous measures for this region flags_history = get_region_history(args.region, force_flags) #Replay region with flags to test best_id, best_cycles = run_replay(flags, args, flags_history) best_flags.append(get_best_flags(args.region, best_history, d['_invivo'], best_id, best_cycles)) compute_theorical_speedup(best_flags, graph) dump_results(best_flags, args.FLAGSFILE, flags) clean_environ() return True
parser.add_argument('--graph', type=str, default='models/transformed_graph.pb', help='graph for inference') args = parser.parse_args() video = args.video name_graph = args.graph #sess, _ = load_graph('models/base_graph.pb') #sess, _ = load_graph('models/frozen_graph.pb') #sess, _ = load_graph('models/frozen_model.pb') #sess, _ = load_graph('models/optimized_graph.pb') #sess, _ = load_graph('models/transformed_graph.pb') sess, _ = load_graph(name_graph) graph = sess.graph input_image = graph.get_tensor_by_name('image_input:0') keep_prob = graph.get_tensor_by_name('keep_prob:0') softmax = graph.get_tensor_by_name('Softmax:0') predictions_argmax = tf.argmax(softmax, axis=-1, output_type=tf.int64) num_classes = len(label_defs) label_colors = {i: np.array(l.color) for i, l in enumerate(label_defs)} test_image = scipy.misc.imread("data/test_image.png") image_shape = (256, 512) def predict_image(image):
for lemma_obj in frame_obj.lemma_objs: if lemma_obj.language == 'Dutch': fn_obj.update_lemma_obj_with_lemmapos_id(lemma_obj) fn_obj.framelabel2frame_obj[frame_label] = frame_obj # validate fn_obj.validate_lemmapos_identifiers() if verbose: print(fn_obj) # load graph g = graph_utils.load_graph(fn_obj.framelabel2frame_obj, synset_id2synset_obj, rbn_objs, configuration['graph_options'], verbose=verbose) # save to file output_path = out_dir / 'combined.p' with open(output_path, 'wb') as outfile: pickle.dump(fn_obj, outfile) graph_path = out_dir / 'graph.p' nx.write_gpickle(g, graph_path) if verbose: print(f'saved output to {output_path}') print(f'saved graph to {graph_path}') print(datetime.now())
self.dfs(v, w) elif w != u: # just found a cycle, store the cyclic path self._cycle = [] p = v while True: self._cycle.insert(0, p) p = self._parentChain[p] if p is None or p == w: break self._cycle.insert(0, w) self._cycle.insert(0, v) def cycle(self): return self._cycle def __has_cycle(self): return len(self._cycle) > 0 if __name__ == "__main__": import graph_utils G = graph_utils.load_graph("../data/cyclicG.txt") detector = CycleDetector(G) assert detector.has_cycle() assert [3, 0, 1, 2, 3] == detector.cycle() G = graph_utils.load_graph("../data/tinyG.txt") detector = CycleDetector(G) assert detector.has_cycle() print(detector.cycle())
import sys import os import logging import networkx as nx sys.path.insert(0, "../../src/cere/") import cere_configure from graph_utils import load_graph import vars as var logger = logging.getLogger('Test Profile') if __name__ == "__main__": cere_configure.init() #Check if call graph exists graph = load_graph() if graph == None: logger.critical("Cannot load graph.") sys.exit(1) #Is there any cere region? if( len(graph.nodes()) == 0): logger.info('Graph is empty.') sys.exit(1) #Check if measuring application cycles works if not os.path.isfile("{0}/app_cycles.csv".format(var.CERE_PROFILE_PATH)): logger.critical('Measuring cycles failed.') sys.exit(1) logger.info("Profiling ok.")