Example #1
0
def inference():
    with tf.Graph().as_default():

        img_plac = tf.placeholder(shape=[None, None, 3], dtype=tf.uint8)

        img_tensor = tf.cast(img_plac, tf.float32) - tf.constant(
            [103.939, 116.779, 123.68])
        img_batch = image_preprocess.short_side_resize_for_inference_data(
            img_tensor,
            target_shortside_len=cfgs.SHORT_SIDE_LEN,
            is_resize=True)

        # ***********************************************************************************************
        # *                                         share net                                           *
        # ***********************************************************************************************
        _, share_net = get_network_byname(net_name=cfgs.NET_NAME,
                                          inputs=img_batch,
                                          num_classes=None,
                                          is_training=True,
                                          output_stride=None,
                                          global_pool=False,
                                          spatial_squeeze=False)
        # ***********************************************************************************************
        # *                                            RPN                                              *
        # ***********************************************************************************************
        rpn = build_rpn.RPN(
            net_name=cfgs.NET_NAME,
            inputs=img_batch,
            gtboxes_and_label=None,
            is_training=False,
            share_head=cfgs.SHARE_HEAD,
            share_net=share_net,
            stride=cfgs.STRIDE,
            anchor_ratios=cfgs.ANCHOR_RATIOS,
            anchor_scales=cfgs.ANCHOR_SCALES,
            scale_factors=cfgs.SCALE_FACTORS,
            base_anchor_size_list=cfgs.
            BASE_ANCHOR_SIZE_LIST,  # P2, P3, P4, P5, P6
            level=cfgs.LEVEL,
            top_k_nms=cfgs.RPN_TOP_K_NMS,
            rpn_nms_iou_threshold=cfgs.RPN_NMS_IOU_THRESHOLD,
            max_proposals_num=cfgs.MAX_PROPOSAL_NUM,
            rpn_iou_positive_threshold=cfgs.RPN_IOU_POSITIVE_THRESHOLD,
            rpn_iou_negative_threshold=cfgs.RPN_IOU_NEGATIVE_THRESHOLD,
            rpn_mini_batch_size=cfgs.RPN_MINIBATCH_SIZE,
            rpn_positives_ratio=cfgs.RPN_POSITIVE_RATE,
            remove_outside_anchors=False,  # whether remove anchors outside
            rpn_weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME])

        # rpn predict proposals
        rpn_proposals_boxes, rpn_proposals_scores = rpn.rpn_proposals(
        )  # rpn_score shape: [300, ]

        # ***********************************************************************************************
        # *                                         Fast RCNN                                           *
        # ***********************************************************************************************
        fast_rcnn = build_fast_rcnn.FastRCNN(
            img_batch=img_batch,
            feature_pyramid=rpn.feature_pyramid,
            rpn_proposals_boxes=rpn_proposals_boxes,
            rpn_proposals_scores=rpn_proposals_scores,
            img_shape=tf.shape(img_batch),
            roi_size=cfgs.ROI_SIZE,
            scale_factors=cfgs.SCALE_FACTORS,
            roi_pool_kernel_size=cfgs.ROI_POOL_KERNEL_SIZE,
            gtboxes_and_label=None,
            fast_rcnn_nms_iou_threshold=cfgs.FAST_RCNN_NMS_IOU_THRESHOLD,
            fast_rcnn_maximum_boxes_per_img=100,
            fast_rcnn_nms_max_boxes_per_class=cfgs.
            FAST_RCNN_NMS_MAX_BOXES_PER_CLASS,
            show_detections_score_threshold=cfgs.FINAL_SCORE_THRESHOLD,
            # show detections which score >= 0.6
            num_classes=cfgs.CLASS_NUM,
            fast_rcnn_minibatch_size=cfgs.FAST_RCNN_MINIBATCH_SIZE,
            fast_rcnn_positives_ratio=cfgs.FAST_RCNN_POSITIVE_RATE,
            fast_rcnn_positives_iou_threshold=cfgs.
            FAST_RCNN_IOU_POSITIVE_THRESHOLD,
            use_dropout=False,
            weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME],
            is_training=False,
            level=cfgs.LEVEL)

        fast_rcnn_decode_boxes, fast_rcnn_score, num_of_objects, detection_category = \
            fast_rcnn.fast_rcnn_predict()

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

        restorer, restore_ckpt = restore_model.get_restorer()

        config = tf.ConfigProto()
        # config.gpu_options.per_process_gpu_memory_fraction = 0.5
        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')

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess, coord)

            imgs, img_names = get_imgs()
            for i, img in enumerate(imgs):

                start = time.time()

                _img_batch, _fast_rcnn_decode_boxes, _fast_rcnn_score, _detection_category = \
                    sess.run([img_batch, fast_rcnn_decode_boxes, fast_rcnn_score, detection_category],
                             feed_dict={img_plac: img})
                end = time.time()

                img_np = np.squeeze(_img_batch, axis=0)

                img_np = draw_box_cv(img_np,
                                     boxes=_fast_rcnn_decode_boxes,
                                     labels=_detection_category,
                                     scores=_fast_rcnn_score)
                mkdir(cfgs.INFERENCE_SAVE_PATH)
                cv2.imwrite(
                    cfgs.INFERENCE_SAVE_PATH +
                    '/{}_fpn.jpg'.format(img_names[i]), img_np)
                view_bar('{} cost {}s'.format(img_names[i], (end - start)),
                         i + 1, len(imgs))

            coord.request_stop()
            coord.join(threads)
Example #2
0
def test(img_num):
    with tf.Graph().as_default():

        img_name_batch, img_batch, gtboxes_and_label_batch, num_objects_batch = \
            next_batch(dataset_name=cfgs.DATASET_NAME,
                       batch_size=cfgs.BATCH_SIZE,
                       shortside_len=cfgs.SHORT_SIDE_LEN,
                       is_training=False)

        gtboxes_and_label, head = get_head(
            tf.squeeze(gtboxes_and_label_batch, 0))
        gtboxes_and_label = tf.py_func(back_forward_convert,
                                       inp=[gtboxes_and_label],
                                       Tout=tf.float32)
        gtboxes_and_label = tf.reshape(gtboxes_and_label, [-1, 6])
        head_quadrant = tf.py_func(get_head_quadrant,
                                   inp=[head, gtboxes_and_label],
                                   Tout=tf.float32)
        head_quadrant = tf.reshape(head_quadrant, [-1, 1])

        gtboxes_and_label_minAreaRectangle = get_horizen_minAreaRectangle(
            gtboxes_and_label)

        gtboxes_and_label_minAreaRectangle = tf.reshape(
            gtboxes_and_label_minAreaRectangle, [-1, 5])

        # ***********************************************************************************************
        # *                                         share net                                           *
        # ***********************************************************************************************
        _, share_net = get_network_byname(net_name=cfgs.NET_NAME,
                                          inputs=img_batch,
                                          num_classes=None,
                                          is_training=True,
                                          output_stride=None,
                                          global_pool=False,
                                          spatial_squeeze=False)

        # ***********************************************************************************************
        # *                                            RPN                                              *
        # ***********************************************************************************************
        rpn = build_rpn.RPN(
            net_name=cfgs.NET_NAME,
            inputs=img_batch,
            gtboxes_and_label=None,
            is_training=False,
            share_head=cfgs.SHARE_HEAD,
            share_net=share_net,
            stride=cfgs.STRIDE,
            anchor_ratios=cfgs.ANCHOR_RATIOS,
            anchor_scales=cfgs.ANCHOR_SCALES,
            scale_factors=cfgs.SCALE_FACTORS,
            base_anchor_size_list=cfgs.
            BASE_ANCHOR_SIZE_LIST,  # P2, P3, P4, P5, P6
            level=cfgs.LEVEL,
            top_k_nms=cfgs.RPN_TOP_K_NMS,
            rpn_nms_iou_threshold=cfgs.RPN_NMS_IOU_THRESHOLD,
            max_proposals_num=cfgs.MAX_PROPOSAL_NUM,
            rpn_iou_positive_threshold=cfgs.RPN_IOU_POSITIVE_THRESHOLD,
            rpn_iou_negative_threshold=cfgs.RPN_IOU_NEGATIVE_THRESHOLD,
            rpn_mini_batch_size=cfgs.RPN_MINIBATCH_SIZE,
            rpn_positives_ratio=cfgs.RPN_POSITIVE_RATE,
            remove_outside_anchors=False,  # whether remove anchors outside
            rpn_weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME])

        # rpn predict proposals
        rpn_proposals_boxes, rpn_proposals_scores = rpn.rpn_proposals(
        )  # rpn_score shape: [300, ]

        # ***********************************************************************************************
        # *                                         Fast RCNN                                           *
        # ***********************************************************************************************
        fast_rcnn = build_fast_rcnn.FastRCNN(
            feature_pyramid=rpn.feature_pyramid,
            rpn_proposals_boxes=rpn_proposals_boxes,
            rpn_proposals_scores=rpn_proposals_scores,
            img_shape=tf.shape(img_batch),
            img_batch=img_batch,
            roi_size=cfgs.ROI_SIZE,
            roi_pool_kernel_size=cfgs.ROI_POOL_KERNEL_SIZE,
            scale_factors=cfgs.SCALE_FACTORS,
            gtboxes_and_label=None,
            gtboxes_and_label_minAreaRectangle=
            gtboxes_and_label_minAreaRectangle,
            fast_rcnn_nms_iou_threshold=cfgs.FAST_RCNN_NMS_IOU_THRESHOLD,
            fast_rcnn_maximum_boxes_per_img=100,
            fast_rcnn_nms_max_boxes_per_class=cfgs.
            FAST_RCNN_NMS_MAX_BOXES_PER_CLASS,
            show_detections_score_threshold=cfgs.FINAL_SCORE_THRESHOLD,
            # show detections which score >= 0.6
            num_classes=cfgs.CLASS_NUM,
            fast_rcnn_minibatch_size=cfgs.FAST_RCNN_MINIBATCH_SIZE,
            fast_rcnn_positives_ratio=cfgs.FAST_RCNN_POSITIVE_RATE,
            fast_rcnn_positives_iou_threshold=cfgs.
            FAST_RCNN_IOU_POSITIVE_THRESHOLD,
            # iou>0.5 is positive, iou<0.5 is negative
            use_dropout=cfgs.USE_DROPOUT,
            weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME],
            is_training=False,
            level=cfgs.LEVEL,
            head_quadrant=head_quadrant)

        fast_rcnn_decode_boxes, fast_rcnn_score, num_of_objects, detection_category, \
        fast_rcnn_decode_boxes_rotate, fast_rcnn_score_rotate, fast_rcnn_head_quadrant, \
        num_of_objects_rotate, detection_category_rotate = fast_rcnn.fast_rcnn_predict()

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

        restorer, restore_ckpt = restore_model.get_restorer()

        config = tf.ConfigProto()
        # config.gpu_options.per_process_gpu_memory_fraction = 0.5
        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')

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess, coord)

            for i in range(img_num):

                start = time.time()

                _img_name_batch, _img_batch, _gtboxes_and_label, _gtboxes_and_label_minAreaRectangle, _head_quadrant,  \
                _fast_rcnn_decode_boxes, _fast_rcnn_score, _detection_category, _fast_rcnn_decode_boxes_rotate, \
                _fast_rcnn_score_rotate, _fast_rcnn_head_quadrant, _detection_category_rotate \
                    = sess.run([img_name_batch, img_batch, gtboxes_and_label, gtboxes_and_label_minAreaRectangle, head_quadrant,
                                fast_rcnn_decode_boxes, fast_rcnn_score, detection_category, fast_rcnn_decode_boxes_rotate,
                                fast_rcnn_score_rotate, fast_rcnn_head_quadrant, detection_category_rotate])
                end = time.time()

                _img_batch = np.squeeze(_img_batch, axis=0)

                _img_batch_fpn_horizonal = help_utils.draw_box_cv(
                    _img_batch,
                    boxes=_fast_rcnn_decode_boxes,
                    labels=_detection_category,
                    scores=_fast_rcnn_score)

                _img_batch_fpn_rotate = help_utils.draw_rotate_box_cv(
                    _img_batch,
                    boxes=_fast_rcnn_decode_boxes_rotate,
                    labels=_detection_category_rotate,
                    scores=_fast_rcnn_score_rotate,
                    head=np.argmax(_fast_rcnn_head_quadrant, axis=1))
                mkdir(cfgs.TEST_SAVE_PATH)
                cv2.imwrite(
                    cfgs.TEST_SAVE_PATH +
                    '/{}_horizontal_fpn.jpg'.format(str(_img_name_batch[0])),
                    _img_batch_fpn_horizonal)
                cv2.imwrite(
                    cfgs.TEST_SAVE_PATH +
                    '/{}_rotate_fpn.jpg'.format(str(_img_name_batch[0])),
                    _img_batch_fpn_rotate)

                temp_label_horizontal = np.reshape(_gtboxes_and_label[:, -1:],
                                                   [
                                                       -1,
                                                   ]).astype(np.int64)
                temp_label_rotate = np.reshape(_gtboxes_and_label[:, -1:], [
                    -1,
                ]).astype(np.int64)

                _img_batch_gt_horizontal = help_utils.draw_box_cv(
                    _img_batch,
                    boxes=_gtboxes_and_label_minAreaRectangle[:, :-1],
                    labels=temp_label_horizontal,
                    scores=None)

                _img_batch_gt_rotate = help_utils.draw_rotate_box_cv(
                    _img_batch,
                    boxes=_gtboxes_and_label[:, :-1],
                    labels=temp_label_rotate,
                    scores=None,
                    head=np.reshape(_head_quadrant, [
                        -1,
                    ]))

                cv2.imwrite(
                    cfgs.TEST_SAVE_PATH +
                    '/{}_horizontal_gt.jpg'.format(str(_img_name_batch[0])),
                    _img_batch_gt_horizontal)
                cv2.imwrite(
                    cfgs.TEST_SAVE_PATH +
                    '/{}_rotate_gt.jpg'.format(str(_img_name_batch[0])),
                    _img_batch_gt_rotate)

                view_bar(
                    '{} image cost {}s'.format(str(_img_name_batch[0]),
                                               (end - start)), i + 1, img_num)

            coord.request_stop()
            coord.join(threads)
Example #3
0
def test(args):
    with tf.Graph().as_default():

        img_batch = tf.placeholder(shape=[1, None, None, 3], dtype=tf.float32)

        # img_name_batch, img_batch, gtboxes_and_label_batch, num_objects_batch = \
        #     next_batch(dataset_name=cfgs.DATASET_NAME,
        #                batch_size=cfgs.BATCH_SIZE,
        #                shortside_len=cfgs.SHORT_SIDE_LEN,
        #                is_training=False)

        # ***********************************************************************************************
        # *                                         share net                                           *
        # ***********************************************************************************************
        _, share_net = get_network_byname(net_name=cfgs.NET_NAME,
                                          inputs=img_batch,
                                          num_classes=None,
                                          is_training=True,
                                          output_stride=None,
                                          global_pool=False,
                                          spatial_squeeze=False)

        # ***********************************************************************************************
        # *                                            RPN                                              *
        # ***********************************************************************************************
        rpn = build_rpn.RPN(
            net_name=cfgs.NET_NAME,
            inputs=img_batch,
            gtboxes_and_label=None,
            is_training=False,
            share_head=cfgs.SHARE_HEAD,
            share_net=share_net,
            stride=cfgs.STRIDE,
            anchor_ratios=cfgs.ANCHOR_RATIOS,
            anchor_scales=cfgs.ANCHOR_SCALES,
            scale_factors=cfgs.SCALE_FACTORS,
            base_anchor_size_list=cfgs.
            BASE_ANCHOR_SIZE_LIST,  # P2, P3, P4, P5, P6
            level=cfgs.LEVEL,
            top_k_nms=cfgs.RPN_TOP_K_NMS,
            rpn_nms_iou_threshold=cfgs.RPN_NMS_IOU_THRESHOLD,
            max_proposals_num=cfgs.MAX_PROPOSAL_NUM,
            rpn_iou_positive_threshold=cfgs.RPN_IOU_POSITIVE_THRESHOLD,
            rpn_iou_negative_threshold=cfgs.RPN_IOU_NEGATIVE_THRESHOLD,
            rpn_mini_batch_size=cfgs.RPN_MINIBATCH_SIZE,
            rpn_positives_ratio=cfgs.RPN_POSITIVE_RATE,
            remove_outside_anchors=False,  # whether remove anchors outside
            rpn_weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME])

        # rpn predict proposals
        rpn_proposals_boxes, rpn_proposals_scores = rpn.rpn_proposals(
        )  # rpn_score shape: [300, ]

        # ***********************************************************************************************
        # *                                         Fast RCNN                                           *
        # ***********************************************************************************************
        fast_rcnn = build_fast_rcnn.FastRCNN(
            img_batch=img_batch,
            feature_pyramid=rpn.feature_pyramid,
            rpn_proposals_boxes=rpn_proposals_boxes,
            rpn_proposals_scores=rpn_proposals_scores,
            img_shape=tf.shape(img_batch),
            roi_size=cfgs.ROI_SIZE,
            scale_factors=cfgs.SCALE_FACTORS,
            roi_pool_kernel_size=cfgs.ROI_POOL_KERNEL_SIZE,
            gtboxes_and_label=None,
            fast_rcnn_nms_iou_threshold=cfgs.FAST_RCNN_NMS_IOU_THRESHOLD,
            fast_rcnn_maximum_boxes_per_img=100,
            fast_rcnn_nms_max_boxes_per_class=cfgs.
            FAST_RCNN_NMS_MAX_BOXES_PER_CLASS,
            show_detections_score_threshold=cfgs.
            FINAL_SCORE_THRESHOLD,  # show detections which score >= 0.6
            num_classes=cfgs.CLASS_NUM,
            fast_rcnn_minibatch_size=cfgs.FAST_RCNN_MINIBATCH_SIZE,
            fast_rcnn_positives_ratio=cfgs.FAST_RCNN_POSITIVE_RATE,
            fast_rcnn_positives_iou_threshold=cfgs.
            FAST_RCNN_IOU_POSITIVE_THRESHOLD,
            use_dropout=False,
            weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME],
            is_training=False,
            level=cfgs.LEVEL)

        fast_rcnn_decode_boxes, fast_rcnn_score, num_of_objects, detection_category = \
            fast_rcnn.fast_rcnn_predict()

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

        restorer, restore_ckpt = restore_model.get_restorer(
            checkpoint_path=args.weights)

        config = tf.ConfigProto()
        # config.gpu_options.per_process_gpu_memory_fraction = 0.5
        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')

            # coord = tf.train.Coordinator()
            # threads = tf.train.start_queue_runners(sess, coord)
            test_filenames = [file for file in os.listdir(args.test_dir)]
            for i in tqdm(range(args.img_num)):

                # _img_name_batch, _img_batch, _gtboxes_and_label_batch, _fast_rcnn_decode_boxes, \
                #     _fast_rcnn_score, _detection_category \
                #     = sess.run([img_name_batch, img_batch, gtboxes_and_label_batch, fast_rcnn_decode_boxes,
                #                 fast_rcnn_score, detection_category])
                img_test = cv2.imread(args.test_dir + '/' + test_filenames[i])
                img_test, img_test_4dim = no_tensor_pic_prepross(
                    img_test, cfgs.SHORT_SIDE_LEN)

                _fast_rcnn_decode_boxes, \
                    _fast_rcnn_score, _detection_category \
                    = sess.run([fast_rcnn_decode_boxes,
                                fast_rcnn_score, detection_category], feed_dict={img_batch: img_test_4dim})

                # _img_batch = np.squeeze(_img_batch, axis=0)
                # print(_detection_category)
                _img_batch_fpn = help_utils.draw_box_cv(
                    img_test,
                    boxes=_fast_rcnn_decode_boxes,
                    labels=_detection_category,
                    scores=_fast_rcnn_score)
                mkdir(cfgs.TEST_SAVE_PATH)
                cv2.imwrite(
                    cfgs.TEST_SAVE_PATH +
                    '/{}_fpn.jpg'.format(str(test_filenames[i])),
                    _img_batch_fpn)
Example #4
0
def test(img_num):
    with tf.Graph().as_default():

        # img = tf.placeholder(shape=[None, None, 3], dtype=tf.uint8)

        img_name_batch, img_batch, gtboxes_and_label_batch, num_objects_batch = \
            next_batch(dataset_name=cfgs.DATASET_NAME,
                       batch_size=cfgs.BATCH_SIZE,
                       shortside_len=cfgs.SHORT_SIDE_LEN,
                       is_training=False)

        gtboxes_and_label = tf.py_func(back_forward_convert,
                                       inp=[tf.squeeze(gtboxes_and_label_batch, 0)],
                                       Tout=tf.float32)
        gtboxes_and_label = tf.reshape(gtboxes_and_label, [-1, 6])

        # ***********************************************************************************************
        # *                                         share net                                           *
        # ***********************************************************************************************
        _, share_net = get_network_byname(net_name=cfgs.NET_NAME,
                                          inputs=img_batch,
                                          num_classes=None,
                                          is_training=True,
                                          output_stride=None,
                                          global_pool=False,
                                          spatial_squeeze=False)
        # ***********************************************************************************************
        # *                                            RPN                                              *
        # ***********************************************************************************************
        rpn = build_rpn.RPN(net_name=cfgs.NET_NAME,
                            inputs=img_batch,
                            gtboxes_and_label=gtboxes_and_label,
                            is_training=False,
                            share_head=False,
                            share_net=share_net,
                            anchor_ratios=cfgs.ANCHOR_RATIOS,
                            anchor_scales=cfgs.ANCHOR_SCALES,
                            anchor_angles=cfgs.ANCHOR_ANGLES,
                            scale_factors=cfgs.SCALE_FACTORS,
                            base_anchor_size_list=cfgs.BASE_ANCHOR_SIZE_LIST,  # P2, P3, P4, P5, P6
                            level=cfgs.LEVEL,
                            anchor_stride=cfgs.ANCHOR_STRIDE,
                            top_k_nms=cfgs.RPN_TOP_K_NMS,
                            kernel_size=cfgs.KERNEL_SIZE,
                            use_angles_condition=True,
                            anchor_angle_threshold=cfgs.RPN_ANCHOR_ANGLES_THRESHOLD,
                            nms_angle_threshold=cfgs.RPN_NMS_ANGLES_THRESHOLD,
                            rpn_nms_iou_threshold=cfgs.RPN_NMS_IOU_THRESHOLD,
                            max_proposals_num=cfgs.MAX_PROPOSAL_NUM,
                            rpn_iou_positive_threshold=cfgs.RPN_IOU_POSITIVE_THRESHOLD,
                            rpn_iou_negative_threshold=cfgs.RPN_IOU_NEGATIVE_THRESHOLD,
                            rpn_mini_batch_size=cfgs.RPN_MINIBATCH_SIZE,
                            rpn_positives_ratio=cfgs.RPN_POSITIVE_RATE,
                            remove_outside_anchors=False,  # whether remove anchors outside
                            rpn_weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME],
                            scope='')

        # rpn predict proposals
        rpn_proposals_boxes, rpn_proposals_scores = rpn.rpn_proposals()  # rpn_score shape: [300, ]
        # _, _, rpn_top_k_boxes, rpn_top_k_scores = rpn.rpn_losses()

        # ***********************************************************************************************
        # *                                         Fast RCNN                                           *
        # ***********************************************************************************************
        fast_rcnn = build_fast_rcnn.FastRCNN(img_batch=img_batch,
                                             feature_pyramid=rpn.feature_pyramid,
                                             rpn_proposals_boxes=rpn_proposals_boxes,
                                             rpn_proposals_scores=rpn_proposals_scores,
                                             stop_gradient_for_proposals=False,
                                             img_shape=tf.shape(img_batch),
                                             roi_size=cfgs.ROI_SIZE,
                                             roi_pool_kernel_size=cfgs.ROI_POOL_KERNEL_SIZE,
                                             scale_factors=cfgs.SCALE_FACTORS,
                                             gtboxes_and_label=None,
                                             fast_rcnn_nms_iou_threshold=cfgs.FAST_RCNN_NMS_IOU_THRESHOLD,
                                             top_k_nms=cfgs.FAST_RCNN_TOP_K_NMS,
                                             nms_angle_threshold=cfgs.FAST_RCNN_NMS_ANGLES_THRESHOLD,
                                             use_angle_condition=False,
                                             level=cfgs.LEVEL,
                                             fast_rcnn_maximum_boxes_per_img=100,
                                             fast_rcnn_nms_max_boxes_per_class=cfgs.FAST_RCNN_NMS_MAX_BOXES_PER_CLASS,
                                             show_detections_score_threshold=cfgs.FINAL_SCORE_THRESHOLD,
                                             # show detections which score >= 0.6
                                             num_classes=cfgs.CLASS_NUM,
                                             fast_rcnn_minibatch_size=cfgs.FAST_RCNN_MINIBATCH_SIZE,
                                             fast_rcnn_positives_ratio=cfgs.FAST_RCNN_POSITIVE_RATE,
                                             fast_rcnn_positives_iou_threshold=cfgs.FAST_RCNN_IOU_POSITIVE_THRESHOLD,
                                             boxes_angle_threshold=cfgs.FAST_RCNN_BOXES_ANGLES_THRESHOLD,
                                             use_dropout=cfgs.USE_DROPOUT,
                                             weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME],
                                             is_training=False)

        fast_rcnn_decode_boxes, fast_rcnn_score, num_of_objects, detection_category = \
            fast_rcnn.fast_rcnn_predict()

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

        restorer, restore_ckpt = restore_model.get_restorer()

        config = tf.ConfigProto()
        # config.gpu_options.per_process_gpu_memory_fraction = 0.5
        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')

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess, coord)

            for i in range(img_num):

                start = time.time()

                _img_name_batch, _img_batch, _gtboxes_and_label, _fast_rcnn_decode_boxes, \
                _fast_rcnn_score, _detection_category \
                    = sess.run([img_name_batch, img_batch, gtboxes_and_label, fast_rcnn_decode_boxes,
                                fast_rcnn_score, detection_category])
                end = time.time()

                _img_batch = np.squeeze(_img_batch, axis=0)

                _img_batch_fpn = help_utils.draw_box_cv(_img_batch,
                                                        boxes=_fast_rcnn_decode_boxes,
                                                        labels=_detection_category,
                                                        scores=_fast_rcnn_score)

                cv2.imwrite(cfgs.TEST_SAVE_PATH + '/{}_fpn.jpg'.format(str(_img_name_batch[0]).split('.tif')[0]),
                            _img_batch_fpn)

                temp_label = np.reshape(_gtboxes_and_label[:, -1:], [-1, ]).astype(np.int64)
                _img_batch_gt = help_utils.draw_box_cv(_img_batch,
                                                       boxes=_gtboxes_and_label[:, :-1],
                                                       labels=temp_label,
                                                       scores=None)

                cv2.imwrite(cfgs.TEST_SAVE_PATH + '/{}_gt.jpg'.format(str(_img_name_batch[0]).split('.tif')[0]),
                            _img_batch_gt)

                view_bar('{} image cost {}s'.format(str(_img_name_batch[0]), (end - start)), i+1, img_num)

            coord.request_stop()
            coord.join(threads)
Example #5
0
def test(img_num):
    with tf.Graph().as_default():

        img_name_batch, img_batch, gtboxes_and_label_batch, num_objects_batch = \
            next_batch(dataset_name=cfgs.DATASET_NAME,
                       batch_size=cfgs.BATCH_SIZE,
                       shortside_len=cfgs.SHORT_SIDE_LEN,
                       is_training=False)

        gtboxes_and_label = tf.py_func(
            back_forward_convert,
            inp=[tf.squeeze(gtboxes_and_label_batch, 0)],
            Tout=tf.float32)
        gtboxes_and_label = tf.reshape(gtboxes_and_label, [-1, 6])

        gtboxes_and_label_minAreaRectangle = get_horizen_minAreaRectangle(
            gtboxes_and_label)

        gtboxes_and_label_minAreaRectangle = tf.reshape(
            gtboxes_and_label_minAreaRectangle, [-1, 5])

        # ***********************************************************************************************
        # *                                         share net                                           *
        # ***********************************************************************************************
        _, share_net = get_network_byname(net_name=cfgs.NET_NAME,
                                          inputs=img_batch,
                                          num_classes=None,
                                          is_training=True,
                                          output_stride=None,
                                          global_pool=False,
                                          spatial_squeeze=False)

        # ***********************************************************************************************
        # *                                            RPN                                              *
        # ***********************************************************************************************
        rpn = build_rpn.RPN(
            net_name=cfgs.NET_NAME,
            inputs=img_batch,
            gtboxes_and_label=None,
            is_training=False,
            share_head=cfgs.SHARE_HEAD,
            share_net=share_net,
            stride=cfgs.STRIDE,
            anchor_ratios=cfgs.ANCHOR_RATIOS,
            anchor_scales=cfgs.ANCHOR_SCALES,
            scale_factors=cfgs.SCALE_FACTORS,
            base_anchor_size_list=cfgs.
            BASE_ANCHOR_SIZE_LIST,  # P2, P3, P4, P5, P6
            level=cfgs.LEVEL,
            top_k_nms=cfgs.RPN_TOP_K_NMS,
            rpn_nms_iou_threshold=cfgs.RPN_NMS_IOU_THRESHOLD,
            max_proposals_num=cfgs.MAX_PROPOSAL_NUM,
            rpn_iou_positive_threshold=cfgs.RPN_IOU_POSITIVE_THRESHOLD,
            rpn_iou_negative_threshold=cfgs.RPN_IOU_NEGATIVE_THRESHOLD,
            rpn_mini_batch_size=cfgs.RPN_MINIBATCH_SIZE,
            rpn_positives_ratio=cfgs.RPN_POSITIVE_RATE,
            remove_outside_anchors=False,  # whether remove anchors outside
            rpn_weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME])

        # rpn predict proposals
        rpn_proposals_boxes, rpn_proposals_scores = rpn.rpn_proposals(
        )  # rpn_score shape: [300, ]

        # ***********************************************************************************************
        # *                                         Fast RCNN                                           *
        # ***********************************************************************************************
        fast_rcnn = build_fast_rcnn1.FastRCNN(
            feature_pyramid=rpn.feature_pyramid,
            rpn_proposals_boxes=rpn_proposals_boxes,
            rpn_proposals_scores=rpn_proposals_scores,
            img_shape=tf.shape(img_batch),
            roi_size=cfgs.ROI_SIZE,
            roi_pool_kernel_size=cfgs.ROI_POOL_KERNEL_SIZE,
            scale_factors=cfgs.SCALE_FACTORS,
            gtboxes_and_label=None,
            gtboxes_and_label_minAreaRectangle=
            gtboxes_and_label_minAreaRectangle,
            fast_rcnn_nms_iou_threshold=cfgs.FAST_RCNN_NMS_IOU_THRESHOLD,
            fast_rcnn_maximum_boxes_per_img=100,
            fast_rcnn_nms_max_boxes_per_class=cfgs.
            FAST_RCNN_NMS_MAX_BOXES_PER_CLASS,
            show_detections_score_threshold=cfgs.FINAL_SCORE_THRESHOLD,
            # show detections which score >= 0.6
            num_classes=cfgs.CLASS_NUM,
            fast_rcnn_minibatch_size=cfgs.FAST_RCNN_MINIBATCH_SIZE,
            fast_rcnn_positives_ratio=cfgs.FAST_RCNN_POSITIVE_RATE,
            fast_rcnn_positives_iou_threshold=cfgs.
            FAST_RCNN_IOU_POSITIVE_THRESHOLD,
            # iou>0.5 is positive, iou<0.5 is negative
            use_dropout=cfgs.USE_DROPOUT,
            weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME],
            is_training=False,
            level=cfgs.LEVEL)

        fast_rcnn_decode_boxes, fast_rcnn_score, num_of_objects, detection_category, \
        fast_rcnn_decode_boxes_rotate, fast_rcnn_score_rotate, num_of_objects_rotate, detection_category_rotate,_, _, _, _ = \
            fast_rcnn.fast_rcnn_predict()

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

        restorer, restore_ckpt = restore_model.get_restorer()

        config = tf.ConfigProto()
        # config.gpu_options.per_process_gpu_memory_fraction = 0.5
        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')

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess, coord)

            TrueDetection = 0
            FalseAlarm = 0
            TotalObjects = 0
            TotalDetections = 0
            for i in range(img_num):

                start = time.time()

                _img_name_batch, _img_batch, _gtboxes_and_label, _gtboxes_and_label_minAreaRectangle, \
                _fast_rcnn_decode_boxes, _fast_rcnn_score, _detection_category, _fast_rcnn_decode_boxes_rotate, \
                _fast_rcnn_score_rotate, _detection_category_rotate \
                    = sess.run([img_name_batch, img_batch, gtboxes_and_label, gtboxes_and_label_minAreaRectangle,
                                fast_rcnn_decode_boxes, fast_rcnn_score, detection_category, fast_rcnn_decode_boxes_rotate,
                                fast_rcnn_score_rotate, detection_category_rotate])
                end = time.time()

                _img_batch = np.squeeze(_img_batch, axis=0)

                _img_batch_fpn_horizonal = help_utils.draw_box_cv(
                    _img_batch,
                    boxes=_fast_rcnn_decode_boxes,
                    labels=_detection_category,
                    scores=_fast_rcnn_score)

                _img_batch_fpn_rotate = help_utils.draw_rotate_box_cv(
                    _img_batch,
                    boxes=_fast_rcnn_decode_boxes_rotate,
                    labels=_detection_category_rotate,
                    scores=_fast_rcnn_score_rotate)
                mkdir(cfgs.TEST_SAVE_PATH)
                cv2.imwrite(
                    cfgs.TEST_SAVE_PATH + str(_img_name_batch[0]) +
                    '_horizontal_fpn.jpg', _img_batch_fpn_horizonal)
                cv2.imwrite(
                    cfgs.TEST_SAVE_PATH + str(_img_name_batch[0]) +
                    '_rotate_fpn.jpg', _img_batch_fpn_rotate)

                temp_label_horizontal = np.reshape(_gtboxes_and_label[:, -1:],
                                                   [
                                                       -1,
                                                   ]).astype(np.int64)
                temp_label_rotate = np.reshape(_gtboxes_and_label[:, -1:], [
                    -1,
                ]).astype(np.int64)

                _img_batch_gt_horizontal = help_utils.draw_box_cv(
                    _img_batch,
                    boxes=_gtboxes_and_label_minAreaRectangle[:, :-1],
                    labels=temp_label_horizontal,
                    scores=None)

                _img_batch_gt_rotate = help_utils.draw_rotate_box_cv(
                    _img_batch,
                    boxes=_gtboxes_and_label[:, :-1],
                    labels=temp_label_rotate,
                    scores=None)

                cv2.imwrite(
                    cfgs.TEST_SAVE_PATH + str(_img_name_batch[0]) +
                    '_horizontal_gt.jpg', _img_batch_gt_horizontal)
                cv2.imwrite(
                    cfgs.TEST_SAVE_PATH + str(_img_name_batch[0]) +
                    '_rotate_gt.jpg', _img_batch_gt_rotate)

                Current_TrueDetection = 0
                lock_list = []
                for m in range(len(_gtboxes_and_label)):
                    for n, box in enumerate(_fast_rcnn_decode_boxes_rotate):
                        if n in lock_list:
                            continue
                        y_c, x_c, h, w, theta = box[0], box[1], box[2], box[
                            3], box[4]
                        r1 = ((x_c, y_c), (w, h), theta)
                        gt_box = _gtboxes_and_label[m]
                        gt_yc, gt_xc, gt_h, gt_w, gt_theta = gt_box[0], gt_box[
                            1], gt_box[2], gt_box[3], gt_box[4]
                        r2 = ((gt_xc, gt_yc), (gt_w, gt_h), gt_theta)
                        int_pts = cv2.rotatedRectangleIntersection(r1, r2)[0]
                        if int_pts > 0.5:
                            Current_TrueDetection += 1
                            lock_list.append(n)
                TrueDetection += Current_TrueDetection
                if (len(_fast_rcnn_decode_boxes_rotate) > 0):
                    FalseAlarm += len(
                        _fast_rcnn_score_rotate) - Current_TrueDetection
                    TotalDetections += len(_fast_rcnn_score_rotate)
                TotalObjects += len(_gtboxes_and_label)

                view_bar(
                    '{} image cost {}s'.format(str(_img_name_batch[0]),
                                               (end - start)), i + 1, img_num)
            Pd = float(TrueDetection) / float(TotalDetections)
            Pf = float(FalseAlarm) / float(TotalDetections)
            F1 = 2 * float(Pd * (1 - Pf)) / float(Pd + (1 - Pf))

            Op = float(TrueDetection) / float(TotalObjects)

            print("\nACC:  " + str(Pd))
            print("FalseAlarm:  " + str(Pf))
            print("F1:  " + str(F1))

            print("Op:  " + str(TrueDetection) + "/" + str(TotalObjects) +
                  "=" + str(Op))

            coord.request_stop()
            coord.join(threads)
Example #6
0
def test(img_num):
    with tf.Graph().as_default():

        # img = tf.placeholder(shape=[None, None, 3], dtype=tf.uint8)

        img_name_batch, img_batch, gtboxes_and_label_batch, num_objects_batch = \
            next_batch(dataset_name=cfgs.DATASET_NAME,
                       batch_size=cfgs.BATCH_SIZE,
                       shortside_len=cfgs.SHORT_SIDE_LEN,
                       is_training=False)

        gtboxes_and_label = tf.py_func(
            back_forward_convert,
            inp=[tf.squeeze(gtboxes_and_label_batch, 0)],
            Tout=tf.float32)
        gtboxes_and_label = tf.reshape(gtboxes_and_label, [-1, 6])

        # ***********************************************************************************************
        # *                                         share net                                           *
        # ***********************************************************************************************
        _, share_net = get_network_byname(net_name=cfgs.NET_NAME,
                                          inputs=img_batch,
                                          num_classes=None,
                                          is_training=True,
                                          output_stride=None,
                                          global_pool=False,
                                          spatial_squeeze=False)
        # ***********************************************************************************************
        # *                                            RPN                                              *
        # ***********************************************************************************************
        rpn = build_rpn.RPN(
            net_name=cfgs.NET_NAME,
            inputs=img_batch,
            gtboxes_and_label=gtboxes_and_label,
            is_training=False,
            share_head=cfgs.SHARE_HEAD,
            share_net=share_net,
            anchor_ratios=cfgs.ANCHOR_RATIOS,
            anchor_scales=cfgs.ANCHOR_SCALES,
            anchor_angles=cfgs.ANCHOR_ANGLES,
            scale_factors=cfgs.SCALE_FACTORS,
            base_anchor_size_list=cfgs.
            BASE_ANCHOR_SIZE_LIST,  # P2, P3, P4, P5, P6
            level=cfgs.LEVEL,
            anchor_stride=cfgs.ANCHOR_STRIDE,
            pool_stride=cfgs.POOL_STRIDE,
            top_k_nms=cfgs.RPN_TOP_K_NMS,
            kernel_size=cfgs.KERNEL_SIZE,
            use_angles_condition=True,
            anchor_angle_threshold=cfgs.RPN_ANCHOR_ANGLES_THRESHOLD,
            nms_angle_threshold=cfgs.RPN_NMS_ANGLES_THRESHOLD,
            rpn_nms_iou_threshold=cfgs.RPN_NMS_IOU_THRESHOLD,
            max_proposals_num=cfgs.MAX_PROPOSAL_NUM,
            rpn_iou_positive_threshold=cfgs.RPN_IOU_POSITIVE_THRESHOLD,
            rpn_iou_negative_threshold=cfgs.RPN_IOU_NEGATIVE_THRESHOLD,
            rpn_mini_batch_size=cfgs.RPN_MINIBATCH_SIZE,
            rpn_positives_ratio=cfgs.RPN_POSITIVE_RATE,
            remove_outside_anchors=False,  # whether remove anchors outside
            rpn_weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME],
            scope='')

        # rpn predict proposals
        rpn_proposals_boxes, rpn_proposals_scores = rpn.rpn_proposals(
        )  # rpn_score shape: [300, ]
        _, _, rpn_top_k_boxes, rpn_top_k_scores = rpn.rpn_losses()

        # ***********************************************************************************************
        # *                                         Fast RCNN                                           *
        # ***********************************************************************************************
        fast_rcnn = build_fast_rcnn.FastRCNN(
            img_batch=img_batch,
            feature_pyramid=rpn.feature_pyramid,
            rpn_proposals_boxes=rpn_proposals_boxes,
            rpn_proposals_scores=rpn_proposals_scores,
            stop_gradient_for_proposals=False,
            img_shape=tf.shape(img_batch),
            roi_size=cfgs.ROI_SIZE,
            roi_pool_kernel_size=cfgs.ROI_POOL_KERNEL_SIZE,
            scale_factors=cfgs.SCALE_FACTORS,
            gtboxes_and_label=None,
            fast_rcnn_nms_iou_threshold=cfgs.FAST_RCNN_NMS_IOU_THRESHOLD,
            top_k_nms=cfgs.FAST_RCNN_TOP_K_NMS,
            nms_angle_threshold=cfgs.FAST_RCNN_NMS_ANGLES_THRESHOLD,
            use_angle_condition=False,
            level=cfgs.LEVEL,
            fast_rcnn_maximum_boxes_per_img=100,
            fast_rcnn_nms_max_boxes_per_class=cfgs.
            FAST_RCNN_NMS_MAX_BOXES_PER_CLASS,
            show_detections_score_threshold=cfgs.FINAL_SCORE_THRESHOLD,
            # show detections which score >= 0.6
            num_classes=cfgs.CLASS_NUM,
            fast_rcnn_minibatch_size=cfgs.FAST_RCNN_MINIBATCH_SIZE,
            fast_rcnn_positives_ratio=cfgs.FAST_RCNN_POSITIVE_RATE,
            fast_rcnn_positives_iou_threshold=cfgs.
            FAST_RCNN_IOU_POSITIVE_THRESHOLD,
            boxes_angle_threshold=cfgs.FAST_RCNN_BOXES_ANGLES_THRESHOLD,
            use_dropout=cfgs.USE_DROPOUT,
            weight_decay=cfgs.WEIGHT_DECAY[cfgs.NET_NAME],
            is_training=False)

        fast_rcnn_decode_boxes, fast_rcnn_score, num_of_objects, detection_category = \
            fast_rcnn.fast_rcnn_predict()

        ##############################################################################################
        if cfgs.NEED_AUXILIARY:
            predict_boxes = tf.concat(
                [fast_rcnn_decode_boxes, rpn_top_k_boxes], axis=0)
            predict_scores = tf.concat(
                [fast_rcnn_score, rpn_top_k_scores - 0.2], axis=0)
            rpn_top_k_label = tf.ones([
                tf.shape(rpn_top_k_scores)[0],
            ], tf.int64)
            labels = tf.concat([detection_category, rpn_top_k_label], axis=0)

            # valid_indices = nms_rotate.nms_rotate(decode_boxes=predict_boxes,
            #                                       scores=predict_scores,
            #                                       iou_threshold=0.15,
            #                                       max_output_size=30,
            #                                       use_angle_condition=False,
            #                                       angle_threshold=15,
            #                                       use_gpu=True)
            valid_indices = tf.py_func(nms_rotate.nms_rotate_cpu,
                                       inp=[
                                           predict_boxes, predict_scores,
                                           tf.constant(0.15, tf.float32),
                                           tf.constant(30, tf.float32)
                                       ],
                                       Tout=tf.int64)

            fast_rcnn_decode_boxes = tf.gather(predict_boxes, valid_indices)
            fast_rcnn_score = tf.gather(predict_scores, valid_indices)
            detection_category = tf.gather(labels, valid_indices)

            ##############################################################################################

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

        restorer, restore_ckpt = restore_model.get_restorer()

        config = tf.ConfigProto()
        # config.gpu_options.per_process_gpu_memory_fraction = 0.5
        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')

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess, coord)

            for i in range(img_num):

                start = time.time()

                _img_name_batch, _img_batch, _gtboxes_and_label, _fast_rcnn_decode_boxes, \
                _fast_rcnn_score, _detection_category \
                    = sess.run([img_name_batch, img_batch, gtboxes_and_label, fast_rcnn_decode_boxes,
                                fast_rcnn_score, detection_category])
                end = time.time()

                _img_batch = np.squeeze(_img_batch, axis=0)

                _img_batch_fpn = help_utils.draw_box_cv(
                    _img_batch,
                    boxes=_fast_rcnn_decode_boxes,
                    labels=_detection_category,
                    scores=_fast_rcnn_score)
                mkdir(cfgs.TEST_SAVE_PATH)
                cv2.imwrite(
                    cfgs.TEST_SAVE_PATH + '/{}_fpn.jpg'.format(
                        str(_img_name_batch[0]).split('.tif')[0]),
                    _img_batch_fpn)

                # _gtboxes_and_label_batch = np.squeeze(_gtboxes_and_label_batch, axis=0)

                temp_label = np.reshape(_gtboxes_and_label[:, -1:], [
                    -1,
                ]).astype(np.int64)
                _img_batch_gt = help_utils.draw_box_cv(
                    _img_batch,
                    boxes=_gtboxes_and_label[:, :-1],
                    labels=temp_label,
                    scores=None)

                cv2.imwrite(
                    cfgs.TEST_SAVE_PATH + '/{}_gt.jpg'.format(
                        str(_img_name_batch[0]).split('.tif')[0]),
                    _img_batch_gt)

                #view_bar('{} image cost {}s'.format(str(_img_name_batch[0]), (end - start)), i+1, img_num)
                print('{} image cost {}s'.format(str(_img_name_batch[0]),
                                                 (end - start)) + str(i + 1) +
                      '/' + str(img_num))

            coord.request_stop()
            coord.join(threads)