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)
示例#2
0
def rnet_detect_faces_gpu(img,
                          bounding_boxes,
                          r_model_path,
                          thresholds=0.7,
                          nms_thresholds=0.7):
    with torch.no_grad():
        _, rnet, _ = create_mtcnn_model(r_model_path=r_model_path)

        img_boxes, bboxes = gpu_get_image_boxes(bounding_boxes, img, size=24)

        probs, offsets, _ = rnet(img_boxes)

        mask = (probs >= thresholds)
        boxes = bboxes[mask.squeeze()]
        box_regs = offsets[mask.squeeze()]
        scores = probs[mask].reshape((-1, ))

        if boxes.shape[0] > 0:
            # print(boxes,box_regs)
            boxes = gpu_calibrate_box(boxes.float(), box_regs)
            # nms
            keep = gpu_nms(boxes, scores, nms_thresholds)
            boxes = boxes[keep]

            # 将检测出的框转化成矩形
            boxes = gpu_convert_to_square(boxes)

        return boxes
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",
            ])
示例#4
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)
示例#5
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)
示例#6
0
def onet_detect_faces_gpu(img,
                          bounding_boxes,
                          o_model_path,
                          thresholds=0.9,
                          nms_thresholds=0.7):
    '''跟rnet 一样的方法'''
    with torch.no_grad():
        _, _, onet = create_mtcnn_model(o_model_path=o_model_path)

        img_boxes, bboxes = gpu_get_image_boxes(bounding_boxes, img, size=48)

        probs, offsets, landmarks = onet(img_boxes)

        # filter negative boxes
        mask = (probs.squeeze() >= thresholds)
        boxes = bboxes[mask]
        box_regs = offsets[mask]
        scores = probs[mask].reshape((-1, ))
        landmarks = landmarks[mask]

        if boxes.shape[0] > 0:
            # 计算面部地标点
            landmarks = gpu_calibrate_landmarks(boxes, landmarks)
            boxes = gpu_calibrate_box(boxes.float(), box_regs)

            height = img.shape[2]
            width = img.shape[3]

            bboxes = torch.max(
                torch.zeros_like(boxes, device=torch.device('cuda')), boxes)
            sizes = torch.IntTensor([[width, height, width, height]] *
                                    bboxes.shape[0]).to(torch.device('cuda'))
            boxes = torch.min(bboxes.int(), sizes)

            # nms
            keep = gpu_nms(boxes, scores, nms_thresholds, mode='min')
            boxes = boxes[keep]
            landmarks = landmarks[keep]

        return boxes, landmarks
示例#7
0
def pnet_detect_faces_gpu(image,
                          p_model_path,
                          min_face_size=12.0,
                          thresholds=0.6,
                          nms_thresholds=0.7):
    with torch.no_grad():
        pnet, _, _ = create_mtcnn_model(p_model_path=p_model_path)

        # 建立一个图像金字塔
        h = image.shape[2]
        w = image.shape[3]

        max_face_size = min(h, w)

        min_detection_size = 12
        factor = 0.707  # sqrt(0.5)
        # 在某种意义上,应用P-Net相当于用步幅2移动12x12窗口,
        stride = 2.0
        cell_size = 12

        candidate_boxes = torch.empty((0, 4),
                                      dtype=torch.float,
                                      device=torch.device('cuda'))
        candidate_scores = torch.empty((0), device=torch.device('cuda'))
        candidate_offsets = torch.empty((0, 4),
                                        dtype=torch.float,
                                        device=torch.device('cuda'))

        while min_face_size <= max_face_size:
            current_scale = min_detection_size / min_face_size
            min_face_size /= factor

            img_h = math.ceil(h * current_scale)  # 向上取整 貌似有问题哟
            img_w = math.ceil(w * current_scale)

            # https://www.cnblogs.com/ocean1100/p/9494640.html
            # 坑:cv2 把 [n,c,h,w] --> [n,c,w,h]
            resize_img = torch.nn.functional.interpolate(image,
                                                         size=(img_h, img_w),
                                                         mode='bilinear',
                                                         align_corners=True)

            # prob: [n,1,m,n] offset: [n,4:(x1,y1,x2,y2),m,n]
            prob, offset, _ = pnet(resize_img)

            # 去掉多余的维度,[m,n]
            prob = prob.squeeze()
            inds = (prob >
                    thresholds).nonzero()  # inds: [[rows_idx,clos_idx],]

            # 没有发现人脸
            if inds.shape[0] == 0:
                continue
            else:
                # 边界框的转换 i:四个坐标,inds[:, 0]:图片高,inds[:, 1]:图片宽
                reg_x1, reg_y1, reg_x2, reg_y2 = [
                    offset[0, i, inds[:, 0], inds[:, 1]] for i in range(4)
                ]

                # [n] --> [n,4] == [n:(x1,y1,x2,y2),4]
                offset = torch.stack([reg_x1, reg_y1, reg_x2, reg_y2], dim=1)
                score = prob[inds[:, 0], inds[:, 1]]

                # 当前的候选框 [n] --> [4,n] == [4,{n:x1,n:y1,n:x2,n:y2)]
                bounding_boxes = torch.stack(
                    [
                        (stride * inds[:, 1] + 1.0),  # x1
                        (stride * inds[:, 0] + 1.0),  # y1
                        (stride * inds[:, 1] + cell_size + 1.0),  # x2
                        (stride * inds[:, 0] + cell_size + 1.0),  # y2
                    ],
                    dim=0).transpose(0, 1).float()

                bounding_boxes = torch.round(bounding_boxes.float() /
                                             current_scale)

                keep = gpu_nms(bounding_boxes, score, overlap_threshold=0.5)

                bboxes = bounding_boxes[keep]
                scores = score[keep]
                offset = offset[keep]

                candidate_boxes = torch.cat([candidate_boxes, bboxes])
                candidate_scores = torch.cat([candidate_scores, scores])
                candidate_offsets = torch.cat([candidate_offsets, offset])

            # nms
        if candidate_boxes.shape[0] != 0:
            keep = gpu_nms(candidate_boxes, candidate_scores, nms_thresholds)
            candidate_boxes = candidate_boxes[keep]

            # 使用pnet预测的偏移量来变换边界框,根据 w、h 对 x1,y1,x2,y2 的位置进行调整
            candidate_boxes = gpu_calibrate_box(candidate_boxes,
                                                candidate_offsets)

            # 将检测出的框转化成矩形
            candidate_boxes = gpu_convert_to_square(candidate_boxes)

        return candidate_boxes

    with tf.Graph().as_default() as graph:
        sess = tf.Session(graph=graph)
        inputs = tf.placeholder(tf.float32, [1, image_h, image_w, 3])
        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, num_classes,
                                              score_thresh=score_threshold,
                                              iou_thresh=score_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 not os.path.exists(weights_path):
            print('=> weight file not exist!!!')
        else:
            load_ops = utils.load_weights(tf.global_variables(scope='yolov3'), weights_path)
            sess.run(load_ops)
            save_path = saver.save(sess, save_path=ckpt_file)
            print("=> model saved in path: {}".format(save_path))