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): thread_num = len(ctx) mtcnn_detectors = list() for i in range(thread_num): mtcnn_detectors.append( creat_mtcnn_detector(prefix, epoch, batch_size, test_mode, thresh, min_face_size, ctx[i])) imdb = IMDB("wider", image_set, root_path, dataset_path, 'test') annotations = imdb.get_annotations() image_num = len(annotations) test_batch_size = 10 * thread_num start_idx = 0 detections = list() while start_idx < image_num: end_idx = min(start_idx + test_batch_size, image_num) cur_annotations = annotations[start_idx:end_idx] cur_detections = test_minibatch(cur_annotations, mtcnn_detectors) detections = detections + cur_detections start_idx = end_idx print '%d images done' % start_idx return detections
def test_net(root_path, dataset_path, image_set, prefix, epoch, batch_size, test_mode="rnet", thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=2, slide_window=False, shuffle=False, vis=False): detectors = [None, None, None] model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)] # load pnet model if slide_window: PNet = Detector(P_Net, 12, batch_size[0], model_path[0]) else: PNet = FcnDetector(P_Net, model_path[0]) detectors[0] = PNet # load rnet model if test_mode in ["rnet", "onet"]: RNet = Detector(R_Net, 24, batch_size[1], model_path[1]) detectors[1] = RNet # load onet model if test_mode == "onet": ONet = Detector(O_Net, 48, batch_size[2], model_path[2]) detectors[2] = ONet mtcnn_detector = MtcnnDetector(detectors=detectors, 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 == "pnet": net = "rnet" elif test_mode == "rnet": net = "onet" save_path = os.path.join(data_dir, 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)
def train_P_net(image_set, root_path, dataset_path, prefix, end_epoch, frequent, lr): imdb = IMDB(data_name, image_set, root_path, dataset_path) gt_imdb = imdb.gt_imdb() gt_imdb = imdb.append_flipped_images(gt_imdb) sym = P_Net train_net(sym, prefix, end_epoch, gt_imdb, 12, frequent, lr)
def test_net(root_path, dataset_path, image_set, prefix, epoch, batch_size, ctx, test_mode="onet", thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=2, 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_Net("test"), 12, batch_size[0], ctx, args, auxs) else: PNet = FcnDetector(P_Net("test"), ctx, args, auxs) detectors[0] = PNet # load rnet model if test_mode in ["rnet", "onet"]: 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 == "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("fddb", image_set, root_path, dataset_path, 'test') gt_imdb = imdb.gt_imdb() test_data = TestLoader(gt_imdb) print('1') # 得到的detections格式为:[[第一张图的所有预测框],[第二张图片的所有预测框],[第三张图片的所有预测框],...,[最后一张图片的所有预测框]],每个预测框为[x1,y1,x2,y2,score],score为该预测框为人脸的分数 detections = mtcnn_detector.detect_face(imdb, test_data, vis=vis) save_path = "/Users/qiuxiaocong/Downloads/mtcnn1" if not os.path.exists(save_path): os.mkdir(save_path) save_file = os.path.join(save_path, "detections_onet_0009_givenPRnet.pkl") with open(save_file, 'wb') as f: pickle.dump(detections, f, pickle.HIGHEST_PROTOCOL) print('detections saved done!')
def train_O_net(image_set, root_path, dataset_path, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, frequent, lr, resume): imdb = IMDB("mtcnn", image_set, root_path, dataset_path) gt_imdb = imdb.gt_imdb() gt_imdb = imdb.append_flipped_images(gt_imdb) sym = O_Net() train_net(sym, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, gt_imdb, 48, frequent, not resume, lr)
def train_O_net(image_set, root_path, dataset_path, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, batch_size, thread_num, frequent, lr,lr_epoch, resume, with_landmark): imdb = IMDB("mtcnn", image_set, root_path, dataset_path, 'train') gt_imdb = imdb.get_annotations() sym = O_Net('train',with_landmark) train_net(sym, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, gt_imdb, batch_size, thread_num, 48, True, True, with_landmark, frequent, not resume, lr, lr_epoch)
def train_R_net(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 = R_Net() train_net(sym, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, gt_imdb, batch_size, thread_num, 24, True, True, False, frequent, not resume, lr, lr_epoch)
def train_R_net(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 = R_Net() train_net(sym, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, gt_imdb, batch_size, thread_num, 24, frequent, not resume, lr, lr_epoch)
def train_GA_net(mode, 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("GA", 112, image_set, root_path, dataset_path) gt_imdb = imdb.gt_imdb() gt_imdb = imdb.append_flipped_images(gt_imdb) sym = GA_Net112(mode, batch_size) train_net(mode, sym, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, gt_imdb, batch_size, thread_num, 112, 112, 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=2, slide_window=False, shuffle=False, vis=False): detectors = [None, None, None] # load pnet model args, auxs = load_param(prefix[0], epoch[0], convert=False, ctx=ctx) if slide_window: PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs) else: PNet = FcnDetector(P_Net("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=False, 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=False, 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="rnet", thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=2, 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_Net("test"), 12, batch_size[0], ctx, args, auxs) else: PNet = FcnDetector(P_Net("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 == "pnet": net = "rnet" elif test_mode == "rnet": net = "onet" save_path = "./prepare_data/%s"%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)
def test_net(root_path, dataset_path, prefix, epoch, batch_size, test_mode="onet", thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=2, slide_window=False, shuffle=False, vis=False): detectors = [None, None, None] model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)] # load pnet model if slide_window: PNet = Detector(P_Net, 12, batch_size[0], model_path[0]) else: PNet = FcnDetector(P_Net, model_path[0]) detectors[0] = PNet # load rnet model if test_mode in ["rnet", "onet"]: RNet = Detector(R_Net, 24, batch_size[1], model_path[1]) detectors[1] = RNet # load onet model if test_mode == "onet": ONet = Detector(O_Net, 48, batch_size[2], model_path[2]) detectors[2] = ONet mtcnn_detector = MtcnnDetector(detectors=detectors, 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, prefix, epoch, batch_size, ctx, test_mode="onet", thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=2, 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_Net("test"), 12, batch_size[0], ctx, args, auxs) else: PNet = FcnDetector(P_Net("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)