def load(self, saved_model_dir_or_frozen_graph: Text): """Load the model using saved model or a frozen graph.""" if not self.sess: self.sess = self._build_session() self.signitures = { 'image_files': 'image_files:0', 'image_arrays': 'image_arrays:0', 'prediction': 'detections:0', } # Load saved model if it is a folder. if tf.io.gfile.isdir(saved_model_dir_or_frozen_graph): return tf.saved_model.load(self.sess, ['serve'], saved_model_dir_or_frozen_graph) # Load a frozen graph. graph_def = tf.GraphDef() with tf.gfile.GFile(saved_model_dir_or_frozen_graph, 'rb') as f: graph_def.ParseFromString(f.read()) return tf.import_graph_def(graph_def, name='')
def analyse(imageObj): print('analyse') # Read the image_data image_data = tf.gfile.FastGFile(imageObj, 'rb').read() print('analyse2') # Loads label file, strips off carriage return label_lines = [ line.rstrip() for line in tf.io.gfile.GFile("tf_files/retrained_labels.txt") ] print('analyse3') # Unpersists graph from file with tf.gfile.FastGFile("tf_files/retrained_graph.pb", 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') print('analyse4') with tf.Session() as sess: # Feed the image_data as input to the graph and get first prediction softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') predictions = sess.run(softmax_tensor, \ {'DecodeJpeg/contents:0': image_data}) # Sort to show labels of first prediction in order of confidence top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] obj = {} for node_id in top_k: human_string = label_lines[node_id] score = predictions[0][node_id] obj[human_string] = float(score) return obj
def _init_inception(): global softmax if not os.path.exists(MODEL_DIR): os.makedirs(MODEL_DIR) filename = DATA_URL.split('/')[-1] filepath = os.path.join(MODEL_DIR, filename) if not os.path.exists(filepath): def _progress(count, block_size, total_size): sys.stdout.write('\r>> Downloading %s %.1f%%' % (filename, float(count * block_size) / float(total_size) * 100.0)) sys.stdout.flush() filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress) print() statinfo = os.stat(filepath) print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.') tarfile.open(filepath, 'r:gz').extractall(MODEL_DIR) with tf.gfile.FastGFile( os.path.join(MODEL_DIR, 'classify_image_graph_def.pb'), 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') # Works with an arbitrary minibatch size. pool3 = sess.graph.get_tensor_by_name('pool_3:0') ops = pool3.graph.get_operations() for op_idx, op in enumerate(ops): for o in op.outputs: shape = o.get_shape() shape = [s.value for s in shape] new_shape = [] for j, s in enumerate(shape): if s == 1 and j == 0: new_shape.append(None) else: new_shape.append(s) o.set_shape(tf.TensorShape(new_shape)) w = sess.graph.get_operation_by_name("softmax/logits/MatMul").inputs[1] logits = tf.matmul(tf.squeeze(pool3, [1, 2]), w) softmax = tf.nn.softmax(logits)
def create_inception_graph(): """"Creates a graph from saved GraphDef file and returns a Graph object. Returns: Graph holding the trained Inception network, and various tensors we'll be manipulating. """ with tf.Graph().as_default() as graph: model_filename = os.path.join(FLAGS.model_dir, 'classify_image_graph_def.pb') with gfile.FastGFile(model_filename, 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) bottleneck_tensor, jpeg_data_tensor, resized_input_tensor = ( tf.import_graph_def(graph_def, name='', return_elements=[ BOTTLENECK_TENSOR_NAME, JPEG_DATA_TENSOR_NAME, RESIZED_INPUT_TENSOR_NAME ])) return graph, bottleneck_tensor, jpeg_data_tensor, resized_input_tensor
def predict_img(img_path): image_data = tf.gfile.GFile(img_path, 'rb').read() label_lines = [ line.rstrip() for line in tf.gfile.GFile("backend/tf_files/retrained_labels.txt") ] with tf.gfile.GFile("backend/tf_files/retrained_graph.pb", 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') with tf.Session() as sess: softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') predictions = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data}) top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] for node_id in top_k: human_string = label_lines[node_id] score = predictions[0][node_id] print('%s (Prob: %.2f)' % (human_string, score)) return human_string
def _load_inception(self, proto_file): graph_def = tf.GraphDef.FromString(open(proto_file, 'rb').read()) self._inception_graph = tf.Graph() with self._inception_graph.as_default(): _ = tf.import_graph_def(graph_def, name='') self.session = tf.Session() Frame_Features = self.session.graph.get_tensor_by_name( 'pool_3/_reshape:0') Pca_Mean = tf.constant(value=self.pca_mean, dtype=tf.float32) Pca_Eigenvecs = tf.constant(value=self.pca_eigenvecs, dtype=tf.float32) Pca_Eigenvals = tf.constant(value=self.pca_eigenvals, dtype=tf.float32) Feats = Frame_Features[0] - Pca_Mean Feats = tf.reshape( tf.matmul(tf.reshape(Feats, [1, 2048]), Pca_Eigenvecs), [ 1024, ]) tf.divide(Feats, tf.sqrt(Pca_Eigenvals + 1e-4), name='pca_final_feature')
def upload_image(request): img_string = request.data['img_base64'] imgdata = base64.b64decode(img_string) now = datetime.now() now = datetime.timestamp(now) filename = f'temp_image_{request.user}.jpg' with open(filename, 'wb') as f: f.write(imgdata) config = ConfigProto( device_count = {'GPU': 0} ) config.gpu_options.allow_growth = False session = InteractiveSession(config=config) modelFullPath = './tmp/output_graph.pb' with tf.gfile.FastGFile(modelFullPath, 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') result = run_inference_on_image(filename, './tmp/output_labels.txt') os.remove(filename) return Response({'result' : result})
def submit_file(): if request.method == 'POST': if 'file' not in request.files: flash('No file part') return redirect(request.url) file = request.files['file'] if file.filename == '': flash('No file selected for uploading') return redirect(request.url) if file: filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) image_data = tf.gfile.GFile( os.path.join(app.config['UPLOAD_FOLDER'], filename), 'rb').read() label_lines = [ line.rstrip() for line in tf.gfile.GFile("tf_files/retrained_labels.txt") ] with tf.gfile.GFile("tf_files/retrained_graph.pb", 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') with tf.Session() as sess: softmax_tensor = sess.graph.get_tensor_by_name( 'final_result:0') predictions = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data}) top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] #flash(filename) for node_id in top_k: human_string = label_lines[node_id] score = predictions[0][node_id] #print('%s (Prob: %.2f)' % (human_string, score)) flash(json.dumps(str(human_string))) flash(json.dumps(str(score))) return redirect('/')
def tensorflow_pred(imageUrl): #suppress TF log-info messages - remove to display TF logs os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' response = urlopen(imageUrl) image_data = response.read() # Loads label file, strips off carriage return label_lines = [line.rstrip() for line in tf.io.gfile.GFile("./predictor/retrained_labels.txt")] try: graph = tf.Graph() with graph.as_default(): # Unpersists graph from file with tf.gfile.GFile("./predictor/retrained_graph.pb", 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') with tf.Session(graph=graph) as sess: # Feed the image_data as input to the graph and get first prediction softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') predictions = sess.run(softmax_tensor, \ {'DecodeJpeg/contents:0': image_data}) # Sort to show labels of first prediction in order of confidence top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] for node_id in top_k: congestion_type = label_lines[node_id] score = predictions[0][node_id] if (score >=0.5): return ('%s (score = %.5f)' % (congestion_type, score)) except tf.errors.InvalidArgumentError: pass
def _load_frozen_graph(self, frozen_graph_path): trt_graph = tf.GraphDef() with open(frozen_graph_path, 'rb') as f: trt_graph.ParseFromString(f.read()) self._is_lstm = self._check_lstm(trt_graph) if self._is_lstm: print('Loading an LSTM model.') self.graph = tf.Graph() with self.graph.as_default(): self.output_node = tf.import_graph_def( trt_graph, return_elements=[ 'detection_boxes:0', 'detection_classes:0', 'detection_scores:0', 'num_detections:0' ] + (['raw_outputs/lstm_c:0', 'raw_outputs/lstm_h:0'] if self._is_lstm else [])) self.session = tf.InteractiveSession(graph=self.graph) tf_scores = self.graph.get_tensor_by_name('import/detection_scores:0') tf_boxes = self.graph.get_tensor_by_name('import/detection_boxes:0') tf_classes = self.graph.get_tensor_by_name( 'import/detection_classes:0') tf_num_detections = self.graph.get_tensor_by_name( 'import/num_detections:0') if self._is_lstm: tf_lstm_c = self.graph.get_tensor_by_name( 'import/raw_outputs/lstm_c:0') tf_lstm_h = self.graph.get_tensor_by_name( 'import/raw_outputs/lstm_h:0') self._output_nodes = [ tf_scores, tf_boxes, tf_classes, tf_num_detections ] + ([tf_lstm_c, tf_lstm_h] if self._is_lstm else []) if self._is_lstm: self.lstm_c = np.ones((1, 8, 8, 320)) self.lstm_h = np.ones((1, 8, 8, 320))
def inference(): graph = tf.Graph() with graph.as_default(): with tf.gfile.FastGFile(FLAGS.input, 'rb') as f: image_data = f.read() input_image = tf.image.decode_jpeg(image_data, channels=3) input_image = tf.image.resize_images(input_image, size=(FLAGS.image_size, FLAGS.image_size)) input_image = utils.convert2float(input_image) input_image.set_shape([FLAGS.image_size, FLAGS.image_size, 3]) with tf.gfile.FastGFile(FLAGS.model, 'rb') as model_file: graph_def = tf.GraphDef() graph_def.ParseFromString(model_file.read()) [output_image] = tf.import_graph_def(graph_def, input_map={'input_image': input_image}, return_elements=['output_image:0'], name='output.jpg') with tf.Session(graph=graph) as sess: generated = output_image.eval() with open(FLAGS.output, 'wb') as f: f.write(generated)
def load_model(model): """to load the deep learning model Args: model: model path Returns: tensorflow graph """ logger.info(msg="load_model called") model_exp = os.path.expanduser(model) if os.path.isfile(model_exp): with gfile.FastGFile(model_exp, 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) graph = tf.import_graph_def(graph_def, name='') return graph else: meta_file, ckpt_file = get_model_filenames(model_exp) saver = tf.train.import_meta_graph(os.path.join(model_exp, meta_file)) graph = saver.restore(tf.get_default_session(), os.path.join(model_exp, ckpt_file)) return graph
def __init__(self): self.ckpt_path = cfg.CKPT_PATH self.anchors = cfg.ANCHORS self.img_size = cfg.IMG_SIZE self.SMALL_SIZE = cfg.SMALL_SIZE self.MID_SIZE = cfg.MID_SIZE self.LARGE_SIZE = cfg.LARGE_SIZE self.CONFIDENCE_THRESHOLD = cfg.CONFIDENCE_THRESHOLD self.IOU_THRESHOLD = cfg.IOU_THRESHOLD self.graph = tf.Graph() self.sess = tf.Session(graph=self.graph) with self.graph.as_default(): f = tf.gfile.GFile('../frozen/inference_graph.pb', 'rb') graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) self.images, self.training, self.detect1, self.detect2, self.detect3 = tf.import_graph_def( graph_def, return_elements=[ cfg.INPUT_NAME, cfg.TRAINING_NAME, cfg.DETECT1_NAME, cfg.DETECT2_NAME, cfg.DETECT3_NAME ])
def main(): parser = argparse.ArgumentParser( description='Process a mixture .wav file to separate into sources.') parser.add_argument('-i', '--input', help='Input .wav file.', required=True, type=str) parser.add_argument('-o', '--output', help='Output .wav file.', required=True, type=str) parser.add_argument( '-m', '--model_dir', help='Model root directory, required. ' 'Must contain inference.meta and at least one checkpoint.', type=str) parser.add_argument('-ic', '--input_channels', help='Truncate/pad input to this many ' 'channels, if positive.', default=0, type=int) parser.add_argument('-ns', '--num_sources', help='Number of output sources in the model.', default=2, type=int) parser.add_argument('-oc', '--output_channels', help='Truncate output to this many channels, ' 'i.e. sources, if positive.', default=0, type=int) parser.add_argument('-it', '--input_tensor', default='input_audio/receiver_audio:0', help='Name of tensor to which to feed input_wav.', type=str) parser.add_argument('-ot', '--output_tensor', default='denoised_waveforms:0', help='Name of tensor to output as output_wav.', type=str) parser.add_argument('-si', '--scale_input', default=False, help='If True, scale the input ' 'signal such that its absolute maximum value is 0.99.', type=strtobool) parser.add_argument('-c', '--checkpoint', default=None, help='Override for checkpoint path.') parser.add_argument( '-wos', '--write_outputs_separately', default=True, help='Write output source signals into separate wav files.', type=strtobool) args = parser.parse_args() tf.disable_v2_behavior() meta_graph_filename = os.path.join(args.model_dir, 'inference.meta') tf.logging.info('Importing meta graph: %s', meta_graph_filename) with tf.Graph().as_default() as g: saver = tf.train.import_meta_graph(meta_graph_filename) meta_graph_def = g.as_graph_def() tf.reset_default_graph() input_wav, sample_rate = inference.read_wav_file(args.input, args.input_channels, args.scale_input) input_wav = tf.transpose(input_wav) input_wav = tf.expand_dims(input_wav, axis=0) # shape: [1, mics, samples] output_wav, = tf.import_graph_def(meta_graph_def, name='', input_map={args.input_tensor: input_wav}, return_elements=[args.output_tensor]) output_wav = tf.squeeze(output_wav, 0) # shape: [sources, samples] output_wav = tf.transpose(output_wav) if args.output_channels > 0: output_wav = output_wav[:, :args.output_channels] write_output_ops = inference.write_wav_file( args.output, output_wav, sample_rate=sample_rate, num_channels=args.num_sources, output_channels=args.output_channels, write_outputs_separately=args.write_outputs_separately, channel_name='source') checkpoint = args.checkpoint if not checkpoint: checkpoint = tf.train.latest_checkpoint(args.model_dir) with tf.Session() as sess: tf.logging.info('Restoring from checkpoint: %s', checkpoint) saver.restore(sess, checkpoint) sess.run(write_output_ops)
def benchmark_model(self, warmup_runs, bm_runs, num_threads, trace_filename=None): """Benchmark model.""" if self.tensorrt: print('Using tensorrt ', self.tensorrt) graphdef = self.freeze_model() if num_threads > 0: print('num_threads for benchmarking: {}'.format(num_threads)) sess_config = tf.ConfigProto( intra_op_parallelism_threads=num_threads, inter_op_parallelism_threads=1) else: sess_config = tf.ConfigProto() # rewriter_config_pb2.RewriterConfig.OFF sess_config.graph_options.rewrite_options.dependency_optimization = 2 if self.use_xla: sess_config.graph_options.optimizer_options.global_jit_level = ( tf.OptimizerOptions.ON_2) with tf.Graph().as_default(), tf.Session(config=sess_config) as sess: inputs = tf.placeholder(tf.float32, name='input', shape=self.inputs_shape) output = self.build_model(inputs) img = np.random.uniform(size=self.inputs_shape) sess.run(tf.global_variables_initializer()) if self.tensorrt: fetches = [inputs.name] + [i.name for i in output] goutput = self.convert_tr(graphdef, fetches) inputs, output = goutput[0], goutput[1:] if not self.use_xla: # Don't use tf.group because XLA removes the whole graph for tf.group. output = tf.group(*output) else: output = tf.add_n([tf.reduce_sum(x) for x in output]) output_name = [output.name] input_name = inputs.name graphdef = tf.graph_util.convert_variables_to_constants( sess, sess.graph_def, output_name) with tf.Graph().as_default(), tf.Session(config=sess_config) as sess: tf.import_graph_def(graphdef, name='') for i in range(warmup_runs): start_time = time.time() sess.run(output_name, feed_dict={input_name: img}) logging.info('Warm up: {} {:.4f}s'.format( i, time.time() - start_time)) print('Start benchmark runs total={}'.format(bm_runs)) start = time.perf_counter() for i in range(bm_runs): sess.run(output_name, feed_dict={input_name: img}) end = time.perf_counter() inference_time = (end - start) / bm_runs print('Per batch inference time: ', inference_time) print('FPS: ', self.batch_size / inference_time) if trace_filename: run_options = tf.RunOptions() run_options.trace_level = tf.RunOptions.FULL_TRACE run_metadata = tf.RunMetadata() sess.run(output_name, feed_dict={input_name: img}, options=run_options, run_metadata=run_metadata) logging.info('Dumping trace to %s', trace_filename) trace_dir = os.path.dirname(trace_filename) if not tf.io.gfile.exists(trace_dir): tf.io.gfile.makedirs(trace_dir) with tf.io.gfile.GFile(trace_filename, 'w') as trace_file: trace = timeline.Timeline( step_stats=run_metadata.step_stats) trace_file.write( trace.generate_chrome_trace_format(show_memory=True))
def create_graph(): with tf.gfile.FastGFile(graph_pb_file_path, 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='')
def predict(): if request.method == 'POST': # Get the image from post request img = base64_to_pil(request.json) # Save the image to ./uploads img.save("/var/www/FlaskApp/FlaskApp/tf_files/image.png") img = img.resize((224, 224)) print("image saved") # Make prediction #preds = model_predict(img) # Preprocessing the image #x = image.img_to_array(img) # x = np.true_divide(x, 255) #x = np.expand_dims(x, axis=0) # Be careful how your trained model deals with the input # otherwise, it won't make correct prediction! #x = preprocess_input(x, mode='tf') #preds = model.predict(x) #!!!!!!!!!!!!!!!! image_data = tf.io.gfile.GFile( "/var/www/FlaskApp/FlaskApp/tf_files/image.png", 'rb').read() print("Image read") label_lines = [ line.rstrip() for line in tf.io.gfile.GFile( "/var/www/FlaskApp/FlaskApp/tf_files/retrained_labels.txt") ] print("Implementing model") # Unpersists graph from file with tf.io.gfile.GFile( "/var/www/FlaskApp/FlaskApp/tf_files/retrained_graph.pb", 'rb') as f: graph_def = tf.compat.v1.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') print("Model") with tf.compat.v1.Session() as sess: # Feed the image_data as input to the graph and get first prediction softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') predictions = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data}) print("Making Predictions") # Sort to show labels of first prediction in order of confidence top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] for node_id in top_k: human_string = label_lines[node_id] score = predictions[0][node_id] if human_string == 'plastic': #print(str(score)) score = score * 100 score = "{:.2f}".format(score) result = "Plastic-" + str(score) #print('%s (score = %.2f)' % (human_string, score)) # Process your result for human #pred_proba = "{:.3f}".format(np.amax(preds)) # Max probability #pred_class = decode_predictions(preds, top=1) # ImageNet Decode #result = str(pred_class[0][0][1]) # Convert to string #result = result.replace('_', ' ').capitalize() #score = str(score) #result ="Plastic-" + str(score) print(result, "as") # Serialize the result, you can add additional fields return jsonify(result=result) return None
def load(self, graph: Graph): argv = graph.graph['cmd_params'] if argv.tensorflow_custom_layer_libraries: libraries = argv.tensorflow_custom_layer_libraries.split(',') for library in libraries: log.info('Loading library "{}" with custom operations'.format( library)) tf_v1.load_op_library(library) graph_def, variables_values, framework, inputs_outputs_order = load_tf_graph_def( graph_file_name=argv.input_model, is_binary=not argv.input_model_is_text, checkpoint=argv.input_checkpoint, user_output_node_names_list=argv.output, model_dir=argv.saved_model_dir, meta_graph_file=argv.input_meta_graph, saved_model_tags=argv.saved_model_tags) if inputs_outputs_order is not None and isinstance( inputs_outputs_order, tuple): graph.inputs_order = inputs_outputs_order[0] graph.outputs_order = inputs_outputs_order[1] send_framework_info(framework) try: tf_v1.import_graph_def(graph_def, name='') except: log.warning( "TensorFlow post-processing of loaded model was unsuccessful. " "This is an optional step that Model Optimizer performs for any input model but it is not usually " "required for all models. " "It likely means that the original model is ill-formed. " "Model Optimizer will continue converting this model.") log.debug("Number of nodes in graph_def: {}".format(len( graph_def.node))) # pylint: disable=no-member if argv.tensorboard_logdir: tensorboard_util.dump_for_tensorboard(graph_def, argv.tensorboard_logdir) update_extractors_with_extensions(tf_op_extractors) try: protobuf2nx(graph, graph_def) except Exception as e: raise Error( 'Cannot pre-process TensorFlow graph after reading from model file "{}". ' \ 'File is corrupt or has unsupported format. Details: {}. ' + refer_to_faq_msg(44), argv.model_name, str(e) ) from e graph.__setattr__('name', argv.model_name) # 'layout' parameter change may cause an issue in EltwiseInputReshape replacer # and convert_nhwc_to_nchw(graph) graph.graph['layout'] = 'NCHW' if argv.disable_nhwc_to_nchw else 'NHWC' graph.graph['fw'] = 'tf' graph.graph['variables_values'] = variables_values del variables_values used_tensors = restore_edges(graph, get_tf_edges) # Tensor names information corresponding to a node is stored on outgoing edges. # As output nodes do not have outgoing edges, fake outputs are required. In the following code # for each output Identity node is added, and tensor name for the output is kept # on (output, fake output) edge. After Result nodes adding transformation fake outputs # are deleted from graph. add_outputs_identity( graph, graph.nodes - used_tensors, lambda g, output, fake_node_name: g.add_edges_from( [create_tf_edge(output, fake_node_name, 0)])) remove_control_dependency_inputs(graph) graph.check_empty_graph( 'protobuf2nx. It may happen due to problems with loaded model') extract_node_attrs( graph, lambda node: tf_op_extractor( node, check_for_duplicates(tf_op_extractors))) # try to detect layout from the nodes of the graph. If there are no convolution nodes in N(D)HWC layout then we # consider that the graph is in NCHW layout and no layout conversion should be performed if not argv.disable_nhwc_to_nchw and not graph_or_sub_graph_has_nhwc_ops( graph): if not argv.silent: log.debug('disable_nhwc_to_nchw" was automatically enabled.') for_graph_and_each_sub_graph_recursively( graph, update_cmd_params_and_layout) send_op_names_info(framework, graph) send_shapes_info(framework, graph)
def main(path_to_model, graph_pb_type="tf"): if graph_pb_type != "tf": raise KeyError("The type {} is not supported".format(graph_pb_type)) list_of_links = [] # Used to store i/p nodes that are input to the graph and are non-dummy nodes. node_set = set() # Simultaneously creating nx graph to get absolute node positions to space # them appropriately. nxGraph = nx.Graph() dummy_nodes_counter = 0 with tf.gfile.GFile(path_to_model, "rb") as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) not_in_graph_node_to_dummy_map = {} with tf.Session(graph=tf.Graph()) as sess: tf.import_graph_def(graph_def, name="") g = tf.get_default_graph() [graph_inputs, graph_outputs] = analyze_inputs_outputs(g) graph_inputs = [x.name for x in graph_inputs] graph_outputs = [x.name for x in graph_outputs] for node in g.as_graph_def().node: op = g.get_operation_by_name(node.name) for input_node_obj in op.inputs: [name, port] = get_node_name_and_port(input_node_obj.name) try: g.get_operation_by_name(name) source = name except Exception: if name not in not_in_graph_node_to_dummy_map.keys(): source = "dummy" + str(dummy_nodes_counter) not_in_graph_node_to_dummy_map[name] = source dummy_nodes_counter += 1 else: # do not update counter if node name is cached source = not_in_graph_node_to_dummy_map[name] target = node.name # op.name and node.name should be same ideally linkname = source + ":" + str(port) node_set.add(target) node_set.add(source) list_of_links.append((source, target, linkname)) for ctrl_input_node_obj in op.control_inputs: try: g.get_operation_by_name(ctrl_input_node_obj.name) except Exception: print( "Could not find control input {0} in the graph".format( ctrl_input_node_obj.name)) node_set.add(ctrl_input_node_obj.name) list_of_links.append( (ctrl_input_node_obj.name, node.name, "ctrl_input")) for output_node_obj in op.outputs: [output_edge_name, port] = get_node_name_and_port(output_node_obj.name) try: g.get_operation_by_name(output_edge_name) except Exception: source = node.name target = "Dummy" + dummy_nodes_counter dummy_nodes_counter += 1 linkname = source + ":" + str(port) list_of_links.append((source, target, linkname)) node_set.add(source) node_set.add(target) # list provides a better layout than set list_of_nodes = list(node_set) for node in list_of_nodes: nxGraph.add_node(node) for link in list_of_links: nxGraph.add_edge(link[0], link[1]) positions_dict = nx.spectral_layout(nxGraph) # get position from spectral layout which is faster than others. graph = {} graph["nodes"] = [] graph["links"] = [] graph["directories"] = ["Graph"] graph["workload"] = [] graph["op_type"] = [] graph["all_opu_type_names"] = [] opu_type_set = set(["Dummy"]) for index, node in enumerate(g.as_graph_def().node): group = -1 hover_text = "" error_value = 0 opu_type_name = g.get_operation_by_name(node.name).type opu_type_set.add(opu_type_name) graph["nodes"].append({ "name": node.name, "node_info": opu_type_name, "group": group + 1, "hover_text": hover_text, "error": error_value }) # Take position from the layout algorithm. graph["nodes"][index]["x"] = positions_dict[node.name][0] graph["nodes"][index]["y"] = positions_dict[node.name][1] graph["all_opu_type_names"] = list(opu_type_set) for link in list_of_links: port = int(get_node_name_and_port( link[2])[-1]) if not link[2].startswith("ctrl_input") else -1 hover_text = "" if not link[0].startswith("Dummy") and port != -1: related_tensor = g.get_operation_by_name(link[0]).outputs[port] hover_text = "Shape:" + extract_shape( related_tensor) + "<br> Dtype:" + related_tensor.dtype.name graph["links"].append({ "source": link[0], "target": link[1], "linkname": link[0] + ":" + str(port), "hover_info": hover_text, "edge_hist_data": [], "port": port }) graph["input_nodes"] = [] for node in graph_inputs: graph["input_nodes"].append({"name": node}) return graph
def load(self, graph: Graph): argv = graph.graph['cmd_params'] if argv.tensorflow_custom_layer_libraries: libraries = argv.tensorflow_custom_layer_libraries.split(',') for library in libraries: log.info('Loading library "{}" with custom operations'.format( library)) tf_v1.load_op_library(library) graph_def, variables_values = load_tf_graph_def( graph_file_name=argv.input_model, is_binary=not argv.input_model_is_text, checkpoint=argv.input_checkpoint, user_output_node_names_list=argv.output, model_dir=argv.saved_model_dir, meta_graph_file=argv.input_meta_graph, saved_model_tags=argv.saved_model_tags) try: tf_v1.import_graph_def(graph_def, name='') except: log.warning( "TensorFlow post-processing of loaded model was unsuccessful. " "This is an optional step that Model Optimizer performs for any input model but it is not usually " "required for all models." "It likely means that the original model is ill-formed. " "Model Optimizer will continue converting this model.") log.debug("Number of nodes in graph_def: {}".format(len( graph_def.node))) # pylint: disable=no-member if argv.tensorboard_logdir: tensorboard_util.dump_for_tensorboard(graph_def, argv.tensorboard_logdir) update_extractors_with_extensions(tf_op_extractors) try: protobuf2nx(graph, graph_def) except Exception as e: raise Error( 'Cannot pre-process TensorFlow graph after reading from model file "{}". ' \ 'File is corrupt or has unsupported format. Details: {}. ' + refer_to_faq_msg(44), argv.model_name, str(e) ) from e graph.__setattr__('name', argv.model_name) # 'layout' parameter change may cause an issue in EltwiseInputReshape replacer # and convert_nhwc_to_nchw(graph) graph.graph['layout'] = 'NCHW' if argv.disable_nhwc_to_nchw else 'NHWC' graph.graph['fw'] = 'tf' graph.graph['variables_values'] = variables_values del variables_values restore_edges(graph, get_tf_edges) remove_control_dependency_inputs(graph) graph.check_empty_graph( 'protobuf2nx. It may happen due to problems with loaded model') extract_node_attrs( graph, lambda node: tf_op_extractor( node, check_for_duplicates(tf_op_extractors)))
def main(argv): del argv # Unused np_aggregates = None test_count = 0 with tf.device('/cpu:0'): graph_def = tf.GraphDef() with open(FLAGS.model_path, 'rb') as f: graph_def.ParseFromString(f.read()) # generate lists of images to process iml_files = sorted(glob.glob(FLAGS.data_pattern + FLAGS.iml_pattern)) imr_files = sorted(glob.glob(FLAGS.data_pattern + FLAGS.imr_pattern)) if FLAGS.evaluate: gtl_files = sorted( glob.glob(FLAGS.data_pattern + FLAGS.gtl_pattern)) if not FLAGS.predict_right: all_files = zip(iml_files, imr_files, gtl_files) else: gtr_files = sorted( glob.glob(FLAGS.data_pattern + FLAGS.gtr_pattern)) all_files = zip(iml_files, imr_files, gtl_files, gtr_files) else: all_files = zip(iml_files, imr_files) for file_names in all_files: if test_count > FLAGS.max_test_number: break print(file_names) # Load new pair of images to process. np_images, np_gt = load_images(file_names, FLAGS.input_channels, FLAGS.crop_left, FLAGS.crop_right, FLAGS.crop_top, FLAGS.crop_bottom) filename = file_names[0].replace('.png', '') with tf.Graph().as_default() as default_graph: tf.import_graph_def(graph_def, name='graph') # Setup input-output tensors for the frozen model. xl = default_graph.get_tensor_by_name('graph/input:0') reference = default_graph.get_tensor_by_name( 'graph/reference_output_disparity:0') if FLAGS.predict_right: secondary = default_graph.get_tensor_by_name( 'graph/secondary_output_disparity:0') # Run the model. with tf.Session(graph=default_graph) as sess: feed_dict = {xl: np.expand_dims(np_images, 0)} if FLAGS.predict_right: (reference_disparity, secondary_disparity) = sess.run( (reference, secondary), feed_dict=feed_dict) else: reference_disparity = sess.run(reference, feed_dict=feed_dict) if FLAGS.evaluate: if FLAGS.predict_right: # Treat left and predictions as separate examples, same as in PSMNet # code. np_result_reference = evaluate(reference_disparity, np_gt[:, :, :1]) np_result_secondary = evaluate(secondary_disparity, np_gt[:, :, 1:]) np_result = np_result_reference + np_result_secondary np_result = 0.5 * np_result else: np_result = evaluate(reference_disparity, np_gt) if np_aggregates is not None: np_aggregates += np_result else: np_aggregates = np_result if not test_count % _PRINT_SUMMARY_EVERY_X_LINES: to_print = np_aggregates / ((float)(test_count + 1)) print(test_count, to_print) # Save output disparity. if FLAGS.save_png: encode_image_as_16bit_png( reference_disparity[0, :, :, 0] * FLAGS.png_disparity_factor, filename + '_reference.png') if FLAGS.predict_right: encode_image_as_16bit_png( secondary_disparity[0, :, :, 0] * FLAGS.png_disparity_factor, filename + '_secondary.png') if FLAGS.save_pfm: encode_image_as_pfm( reference_disparity[0, :, :, 0] * FLAGS.png_disparity_factor, filename + '_reference.pfm') if FLAGS.predict_right: encode_image_as_16bit_png( secondary_disparity[0, :, :, 0] * FLAGS.png_disparity_factor, filename + '_secondary.pfm') test_count += 1 if FLAGS.evaluate: print('Images processed:') print(test_count) print('psm_epe bad_0.1 bad_0.5 bad_1.0 bad_2.0 bad_3.0') to_print = np_aggregates / ((float)(test_count)) print(to_print)
if not os.path.exists(MODEL_FILE): opener = urllib.request.URLopener() opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE) tar_file = tarfile.open(MODEL_FILE) for file in tar_file.getmembers(): file_name = os.path.basename(file.name) if "frozen_inference_graph.pb" in file_name: tar_file.extract(file, os.getcwd()) detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, "rb") as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name="") NUM_CLASSES = 50 # loading specified class/category description label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=NUM_CLASSES, use_display_name=True ) category_index = label_map_util.create_category_index(categories) # some helper code def load_image_into_numpy_array(image): (im_width, im_height) = image.size return np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)
# network predicts `5`, we know that this corresponds to `airplane`. # Here we use internal utility functions, but anything that returns a # dictionary mapping integers to appropriate string labels would be fine label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=NUM_CLASSES, use_display_name=True) category_index = label_map_util.create_category_index(categories) # Load the Tensorflow model into memory. detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') sess = tf.Session(graph=detection_graph) # Define input and output tensors (i.e. data) for the object detection classifier # Input tensor is the image image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Output tensors are the detection boxes, scores, and classes # Each box represents a part of the image where a particular object was detected detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') # Each score represents level of confidence for each of the objects. # The score is shown on the result image, together with the class label. detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
#!/usr/bin/python3 #import tensorflow as tf import tensorflow.compat.v1 as tf tf.disable_v2_behavior() import sys import os model = sys.argv[1] graph = tf.get_default_graph() graph_def = graph.as_graph_def() graph_def.ParseFromString(tf.gfile.FastGFile(model, 'rb').read()) tf.import_graph_def(graph_def, name='graph') os.system('rm -f log/*') summaryWriter = tf.summary.FileWriter('log/', graph) os.system('tensorboard --logdir log/')
# Just disables the warning, doesn't enable AVX/FMA import os import tensorflow.compat.v1 as tf import numpy as np import PIL # from nets.mobilenet import mobilenet_v2 # suppress warning about AVX/FMA instruction set for CPU (this app does not use cpu) os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' img = np.array(PIL.Image.open('panda.jpeg').resize( (224, 224))).astype(np.float) / 128 - 1 gd = tf.GraphDef.FromString( open('mobilenet_v2_0.35_96' + '_frozen.pb', 'rb').read()) inp, predictions = tf.import_graph_def( gd, return_elements=['input:0', 'MobilenetV2/Predictions/Reshape_1:0']) def load_graph(frozen_graph_filename): # We load the protobuf file from the disk and parse it to retrieve the # unserialized graph_def with tf.gfile.GFile(frozen_graph_filename, "rb") as f: graph_def = tf.compat.v1.GraphDef() graph_def.ParseFromString(f.read()) # Then, we import the graph_def into a new Graph and returns it with tf.Graph().as_default() as graph: # The name var will prefix every op/nodes in your graph # Since we load everything in a new graph, this is not needed tf.import_graph_def(graph_def, name="prefix") return graph
def load_frozenmodel(): print('> Loading frozen model into memory') config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=log_device) config.gpu_options.allow_growth = allow_memory_growth config.gpu_options.per_process_gpu_memory_fraction = 0.5 ###Jetson only if not split_model: detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(model_path, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') return detection_graph, None, None else: # load a frozen Model and split it into GPU and CPU graphs # Hardcoded for ssd_mobilenet input_graph = tf.Graph() with tf.Session(graph=input_graph, config=config): if ssd_shape == 600: shape = 7326 print('ssd_shape = 600 :(') exit() else: shape = 1917 score = tf.placeholder(tf.float32, shape=(None, shape, num_classes), name="Postprocessor/convert_scores") expand = tf.placeholder(tf.float32, shape=(None, shape, 1, 4), name="Postprocessor/ExpandDims_1") for node in input_graph.as_graph_def().node: if node.name == "Postprocessor/convert_scores": score_def = node if node.name == "Postprocessor/ExpandDims_1": expand_def = node detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(model_path, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) dest_nodes = [ 'Postprocessor/convert_scores', 'Postprocessor/ExpandDims_1' ] edges = {} name_to_node_map = {} node_seq = {} seq = 0 for node in od_graph_def.node: n = _node_name(node.name) name_to_node_map[n] = node edges[n] = [_node_name(x) for x in node.input] node_seq[n] = seq seq += 1 for d in dest_nodes: assert d in name_to_node_map, "%s is not in graph" % d nodes_to_keep = set() next_to_visit = dest_nodes[:] while next_to_visit: n = next_to_visit[0] del next_to_visit[0] if n in nodes_to_keep: continue nodes_to_keep.add(n) next_to_visit += edges[n] nodes_to_keep_list = sorted(list(nodes_to_keep), key=lambda n: node_seq[n]) nodes_to_remove = set() for n in node_seq: if n in nodes_to_keep_list: continue nodes_to_remove.add(n) nodes_to_remove_list = sorted(list(nodes_to_remove), key=lambda n: node_seq[n]) keep = graph_pb2.GraphDef() for n in nodes_to_keep_list: keep.node.extend([copy.deepcopy(name_to_node_map[n])]) remove = graph_pb2.GraphDef() remove.node.extend([score_def]) remove.node.extend([expand_def]) for n in nodes_to_remove_list: remove.node.extend([copy.deepcopy(name_to_node_map[n])]) with tf.device('/gpu:0'): tf.import_graph_def(keep, name='') with tf.device('/cpu:0'): tf.import_graph_def(remove, name='') return detection_graph, score, expand
def freeze_checkpoints(graph_def: tf_v1.GraphDef, checkpoint_dir: str, output_node_names: list): """ Loads all the variables in a graph and stores them in a separate dictionary. Freezes output nodes in the graph :param graph_def: GraphDef object holding the network. :param checkpoint_dir: path to directory with checkpoint files with values of graph variables. :param output_node_names: list of output node names. :return: GraphDef containing a simplified version of the original. """ log.debug( "Loading checkpoint files from directory: {}".format(checkpoint_dir)) checkpoint_files = [] for checkpoint_name in sorted(os.listdir(checkpoint_dir)): checkpoint_path = os.path.join(checkpoint_dir, checkpoint_name) if os.path.isfile(checkpoint_path): checkpoint_files.append(checkpoint_path) log.debug("File {} will be loaded".format(checkpoint_path)) else: log.debug("Path {} is not a file. Skipping") if len(checkpoint_files) == 0: raise Error("There are no checkpoint files in directory: {}".format( checkpoint_dir)) tf_v1.import_graph_def(graph_def, name='') with tf_v1.Session() as sess: uninitialized_variables = [ str(v, 'utf-8') for v in set(sess.run(tf_v1.report_uninitialized_variables())) ] all_variables = [ n.name for n in sess.graph.as_graph_def().node if n.op in ['Variable', 'VariableV2'] ] white_list = [ v for v in all_variables if v not in uninitialized_variables ] black_list = [v for v in all_variables if v in uninitialized_variables] output_graph_def = tf_v1.graph_util.convert_variables_to_constants( sess, graph_def, output_node_names, variable_names_whitelist=white_list, variable_names_blacklist=black_list) variable_values = {} for checkpoint_file in checkpoint_files: log.debug("Loading {}".format(checkpoint_file)) with tf_v1.Session() as sess: var_list = {} var_to_shape_map = tf_v1.train.load_checkpoint( checkpoint_file).get_variable_to_shape_map() for key in var_to_shape_map: try: tensor = sess.graph.get_operation_by_name(key).outputs[0] except KeyError: continue var_list[key] = tensor tf_v1.train.Saver(var_list=var_list).restore(sess, checkpoint_file) for name, tensor in var_list.items(): variable_values[name] = sess.run(tensor) return output_graph_def, variable_values
#imgs = tf.reverse(imgs, axis=[-1]) #convert rgb to bgr #imgs = input_image[..., ::-1] imgs = tf.transpose(imgs, [2, 0, 1]) #imgs = imgs / 255 #imgs = imgs - mean #imgs = imgs / std # imgs = tf.reshape(imgs, (-1, 3, 299, 299)) imgs = tf.expand_dims(imgs, 0) # imgs = tf.transpose(imgs, [2, 0, 1]) #img_float32 = tf.image.convert_image_dtype(imgs, dtype=tf.float32, saturate=False) #img_float32 = tf.cast(imgs, dtype=tf.float32) img_float32 = imgs # import the model graph with the new input tf.import_graph_def(graph_model_def, name='', input_map={"input:0": img_float32}) builder = tf.saved_model.builder.SavedModelBuilder(saved_model_path) inp = sess.graph.get_tensor_by_name('input_image:0') out = sess.graph.get_tensor_by_name('global_descriptor:0') signature = tf.saved_model.signature_def_utils.predict_signature_def( inputs={'input_image': input_image}, outputs={'global_descriptor': out}) builder.add_meta_graph_and_variables( sess=sess, tags=[tf.saved_model.tag_constants.SERVING], signature_def_map={ tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature })
std = np.std(x) std_adj = np.maximum(std, 1.0 / np.sqrt(x.size)) y = np.multiply(np.subtract(x, mean), 1 / std_adj) return y if __name__ == '__main__': with tf.Graph().as_default(): with tf.Session() as sess: #load model with tf.gfile.FastGFile(str(args.model + "/20180402-114759.pb"), 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') # facenet.load_model(args.model) img_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") img_file = [] output_dir = 'processed_data/embedded_face_val/{}'.format( args.model.split("/")[3]) labels = pd.read_csv(args.pathfile)
def main(args): dataset = facenet.get_dataset(args.dataset_dir) with tf.Graph().as_default(): # Get a list of image paths and their labels image_list, label_list = facenet.get_image_paths_and_labels(dataset) nrof_images = len(image_list) image_indices = range(nrof_images) image_batch, label_batch = facenet.read_and_augment_data(image_list, image_indices, args.image_size, args.batch_size, None, False, False, False, nrof_preprocess_threads=4, shuffle=False) model_exp = os.path.expanduser(args.model_file) with gfile.FastGFile(model_exp,'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) input_map={'input':image_batch, 'phase_train':False} tf.import_graph_def(graph_def, input_map=input_map, name='net') embeddings = tf.get_default_graph().get_tensor_by_name("net/embeddings:0") with tf.Session() as sess: tf.train.start_queue_runners(sess=sess) embedding_size = int(embeddings.get_shape()[1]) nrof_batches = int(math.ceil(nrof_images / args.batch_size)) nrof_classes = len(dataset) label_array = np.array(label_list) class_names = [cls.name for cls in dataset] nrof_examples_per_class = [ len(cls.image_paths) for cls in dataset ] class_variance = np.zeros((nrof_classes,)) class_center = np.zeros((nrof_classes,embedding_size)) distance_to_center = np.ones((len(label_list),))*np.NaN emb_array = np.zeros((0,embedding_size)) idx_array = np.zeros((0,), dtype=np.int32) lab_array = np.zeros((0,), dtype=np.int32) index_arr = np.append(0, np.cumsum(nrof_examples_per_class)) for i in range(nrof_batches): t = time.time() emb, idx = sess.run([embeddings, label_batch]) emb_array = np.append(emb_array, emb, axis=0) idx_array = np.append(idx_array, idx, axis=0) lab_array = np.append(lab_array, label_array[idx], axis=0) for cls in set(lab_array): cls_idx = np.where(lab_array==cls)[0] if cls_idx.shape[0]==nrof_examples_per_class[cls]: # We have calculated all the embeddings for this class i2 = np.argsort(idx_array[cls_idx]) emb_class = emb_array[cls_idx,:] emb_sort = emb_class[i2,:] center = np.mean(emb_sort, axis=0) diffs = emb_sort - center dists_sqr = np.sum(np.square(diffs), axis=1) class_variance[cls] = np.mean(dists_sqr) class_center[cls,:] = center distance_to_center[index_arr[cls]:index_arr[cls+1]] = np.sqrt(dists_sqr) emb_array = np.delete(emb_array, cls_idx, axis=0) idx_array = np.delete(idx_array, cls_idx, axis=0) lab_array = np.delete(lab_array, cls_idx, axis=0) print('Batch %d in %.3f seconds' % (i, time.time()-t)) print('Writing filtering data to %s' % args.data_file_name) mdict = {'class_names':class_names, 'image_list':image_list, 'label_list':label_list, 'distance_to_center':distance_to_center } with h5py.File(args.data_file_name, 'w') as f: for key, value in iteritems(mdict): f.create_dataset(key, data=value)