def main(argv=None): if FLAGS.tiny: model = yolov3_tiny_3l.yolo_v3_tiny elif FLAGS.dense: model = dense_yolov3_v1.dense_yolo_v3 else: model = yolo_v3.yolo_v3 classes = load_names(FLAGS.class_names) # placeholder for detector inputs inputs = tf.placeholder(tf.float32, [None, FLAGS.size, FLAGS.size, 3], "inputs") with tf.variable_scope('detector'): detections = model(inputs, len(classes), data_format=FLAGS.data_format) load_ops = load_weights(tf.global_variables(scope='detector'), FLAGS.weights_file) # Sets the output nodes in the current session boxes = detections_boxes(detections) with tf.Session() as sess: sess.run(load_ops) freeze_graph(sess, FLAGS.output_graph) writer = tf.summary.FileWriter("logs/", sess.graph)
def main(argv=None): if FLAGS.tiny: model = yolo_v3_tiny.yolo_v3_tiny else: model = yolo_v3.yolo_v3 config = configparser.ConfigParser(strict=False) config.read(FLAGS.model_config) classes = load_coco_names(FLAGS.class_names) # placeholder for detector inputs inputs = tf.placeholder(tf.float32, [ None, config.getint("net", "height"), config.getint("net", "width"), 3 ], "inputs") with tf.variable_scope('detector'): detections = model(inputs, len(classes), data_format=FLAGS.data_format) load_ops = load_weights(tf.global_variables(scope='detector'), FLAGS.weights_file) # Sets the output nodes in the current session boxes = detections_boxes(detections) with tf.Session() as sess: sess.run(load_ops) freeze_graph(sess, FLAGS.output_graph)
def main(argv=None): if FLAGS.tiny: model = yolo_v3_tiny.yolo_v3_tiny elif FLAGS.spp: model = yolo_v3.yolo_v3_spp else: model = yolo_v3.yolo_v3 classes = load_coco_names(FLAGS.class_names) # placeholder for detector inputs inputs = tf.placeholder( tf.float32, [None, FLAGS.size, FLAGS.size, 3], "inputs") with tf.variable_scope('detector'): detections = model(inputs, len(classes), data_format=FLAGS.data_format) load_ops = load_weights(tf.global_variables( scope='detector'), FLAGS.weights_file) # Sets the output nodes in the current session boxes = detections_boxes(detections) with tf.Session() as sess: sess.run(load_ops) savepb(sess, FLAGS.output_graph)
def main(argv=None): if FLAGS.tiny: model = yolo_v3_tiny.yolo_v3_tiny print ('doing tiny') else: model = yolo_v3.yolo_v3 classes = load_coco_names(FLAGS.class_names) print ('num classes',len(classes)) # placeholder for detector inputs inputs = tf.placeholder(tf.float32, [None, FLAGS.size, FLAGS.size, 3], "inputs") with tf.variable_scope('detector'): detections = model(inputs, len(classes), data_format=FLAGS.data_format) load_ops = load_weights(tf.global_variables(scope='detector'), FLAGS.weights_file) #detect_1.shape = (?, 507, 85) #detect_2.shape = (?, 2028, 85) #detect_3.shape = (?, 8112, 85) #detections.shape = (?, 10647, 85) #detections = Tensor("detector/yolo-v3/detections:0", shape=(?, 10647, 85), dtype=float32) print("detections.shape =", detections.shape) print(detections) print(detections.name) # Sets the output nodes in the current session boxes = detections_boxes(detections) with tf.Session() as sess: sess.run(load_ops) freeze_graph(sess, FLAGS.output_graph, FLAGS.tiny)
def main(argv=None): if FLAGS.tiny: model = yolo_v3_tiny.yolo_v3_tiny elif FLAGS.spp: model = yolo_v3.yolo_v3_spp else: model = yolo_v3.yolo_v3 classes = load_coco_names(FLAGS.class_names) # 定义网络对外的接口, 服务器端是自动base64解码,所以拿到的数据已经解码过了 # 但还需将byte格式转换为图像矩阵格式 jpeg_vec_bytes = tf.placeholder(tf.string, shape=None, name=None) jpeg_sca_bytes = tf.reshape(jpeg_vec_bytes, []) # jpeg_ndarr = tf.image.decode_jpeg(jpeg_sca_bytes, fancy_upscaling=False) # 从字符串变为数组,且将标量形式字节流解码成图片,!!!这里参数必须设置成False否则不能与客服端的结果匹配 jpeg_ndarr = tf.image.resize_images(jpeg_ndarr, [FLAGS.size, FLAGS.size], method=0) # 将图片拉伸成希望的尺寸 inputs = tf.reshape(jpeg_ndarr, [1, FLAGS.size, FLAGS.size, 3], "inputs") # placeholder for detector inputs 原址输入参量处 # inputs = tf.placeholder(tf.float32, [None, FLAGS.size, FLAGS.size, 3], "inputs") # 加载yolov3模型 with tf.variable_scope('detector'): detections = model(inputs, len(classes), data_format=FLAGS.data_format) # 得到yolov3整体模型(包含模型输出(?, 10647, (num_classes + 5))) load_ops = load_weights(tf.global_variables(scope='detector'), FLAGS.weights_file) # Sets the output nodes in the current session boxes = detections_boxes(detections) # 1,将整体输出分解为box结果与概率数值结果;2、将结果名称定义为output_boxes再放入graph中 # checkpoint读取 with tf.Session() as sess: sess.run(load_ops) reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path) var_to_shape_map = reader.get_variable_to_shape_map() for key in var_to_shape_map: print("tensor_name: ", key) ############################################# # output_node_names = ["output_boxes","inputs",] # output_node_names = ",".join(output_node_names) # # output_graph_def = tf.graph_util.convert_variables_to_constants( # sess,tf.get_default_graph().as_graph_def(),output_node_names.split(",")) # # with tf.gfile.GFile(FLAGS.output_graph, "wb") as f: # f.write(output_graph_def.SerializeToString()) # print("{} ops written to {}.".format(len(output_graph_def.node), FLAGS.output_graph)) ############################################# # pb_savemodel模式存储 export_path = 'models/pb/20191226' builder = tf.saved_model.builder.SavedModelBuilder(export_path) images = tf.saved_model.utils.build_tensor_info(jpeg_vec_bytes) boxes = tf.saved_model.utils.build_tensor_info(boxes) prediction_signature = ( tf.saved_model.signature_def_utils.build_signature_def( inputs={'images': images}, outputs={'scores': boxes}, method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)) builder.add_meta_graph_and_variables( sess, [tf.saved_model.tag_constants.SERVING], signature_def_map={'predict_images': prediction_signature}, main_op=tf.tables_initializer(), strip_default_attrs=True) builder.save()
def main(argv=None): if FLAGS.tiny: model = yolo_v3_tiny.yolo_v3_tiny elif FLAGS.spp: model = yolo_v3.yolo_v3_spp else: model = yolo_v3.yolo_v3 classes = load_coco_names(FLAGS.class_names) # placeholder for detector inputs inputs = tf.placeholder(tf.float32, [None, FLAGS.size, FLAGS.size, 3], "inputs") with tf.variable_scope('detector'): detections = model( inputs, len(classes), data_format=FLAGS.data_format ) # 得到yolov3整体模型(包含模型输出(?, 10647, (num_classes + 5))) load_ops = load_weights(tf.global_variables(scope='detector'), FLAGS.weights_file) # Sets the output nodes in the current session boxes = detections_boxes( detections) # 1,将整体输出分解为box结果与概率数值结果;2、将结果名称定义为output_boxes再放入graph中 with tf.Session() as sess: sess.run(load_ops) reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path) var_to_shape_map = reader.get_variable_to_shape_map() for key in var_to_shape_map: print("tensor_name: ", key) freeze_graph(sess, FLAGS.output_graph)
def __init__(self, tiny=False, cls_file='coco.names', img_size=(416, 416), data_format='NHWC', ckpt_file='./saved_model/model.ckpt', conf_threshold=0.5, iou_threshold=0.4): """ Wrapper class for the YOLO v3 detector. :param tiny: if you want to use tiny yolo :param cls_file: file storing detection classes :param img_size: tuple storing image size :param data_format: Data format: NCHW (gpu only) / NHWC :param ckpt_file: path to model checkpoint file """ self.tiny = tiny self.size = img_size self.data_format = data_format # Temporary solution self.ckpt_file = ckpt_file if not tiny else '.' + ckpt_file.split( '.')[1] + '-tiny.ckpt' self.conf_threshold = conf_threshold self.iou_threshold = iou_threshold if self.tiny: self.model = yolo_v3_tiny.yolo_v3_tiny else: self.model = yolo_v3.yolo_v3 self.classes = load_coco_names(cls_file) self.inputs = tf.placeholder(tf.float32, [1, self.size[0], self.size[1], 3]) with tf.variable_scope('detector'): self.detections = self.model(self.inputs, len(self.classes), data_format=self.data_format) self.saver = tf.train.Saver(var_list=tf.global_variables( scope='detector')) self.boxes = detections_boxes(self.detections) self.sess = tf.Session() self.saver.restore(self.sess, self.ckpt_file) print('Model restored.')
def main(): if tiny: model = yolo_v3_tiny.yolo_v3_tiny else: model = yolo_v3.yolo_v3 classes = load_coco_names(class_names) # placeholder for detector inputs inputs = tf.placeholder(tf.float32, [None, size, size, 3], "inputs") with tf.variable_scope('detector'): detections = model(inputs, len(classes), data_format=data_format) load_ops = load_weights(tf.global_variables(scope='detector'), weights_file) # Sets the output nodes in the current session boxes = detections_boxes(detections) with tf.Session() as sess: sess.run(load_ops) freeze_graph(sess, output_graph)
def main(argv=None): if FLAGS.tiny: model = yolo_v3_tiny.yolo_v3_tiny else: model = yolo_v3.yolo_v3 img = Image.open(FLAGS.input_img) img_resized = img.resize(size=(FLAGS.size, FLAGS.size)) classes = load_coco_names(FLAGS.class_names) # placeholder for detector inputs inputs = tf.placeholder(tf.float32, [1, FLAGS.size, FLAGS.size, 3]) with tf.variable_scope('detector'): detections = model(inputs, len(classes), data_format=FLAGS.data_format) saver = tf.train.Saver(var_list=tf.global_variables(scope='detector')) boxes = detections_boxes(detections) with tf.Session() as sess: saver.restore(sess, FLAGS.ckpt_file) print('Model restored.') detected_boxes = sess.run( boxes, feed_dict={inputs: [np.array(img_resized, dtype=np.float32)]}) filtered_boxes = non_max_suppression( detected_boxes, confidence_threshold=FLAGS.conf_threshold, iou_threshold=FLAGS.iou_threshold) draw_boxes(filtered_boxes, img, classes, (FLAGS.size, FLAGS.size)) img.save(FLAGS.output_img)
def convert_to_original_size(box, size, original_size): ratio = original_size / size box = box.reshape(2, 2) * ratio return list(box.reshape(-1)) #### classes = load_coco_names(class_names) inputs = tf.placeholder(tf.float32, [None, size, size, 3]) with tf.variable_scope('detector2'): detections, cl_de = yolo_v3(inputs, len(classes), data_format='NHWC') load_ops = load_weights(tf.global_variables(scope='detector2'), weights_file) boxes, attrs = detections_boxes(detections) gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.80) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) sess.run(load_ops) def py_nms(boxes, scores, max_boxes=50, iou_thresh=0.5): """ Pure Python NMS baseline. Arguments: boxes => shape of [-1, 4], the value of '-1' means that dont know the exact number of boxes scores => shape of [-1,] max_boxes => representing the maximum of boxes to be selected by non_max_suppression iou_thresh => representing iou_threshold for deciding to keep boxes """
def yolo_full(): ''' if FLAGS.tiny: model = yolo_v3_tiny.yolo_v3_tiny ckpt_file = './saved_model/yolov3-tiny.ckpt' else: model = yolo_v3.yolo_v3 ckpt_file = './saved_model/yolov3.ckpt' ''' model = yolo_v3_tiny.yolo_v3_tiny ckpt_file = './save_model/tiny/yolov3-tiny.ckpt' img = Image.open(FLAGS.input_img) img_resized = img.resize(size=(FLAGS.size, FLAGS.size)) classes = load_coco_names(FLAGS.class_names) # placeholder for detector inputs inputs = tf.placeholder(tf.float32, [1, FLAGS.size, FLAGS.size, 3]) with tf.variable_scope('detector'): detections = model(inputs, len(classes), data_format=FLAGS.data_format) saver = tf.train.Saver(var_list=tf.global_variables(scope='detector')) boxes = detections_boxes(detections) session_conf = tf.ConfigProto(intra_op_parallelism_threads=FLAGS.thread, inter_op_parallelism_threads=FLAGS.thread, \ allow_soft_placement=True, device_count = {'GPU': 1}) with tf.Session(config=session_conf) as sess: saver.restore(sess, ckpt_file) tf.summary.FileWriter("TensorBoard/", graph=sess.graph) print(">>>>>>>>>>>>>>>>> %d" % len(tf.get_default_graph().as_graph_def().node)) print('Model restored.') start = time.time() detected_boxes = sess.run( boxes, feed_dict={inputs: [np.array(img_resized, dtype=np.float32)]}) end = time.time() print("%2.2f secs" % (end - start)) ''' opts = tf.profiler.ProfileOptionBuilder.float_operation() flops = tf.profiler.profile(tf.get_default_graph() , run_meta=tf.RunMetadata(), cmd='op', options=opts) if flops is not None: #print('Flops should be ~',2*25*16*9) #print('25 x 25 x 9 would be',2*25*25*9) # ignores internal dim, repeats first print('TF stats gives',flops.total_float_ops) ''' #output_node_names = "detector/yolo-v3-tiny/detections" output_node_names = "concat_1" output_graph_def = graph_util.convert_variables_to_constants( sess=sess, input_graph_def=sess.graph_def, output_node_names=output_node_names.split(",")) print(">>>>>>>>>>> %d ops in the final graph." % len(output_graph_def.node)) with tf.gfile.GFile("save_model/tiny/pb/frozen_model_yolov3-tiny.pb", "wb") as f: f.write(output_graph_def.SerializeToString()) #builder = tf.saved_model.builder.SavedModelBuilder('./savemodel') #builder.add_meta_graph_and_variables(sess, ['cpu_server_1']) #builder.save() print(detected_boxes.shape) #print (detected_boxes[0,1,1]) #print (np.array(img_resized, dtype=np.float32)[111,111]) #print (inputs.shape) filtered_boxes = non_max_suppression( detected_boxes, confidence_threshold=FLAGS.conf_threshold, iou_threshold=FLAGS.iou_threshold) draw_boxes(filtered_boxes, img, classes, (FLAGS.size, FLAGS.size)) img.save(FLAGS.output_img)