def test_net(prefix, epoch, batch_size, ctx, thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=4, slide_window=False): detectors = [None, None, None] # load pnet model args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx) if slide_window: PNet = Detector(P_Net20("test"), 20, batch_size[0], ctx, args, auxs) else: PNet = FcnDetector(P_Net20("test"), ctx, args, auxs) detectors[0] = PNet # load rnet model args, auxs = load_param(prefix[1], epoch[1], convert=True, ctx=ctx) RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs) detectors[1] = RNet # load onet model args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx) ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs) detectors[2] = ONet mtcnn_detector = MtcnnDetector(detectors=detectors, ctx=ctx, min_face_size=min_face_size, stride=stride, threshold=thresh, slide_window=slide_window) img = cv2.imread('test01.jpg') t1 = time.time() boxes, boxes_c = mtcnn_detector.detect_pnet20(img) boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c) boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c) print 'time: ', time.time() - t1 if boxes_c is not None: draw = img.copy() font = cv2.FONT_HERSHEY_SIMPLEX for b in boxes_c: cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (0, 255, 255), 1) cv2.putText(draw, '%.3f' % b[4], (int(b[0]), int(b[1])), font, 0.4, (255, 255, 255), 1) cv2.imshow("detection result", draw) cv2.waitKey(0)
def creat_mtcnn_detector(prefix, epoch, batch_size, test_mode, thresh, min_face_size, ctx): detectors = [None, None, None] # load pnet model args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx) PNet = FcnDetector(P_Net20("test"), ctx, args, auxs) detectors[0] = PNet # load rnet model if test_mode in ["onet", "hardrnet", "hardonet"]: args, auxs = load_param(prefix[1], epoch[1], convert=True, ctx=ctx) RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs) detectors[1] = RNet # load onet model if test_mode == "hardonet": args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx) ONet = Detector(O_Net("test", False), 48, batch_size[2], ctx, args, auxs) detectors[2] = ONet mtcnn_detector = MtcnnDetector(detectors=detectors, ctx=ctx, min_face_size=min_face_size, stride=4, threshold=thresh, slide_window=False) return mtcnn_detector
def train_P_net20(image_set, root_path, dataset_path, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, batch_size, thread_num, frequent, lr, lr_epoch, resume): imdb = IMDB("mtcnn", image_set, root_path, dataset_path, 'train') gt_imdb = imdb.get_annotations() sym = P_Net20() train_net(sym, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, gt_imdb, batch_size, thread_num, 20, True, True, False, frequent, not resume, lr, lr_epoch)
def train_P_net20(image_set, root_path, dataset_path, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, batch_size, thread_num, frequent, lr, lr_epoch, resume): imdb = IMDB("mtcnn", image_set, root_path, dataset_path) gt_imdb = imdb.gt_imdb() gt_imdb = imdb.append_flipped_images(gt_imdb) sym = P_Net20() train_net(sym, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, gt_imdb, batch_size, thread_num, 20, frequent, not resume, lr, lr_epoch)
def test_net(root_path, dataset_path, prefix, epoch, batch_size, ctx, test_mode="onet", thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=4, slide_window=False, shuffle=False, vis=False): detectors = [None, None, None] # load pnet model args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx) if slide_window: PNet = Detector(P_Net20("test"), 20, batch_size[0], ctx, args, auxs) else: PNet = FcnDetector(P_Net20("test"), ctx, args, auxs) detectors[0] = PNet # load rnet model if test_mode in ["rnet", "onet"]: args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx) RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs) detectors[1] = RNet # load onet model if test_mode == "onet": args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx) ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs) detectors[2] = ONet mtcnn_detector = MtcnnDetector(detectors=detectors, ctx=ctx, min_face_size=min_face_size, stride=stride, threshold=thresh, slide_window=slide_window) for i in range(1,11): image_set = "fold-" + str(i).zfill(2) imdb = IMDB("fddb", image_set, root_path, dataset_path, 'test') gt_imdb = imdb.gt_imdb() test_data = TestLoader(gt_imdb) all_boxes = mtcnn_detector.detect_face(imdb, test_data, vis=vis) imdb.write_results(all_boxes)
def test_net(root_path, dataset_path, image_set, prefix, epoch, batch_size, ctx, test_mode="hardpnet20", thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=4, slide_window=False, shuffle=False, vis=False): detectors = [None, None, None] # load pnet model args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx) if slide_window: PNet = Detector(P_Net20("test"), 20, batch_size[0], ctx, args, auxs) else: PNet = FcnDetector(P_Net20("test"), ctx, args, auxs) detectors[0] = PNet # load rnet model if test_mode in ["rnet", "onet"]: args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx) RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs) detectors[1] = RNet # load onet model if test_mode == "onet": args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx) ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs) detectors[2] = ONet mtcnn_detector = MtcnnDetector(detectors=detectors, ctx=ctx, min_face_size=min_face_size, stride=stride, threshold=thresh, slide_window=slide_window) imdb = IMDB("wider", image_set, root_path, dataset_path, 'test') gt_imdb = imdb.gt_imdb() test_data = TestLoader(gt_imdb) detections = mtcnn_detector.detect_face(imdb, test_data, vis=vis) if test_mode == "hardpnet20": net = "pnet20" if test_mode == "hardrnet": net = "rnet" elif test_mode == "hardonet": net = "onet" save_path = "%s/prepare_data/%s" % (config.root, net) if not os.path.exists(save_path): os.mkdir(save_path) save_file = os.path.join(save_path, "detections.pkl") with open(save_file, 'wb') as f: cPickle.dump(detections, f, cPickle.HIGHEST_PROTOCOL) save_hard_example(net)