コード例 #1
0
def inference(det_net, data_dir):

    # 1. preprocess img
    img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3])
    img_batch = tf.cast(img_plac, tf.float32)
    img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN)
    img_batch = short_side_resize_for_inference_data(
        img_tensor=img_batch, target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN)

    det_boxes_r, det_scores_r, det_category_r = det_net.build_whole_detection_network(
        input_img_batch=img_batch, gtboxes_batch=None)

    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())

    restorer, restore_ckpt = det_net.get_restorer()

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:
        sess.run(init_op)
        if not restorer is None:
            restorer.restore(sess, restore_ckpt)
            print('restore model')

        imgs = os.listdir(data_dir)
        for i, a_img_name in enumerate(imgs):

            # f = open('./res_icdar_r/res_{}.txt'.format(a_img_name.split('.jpg')[0]), 'w')

            raw_img = cv2.imread(os.path.join(data_dir, a_img_name))
            # raw_h, raw_w = raw_img.shape[0], raw_img.shape[1]

            start = time.time()
            resized_img, det_boxes_r_, det_scores_r_, det_category_r_ = \
                sess.run(
                    [img_batch, det_boxes_r, det_scores_r, det_category_r],
                    feed_dict={img_plac: raw_img}
                )
            end = time.time()

            # res_r = coordinate_convert.forward_convert(det_boxes_r_, False)
            # res_r = np.array(res_r, np.int32)
            # for r in res_r:
            #     f.write('{},{},{},{},{},{},{},{}\n'.format(r[0], r[1], r[2], r[3],
            #                                                r[4], r[5], r[6], r[7]))
            # f.close()

            det_detections_r = draw_box_in_img.draw_rotate_box_cv(
                np.squeeze(resized_img, 0),
                boxes=det_boxes_r_,
                labels=det_category_r_,
                scores=det_scores_r_)
            save_dir = os.path.join(cfgs.INFERENCE_SAVE_PATH, cfgs.VERSION)
            tools.mkdir(save_dir)
            cv2.imwrite(save_dir + '/' + a_img_name + '_r.jpg',
                        det_detections_r)
            view_bar('{} cost {}s'.format(a_img_name, (end - start)), i + 1,
                     len(imgs))
コード例 #2
0
    def inference(self, image):
        # if not self.restorer is None:
        #     self.restorer.restore(self.sess, self.restore_ckpt)
        #     print('restore model')

        inference_start = time.time()

        resized_img, det_boxes_h_, det_scores_h_, det_category_h_, \
        det_boxes_r_, det_scores_r_, det_category_r_ = \
            self.sess.run(
                [self.img_batch, self.det_boxes_h, self.det_scores_h, self.det_category_h,
                 self.det_boxes_r, self.det_scores_r, self.det_category_r],
                feed_dict={self.img_plac: image}
            )
        inference_end = time.time()
        inference_time = inference_end - inference_start
        print('R2CNN inference time : ', inference_time)

        result_image = draw_box_in_img.draw_rotate_box_cv(
            np.squeeze(resized_img, 0),
            boxes=det_boxes_r_,
            labels=det_category_r_,
            scores=det_scores_r_)

        return result_image, inference_time
コード例 #3
0
def inference(det_net,
              file_paths,
              des_folder,
              h_len,
              w_len,
              h_overlap,
              w_overlap,
              save_res=False):

    if save_res:
        assert cfgs.SHOW_SCORE_THRSHOLD >= 0.5, \
            'please set score threshold (example: SHOW_SCORE_THRSHOLD = 0.5) in cfgs.py'

    else:
        assert cfgs.SHOW_SCORE_THRSHOLD < 0.005, \
            'please set score threshold (example: SHOW_SCORE_THRSHOLD = 0.00) in cfgs.py'

    # 1. preprocess img
    img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3])
    img_batch = tf.cast(img_plac, tf.float32)
    img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN)
    img_batch = short_side_resize_for_inference_data(
        img_tensor=img_batch,
        target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN,
        is_resize=False)

    det_boxes_h, det_scores_h, det_category_h, \
    det_boxes_r, det_scores_r, det_category_r = det_net.build_whole_detection_network(input_img_batch=img_batch,
                                                                                      gtboxes_h_batch=None,
                                                                                      gtboxes_r_batch=None)

    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())

    restorer, restore_ckpt = det_net.get_restorer()

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:
        sess.run(init_op)
        if not restorer is None:
            restorer.restore(sess, restore_ckpt)
            print('restore model')

        for count, img_path in enumerate(file_paths):
            start = timer()
            img = cv2.imread(img_path)

            box_res = []
            label_res = []
            score_res = []
            box_res_rotate = []
            label_res_rotate = []

            score_res_rotate = []

            imgH = img.shape[0]
            imgW = img.shape[1]

            if imgH < h_len:
                temp = np.zeros([h_len, imgW, 3], np.float32)
                temp[0:imgH, :, :] = img
                img = temp
                imgH = h_len

            if imgW < w_len:
                temp = np.zeros([imgH, w_len, 3], np.float32)
                temp[:, 0:imgW, :] = img
                img = temp
                imgW = w_len

            for hh in range(0, imgH, h_len - h_overlap):
                if imgH - hh - 1 < h_len:
                    hh_ = imgH - h_len
                else:
                    hh_ = hh
                for ww in range(0, imgW, w_len - w_overlap):
                    if imgW - ww - 1 < w_len:
                        ww_ = imgW - w_len
                    else:
                        ww_ = ww
                    src_img = img[hh_:(hh_ + h_len), ww_:(ww_ + w_len), :]

                    det_boxes_h_, det_scores_h_, det_category_h_, \
                    det_boxes_r_, det_scores_r_, det_category_r_ = \
                        sess.run(
                            [det_boxes_h, det_scores_h, det_category_h,
                             det_boxes_r, det_scores_r, det_category_r],
                            feed_dict={img_plac: src_img[:, :, ::-1]}
                        )

                    if len(det_boxes_h_) > 0:
                        for ii in range(len(det_boxes_h_)):
                            box = det_boxes_h_[ii]
                            box[0] = box[0] + ww_
                            box[1] = box[1] + hh_
                            box[2] = box[2] + ww_
                            box[3] = box[3] + hh_
                            box_res.append(box)
                            label_res.append(det_category_h_[ii])
                            score_res.append(det_scores_h_[ii])
                    if len(det_boxes_r_) > 0:
                        for ii in range(len(det_boxes_r_)):
                            box_rotate = det_boxes_r_[ii]
                            box_rotate[0] = box_rotate[0] + ww_
                            box_rotate[1] = box_rotate[1] + hh_
                            box_res_rotate.append(box_rotate)
                            label_res_rotate.append(det_category_r_[ii])
                            score_res_rotate.append(det_scores_r_[ii])

            box_res = np.array(box_res)
            label_res = np.array(label_res)
            score_res = np.array(score_res)

            box_res_rotate = np.array(box_res_rotate)
            label_res_rotate = np.array(label_res_rotate)
            score_res_rotate = np.array(score_res_rotate)

            box_res_rotate_, label_res_rotate_, score_res_rotate_ = [], [], []
            box_res_, label_res_, score_res_ = [], [], []

            r_threshold = {
                'roundabout': 0.1,
                'tennis-court': 0.3,
                'swimming-pool': 0.1,
                'storage-tank': 0.2,
                'soccer-ball-field': 0.3,
                'small-vehicle': 0.2,
                'ship': 0.05,
                'plane': 0.3,
                'large-vehicle': 0.1,
                'helicopter': 0.2,
                'harbor': 0.0001,
                'ground-track-field': 0.3,
                'bridge': 0.0001,
                'basketball-court': 0.3,
                'baseball-diamond': 0.3
            }

            h_threshold = {
                'roundabout': 0.35,
                'tennis-court': 0.35,
                'swimming-pool': 0.4,
                'storage-tank': 0.3,
                'soccer-ball-field': 0.3,
                'small-vehicle': 0.4,
                'ship': 0.35,
                'plane': 0.35,
                'large-vehicle': 0.4,
                'helicopter': 0.4,
                'harbor': 0.3,
                'ground-track-field': 0.4,
                'bridge': 0.3,
                'basketball-court': 0.4,
                'baseball-diamond': 0.3
            }

            for sub_class in range(1, cfgs.CLASS_NUM + 1):
                index = np.where(label_res_rotate == sub_class)[0]
                if len(index) == 0:
                    continue
                tmp_boxes_r = box_res_rotate[index]
                tmp_label_r = label_res_rotate[index]
                tmp_score_r = score_res_rotate[index]

                tmp_boxes_r = np.array(tmp_boxes_r)
                tmp = np.zeros(
                    [tmp_boxes_r.shape[0], tmp_boxes_r.shape[1] + 1])
                tmp[:, 0:-1] = tmp_boxes_r
                tmp[:, -1] = np.array(tmp_score_r)

                try:
                    inx = nms_rotate.nms_rotate_cpu(
                        boxes=np.array(tmp_boxes_r),
                        scores=np.array(tmp_score_r),
                        iou_threshold=r_threshold[LABEl_NAME_MAP[sub_class]],
                        max_output_size=500)
                except:
                    # Note: the IoU of two same rectangles is 0, which is calculated by rotate_gpu_nms
                    jitter = np.zeros(
                        [tmp_boxes_r.shape[0], tmp_boxes_r.shape[1] + 1])
                    jitter[:,
                           0] += np.random.rand(tmp_boxes_r.shape[0], ) / 1000
                    inx = rotate_gpu_nms(
                        np.array(tmp, np.float32) +
                        np.array(jitter, np.float32),
                        float(r_threshold[LABEl_NAME_MAP[sub_class]]), 0)

                box_res_rotate_.extend(np.array(tmp_boxes_r)[inx])
                score_res_rotate_.extend(np.array(tmp_score_r)[inx])
                label_res_rotate_.extend(np.array(tmp_label_r)[inx])

            for sub_class in range(1, cfgs.CLASS_NUM + 1):
                index = np.where(label_res == sub_class)[0]
                if len(index) == 0:
                    continue
                tmp_boxes_h = box_res[index]
                tmp_label_h = label_res[index]
                tmp_score_h = score_res[index]

                tmp_boxes_h = np.array(tmp_boxes_h)
                tmp = np.zeros(
                    [tmp_boxes_h.shape[0], tmp_boxes_h.shape[1] + 1])
                tmp[:, 0:-1] = tmp_boxes_h
                tmp[:, -1] = np.array(tmp_score_h)

                inx = nms.py_cpu_nms(
                    dets=np.array(tmp, np.float32),
                    thresh=h_threshold[LABEl_NAME_MAP[sub_class]],
                    max_output_size=500)

                box_res_.extend(np.array(tmp_boxes_h)[inx])
                score_res_.extend(np.array(tmp_score_h)[inx])
                label_res_.extend(np.array(tmp_label_h)[inx])

            time_elapsed = timer() - start

            if save_res:
                det_detections_h = draw_box_in_img.draw_box_cv(
                    np.array(img, np.float32) - np.array(cfgs.PIXEL_MEAN),
                    boxes=np.array(box_res_),
                    labels=np.array(label_res_),
                    scores=np.array(score_res_))
                det_detections_r = draw_box_in_img.draw_rotate_box_cv(
                    np.array(img, np.float32) - np.array(cfgs.PIXEL_MEAN),
                    boxes=np.array(box_res_rotate_),
                    labels=np.array(label_res_rotate_),
                    scores=np.array(score_res_rotate_))
                save_dir = os.path.join(des_folder, cfgs.VERSION)
                tools.mkdir(save_dir)
                cv2.imwrite(
                    save_dir + '/' + img_path.split('/')[-1].split('.')[0] +
                    '_h.jpg', det_detections_h)
                cv2.imwrite(
                    save_dir + '/' + img_path.split('/')[-1].split('.')[0] +
                    '_r.jpg', det_detections_r)

                view_bar(
                    '{} cost {}s'.format(
                        img_path.split('/')[-1].split('.')[0], time_elapsed),
                    count + 1, len(file_paths))

            else:
                # eval txt
                CLASS_DOTA = NAME_LABEL_MAP.keys()
                # Task1
                write_handle_r = {}
                write_handle_h_ = {}
                txt_dir_r = os.path.join('txt_output', cfgs.VERSION + '_r')
                txt_dir_h_minAreaRect = os.path.join(
                    'txt_output', cfgs.VERSION + '_h_minAreaRect')
                tools.mkdir(txt_dir_r)
                tools.mkdir(txt_dir_h_minAreaRect)
                for sub_class in CLASS_DOTA:
                    if sub_class == 'back_ground':
                        continue
                    write_handle_r[sub_class] = open(
                        os.path.join(txt_dir_r, 'Task1_%s.txt' % sub_class),
                        'a+')
                    write_handle_h_[sub_class] = open(
                        os.path.join(txt_dir_h_minAreaRect,
                                     'Task2_%s.txt' % sub_class), 'a+')

                rboxes = coordinate_convert.forward_convert(box_res_rotate_,
                                                            with_label=False)

                for i, rbox in enumerate(rboxes):
                    command = '%s %.3f %.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f\n' % (
                        img_path.split('/')[-1].split('.')[0],
                        score_res_rotate_[i],
                        rbox[0],
                        rbox[1],
                        rbox[2],
                        rbox[3],
                        rbox[4],
                        rbox[5],
                        rbox[6],
                        rbox[7],
                    )
                    command_ = '%s %.3f %.1f %.1f %.1f %.1f\n' % (
                        img_path.split('/')[-1].split('.')[0],
                        score_res_rotate_[i], min(rbox[::2]), min(
                            rbox[1::2]), max(rbox[::2]), max(rbox[1::2]))
                    write_handle_r[LABEl_NAME_MAP[label_res_rotate_[i]]].write(
                        command)
                    write_handle_h_[LABEl_NAME_MAP[
                        label_res_rotate_[i]]].write(command_)

                for sub_class in CLASS_DOTA:
                    if sub_class == 'back_ground':
                        continue
                    write_handle_r[sub_class].close()

                # Task2
                write_handle_h = {}
                txt_dir_h = os.path.join('txt_output', cfgs.VERSION + '_h')
                tools.mkdir(txt_dir_h)
                for sub_class in CLASS_DOTA:
                    if sub_class == 'back_ground':
                        continue
                    write_handle_h[sub_class] = open(
                        os.path.join(txt_dir_h, 'Task2_%s.txt' % sub_class),
                        'a+')

                for i, hbox in enumerate(box_res_):
                    command = '%s %.3f %.1f %.1f %.1f %.1f\n' % (
                        img_path.split('/')[-1].split('.')[0], score_res_[i],
                        hbox[0], hbox[1], hbox[2], hbox[3])
                    write_handle_h[LABEl_NAME_MAP[label_res_[i]]].write(
                        command)

                for sub_class in CLASS_DOTA:
                    if sub_class == 'back_ground':
                        continue
                    write_handle_h[sub_class].close()

                view_bar(
                    '{} cost {}s'.format(
                        img_path.split('/')[-1].split('.')[0], time_elapsed),
                    count + 1, len(file_paths))
コード例 #4
0
def inference(det_net, cap):

    # 1. preprocess img
    img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3])
    img_batch = tf.cast(img_plac, tf.float32)
    img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN)
    img_batch = short_side_resize_for_inference_data(
        img_tensor=img_batch,
        target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN,
        is_resize=False)

    det_boxes_h, det_scores_h, det_category_h, \
    det_boxes_r, det_scores_r, det_category_r = det_net.build_whole_detection_network(input_img_batch=img_batch,
                                                                                      gtboxes_h_batch=None,
                                                                                      gtboxes_r_batch=None)

    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())

    restorer, restore_ckpt = det_net.get_restorer()

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:
        sess.run(init_op)
        if not restorer is None:
            restorer.restore(sess, restore_ckpt)
            print('restore model')
        fourcc = cv2.VideoWriter_fourcc(*'xvid')
        out = cv2.VideoWriter('./camera_demo.avi', fourcc, 10, (640, 480))
        ret, frame = cap.read()
        while ret:
            ret, frame = cap.read()
            start = time.time()
            resized_img, det_boxes_h_, det_scores_h_, det_category_h_, \
            det_boxes_r_, det_scores_r_, det_category_r_ = \
                sess.run(
                    [img_batch, det_boxes_h, det_scores_h, det_category_h, det_boxes_r, det_scores_r, det_category_r],
                    feed_dict={img_plac: frame}
                )
            end = time.time()
            # det_detections_h = draw_box_in_img.draw_box_cv(np.squeeze(resized_img, 0),
            #                                                boxes=det_boxes_h_,
            #                                                labels=det_category_h_,
            #                                                scores=det_scores_h_)
            det_detections_r = draw_box_in_img.draw_rotate_box_cv(
                np.squeeze(resized_img, 0),
                boxes=det_boxes_r_,
                labels=det_category_r_,
                scores=det_scores_r_)
            # det_detections_h = cv2.resize(det_detections_h,
            #                               (det_detections_h.shape[0] // 2, det_detections_h.shape[1] // 2))
            # cv2.putText(det_detections_h,
            #             text="horizon bbox",
            #             org=(0, 0),
            #             fontFace=3,
            #             fontScale=1,
            #             color=(255, 0, 0))
            # det_detections_r = cv2.resize(det_detections_r,
            #                               (det_detections_r.shape[0] // 2, det_detections_r.shape[1] // 2))
            cv2.putText(det_detections_r,
                        text="rotated bbox--%3.2f" % (1 / (end - start)),
                        org=(0, 10),
                        fontFace=1,
                        fontScale=1,
                        color=(0, 255, 0))
            out.write(det_detections_r)
            # hmerge = np.hstack((det_detections_h, det_detections_r))  # 水平拼接
            cv2.imshow("faceDetection", det_detections_r)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        cap.release()
        out.release()
        cv2.destroyAllWindows()
コード例 #5
0
ファイル: eval.py プロジェクト: zhangiguang/ship
def eval_with_plac(img_dir, det_net, image_ext, draw_imgs=False):

    # 1. preprocess img
    img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None,
                                                     3])  # is RGB. not GBR
    img_batch = tf.cast(img_plac, tf.float32)
    img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN)
    img_batch = short_side_resize_for_inference_data(
        img_tensor=img_batch,
        target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN,
        is_resize=False)

    det_boxes_r, det_scores_r, det_category_r = det_net.build_whole_detection_network(
        input_img_batch=img_batch, gtboxes_h_batch=None, gtboxes_r_batch=None)

    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())

    restorer, restore_ckpt = det_net.get_restorer()

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:
        sess.run(init_op)
        if not restorer is None:
            restorer.restore(sess, restore_ckpt)
            print('restore model')

        all_boxes_h = []
        all_boxes_r = []
        imgs = os.listdir(img_dir)
        for i, a_img_name in enumerate(imgs):

            a_img_name = a_img_name.split(image_ext)[0]
            recs = {}
            recs[a_img_name] = parse_rec(
                os.path.join(test_annotation_path, a_img_name + '.xml'))
            #R = [obj for obj in recs[a_img_name]]
            bbox = np.squeeze(np.array([x['bbox'] for x in recs[a_img_name]]))
            #labels = bbox[:, -1]
            if len(bbox.shape) == 1:
                bbox = np.expand_dims(bbox, axis=0)
            labels = bbox[:, -1]

            raw_img = cv2.imread(os.path.join(img_dir, a_img_name + image_ext))
            raw_h, raw_w = raw_img.shape[0], raw_img.shape[1]

            start = time.time()
            resized_img,  \
            det_boxes_r_, det_scores_r_, det_category_r_ = \
                sess.run(
                    [img_batch,
                     det_boxes_r, det_scores_r, det_category_r],
                    feed_dict={img_plac: raw_img}
                )
            end = time.time()
            det_boxes_r_ = det_boxes_r_[det_scores_r_ >= 0.4]
            det_category_r_ = det_category_r_[det_scores_r_ >= 0.4]
            det_scores_r_ = det_scores_r_[det_scores_r_ >= 0.4]

            keep = nms_rotate.nms_rotate_cpu(det_boxes_r_, det_scores_r_, 0.3,
                                             20)
            det_boxes_r_ = det_boxes_r_[keep]
            det_scores_r_ = det_scores_r_[keep]
            det_category_r_ = det_category_r_[keep]
            ##### box_ratio > 2  or < 1/2
            index = (det_boxes_r_[:, 2] / det_boxes_r_[:, 3] >
                     2) | (det_boxes_r_[:, 2] / det_boxes_r_[:, 3] <= 0.5)
            det_boxes_r_ = det_boxes_r_[index]
            det_scores_r_ = det_scores_r_[index]
            det_category_r_ = det_category_r_[index]

            # print("{} cost time : {} ".format(img_name, (end - start)))
            if draw_imgs:
                det_detections_h = draw_box_in_img.draw_rotate_box_cv1(
                    np.squeeze(resized_img, 0),
                    boxes=bbox,
                    labels=labels,
                    scores=np.ones(bbox.shape[0]))
                det_detections_r = draw_box_in_img.draw_rotate_box_cv(
                    np.squeeze(resized_img, 0),
                    boxes=det_boxes_r_,
                    labels=det_category_r_,
                    scores=det_scores_r_)
                save_dir = os.path.join(cfgs.TEST_SAVE_PATH, cfgs.VERSION)
                tools.mkdir(save_dir)
                cv2.imwrite(save_dir + '/' + a_img_name + '_h.jpg',
                            det_detections_h[:, :, ::-1])
                cv2.imwrite(save_dir + '/' + a_img_name + '_r.jpg',
                            det_detections_r[:, :, ::-1])

            # xmin, ymin, xmax, ymax = det_boxes_h_[:, 0], det_boxes_h_[:, 1], \
            #                          det_boxes_h_[:, 2], det_boxes_h_[:, 3]

            if det_boxes_r_.shape[0] != 0:
                resized_h, resized_w = resized_img.shape[1], resized_img.shape[
                    2]
                det_boxes_r_ = forward_convert(det_boxes_r_, False)
                det_boxes_r_[:, 0::2] *= (raw_w / resized_w)
                det_boxes_r_[:, 1::2] *= (raw_h / resized_h)
                det_boxes_r_ = back_forward_convert(det_boxes_r_, False)

            x_c, y_c, w, h, theta = det_boxes_r_[:, 0], det_boxes_r_[:, 1], det_boxes_r_[:, 2], \
                                    det_boxes_r_[:, 3], det_boxes_r_[:, 4]

            # xmin = xmin * raw_w / resized_w
            # xmax = xmax * raw_w / resized_w
            # ymin = ymin * raw_h / resized_h
            # ymax = ymax * raw_h / resized_h

            # boxes_h = np.transpose(np.stack([xmin, ymin, xmax, ymax]))
            boxes_r = np.transpose(np.stack([x_c, y_c, w, h, theta]))
            # dets_h = np.hstack((det_category_h_.reshape(-1, 1),
            #                     det_scores_h_.reshape(-1, 1),
            #                     boxes_h))
            dets_r = np.hstack((det_category_r_.reshape(-1, 1),
                                det_scores_r_.reshape(-1, 1), boxes_r))
            # all_boxes_h.append(dets_h)
            all_boxes_r.append(dets_r)

            tools.view_bar(
                '{} image cost {}s'.format(a_img_name, (end - start)), i + 1,
                len(imgs))

        fw1 = open(cfgs.VERSION + '_detections_h.pkl', 'w')
        fw2 = open(cfgs.VERSION + '_detections_r.pkl', 'w')
        pickle.dump(all_boxes_h, fw1)
        pickle.dump(all_boxes_r, fw2)
コード例 #6
0
def eval_with_plac(img_dir, det_net, num_imgs, image_ext, draw_imgs=False):

    # 1. preprocess img
    img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3])  # is RGB. not GBR
    img_batch = tf.cast(img_plac, tf.float32)
    img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN)
    img_batch = short_side_resize_for_inference_data(img_tensor=img_batch,
                                                     target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN,
                                                     is_resize=False)

    det_boxes_h, det_scores_h, det_category_h, \
    det_boxes_r, det_scores_r, det_category_r = det_net.build_whole_detection_network(
        input_img_batch=img_batch,
        gtboxes_h_batch=None, gtboxes_r_batch=None)

    init_op = tf.group(
        tf.global_variables_initializer(),
        tf.local_variables_initializer()
    )

    restorer, restore_ckpt = det_net.get_restorer()

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:
        sess.run(init_op)
        if not restorer is None:
            restorer.restore(sess, restore_ckpt)
            print('restore model')

        all_boxes_r = []
        imgs = os.listdir(img_dir)
        for i, a_img_name in enumerate(imgs):
            a_img_name = a_img_name.split(image_ext)[0]

            raw_img = cv2.imread(os.path.join(img_dir,
                                              a_img_name + image_ext))
            raw_h, raw_w = raw_img.shape[0], raw_img.shape[1]

            start = time.time()
            resized_img, det_boxes_r_, det_scores_r_, det_category_r_ = \
                sess.run(
                    [img_batch, det_boxes_h, det_scores_h, det_category_h,
                     det_boxes_r, det_scores_r, det_category_r],
                    feed_dict={img_plac: raw_img}
                )
            end = time.time()
            # print("{} cost time : {} ".format(img_name, (end - start)))
            if draw_imgs:

                det_detections_r = draw_box_in_img.draw_rotate_box_cv(np.squeeze(resized_img, 0),
                                                                      boxes=det_boxes_r_,
                                                                      labels=det_category_r_,
                                                                      scores=det_scores_r_)
                save_dir = os.path.join(cfgs.TEST_SAVE_PATH, cfgs.VERSION)
                tools.mkdir(save_dir)

                cv2.imwrite(save_dir + '/' + a_img_name + '_r.jpg',
                            det_detections_r[:, :, ::-1])

            resized_h, resized_w = resized_img.shape[1], resized_img.shape[2]
            det_boxes_r_ = forward_convert(det_boxes_r_, False)
            det_boxes_r_[:, 0::2] *= (raw_w / resized_w)
            det_boxes_r_[:, 1::2] *= (raw_h / resized_h)
            det_boxes_r_ = back_forward_convert(det_boxes_r_, False)

            x_c, y_c, w, h, theta = det_boxes_r_[:, 0], det_boxes_r_[:, 1], det_boxes_r_[:, 2], \
                                    det_boxes_r_[:, 3], det_boxes_r_[:, 4]

            boxes_r = np.transpose(np.stack([x_c, y_c, w, h, theta]))

            dets_r = np.hstack((det_category_r_.reshape(-1, 1),
                                det_scores_r_.reshape(-1, 1),
                                boxes_r))
            all_boxes_r.append(dets_r)

            tools.view_bar('{} image cost {}s'.format(a_img_name, (end - start)), i + 1, len(imgs))

        fw2 = open(cfgs.VERSION + '_detections_r.pkl', 'w')
        pickle.dump(all_boxes_r, fw2)
コード例 #7
0
def inference(det_net, data_dir):
    TIME = 0
    TIME_NUM = 0
    # 1. preprocess img
    img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3])
    img_batch = tf.cast(img_plac, tf.float32)
    img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN)
    img_batch = short_side_resize_for_inference_data(
        img_tensor=img_batch,
        target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN,
        is_resize=IS_RESIZE)

    det_boxes_h, det_scores_h, det_category_h, \
    det_boxes_r, det_scores_r, det_category_r = det_net.build_whole_detection_network(input_img_batch=img_batch,
                                                                                      gtboxes_h_batch=None,
                                                                                      gtboxes_r_batch=None)

    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())

    restorer, restore_ckpt = det_net.get_restorer()

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:
        sess.run(init_op)
        if not restorer is None:
            restorer.restore(sess, restore_ckpt)
            print('restore model')

        imgs = os.listdir(data_dir)
        # print(imgs)
        for i, a_img_name in enumerate(imgs):
            file_text, extension = os.path.splitext(a_img_name)
            if extension != ".jpg" and extension != ".tif" and extension != ".png":
                continue
            # f = open('./res_icdar_r/res_{}.txt'.format(a_img_name.split('.jpg')[0]), 'w')

            raw_img = cv2.imread(os.path.join(data_dir, a_img_name))
            # raw_h, raw_w = raw_img.shape[0], raw_img.shape[1]

            start = time.time()
            resized_img, det_boxes_h_, det_scores_h_, det_category_h_, \
            det_boxes_r_, det_scores_r_, det_category_r_ = \
                sess.run(
                    [img_batch, det_boxes_h, det_scores_h, det_category_h,
                     det_boxes_r, det_scores_r, det_category_r],
                    feed_dict={img_plac: raw_img}
                )
            end = time.time()

            TIME += end - start
            TIME_NUM += 1

            if WRITE_VOC == True:
                boxes = np.array(det_boxes_r_, np.int64)
                scores = np.array(det_scores_r_, np.float32)
                labels = np.array(det_category_r_, np.int32)

                det_save_dir = cfgs.INFERENCE_SAVE_PATH
                write_voc_results_file(boxes, labels, scores,
                                       a_img_name.split('.')[0], det_save_dir)
                write_pixel_results(boxes, labels, scores,
                                    a_img_name.split('.')[0], det_save_dir)

            det_detections_h = draw_box_in_img.draw_box_cv(
                np.squeeze(resized_img, 0),
                boxes=det_boxes_h_,
                labels=det_category_h_,
                scores=det_scores_h_)
            det_detections_r = draw_box_in_img.draw_rotate_box_cv(
                np.squeeze(resized_img, 0),
                boxes=det_boxes_r_,
                labels=det_category_r_,
                scores=det_scores_r_)
            save_dir = os.path.join(cfgs.INFERENCE_SAVE_PATH, cfgs.VERSION)
            tools.mkdir(save_dir)
            if OUTPUT_H_IMG:
                cv2.imwrite(save_dir + '/' + a_img_name + '_h.jpg',
                            det_detections_h)

            cv2.imwrite(save_dir + '/' + a_img_name + '_r.jpg',
                        det_detections_r)
            view_bar('{} cost {}s'.format(a_img_name, (end - start)), i + 1,
                     len(imgs))

        print('avg time:{}'.format(TIME / TIME_NUM))
def inference(det_net, device_index):
    # preprocessing data
    img_placeholder = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3])
    img_batch = tf.cast(img_placeholder, tf.float32)
    img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN)

    img_batch = short_side_resize_for_inference_data(
        img_tensor=img_batch,
        target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN,
        is_resize=False)
    # img_batch: [1, h, w, c]

    det_boxes_h, det_scores_h, det_category_h, \
    det_boxes_r, det_scores_r, det_category_r = det_net.build_whole_detection_network(input_img_batch=img_batch,
                                                                                      gtboxes_r_batch=None,
                                                                                      gtboxes_h_batch=None)

    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())

    restorer, restore_ckpt = det_net.get_restorer()

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:
        sess.run(init_op)

        if not restorer is None:
            restorer.restore(sess, restore_ckpt)
            print('restore model')

        cap = cv2.VideoCapture(device_index)

        fps = cap.get(cv2.CAP_PROP_FPS)

        # size = (width, height)
        size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
                int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))

        tmp_directory = current_directory + "/../tmp"
        if not os.path.exists(tmp_directory):
            os.makedirs(tmp_directory)

        # Define the codec and create VideoWriter object
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        video_writer = cv2.VideoWriter(
            '%s/OBB_camera_face.avi' % (tmp_directory), fourcc, fps, size)

        cv2.namedWindow("Press q on keyboard to exit.", cv2.WINDOW_NORMAL)

        while (cap.isOpened()):
            # Capture frame-by-frame
            ret, frame = cap.read()

            if True != ret:
                break

            start = time.time()

            # print("tf.shape(frame):", sess.run(tf.shape(frame)))  # tf.shape(frame): [480 640   3]

            resized_img, det_boxes_h_, det_scores_h_, det_category_h_, det_boxes_r_, det_scores_r_, det_category_r_ = \
                sess.run(
                    [img_batch, det_boxes_h, det_scores_h, det_category_h, det_boxes_r, det_scores_r, det_category_r],
                    feed_dict={img_placeholder: frame})

            # feed_dict={img_placeholder: frame} Color image loaded by OpenCV is in BGR mode.
            # feed_dict={img_plac: frame[:, :, ::-1]} BGR mode -> RGB mode.

            end = time.time()

            # print("resized_img.shape:", resized_img.shape)
            det_detections_h = draw_box_in_img.draw_box_cv(
                np.squeeze(resized_img, 0),
                boxes=det_boxes_h_,
                labels=det_category_h_,
                scores=det_scores_h_)

            det_detections_r = draw_box_in_img.draw_rotate_box_cv(
                np.squeeze(resized_img, 0),
                boxes=det_boxes_r_,
                labels=det_category_r_,
                scores=det_scores_r_)

            # height, width, number of channels in image
            # height = image.shape[0], width = image.shape[1], channels = image.shape[2]

            det_detections_h = cv2.resize(det_detections_h,
                                          (det_detections_h.shape[1] // 2,
                                           det_detections_h.shape[0] // 2))
            cv2.putText(det_detections_h,
                        text="HBB - %3.2fps" % (1 / (end - start)),
                        org=(10, 10),
                        fontFace=1,
                        fontScale=1,
                        color=(0, 255, 0))

            det_detections_r = cv2.resize(det_detections_r,
                                          (det_detections_r.shape[1] // 2,
                                           det_detections_r.shape[0] // 2))
            cv2.putText(det_detections_r,
                        text="OBB - %3.2fps" % (1.0 / (end - start)),
                        org=(10, 10),
                        fontFace=1,
                        fontScale=1,
                        color=(0, 255, 0))

            # Stack arrays in sequence horizontally (column wise).
            hstack_data = np.hstack((det_detections_h, det_detections_r))

            video_writer.write(frame)

            # Display the resulting frame
            cv2.imshow("Press q on keyboard to exit.", hstack_data)

            # Press q on keyboard to exit.
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        # When everything done, release the capture.
        cap.release()
        video_writer.release()
        cv2.destroyAllWindows()
コード例 #9
0
def eval_with_plac(img_dir, det_net, num_imgs, image_ext, draw_imgs,
                   test_annotation_path):

    # 1. preprocess img
    img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None,
                                                     3])  # is RGB. not GBR
    img_batch = tf.cast(img_plac, tf.float32)
    img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN)
    img_batch = short_side_resize_for_inference_data(
        img_tensor=img_batch,
        target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN,
        is_resize=False)

    det_boxes_h, det_scores_h, det_category_h, \
    det_boxes_r, det_scores_r, det_category_r = det_net.build_whole_detection_network(
        input_img_batch=img_batch,
        gtboxes_h_batch=None, gtboxes_r_batch=None)

    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())

    global_step_tensor = slim.get_or_create_global_step()

    eval_result = []
    last_checkpoint_name = None

    while True:

        restorer, restore_ckpt = det_net.get_restorer()
        #saver = tf.train.Saver(max_to_keep=10)
        start_time = time.time()

        model_path = os.path.splitext(os.path.basename(restore_ckpt))[0]
        if model_path == None:
            print("Wait for available checkpoint")
        elif last_checkpoint_name == model_path:
            print(
                "Already evaluated checkpoint {}, we will try evaluation in {} seconds"
                .format(model_path, EVAL_INTERVAL))
            #continue
        else:
            print('Last ckpt was {}, new ckpt is {}'.format(
                last_checkpoint_name, model_path))
            last_checkpoint_name = model_path

            config = tf.ConfigProto()
            config.gpu_options.allow_growth = True

            with tf.Session(config=config) as sess:
                sess.run(init_op)
                sess.run(global_step_tensor.initializer)
                if not restorer is None:
                    restorer.restore(sess, restore_ckpt)
                    print('restore model', restore_ckpt)

                global_stepnp = tf.train.global_step(sess, global_step_tensor)
                print('#########################', global_stepnp)

                all_boxes_h = []
                all_boxes_r = []
                imgs = os.listdir(img_dir)
                imgs_len = len(imgs)
                none_detected_image = []
                for i, a_img_name in enumerate(imgs[:]):
                    a_img_name = a_img_name.split(image_ext)[0]
                    image_name = a_img_name + image_ext
                    print('\n', a_img_name)

                    raw_img = cv2.imread(
                        os.path.join(img_dir, a_img_name + image_ext))
                    raw_h, raw_w = raw_img.shape[0], raw_img.shape[1]

                    start = time.time()
                    resized_img, det_boxes_h_, det_scores_h_, det_category_h_, \
                    det_boxes_r_, det_scores_r_, det_category_r_ = \
                        sess.run(
                            [img_batch, det_boxes_h, det_scores_h, det_category_h,
                             det_boxes_r, det_scores_r, det_category_r],
                            feed_dict={img_plac: raw_img}
                        )
                    end = time.time()
                    print("det category H : ", det_category_h_)
                    print("det category R : ", det_category_r_)
                    # print("{} cost time : {} ".format(img_name, (end - start)))
                    if draw_imgs:
                        det_detections_h = draw_box_in_img.draw_box_cv(
                            np.squeeze(resized_img, 0),
                            boxes=det_boxes_h_,
                            labels=det_category_h_,
                            scores=det_scores_h_)
                        det_detections_r = draw_box_in_img.draw_rotate_box_cv(
                            np.squeeze(resized_img, 0),
                            boxes=det_boxes_r_,
                            labels=det_category_r_,
                            scores=det_scores_r_)
                        save_dir = os.path.join(cfgs.TEST_SAVE_PATH,
                                                cfgs.VERSION)
                        tools.mkdir(save_dir)
                        cv2.imwrite(save_dir + '/' + a_img_name + '_h.jpg',
                                    det_detections_h[:, :, ::-1])
                        cv2.imwrite(save_dir + '/' + a_img_name + '_r.jpg',
                                    det_detections_r[:, :, ::-1])

                    xmin, ymin, xmax, ymax = det_boxes_h_[:, 0], det_boxes_h_[:, 1], \
                                     det_boxes_h_[:, 2], det_boxes_h_[:, 3]

                    if det_boxes_r_.shape[0] != 0:
                        #print('### Has box ###')
                        resized_h, resized_w = resized_img.shape[
                            1], resized_img.shape[2]
                        det_boxes_r_ = forward_convert(det_boxes_r_, False)
                        det_boxes_r_[:, 0::2] *= (raw_w / resized_w)
                        det_boxes_r_[:, 1::2] *= (raw_h / resized_h)
                        det_boxes_r_ = back_forward_convert(
                            det_boxes_r_, False)

                        x_c, y_c, w, h, theta = det_boxes_r_[:, 0], det_boxes_r_[:, 1], det_boxes_r_[:, 2], \
                                                det_boxes_r_[:, 3], det_boxes_r_[:, 4]

                        xmin = xmin * raw_w / resized_w
                        xmax = xmax * raw_w / resized_w
                        ymin = ymin * raw_h / resized_h
                        ymax = ymax * raw_h / resized_h

                        boxes_h = np.transpose(
                            np.stack([xmin, ymin, xmax, ymax]))
                        boxes_r = np.transpose(
                            np.stack([x_c, y_c, w, h, theta]))
                        dets_h = np.hstack((det_category_h_.reshape(-1, 1),
                                            det_scores_h_.reshape(-1,
                                                                  1), boxes_h))
                        dets_r = np.hstack((det_category_r_.reshape(-1, 1),
                                            det_scores_r_.reshape(-1,
                                                                  1), boxes_r))
                        all_boxes_h.append(dets_h)
                        all_boxes_r.append(dets_r)
                    else:
                        imgs.remove(image_name)
                        none_detected_image.append(image_name)
                        print('No detected')

                    tools.view_bar(
                        '{} image cost {}s'.format(a_img_name, (end - start)),
                        i + 1, imgs_len)

                fw1 = open(cfgs.VERSION + '_detections_h.pkl', 'wb')
                fw2 = open(cfgs.VERSION + '_detections_r.pkl', 'wb')
                pickle.dump(all_boxes_h, fw1)
                pickle.dump(all_boxes_r, fw2)

                # with open(cfgs.VERSION + '_detections_h.pkl', 'rb') as f1:
                #     all_boxes_h = pickle.load(f1, encoding='unicode')

                # print(10 * "###")
                # print(len(all_boxes_h))
                #
                # with open(cfgs.VERSION + '_detections_r.pkl', 'rb') as f2:
                #     all_boxes_r = pickle.load(f2, encoding='unicode')
                #
                #     print(len(all_boxes_r))

                # imgs = os.listdir(img_dir)
                real_test_imgname_list = [i.split(image_ext)[0] for i in imgs]

                print(10 * "**")
                print('horizon eval:')
                # print(len(all_boxes_h), len(all_boxes_r))
                # print(len(real_test_imgname_list))
                mAP_h, recall_h, precision_h, total_mAP_h, total_recall_h, total_precision_h = voc_eval.voc_evaluate_detections(
                    all_boxes=all_boxes_h,
                    test_imgid_list=real_test_imgname_list,
                    test_annotation_path=test_annotation_path)
                print('mAP_h: ', mAP_h)
                print('mRecall_h:', recall_h)
                print('mPrecision_h:', precision_h)
                print('total_mAP_h: ', total_mAP_h)
                print('total_recall_h_list:', total_recall_h)
                print('total_precision_h_list:', total_precision_h)

                print(10 * "**")
                print('rotation eval:')
                mAP_r, recall_r, precision_r, total_mAP_r, total_recall_r, total_precision_r = voc_eval_r.voc_evaluate_detections(
                    all_boxes=all_boxes_r,
                    test_imgid_list=real_test_imgname_list,
                    test_annotation_path=test_annotation_path)

                f1score_h_check = (1 + 1**2) * precision_h * recall_h / (
                    1**2 * precision_h + recall_h)
                f1score_h = calc_fscore(precision_h, recall_h, 1)

                f1score_r_check = (1 + 1**2) * precision_r * recall_r / (
                    1**2 * precision_r + recall_r)
                f1score_r = calc_fscore(precision_r, recall_r, 1)

                print(10 * '##')
                print('mAP_r:', mAP_r)
                print('mRecall_r:', recall_r)
                print('mPrecision_r:', precision_r)
                print('total_mAP_r_list: ', total_mAP_r)
                print('total_recall_r_list:', total_recall_r)
                print('total_precision_r_list:', total_precision_r)
                print('f1score_r:', f1score_r)

                summary_path = os.path.join(cfgs.SUMMARY_PATH,
                                            cfgs.VERSION + '/eval_0')
                tools.mkdir(summary_path)

                summary_writer = tf.summary.FileWriter(summary_path,
                                                       graph=sess.graph)

                mAP_h_summ = tf.Summary()
                mAP_h_summ.value.add(tag='EVAL_Global/mAP_h',
                                     simple_value=mAP_h)
                summary_writer.add_summary(mAP_h_summ, global_stepnp)

                mAP_r_summ = tf.Summary()
                mAP_r_summ.value.add(tag='EVAL_Global/mAP_r',
                                     simple_value=mAP_r)
                summary_writer.add_summary(mAP_r_summ, global_stepnp)

                mRecall_h_summ = tf.Summary()
                mRecall_h_summ.value.add(tag='EVAL_Global/Recall_h',
                                         simple_value=recall_h)
                summary_writer.add_summary(mRecall_h_summ, global_stepnp)

                mRecall_r_summ = tf.Summary()
                mRecall_r_summ.value.add(tag='EVAL_Global/Recall_r',
                                         simple_value=recall_r)
                summary_writer.add_summary(mRecall_r_summ, global_stepnp)

                mPrecision_h_summ = tf.Summary()
                mPrecision_h_summ.value.add(tag='EVAL_Global/Precision_h',
                                            simple_value=precision_h)
                summary_writer.add_summary(mPrecision_h_summ, global_stepnp)

                mPrecision_r_summ = tf.Summary()
                mPrecision_r_summ.value.add(tag='EVAL_Global/Precision_r',
                                            simple_value=precision_r)
                summary_writer.add_summary(mPrecision_r_summ, global_stepnp)

                mF1Score_h_summ = tf.Summary()
                mF1Score_h_summ.value.add(tag='EVAL_Global/F1Score_h',
                                          simple_value=f1score_h)
                summary_writer.add_summary(mF1Score_h_summ, global_stepnp)

                mF1Score_r_summ = tf.Summary()
                mF1Score_r_summ.value.add(tag='EVAL_Global/F1Score_r',
                                          simple_value=f1score_r)
                summary_writer.add_summary(mF1Score_r_summ, global_stepnp)

                mAP_h_class_dict = {}
                mAP_r_class_dict = {}
                recall_h_class_dict = {}
                recall_r_class_dict = {}
                precision_h_class_dict = {}
                precision_r_class_dict = {}
                f1score_h_class_dict = {}
                f1score_r_class_dict = {}

                label_list = list(NAME_LABEL_MAP.keys())
                label_list.remove('back_ground')

                for cls in label_list:
                    mAP_h_class_dict["cls_%s_mAP_h_summ" % cls] = tf.Summary()
                    mAP_r_class_dict["cls_%s_mAP_r_summ" % cls] = tf.Summary()
                    recall_h_class_dict["cls_%s_recall_h_summ" %
                                        cls] = tf.Summary()
                    recall_r_class_dict["cls_%s_recall_r_summ" %
                                        cls] = tf.Summary()
                    precision_h_class_dict["cls_%s_precision_h_summ" %
                                           cls] = tf.Summary()
                    precision_r_class_dict["cls_%s_precision_r_summ" %
                                           cls] = tf.Summary()
                    f1score_h_class_dict["cls_%s_f1score_h_summ" %
                                         cls] = tf.Summary()
                    f1score_r_class_dict["cls_%s_f1score_r_summ" %
                                         cls] = tf.Summary()

                for cls in label_list:
                    mAP_h_class_dict["cls_%s_mAP_h_summ" % cls].value.add(
                        tag='EVAL_Class_mAP/{}_mAP_h'.format(cls),
                        simple_value=total_mAP_h[cls])
                    mAP_r_class_dict["cls_%s_mAP_r_summ" % cls].value.add(
                        tag='EVAL_Class_mAP/{}_mAP_r'.format(cls),
                        simple_value=total_mAP_r[cls])
                    recall_h_class_dict[
                        "cls_%s_recall_h_summ" % cls].value.add(
                            tag='EVAL_Class_recall/{}_recall_h'.format(cls),
                            simple_value=total_recall_h[cls])
                    recall_r_class_dict[
                        "cls_%s_recall_r_summ" % cls].value.add(
                            tag='EVAL_Class_recall/{}_recall_r'.format(cls),
                            simple_value=total_recall_r[cls])
                    precision_h_class_dict[
                        "cls_%s_precision_h_summ" % cls].value.add(
                            tag='EVAL_Class_precision/{}_precision_h'.format(
                                cls),
                            simple_value=total_precision_h[cls])
                    precision_r_class_dict[
                        "cls_%s_precision_r_summ" % cls].value.add(
                            tag='EVAL_Class_precision/{}_precision_r'.format(
                                cls),
                            simple_value=total_precision_r[cls])

                    f1score_h_cls = calc_fscore(total_precision_h[cls],
                                                total_recall_h[cls], 1)
                    f1score_r_cls = calc_fscore(total_precision_r[cls],
                                                total_recall_r[cls], 1)
                    f1score_h_class_dict[
                        "cls_%s_f1score_h_summ" % cls].value.add(
                            tag='EVAL_Class_f1score/{}_f1score_h'.format(cls),
                            simple_value=f1score_h_cls)
                    f1score_r_class_dict[
                        "cls_%s_f1score_r_summ" % cls].value.add(
                            tag='EVAL_Class_f1score/{}_f1score_r'.format(cls),
                            simple_value=f1score_r_cls)

                for cls in label_list:
                    summary_writer.add_summary(
                        mAP_h_class_dict["cls_%s_mAP_h_summ" % cls],
                        global_stepnp)
                    summary_writer.add_summary(
                        mAP_r_class_dict["cls_%s_mAP_r_summ" % cls],
                        global_stepnp)
                    summary_writer.add_summary(
                        recall_h_class_dict["cls_%s_recall_h_summ" % cls],
                        global_stepnp)
                    summary_writer.add_summary(
                        recall_r_class_dict["cls_%s_recall_r_summ" % cls],
                        global_stepnp)
                    summary_writer.add_summary(
                        precision_h_class_dict["cls_%s_precision_h_summ" %
                                               cls], global_stepnp)
                    summary_writer.add_summary(
                        precision_r_class_dict["cls_%s_precision_r_summ" %
                                               cls], global_stepnp)
                    summary_writer.add_summary(
                        f1score_h_class_dict["cls_%s_f1score_h_summ" % cls],
                        global_stepnp)
                    summary_writer.add_summary(
                        f1score_r_class_dict["cls_%s_f1score_r_summ" % cls],
                        global_stepnp)

                summary_writer.flush()

        if not os.path.exists(save_dir):
            os.makedirs(save_dir)

        save_ckpt = os.path.join(save_dir,
                                 'voc_' + str(global_stepnp) + 'model.ckpt')
        #saver.save(sess, save_ckpt)
        print(' weights had been saved')

        time_to_next_eval = start_time + EVAL_INTERVAL - time.time()
        if time_to_next_eval > 0:
            time.sleep(time_to_next_eval)
コード例 #10
0
def inference(det_net, data_dir):

    # 1. preprocess img
    img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3])
    img_batch = tf.cast(img_plac, tf.float32)
    img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN)
    img_batch = short_side_resize_for_inference_data(img_tensor=img_batch,
                                                     target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN)

    rois, roi_scores, det_boxes_h, det_scores_h, det_category_h, \
    all_boxes_r, all_scores_r, all_category_r = det_net.build_whole_detection_network(input_img_batch=img_batch,
                                                                                      gtboxes_h_batch=None,
                                                                                      gtboxes_r_batch=None)

    init_op = tf.group(
        tf.global_variables_initializer(),
        tf.local_variables_initializer()
    )

    restorer, restore_ckpt = det_net.get_restorer()

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:
        sess.run(init_op)
        if not restorer is None:
            restorer.restore(sess, restore_ckpt)
            print('restore model')

        imgs = os.listdir(data_dir)
        for i, a_img_name in enumerate(imgs):

            raw_img = cv2.imread(os.path.join(data_dir,
                                              a_img_name))
            # raw_h, raw_w = raw_img.shape[0], raw_img.shape[1]

            start = time.time()
            resized_img, rois_, roi_scores_, det_boxes_h_, det_scores_h_, det_category_h_, \
            all_boxes_r_, all_scores_r_, all_category_r_ = \
                sess.run(
                    [img_batch, rois, roi_scores, det_boxes_h, det_scores_h, det_category_h,
                     all_boxes_r, all_scores_r, all_category_r],
                    feed_dict={img_plac: raw_img}
                )
            end = time.time()

	    print('all rois shape:', rois_.shape)

            all_boxes_new = all_boxes_r_        # [-1, 5]
            all_scores_new = all_scores_r_      # [-1]
            all_category_new = all_category_r_  # [-1]
            
            print('all dets shape:', all_boxes_new.shape)

	    # draw all rois from proposals
	    #rois_img_all = mylibs.draw_rois_scores(np.squeeze(resized_img, 0), rois_, roi_scores_)
	    #score_gre_05 = np.reshape(np.where(np.greater_equal(roi_scores_, 0.5)), -1)
	    #score_gre_05_rois = rois_[score_gre_05]
 	    #score_gre_05_scores = roi_scores_[score_gre_05]
	    #rois_img_part = mylibs.draw_rois_scores(np.squeeze(resized_img, 0), score_gre_05_rois, score_gre_05_scores)

            	     
            # draw all 800 detection boxes
	    all_indices = np.reshape(np.where(np.greater_equal(all_scores_new, cfgs.SHOW_SCORE_THRSHOLD)), -1)
	    left_boxes = all_boxes_new[all_indices]
	    left_scores = all_scores_new[all_indices]
	    left_category = all_category_new[all_indices]
	    #print('greater than score shape:', left_boxes.shape)
	    detection_r = draw_box_in_img.draw_rotate_box_cv(np.squeeze(resized_img, 0),
							     boxes=left_boxes,
   							     labels=left_category,
							     scores=left_scores,
							     imgname=a_img_name)
	    

            """
            while True:
                # nms
                keep = mylibs.nmsRotate(all_boxes_new, all_scores_new,
                                        cfgs.FAST_RCNN_NMS_IOU_THRESHOLD, cfgs.FAST_RCNN_NMS_MAX_BOXES_PER_CLASS)

                final_boxes = all_boxes_new[keep]
                final_scores = all_scores_new[keep]
                final_category = all_category_new[keep]

                kept_indices = np.reshape(np.where(np.greater_equal(final_scores, cfgs.SHOW_SCORE_THRSHOLD)), -1)
                det_boxes_new = final_boxes[kept_indices]
                det_scores_new = final_scores[kept_indices]
                det_category_new = final_category[kept_indices]
                # detected boxes
                contours, angles = mylibs.draw_rotate_box_cv_my(det_boxes_new, det_category_new)
                dtbox = mylibs.getRboxDegree(contours, angles)  # n
                #print('dtbox shape is', dtbox.shape)
                fuv, features = mylibs.geneTestImageFeats(dtbox)          # n-2
                if fuv.shape[0] == 0:
                    print(a_img_name, 'left none bones')
                    break

                dtbox_idx = mylibs.svmPred(fuv, features)  # deleted bones index
                #print('dtbox_idx shape is', dtbox_idx.shape)

                contour_idx = np.reshape(dtbox[dtbox_idx][:, -1], -1).astype(np.int32)
                kept_idx = kept_indices[contour_idx]
                keep_index = keep[kept_idx]
                #print('keep_index shape is', keep_index.shape)

                #n = det_boxes_new.shape[0]
                if len(dtbox_idx) == 0:   # no svm deleted bones
                    break
                else:
                    # delete some rows
                    all_boxes_new = np.delete(all_boxes_new, keep_index, axis=0)
                    all_scores_new = np.delete(all_scores_new, keep_index, axis=0)
                    all_category_new = np.delete(all_category_new, keep_index, axis=0)

            # unsorted
            fcontours = contours
            fangles = angles
            fscores = det_scores_new
   
  	    """
            """
            # save contours and angles to showSVMdecision scores
  	    detection_r = draw_box_in_img.draw_rotate_box_cv(np.squeeze(resized_img, 0),
							     boxes=det_boxes_new,
							     labels=det_category_new,
							     scores=det_scores_new,
							     imgname=a_img_name)           
	    """
            """    
            # final_dtbox[i, :] = [x0, y0, x1, y1, x2, y2, x3, y3, dg, ycenter, from_idx]
            final_dtbox = mylibs.getRboxDegree(fcontours, fangles)
            dt_idx = final_dtbox[:, -1].astype(np.int32)  # sorted
            # sorted
            fcontours = fcontours[dt_idx]
            fangles = fangles[dt_idx]
            fscores = fscores[dt_idx]

            img, tcontours, tangles = mylibs.draw_contour_box(np.squeeze(resized_img, 0), fcontours, fangles, fscores)
            t_dtbox = mylibs.getRboxDegree(tcontours, tangles)

            cobb_img = mylibs.getCobb(t_dtbox, img)
            """
             
            save_dir = os.path.join(cfgs.INFERENCE_SAVE_PATH, cfgs.VERSION)
            tools.mkdir(save_dir)
            cv2.imwrite(save_dir + '/' + a_img_name + '_roi.jpg',
                        detection_r)



            view_bar('{} cost {}s'.format(a_img_name, (end - start)), i + 1, len(imgs))
            print()