Exemplo n.º 1
0
def Run(target):
    assert target in ['rnet', 'onet']

    model_path = GetModelPaths()
    batch_size = [2048, 256, 16]
    thresh = [0.4, 0.05, 0.7]
    min_face_size = 24

    detectors = [None, None, None]
    detectors[0] = FcnDetector(P_Net, model_path[0])

    if target == 'onet':
        detectors[1] = Detector(R_Net, 24, batch_size[1], model_path[1])

    data = read_annotation()

    if cfg.debug:
        data['bboxes'] = data['bboxes'][:20]
        data['images'] = data['images'][:20]

    test_data = TestLoader(data['images'])

    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=2,
                                   threshold=thresh)

    detections, _ = mtcnn_detector.detect_face(test_data)

    save_file = join(cfg.path_output_files, 'detections_%s.pkl' % target)
    with open(save_file, 'wb') as f:
        pickle.dump(detections, f, 1)
    save_hard_example(target, data, save_file)
Exemplo n.º 2
0
def t_net(prefix, epoch,
             batch_size, test_mode="PNet",
             thresh=[0.6, 0.6, 0.7], min_face_size=25,
             stride=2, slide_window=False, shuffle=False, vis=False):
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    #PNet-echo
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    print(model_path[0])
    # 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"]:
        print("==================================", test_mode)
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    # load onet model
    if test_mode == "ONet":
        print("==================================", test_mode)
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet
        
    basedir = '.'    
    #anno_file
    filename = './wider_face_train_bbx_gt.txt'
    #read annatation(type:dict)
    data = read_annotation(basedir,filename)
    mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size,
                                   stride=stride, threshold=thresh, slide_window=slide_window)
    print("==================================")
    # 注意是在“test”模式下
    # imdb = IMDB("wider", image_set, root_path, dataset_path, 'test')
    # gt_imdb = imdb.gt_imdb()
    test_data = TestLoader(data['images'][:100])
    #list
    detections,_ = mtcnn_detector.detect_face(test_data)

    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    #save detect result
    save_path = os.path.join(data_dir, save_net)
    print save_path
    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:
        pickle.dump(detections, f,1)
    print("%s测试完成开始OHEM" % image_size)
    save_hard_example(image_size, data, save_path)
def t_net(prefix, epoch,
             batch_size, test_mode="PNet",
             thresh=[0.6, 0.6, 0.7], min_face_size=25,
             stride=2, slide_window=False, shuffle=False, vis=False):
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    #PNet-echo
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    print(model_path[0])
    # 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"]:
        print("==================================", test_mode)
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    # load onet model
    if test_mode == "ONet":
        print("==================================", test_mode)
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet
        
    basedir = '.'    
    #anno_file
    filename = './wider_face_train_bbx_gt.txt'
    #read annatation(type:dict)
    data = read_annotation(basedir,filename)
    mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size,
                                   stride=stride, threshold=thresh, slide_window=slide_window)
    print("==================================")
    # 注意是在“test”模式下
    # imdb = IMDB("wider", image_set, root_path, dataset_path, 'test')
    # gt_imdb = imdb.gt_imdb()
    test_data = TestLoader(data['images'])
    #list
    detections,_ = mtcnn_detector.detect_face(test_data)

    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    #save detect result
    save_path = os.path.join(data_dir, save_net)
    print save_path
    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:
        pickle.dump(detections, f,1)
    print("%s测试完成开始OHEM" % image_size)
    save_hard_example(image_size, data, save_path)
def t_net(prefix,
          epoch,
          data_dir,
          batch_size,
          PNet_factory,
          RNet_factory,
          test_mode="PNet",
          thresh=[0.6, 0.6, 0.7],
          min_face_size=25,
          stride=2,
          slide_window=False,
          shuffle=False,
          vis=False):

    # load detectors
    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(PNet_factory, 12, batch_size[0], model_path[0])
    else:
        PNet = FcnDetector(PNet_factory, model_path[0])
    detectors[0] = PNet

    # load rnet model
    if test_mode == "RNet":
        RNet = Detector(RNet_factory, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    # Get detections
    basedir = '.'
    filename = './wider_face_train_bbx_gt.txt'
    data = read_annotation(basedir, filename)
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)
    test_data = TestLoader(data['images'])
    print("Getting detections for {} images.".format(test_data.size))
    detections, _ = mtcnn_detector.detect_face(test_data)

    # Save detections
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    save_path = os.path.join(data_dir, save_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:
        pickle.dump(detections, f, 1)

    save_hard_example(image_size, data, save_path)
Exemplo n.º 5
0
    imdb = get_imdb_fddb(data_dir)
    nfold = len(imdb)
    for i in xrange(nfold):
        image_names = imdb[i]
        print image_names
        dets_file_name = os.path.join(out_dir,
                                      'FDDB-det-fold-%02d.txt' % (i + 1))
        fid = open(dets_file_name, 'w')
        sys.stdout.write('%s ' % (i + 1))
        image_names_abs = [
            os.path.join(data_dir, image_name + '.jpg')
            for image_name in image_names
        ]
        print(image_names_abs)
        test_data = TestLoader(image_names_abs)
        all_boxes, _ = mtcnn_detector.detect_face(test_data)

        for idx, im_name in enumerate(image_names):
            img_path = os.path.join(data_dir, im_name + '.jpg')
            image = cv2.imread(img_path)
            boxes = all_boxes[idx]
            if boxes is None:
                fid.write(im_name + '\n')
                fid.write(str(1) + '\n')
                fid.write('%f %f %f %f %f\n' % (0, 0, 0, 0, 0.99))
                continue
            fid.write(im_name + '\n')
            fid.write(str(len(boxes)) + '\n')

            for box in boxes:
                fid.write(
Exemplo n.º 6
0
mtcnn_detector = MtcnnDetector(detectors=detectors,
                               min_face_size=min_face_size,
                               stride=stride,
                               threshold=thresh,
                               slide_window=slide_window)
gt_imdb = []
#gt_imdb.append("35_Basketball_Basketball_35_515.jpg")
#imdb_ = dict()"
#imdb_['image'] = im_path
#imdb_['label'] = 5
path = "../../DATA/test/lfpw_testImage"
for item in os.listdir(path):
    gt_imdb.append(os.path.join(path, item))
test_data = TestLoader(gt_imdb)

all_boxes, landmarks = mtcnn_detector.detect_face(test_data)

count = 0
for imagepath in gt_imdb:
    print(imagepath)
    image = cv2.imread(imagepath)
    for bbox in all_boxes[count]:
        cv2.putText(image,
                    str(np.round(bbox[4], 2)), (int(bbox[0]), int(bbox[1])),
                    cv2.FONT_HERSHEY_TRIPLEX,
                    1,
                    color=(255, 0, 255))
        cv2.rectangle(image, (int(bbox[0]), int(bbox[1])),
                      (int(bbox[2]), int(bbox[3])), (0, 0, 255))
    '''
        for landmark in landmarks[count]:
Exemplo n.º 7
0
def t_net(prefix,
          epoch,
          batch_size,
          test_mode="PNet",
          thresh=(0.6, 0.6, 0.7),
          min_face_size=25,
          stride=2,
          slide_window=False,
          shuffle=False,
          vis=False):
    """
    :param prefix:
    :param epoch:
    :param batch_size:
    :param test_mode:
    :param thresh:
    :param min_face_size:
    :param stride:
    :param slide_window:
    :param shuffle:
    :param vis:
    :return:
    """
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    # PNet-echo
    model_path = []
    for x, y in zip(prefix, epoch):
        model_path.append(os.path.join(x, test_mode.lower() + '.ckpt-%s' % y))
    # model_path = ['%s/pnet.ckpt-%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"]:
        print("==================================", test_mode)
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    # load onet model
    if test_mode == "ONet":
        print("==================================", test_mode)
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet

    basedir = '../../DATA/'
    # anno_file
    filename = './wider_face_train_bbx_gt.txt'
    # read anotation(type:dict), include 'images' and 'bboxes'
    data = read_annotation(basedir, filename)  # 12880张图片
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)
    # 注意是在“test”模式下
    # imdb = IMDB("wider", image_set, root_path, dataset_path, 'test')
    # gt_imdb = imdb.gt_imdb()
    print('load test data')
    test_data = TestLoader(data['images'])
    print('finish loading')
    # list
    print('start detecting....')
    detections, _ = mtcnn_detector.detect_face(test_data)
    print('finish detecting ')
    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    # save detect result
    save_path = os.path.join(data_dir, save_net)
    print('save_path is :')
    print(save_path)
    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:
        pickle.dump(detections, f, 1)
    print("%s测试完成开始OHEM" % image_size)
    save_hard_example(image_size, data, save_path)
 mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size,
                                stride=stride, threshold=thresh, slide_window=slide_window)
 
 
 
 imdb = get_imdb_fddb(data_dir)
 nfold = len(imdb)    
 for i in xrange(nfold):
     image_names = imdb[i]
     print image_names
     dets_file_name = os.path.join(out_dir, 'FDDB-det-fold-%02d.txt' % (i + 1))
     fid = open(dets_file_name,'w')
     sys.stdout.write('%s ' % (i + 1))
     image_names_abs = [os.path.join(data_dir,'originalPics',image_name+'.jpg') for image_name in image_names]
     test_data = TestLoader(image_names_abs)
     all_boxes,_ = mtcnn_detector.detect_face(test_data, vis=vis)
     
     for idx,im_name in enumerate(image_names):
         img_path = os.path.join(data_dir,'originalPics',im_name+'.jpg')
         image = cv2.imread(img_path)
         boxes = all_boxes[idx]
         if boxes is None:
             fid.write(im_name+'\n')
             fid.write(str(1) + '\n')
             fid.write('%f %f %f %f %f\n' % (0, 0, 0, 0, 0.99))
             continue
         fid.write(im_name+'\n')
         fid.write(str(len(boxes)) + '\n')
         
         for box in boxes:
             fid.write('%f %f %f %f %f\n' % (float(box[0]), float(box[1]), float(box[2]-box[0]+1), float(box[3]-box[1]+1),box[4]))                
def t_net(
        prefix,
        epoch,
        batch_size,
        test_mode="PNet",
        thresh=[0.6, 0.6, 0.7],
        min_face_size=25,  # prefix保存模型文件路径
        stride=2,
        slide_window=False,
        shuffle=False,
        vis=False):
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    #PNet-echo
    model_path = [
        '%s-%s' % (x, y) for x, y in zip(prefix, epoch)
    ]  # model_path == <class 'list'>: ['../data/MTCNN_model/PNet_landmark/PNet-16', '../data/MTCNN_model/RNet_landmark/RNet-6', '../data/MTCNN_model/ONet/ONet-22']

    if slide_window:
        PNet = Detector(P_Net, 12, batch_size[0], model_path[0])
    else:
        PNet = FcnDetector(
            P_Net, model_path[0]
        )  # '../data/MTCNN_model/PNet_landmark/PNet-16', 生成全连接detection的检测对象P_Net
    detectors[0] = PNet  # 将PNet对象放入检测器的第1个位

    # load rnet model
    if test_mode in ["RNet", "ONet"]:
        print("==================================", test_mode)
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    # load onet model
    if test_mode == "ONet":
        print("==================================", test_mode)
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet

    basedir = '.'
    filename = './wider_face_train_bbx_gt.txt'  # 获取检测框的ground truth值
    data = read_annotation(
        basedir, filename
    )  # 读pic的文件名,和box的ground truth值,data['images']==all image pathes
    #  data['bboxes'] =all image bboxes
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)
    print("==================================")

    test_data = TestLoader(
        data['images']
    )  # 生成输入图片的管理对象test_data, # 注意是在“test”模式下,  imdb = IMDB("wider", image_set, root_path, dataset_path, 'test'),  gt_imdb = imdb.gt_imdb()
    detections, _ = mtcnn_detector.detect_face(
        test_data)  # 从test_data输入人脸pixel,返回detection和cls

    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    #save detect result
    save_path = os.path.join(data_dir, save_net)  # save_path == “24 / Rnet”
    print(save_path)
    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:  # save_file == detections.pkl
        pickle.dump(detections, f, 1)  # 将detection结果写入文件
    print("%s测试完成开始OHEM" % image_size)
    save_hard_example(image_size, data,
                      save_path)  # data,读pic的文件名,和box的ground truth值,
def t_net(prefix,
          epoch,
          batch_size,
          test_mode='PNet',
          thresh=[0.6, 0.6, 0.6],
          min_face_size=25,
          stride=2,
          slide_window=False,
          shuffle=False,
          vis=False):
    """
    :param prefix: 模型名字的前缀
    :param epoch:上一个模型的训练次数
    :param batch_size:用在预测中的批的尺寸
    :param test_mode:测试模式
    :param thresh:阈值
    :param min_face_size:最小检测人脸大小
    :param stride:
    :param slide_window:是否在pnet中应用了sliding window
    :param shuffle:
    :param vis:
    :return:
    """
    detectors = [None, None, None]
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    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

    if test_mode in ["RNet", "ONet"]:
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    basedir = '.'
    # 注解文件
    filename = './wider_face_train_bbx_gt.txt'

    data = read_annotation(basedir, filename)
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)

    test_data = TestLoader(data['images'])
    detections, _ = mtcnn_detector.detect_face(test_data)

    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    # 保存探测结果
    save_path = os.path.join(data_dir, save_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:
        pickle.dump(detections, f, 1)
    print("%s测试完成开始OHEM" % image_size)
    save_hard_example(image_size, data, save_path)
Exemplo n.º 11
0
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)
gt_imdb = []
#gt_imdb.append("35_Basketball_Basketball_35_515.jpg")
#imdb_ = dict()"
#imdb_['image'] = im_path
#imdb_['label'] = 5
path = "lala"
for item in os.listdir(path):
    gt_imdb.append(os.path.join(path,item))
test_data = TestLoader(gt_imdb)
all_boxes,landmarks = mtcnn_detector.detect_face(test_data)
count = 0
for imagepath in gt_imdb:
    print imagepath
    image = cv2.imread(imagepath)
    for bbox in all_boxes[count]:
        cv2.putText(image,str(np.round(bbox[4],2)),(int(bbox[0]),int(bbox[1])),cv2.FONT_HERSHEY_TRIPLEX,1,color=(255,0,255))
        cv2.rectangle(image, (int(bbox[0]),int(bbox[1])),(int(bbox[2]),int(bbox[3])),(0,0,255))
        
    for landmark in landmarks[count]:
        for i in range(len(landmark)/2):
            cv2.circle(image, (int(landmark[2*i]),int(int(landmark[2*i+1]))), 3, (0,0,255))
        
    count = count + 1
    #cv2.imwrite("result_landmark/%d.png" %(count),image)
    cv2.imshow("lala",image)
Exemplo n.º 12
0
def t_net(data_dir,
          prefix=[
              './modelfiles/PNet/PNet', './modelfiles/RNet/RNet',
              './modelfiles/ONet/ONet'
          ],
          epoch=[18, 14, 16],
          batch_size=[2048, 256, 16],
          test_mode="PNet",
          thresh=[0.6, 0.6, 0.7],
          min_face_size=25,
          stride=2,
          slide_window=False,
          shuffle=False,
          vis=False):

    detectors = [None, None, None]
    print("Test model: ", test_mode)
    #PNet-echo
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    print(model_path[0])
    # 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"]:
        print("==================================", test_mode)
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet
    # load onet model
    if test_mode == "ONet":
        print("==================================", test_mode)
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet

    # anno_file
    # read anotation(type:dict), include 'images' and 'bboxes'
    data = read_annotation(data_dir,
                           './prepare_data/wider_face_train_bbx_gt.txt')
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)

    print("==================================")
    print('load test data')
    test_data = TestLoader(data['images'])
    print('finish loading')

    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
        image_size = 24
    elif test_mode == "RNet":
        save_net = "ONet"
        image_size = 48

    # save detect result
    save_path = os.path.join(data_dir, save_net)
    print('save_path is :')
    print(save_path)
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    save_file = os.path.join(save_path, "detections.pkl")

    print('start detecting....')
    if not os.path.exists(save_file):
        detections, _ = mtcnn_detector.detect_face(test_data)
        with open(save_file, 'wb') as f:
            pickle.dump(detections, f, 1)
    print('finish detecting ')

    print("%s测试完成开始OHEM" % image_size)
Exemplo n.º 13
0
def t_net(prefix, epoch,batch_size, img_saved_dir,anno_file,gen_anno_file,gen_imgs_dir,data_set_name,ignore_det=False,test_mode="PNet",thresh=[0.6, 0.6, 0.7], min_face_size=25,\
             stride=2):
    slide_window = False
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    #PNet-echo
    print("epoch num ", epoch[0])
    ''' #for Pnet test
    epoch_num = epoch[0]
    epoch_c = np.arange(2,epoch_num,2)
    prefix_c = []
    prefix = prefix[0]
    [prefix_c.append(prefix) for i in range(len(epoch_c))]
    '''
    print("prefixs is ", prefix)
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    #print("after zip model_path is ",model_path)
    #model_path[0] = prefix + '-'+str(epoch_num) #for Pnet test
    print("model_path 0 is ", model_path[0])
    # 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"]:
        print("==================================", test_mode)
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    # load onet model
    if test_mode == "ONet":
        print("==================================", test_mode)
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet
    #read annatation(type:dict)
    #img_box_dic = read_annotation(img_saved_dir,anno_file)
    img_box_dic = rd_anotation(img_saved_dir, anno_file, data_set_name)
    print("gen_hardexample  threshold ", thresh)
    if not ignore_det:
        mtcnn_detector = MtcnnDetector(detectors=detectors,
                                       min_face_size=min_face_size,
                                       stride=stride,
                                       threshold=thresh)
    print("==================================")
    # 注意是在“test”模式下
    # imdb = IMDB("wider", image_set, root_path, dataset_path, 'test')
    # gt_imdb = imdb.gt_imdb()
    test_data = TestLoader(img_box_dic['images'])
    #list
    if not ignore_det:
        detections, _ = mtcnn_detector.detect_face(test_data)
    if test_mode == "PNet":
        save_net = "RNet"
        save_path = '24/RNet'
    elif test_mode == "RNet":
        save_net = "ONet"
        save_path = "48/ONet"
    #save detect result
    #save_path = os.path.join(data_dir, save_net)
    print("save path is", save_path)
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    save_file = os.path.join(save_path, "detections.pkl")
    if not ignore_det:
        with open(save_file, 'wb') as f:
            pickle.dump(detections, f, 1)
        f.close()
    print("%s Test is Over and begin OHEM" % image_size)
    save_hard_example(gen_anno_file, gen_imgs_dir, img_box_dic, save_path,
                      test_mode)
Exemplo n.º 14
0
def t_net(prefix,
          epoch,
          batch_size,
          test_mode="PNet",
          thresh=[0.6, 0.6, 0.7],
          min_face_size=25,
          stride=2,
          slide_window=False,
          shuffle=False,
          vis=False):
    """
    Parameter
    ----------------
    



    """
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    #PNet-echo
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    print(model_path[0])
    # 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
    pnet_detections = None
    if test_mode in ["RNet", "ONet"]:
        print("==================================", test_mode)
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet
        print("load pnet predictions...")
        pnet_detections = pickle.load(
            open(os.path.join("../data/24/RNet", 'detections.pkl'), 'rb'))
        print("load pnet predictions done.")

    # load onet model
    rnet_detections = None
    if test_mode == "ONet":
        print("==================================", test_mode)
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet
        print("load rnet predictions...")
        rnet_detections = pickle.load(
            open(os.path.join("../data/48/ONet", 'detections.pkl'), 'rb'))
        print("load rnet predictions done.")

    # detec的数据源始终是一样的
    basedir = 'E:/Document/Datasets/Wider Face'
    #anno_file
    filename = 'wider_face_train_bbx_gt.txt'
    #data是一个dict,data["images"]为所有照片路径list,data["bboxes"]是所有照片bboxes的值,一一对应
    data = read_annotation(basedir, filename)
    # 初始化mtcnn_detector,就是训练好的模型
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)
    print("==================================")
    # 注意是在“test”模式下
    # imdb = IMDB("wider", image_set, root_path, dataset_path, 'test')
    # gt_imdb = imdb.gt_imdb()
    print('load test data')
    #所有照片,训练时为12880张
    test_data = TestLoader(data['images'])
    print('finish loading')
    #list
    print('start detecting....')
    detections, _ = mtcnn_detector.detect_face(
        test_data, pnet_detections, rnet_detections)  #调用训练好的pnet,返回的是candidate
    print('finish detecting ')
    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    # #save detect result
    save_path = os.path.join(data_dir, save_net)
    print('save_path is :')
    print(save_path)
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    # 把所有candidate写到文件中序列化
    save_file = os.path.join(save_path, "detections.pkl")
    with open(save_file, 'wb') as f:
        pickle.dump(detections, f, 1)
    print("save done.")
    print("%s测试完成开始OHEM" % image_size)
    # 然后根据candidate和gt的Iou把每个candidate分类并保存到文本和相应的照片
    save_hard_example(image_size, data, save_path)
Exemplo n.º 15
0
def t_net(prefix,
          epoch,
          batch_size,
          test_mode="PNet",
          thresh=[0.6, 0.6, 0.7],
          min_face_size=25,
          stride=2,
          slide_window=False,
          shuffle=False,
          vis=False):
    detectors = [None, None, None]
    print("Test model: ", test_mode)
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    print(model_path[0])
    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

    if test_mode in ["RNet", "ONet"]:
        print("==================================", test_mode)
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    if test_mode == "ONet":
        print("==================================", test_mode)
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet

    basedir = '../../DATA/'
    filename = './wider_face_train_bbx_gt.txt'
    data = read_annotation(basedir, filename)
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)
    print("==================================")

    print('load test data')
    test_data = TestLoader(data['images'])
    print('finish loading')
    print('start detecting....')
    detections, _ = mtcnn_detector.detect_face(test_data)
    print('finish detecting ')
    save_net = 'RNet'
    if test_mode == "PNet":
        save_net = "RNet"
    elif test_mode == "RNet":
        save_net = "ONet"
    save_path = os.path.join(data_dir, save_net)
    print('save_path is :')
    print(save_path)
    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:
        pickle.dump(detections, f, 1)
    save_hard_example(image_size, data, save_path)