def main(argv):

    flags = parser(
        description="freeze yolov3 graph from checkpoint file").parse_args()
    print("=> the input image size is [%d, %d]" %
          (flags.image_h, flags.image_w))
    anchors = utils.get_anchors(flags.anchors_path, flags.image_h,
                                flags.image_w)
    model = yolov3.yolov3(flags.num_classes, anchors)

    with tf.Graph().as_default() as graph:
        sess = tf.Session(graph=graph)
        inputs = tf.placeholder(tf.float32,
                                [1, flags.image_h, flags.image_w, 3
                                 ])  # placeholder for detector inputs
        print("=>", inputs)

        with tf.variable_scope('yolov3'):
            feature_map = model.forward(inputs, is_training=False)

        boxes, confs, probs = model.predict(feature_map)
        scores = confs * probs
        print("=>", boxes.name[:-2], scores.name[:-2])
        cpu_out_node_names = [boxes.name[:-2], scores.name[:-2]]
        boxes, scores, labels = utils.gpu_nms(
            boxes,
            scores,
            flags.num_classes,
            score_thresh=flags.score_threshold,
            iou_thresh=flags.iou_threshold)
        print("=>", boxes.name[:-2], scores.name[:-2], labels.name[:-2])
        gpu_out_node_names = [
            boxes.name[:-2], scores.name[:-2], labels.name[:-2]
        ]
        feature_map_1, feature_map_2, feature_map_3 = feature_map
        saver = tf.train.Saver(var_list=tf.global_variables(scope='yolov3'))

        if flags.convert:
            if not os.path.exists(flags.weights_path):
                url = 'https://github.com/YunYang1994/tensorflow-yolov3/releases/download/v1.0/yolov3.weights'
                for i in range(3):
                    time.sleep(1)
                    print("=> %s does not exists ! " % flags.weights_path)
                print("=> It will take a while to download it from %s" % url)
                print('=> Downloading yolov3 weights ... ')
                wget.download(url, flags.weights_path)

            load_ops = utils.load_weights(tf.global_variables(scope='yolov3'),
                                          flags.weights_path)
            sess.run(load_ops)
            save_path = saver.save(sess, save_path=flags.ckpt_file)
            print('=> model saved in path: {}'.format(save_path))

        if flags.freeze:
            saver.restore(sess, flags.ckpt_file)
            print('=> checkpoint file restored from ', flags.ckpt_file)
            utils.freeze_graph(sess, './checkpoint/yolov3_cpu_nms.pb',
                               cpu_out_node_names)
            utils.freeze_graph(sess, './checkpoint/yolov3_gpu_nms.pb',
                               gpu_out_node_names)
Exemple #2
0
    def convert_weights(self):
        print(f"=> the input image size is [{self.img_h}, {self.img_w}]")
        anchors = utils.get_anchors(self.anchors_path, self.img_h, self.img_w)
        model = yolov3.yolov3(self.num_classes, anchors)

        with tf.Graph().as_default() as graph:
            sess = tf.Session(graph=graph)
            inputs = tf.placeholder(tf.float32,
                                    [1, self.img_h, self.img_w, 1
                                     ])  # placeholder for detector inputs
            print("=>", inputs)

            with tf.variable_scope('yolov3'):
                feature_map = model.forward(inputs,
                                            n_filters_dn=self.n_filters_dn,
                                            n_strides_dn=self.n_strides_dn,
                                            n_ksizes_dn=self.n_ksizes_dn,
                                            is_training=False)

            boxes, confs, probs = model.predict(feature_map)
            scores = confs * probs
            print("=>", boxes.name[:-2], scores.name[:-2])
            cpu_out_node_names = [boxes.name[:-2], scores.name[:-2]]
            boxes, scores, labels = utils.gpu_nms(boxes, scores,
                                                  self.num_classes)
            print("=>", boxes.name[:-2], scores.name[:-2], labels.name[:-2])
            gpu_out_node_names = [
                boxes.name[:-2], scores.name[:-2], labels.name[:-2]
            ]

            saver = tf.train.Saver(var_list=tf.global_variables(
                scope='yolov3'))

            if self.convert:
                load_ops = utils.load_weights(
                    tf.global_variables(scope='yolov3'), self.weights_dir)
                sess.run(load_ops)
                save_path = saver.save(sess, save_path=self.checkpoint_dir)
                print(f'=> model saved in path: {save_path}')

            if self.freeze:
                ckpt_idx = self.checkpoint_dir + '-' + str(
                    self.checkpoint_step)
                try:
                    saver.restore(sess, ckpt_idx)
                except:
                    print(
                        f"Error: you tried to restore a checkpoint ({self.checkpoint_dir}) that doesn't exist."
                    )
                    print(
                        "Please clear the network and retrain, or load a different checkpoint by changing the steps parameter."
                    )
                print('=> checkpoint file restored from ', ckpt_idx)
                utils.freeze_graph(sess,
                                   '../../data/checkpoint/yolov3_cpu_nms.pb',
                                   cpu_out_node_names)
                utils.freeze_graph(sess,
                                   '../../data/checkpoint/yolov3_gpu_nms.pb',
                                   gpu_out_node_names)
def main(argv=None):
    input_data = tf.placeholder(dtype=tf.float32,
                                shape=(None, 608, 608, 3),
                                name='inputs')
    model = YOLOV3(input_data, trainable=False, tiny=FLAGS.tiny)
    load_ops = load_weights(tf.global_variables(), FLAGS.weights_file)

    with tf.Session() as sess:
        sess.run(load_ops)
        freeze_graph(sess, FLAGS.output_graph)
Exemple #4
0
def main(argv):
    flags = parser(
        description="freeze yolov3 graph from checkpoint file").parse_args()
    print("=> the input image size is [%d, %d]" %
          (flags.image_h, flags.image_w))
    anchors = utils.get_anchors(flags.anchors_path, flags.image_h,
                                flags.image_w)
    # print(anchors)
    # exit()
    model = YOLOv3.yolov3(flags.num_classes, anchors)

    with tf.Graph().as_default() as graph:
        sess = tf.Session(graph=graph)
        inputs = tf.placeholder(tf.float32,
                                [1, flags.image_h, flags.image_w, 3
                                 ])  # placeholder for detector inputs
        print("=>", inputs)

        with tf.variable_scope('yolov3'):
            feature_map = model.forward(
                inputs, is_training=False)  # 返回3个尺度的feature_map

        # 获取网络给出绝对boxes(左上角,右下角)信息, 未经过最大抑制去除多余boxes
        boxes, confs, probs = model.predict(feature_map)
        scores = confs * probs
        print("=>", boxes.name[:-2], scores.name[:-2])
        # cpu 运行是恢复模型所需要的网络节点的名字
        cpu_out_node_names = [boxes.name[:-2], scores.name[:-2]]
        print('cpu_out_node_names: ', cpu_out_node_names)
        boxes, scores, labels = utils.gpu_nms(
            boxes,
            scores,
            flags.num_classes,
            score_thresh=flags.score_threshold,
            iou_thresh=flags.iou_threshold)
        print("=>", boxes.name[:-2], scores.name[:-2], labels.name[:-2])
        # gpu 运行是恢复模型所需要的网络节点的名字 , 直接运算得出最终结果
        gpu_out_node_names = [
            boxes.name[:-2], scores.name[:-2], labels.name[:-2]
        ]
        feature_map_1, feature_map_2, feature_map_3 = feature_map
        saver = tf.train.Saver(var_list=tf.global_variables(scope='yolov3'))

        if flags.freeze:
            saver.restore(sess, flags.ckpt_file)
            print('=> checkpoint file restored from ', flags.ckpt_file)
            utils.freeze_graph(sess, '../checkpoint/yolov3_cpu_nms.pb',
                               cpu_out_node_names)
            utils.freeze_graph(sess, '../checkpoint/yolov3_gpu_nms.pb',
                               gpu_out_node_names)
def main(argv):

    flags = parser(
        description="freeze yolov3 graph from checkpoint file").parse_args()
    classes = utils.read_coco_names("./data/coco.names")
    num_classes = len(classes)
    SIZE = flags.image_size
    print("=> the input image size is [%d, %d]" % (SIZE, SIZE))
    model = yolov3.yolov3(num_classes)

    with tf.Graph().as_default() as graph:
        sess = tf.Session(graph=graph)
        inputs = tf.placeholder(
            tf.float32, [1, SIZE, SIZE, 3])  # placeholder for detector inputs

        with tf.variable_scope('yolov3'):
            feature_map = model.forward(inputs, is_training=False)

        boxes, confs, probs = model.predict(feature_map)
        scores = confs * probs
        print("=>", boxes, scores)
        boxes, scores, labels = utils.gpu_nms(
            boxes,
            scores,
            num_classes,
            score_thresh=flags.score_threshold,
            iou_thresh=flags.iou_threshold)
        print("=>", boxes, scores, labels)
        feature_map_1, feature_map_2, feature_map_3 = feature_map
        print("=>", feature_map_1, feature_map_2, feature_map_3)
        saver = tf.train.Saver(var_list=tf.global_variables(scope='yolov3'))

        if flags.convert:
            if not os.path.exists(flags.weights_path):
                url = 'https://github.com/YunYang1994/tensorflow-yolov3/releases/download/v1.0/yolov3.weights'
                for i in range(3):
                    time.sleep(1)
                    print("=> %s does not exists ! " % flags.weights_path)
                print("=> It will take a while to download it from %s" % url)
                print('=> Downloading yolov3 weights ... ')
                wget.download(url, flags.weights_path)

            load_ops = utils.load_weights(tf.global_variables(scope='yolov3'),
                                          flags.weights_path)
            sess.run(load_ops)
            save_path = saver.save(sess, save_path=flags.ckpt_file)
            print('=> model saved in path: {}'.format(save_path))

        if flags.freeze:
            saver.restore(sess, flags.ckpt_file)
            print('=> checkpoint file restored from ', flags.ckpt_file)
            utils.freeze_graph(sess, './checkpoint/yolov3_cpu_nms.pb',
                               ["concat_9", "mul_6"])
            utils.freeze_graph(sess, './checkpoint/yolov3_gpu_nms.pb',
                               ["concat_10", "concat_11", "concat_12"])
            utils.freeze_graph(sess, './checkpoint/yolov3_feature.pb', [
                "yolov3/yolo-v3/feature_map_1",
                "yolov3/yolo-v3/feature_map_2",
                "yolov3/yolo-v3/feature_map_3",
            ])
sess = tf.Session()

sess.run(tf.global_variables_initializer())

# Loading pre_trained weights
print("Loading weights ...")
sess.run(
    utils.load_weights(tf.global_variables(scope='yolov3'), cfg.weights_file))

saver = tf.train.Saver()

if FLAGS.convert:
    if not os.path.exists(cfg.weights_file):
        url = 'https://pjreddie.com/media/files/yolov3.weights'
        if not os.path.exists("./checkpoint/yolov3.weights"):
            print("{} does not exists ! ".format(cfg.weights_file))
        print("=> It will take a while to download it from {}".format(url))
        print('=> Downloading yolov3 weights ... ')
        wget.download(url, cfg.weights_file)
        sess.run(
            utils.load_weights(tf.global_variables(scope='yolov3'),
                               cfg.weights_file))
    saver.save(sess, save_path=FLAGS.ckpt_file)
    print('ckpt model havs been saved in path: {}'.format(FLAGS.ckpt_file))

if FLAGS.freeze:
    saver.restore(sess, FLAGS.ckpt_file)
    print('=> checkpoint file restored from ', FLAGS.ckpt_file)
    utils.freeze_graph(sess, './checkpoint/yolov3_prediction.pb',
                       ["concat_10", "concat_11", "concat_12"])