示例#1
0
    def __init__(
        self,
        in_channels=512,
        mid_channels=512,
        ratios=[0.5, 1, 2],
        anchor_scales=[8, 16, 32],
        feat_stride=16,
        initialW=None,
        proposal_creator_params={},
    ):
        self.anchor_base = generate_anchors()
        self.feat_stride = feat_stride
        self.proposal_layer = ProposalCreator(**proposal_creator_params)

        n_anchor = self.anchor_base.shape[0]
        super(RegionProposalNetwork, self).__init__()
        with self.init_scope():
            self.conv1 = L.Convolution2D(in_channels,
                                         mid_channels,
                                         3,
                                         1,
                                         1,
                                         initialW=initialW)
            self.score = L.Convolution2D(mid_channels,
                                         n_anchor * 2,
                                         1,
                                         1,
                                         0,
                                         initialW=initialW)
            self.loc = L.Convolution2D(mid_channels,
                                       n_anchor * 4,
                                       1,
                                       1,
                                       0,
                                       initialW=initialW)
示例#2
0
def visualize_config_anchors(image, gt_anchors, cfg):

    # anchor_generator = make_anchor_generator(cfg)
    anchor_sizes = cfg.RPN.ANCHOR_SIZES
    anchor_ratios = cfg.RPN.ASPECT_RATIOS
    stride = cfg.RPN.ANCHOR_STRIDE[0]
    anchor_angles = cfg.RPN.ANCHOR_ANGLES
    H, W = image.shape[:2]

    total_anchors = len(anchor_angles) * len(anchor_ratios) * len(anchor_sizes)
    anchors = generate_anchors(anchor_sizes, anchor_ratios, anchor_angles,
                        height=H // stride,
                        width=W // stride,
                        stride=stride)

    iou_matrix = rotate_iou(FCT(gt_anchors), FCT(anchors)).cpu().numpy()
    # sorted_matrix = np.argsort(iou_matrix, axis=1)[:, ::-1]

    fg_iou_thresh = cfg.RPN.FG_IOU_THRESHOLD
    nms_thresh = cfg.RPN.NMS_THRESH

    a,b = np.nonzero(iou_matrix > fg_iou_thresh)
    # gg = iou_matrix > fg_iou_thresh
    # b = np.unique([ix for g in gg for ix,b in enumerate(g) if b!=0])
    # best_anchors = anchors[sorted_matrix[:,0]]
    best_anchors = anchors[b]#[(iou_matrix > 0.8]
    # best_anchors = best_anchors[nms_rotate_cpu(best_anchors, nms_thresh, 1000)]

    img_best_anchors = draw_anchors(image, best_anchors)
    cv2.imshow("img", img)
    cv2.imshow("best_anchors", img_best_anchors)

    batch = total_anchors
    start = 0 # (len(anchors) // total_anchors // 2)
    for ix in np.arange(start, len(anchors), batch):
        stride_anchors = anchors[ix:ix+batch]
        img_stride_anchors = draw_anchors(image, stride_anchors)
        valid_idx = b[np.logical_and(ix <= b, b < ix + batch)]

        # print("Valids: %d"%(len(valid_idx)))
        if len(valid_idx) == 0:
            continue

        valid_anchors = anchors[valid_idx]
        img_valid_stride_anchors = draw_anchors(image, valid_anchors)

        post_nms_anchors = valid_anchors[nms_rotate_cpu(valid_anchors, nms_thresh, 100)]
        img_valid_stride_anchors_post_nms = draw_anchors(image, post_nms_anchors)
        # post_nms_iou_matrix = iou_rotate_cpu(gt_anchors, post_nms_anchors)
        # print(post_nms_iou_matrix)

        cv2.imshow("stride_anchors", img_stride_anchors)
        cv2.imshow("valid_stride_anchors (>%.2f)"%(fg_iou_thresh), img_valid_stride_anchors)
        cv2.imshow("valid_stride_anchors (post NMS)", img_valid_stride_anchors_post_nms)
        cv2.waitKey(0)
示例#3
0
    def __init__(self, ):
        self.feat_stride = 16
        self.anchor_scales = np.array([8, 16, 32])
        self.anchors = generate_anchors(scales=self.anchor_scales)
        self.num_anchors = self.anchors.shape[0]

        self.phase = 'TRAIN'
        self.pre_nms_topN = cfg[self.phase].RPN_PRE_NMS_TOP_N
        self.post_nms_topN = cfg[self.phase].RPN_POST_NMS_TOP_N
        self.nms_thresh = cfg[self.phase].RPN_NMS_THRESH
        self.min_size = cfg[self.phase].RPN_MIN_SIZE
示例#4
0
from keras.models import model_from_json
from anchor_generator import generate_anchors
from anchor_decode import decode_bbox
from nms import single_class_non_max_suppression
import RPi.GPIO as GPIO
model = model_from_json(open('models/face_mask_detection.json').read())
model.load_weights('models/face_mask_detection.hdf5')

# anchor configuration
feature_map_sizes = [[33, 33], [17, 17], [9, 9], [5, 5], [3, 3]]
anchor_sizes = [[0.04, 0.056], [0.08, 0.11], [0.16, 0.22], [0.32, 0.45], [0.64, 0.72]]
anchor_ratios = [[1, 0.62, 0.42]] * 5

# generate anchors
anchors = generate_anchors(feature_map_sizes, anchor_sizes, anchor_ratios)


# BOARD编号方式,基于插座引脚编号
GPIO.setmode(GPIO.BOARD)
# 输出模式
GPIO.setup(11, GPIO.OUT)  #蜂鸣器
GPIO.setup(13, GPIO.OUT)   # 红灯
GPIO.setup(15, GPIO.OUT)   # 蓝灯
# for inference , the batch size is 1, the model output shape is [1, N, 4],
# so we expand dim for anchors to [1, anchor_num, 4]
anchors_exp = np.expand_dims(anchors, axis=0)

id2class = {0: 'Mask', 1: 'NoMask'}

def inference(image,
示例#5
0
    # import torch
    # def FCT(x): return torch.cuda.FloatTensor(x)

    # anchor_generator = make_anchor_generator(cfg)
    anchor_sizes = cfg.RPN.ANCHOR_SIZES
    anchor_ratios = [1., 2.]  # cfg.RPN.ASPECT_RATIOS
    stride = cfg.RPN.ANCHOR_STRIDE[0]
    anchor_angles = cfg.RPN.ANCHOR_ANGLES

    H, W = (224, 224)

    total_anchors = len(anchor_angles) * len(anchor_ratios) * len(anchor_sizes)
    anchors = generate_anchors(anchor_sizes,
                               anchor_ratios,
                               anchor_angles,
                               height=H // stride,
                               width=W // stride,
                               stride=stride)

    rect1 = np.array([W // 2, H // 2, 80, 50, 30], dtype=np.float32)
    rect2 = np.array([W // 2, H // 2, 80, 50, -30], dtype=np.float32)
    rect3 = np.array([W // 2, H // 2, 60, 50, 0], dtype=np.float32)
    gt_anchors = np.array([rect1, rect2, rect3])[2:]

    img = np.zeros((H, W, 3), dtype=np.uint8)
    # cv2.imshow("gt", draw_anchors(img, gt_anchors))
    # cv2.waitKey(0)

    iou_matrix = iou_rotate_cpu(gt_anchors, anchors)
    fg_iou_thresh = cfg.RPN.FG_IOU_THRESHOLD
    nms_thresh = cfg.RPN.NMS_THRESH