Ejemplo n.º 1
0
Archivo: lfw.py Proyecto: browatbn2/MAD
    def __init__(self, root_dir=cfg.LFW_ROOT, train=True, start=None,
                 max_samples=None, deterministic=True, use_cache=True):

        from utils.face_extractor import FaceExtractor
        self.face_extractor = FaceExtractor()

        self.use_cache = use_cache
        self.root_dir = root_dir
        self.cropped_img_dir = os.path.join(root_dir, 'crops_tight')
        self.fullsize_img_dir = os.path.join(root_dir, 'images')
        self.feature_dir = os.path.join(root_dir, 'features')

        import glob

        ann = []
        person_dirs = sorted(glob.glob(os.path.join(cfg.LFW_ROOT, 'images', '*')))
        for id, person_dir in enumerate(person_dirs):
            name = os.path.split(person_dir)[1]
            for img_file in sorted(glob.glob(os.path.join(person_dir, '*.jpg'))):
                # create fnames of format 'Aaron_Eckhart/Aaron_Eckhart_0001'
                fname = os.path.join(name, os.path.splitext(os.path.split(img_file)[1])[0])
                ann.append({'fname': fname, 'id': id, 'name': name})

        self.annotations = pd.DataFrame(ann)

        # limit number of samples
        st,nd = 0, None
        if start is not None:
            st = start
        if max_samples is not None:
            nd = st+max_samples
        self.annotations = self.annotations[st:nd]

        self.transform = ds_utils.build_transform(deterministic=True, color=True)
Ejemplo n.º 2
0
    def __init__(self, root_dir=cfg.AFLW_ROOT, train=True, color=True, start=None,
                 max_samples=None, deterministic=None, use_cache=True,
                 daug=0, return_modified_images=False, test_split='full', align_face_orientation=True,
                 return_landmark_heatmaps=False, landmark_sigma=9, landmark_ids=range(19), **kwargs):

        assert test_split in ['full', 'frontal']
        from utils.face_extractor import FaceExtractor
        self.face_extractor = FaceExtractor()

        self.use_cache = use_cache
        self.align_face_orientation = align_face_orientation

        self.return_landmark_heatmaps = return_landmark_heatmaps
        self.return_modified_images = return_modified_images
        self.landmark_sigma = landmark_sigma
        self.landmark_ids = landmark_ids

        self.mode = TRAIN if train else VAL

        self.root_dir = root_dir
        root_dir_local = cfg.AFLW_ROOT_LOCAL
        self.fullsize_img_dir = os.path.join(root_dir, 'data/flickr')
        self.cropped_img_dir = os.path.join(root_dir_local, 'crops')
        self.feature_dir = os.path.join(root_dir_local,  'features')
        self.color = color

        annotation_filename = os.path.join(cfg.AFLW_ROOT_LOCAL, 'alfw.pkl')
        self.annotations_original = pd.read_pickle(annotation_filename)
        print("Number of images: {}".format(len(self.annotations_original)))

        self.frontal_only = test_split == 'frontal'
        self.make_split(train, self.frontal_only)

        # limit number of samples
        st,nd = 0, None
        if start is not None:
            st = start
        if max_samples is not None:
            nd = st+max_samples
        self.annotations = self.annotations[st:nd]

        if deterministic is None:
            deterministic = self.mode != TRAIN
        self.transform = ds_utils.build_transform(deterministic, True, daug)

        transforms = [fp.CenterCrop(cfg.INPUT_SIZE)]
        transforms += [fp.ToTensor() ]
        transforms += [fp.Normalize([0.518, 0.418, 0.361], [1, 1, 1])]  # VGGFace(2)
        self.crop_to_tensor = tf.Compose(transforms)

        print("Number of images: {}".format(len(self)))
Ejemplo n.º 3
0
Archivo: lfw.py Proyecto: browatbn2/MAD
    def __init__(self, root_dir=cfg.LFW_ROOT, train=True, start=None,
                 max_samples=None, deterministic=True, use_cache=True, view=2):

        assert(view in [1,2])

        from utils.face_extractor import FaceExtractor
        self.face_extractor = FaceExtractor()

        self.mode = TRAIN if train else VAL

        self.use_cache = use_cache
        self.root_dir = root_dir
        self.cropped_img_dir = os.path.join(root_dir, 'crops_tight')
        self.fullsize_img_dir = os.path.join(root_dir, 'images')
        self.feature_dir = os.path.join(root_dir, 'features')

        self.default_bbox = [65, 80, 65+100, 80+100]

        pairs_file = 'pairsDevTest.txt' if view == 1 else 'pairs.txt'
        path_annotations = os.path.join(self.root_dir, pairs_file)
        # self.annotations = pd.read_csv(path_annotations)
        self.pairs = []
        with open(path_annotations) as txt_file:
            # num_pairs = int(txt_file.readline()) * 2
            # print(num_pairs)
            for line in txt_file:
                items = line.split()
                if len(items) == 3:
                    pair = (items[0], int(items[1]), items[0], int(items[2]))
                elif len(items) == 4:
                    pair = (items[0], int(items[1]), items[2], int(items[3]))
                else:
                    # print("Invalid line: {}".format(line))
                    continue
                self.pairs.append(pair)

        # assert(num_pairs == len(self.pairs))

        from sklearn.utils import shuffle
        self.pairs = shuffle(self.pairs, random_state=0)

        # limit number of samples
        st,nd = 0, None
        if start is not None:
            st = start
        if max_samples is not None:
            nd = st+max_samples
        self.pairs = self.pairs[st:nd]

        self.transform = ds_utils.build_transform(deterministic, color=True)
Ejemplo n.º 4
0
    def __init__(self, root_dir, fullsize_img_dir, root_dir_local=None, train=True, color=True, start=None,
                 max_samples=None, deterministic=None, use_cache=True, detect_face=False, align_face_orientation=True,
                 return_modified_images=False, return_landmark_heatmaps=True, landmark_sigma=9, landmark_ids=range(68),
                 daug=0, **kwargs):

        from utils.face_extractor import FaceExtractor
        self.face_extractor = FaceExtractor()

        self.train = train
        self.mode = TRAIN if train else VAL
        self.use_cache = use_cache
        self.detect_face = detect_face
        self.align_face_orientation = align_face_orientation
        self.start = start
        self.max_samples = max_samples
        self.daug = daug
        self.return_modified_images = return_modified_images

        self.return_landmark_heatmaps = return_landmark_heatmaps
        self.landmark_sigma = landmark_sigma
        self.landmark_ids = landmark_ids

        self.deterministic = deterministic
        if self.deterministic is None:
            self.deterministic = self.mode != TRAIN

        self.fullsize_img_dir = fullsize_img_dir

        self.root_dir = root_dir
        self.root_dir_local = root_dir_local if root_dir_local is not None else self.root_dir

        self.cropped_img_dir = os.path.join(self.root_dir_local, 'crops')
        self.feature_dir = os.path.join(self.root_dir_local,  'features')
        self.color = color

        self.transform = ds_utils.build_transform(self.deterministic, self.color, daug)

        print("Loading annotations... ")
        self.annotations = self.create_annotations()
        print("  Number of images: {}".format(len(self.annotations)))

        self.init()
        self.select_samples()

        transforms = [fp.CenterCrop(cfg.INPUT_SIZE)]
        transforms += [fp.ToTensor() ]
        transforms += [fp.Normalize([0.518, 0.418, 0.361], [1, 1, 1])]  # VGGFace(2)
        self.crop_to_tensor = tf.Compose(transforms)
Ejemplo n.º 5
0
    def __init__(self, root_dir=cfg.VGGFACE2_ROOT, train=True, color=True, start=None,
                 max_samples=None, deterministic=None, min_conf=cfg.MIN_OPENFACE_CONFIDENCE, use_cache=True,
                 crop_source='bb_ground_truth', detect_face=False, align_face_orientation=True,
                 return_landmark_heatmaps=False, return_modified_images=False,
                 daug=0, landmark_sigma=None, landmark_ids=None, **kwargs):

        assert(crop_source in ['bb_ground_truth', 'lm_ground_truth', 'lm_cnn', 'lm_openface'])

        self.mode = TRAIN if train else VAL

        self.face_extractor = FaceExtractor()
        self.use_cache = use_cache
        self.detect_face = detect_face
        self.align_face_orientation = align_face_orientation
        self.color = color
        self.crop_source = crop_source
        self.return_landmark_heatmaps = return_landmark_heatmaps
        self.return_modified_images = return_modified_images
        self.landmark_sigma = landmark_sigma
        self.landmark_ids = landmark_ids

        self.root_dir = root_dir
        root_dir_local = cfg.VGGFACE2_ROOT_LOCAL
        split_subfolder = 'train' if train else 'test'
        crop_folder = 'crops'
        if cfg.INPUT_SIZE == 128:
            crop_folder += '_128'
        self.cropped_img_dir = os.path.join(root_dir_local, split_subfolder, crop_folder, crop_source)
        self.fullsize_img_dir = os.path.join(root_dir, split_subfolder, 'imgs')
        self.feature_dir = os.path.join(root_dir_local, split_subfolder, 'features')
        annotation_filename = 'loose_bb_{}.csv'.format(split_subfolder)
        # annotation_filename = 'loose_landmark_{}.csv'.format(split_subfolder)

        # self.path_annotations_mod = os.path.join(root_dir_local, annotation_filename + '.mod_full_of.pkl')
        self.path_annotations_mod = os.path.join(root_dir_local, annotation_filename + '.mod_full.pkl')
        if os.path.isfile(self.path_annotations_mod):
            print('Reading pickle file...')
            self.annotations = pd.read_pickle(self.path_annotations_mod)
            print('done.')
        else:
            print('Reading CSV file...')
            self.annotations = pd.read_csv(os.path.join(self.root_dir, 'bb_landmark', annotation_filename))
            print('done.')

            of_confs, poses, landmarks = [], [], []
            self.annotations = self.annotations[0:4000000]
            self.annotations = self.annotations[self.annotations.H > 80]
            print("Number of images: {}".format(len(self)))

            def get_face_height(lms):
                return lms[8,1] - lms[27,1]

            read_openface_landmarks = True
            if read_openface_landmarks:
                for cnt, filename in enumerate(self.annotations.NAME_ID):
                    filename_noext = os.path.splitext(filename)[0]

                    bb = self.annotations.iloc[cnt][1:5].values
                    expected_face_center = [bb[0] + bb[2] / 2.0, bb[1] + bb[3] / 2.0]

                    conf, lms, pose, num_faces  = ds_utils.read_openface_detection(os.path.join(self.feature_dir, filename_noext),
                                                                       expected_face_center=expected_face_center,
                                                                       use_cache=True, return_num_faces=True)

                    if num_faces > 1:
                        print("Deleting extracted crop for {}...".format(filename))
                        cache_filepath = os.path.join(self.cropped_img_dir, 'tight', filename + '.jpg')
                        if os.path.isfile(cache_filepath):
                            os.remove(cache_filepath)

                    of_confs.append(conf)
                    landmarks.append(lms)
                    poses.append(pose)
                    if (cnt+1) % 10000 == 0:
                        log.info(cnt+1)
                self.annotations['pose'] = poses
                self.annotations['of_conf'] = of_confs
                self.annotations['landmarks_of'] = landmarks

            # assign new continuous ids to persons (0, range(n))
            print("Creating id labels...")
            _ids = self.annotations.NAME_ID
            _ids = _ids.map(lambda x: int(x.split('/')[0][1:]))
            self.annotations['ID'] = _ids

            self.annotations.to_pickle(self.path_annotations_mod)

        min_face_height = 100
        print('Removing faces with height <={:.2f}px...'.format(min_face_height))
        self.annotations = self.annotations[self.annotations.H > min_face_height]
        print("Number of images: {}".format(len(self)))

        # limit number of samples
        st,nd = 0, None
        if start is not None:
            st = start
        if max_samples is not None:
            nd = st+max_samples
        self.annotations = self.annotations[st:nd]

        if deterministic is None:
            deterministic = self.mode != TRAIN
        self.transform = ds_utils.build_transform(deterministic, self.color, daug)

        print("Number of images: {}".format(len(self)))
        print("Number of identities: {}".format(self.annotations.ID.nunique()))
Ejemplo n.º 6
0
    def __init__(self,
                 root_dir=cfg.AFFECTNET_ROOT,
                 train=True,
                 transform=None,
                 crop_type='tight',
                 color=True,
                 start=None,
                 max_samples=None,
                 outlier_threshold=None,
                 deterministic=None,
                 use_cache=True,
                 detect_face=False,
                 align_face_orientation=False,
                 min_conf=cfg.MIN_OPENFACE_CONFIDENCE,
                 daug=0,
                 return_landmark_heatmaps=False,
                 landmark_sigma=9,
                 landmark_ids=range(68),
                 return_modified_images=False,
                 crop_source='lm_openface',
                 **kwargs):
        assert (crop_type in ['fullsize', 'tight', 'loose'])
        assert (crop_source in [
            'bb_ground_truth', 'lm_ground_truth', 'lm_cnn', 'lm_openface'
        ])

        self.face_extractor = FaceExtractor()

        self.mode = TRAIN if train else VAL

        self.crop_source = crop_source
        self.use_cache = use_cache
        self.detect_face = detect_face
        self.align_face_orientation = align_face_orientation
        self.return_landmark_heatmaps = return_landmark_heatmaps
        self.return_modified_images = return_modified_images
        self.landmark_sigma = landmark_sigma
        self.landmark_ids = landmark_ids

        self.start = start
        self.max_samples = max_samples

        self.root_dir = root_dir
        self.crop_type = crop_type
        self.color = color
        self.outlier_threshold = outlier_threshold
        self.transform = transform
        self.fullsize_img_dir = os.path.join(self.root_dir,
                                             'cropped_Annotated')
        self.cropped_img_dir = os.path.join(self.root_dir, 'crops',
                                            crop_source)
        self.feature_dir = os.path.join(self.root_dir, 'features')

        annotation_filename = 'training' if train else 'validation'
        path_annotations_mod = os.path.join(root_dir,
                                            annotation_filename + '.mod.pkl')
        if os.path.isfile(path_annotations_mod):
            print('Reading pickle file...')
            self._annotations = pd.read_pickle(path_annotations_mod)
        else:
            print('Reading CSV file...')
            self._annotations = pd.read_csv(
                os.path.join(root_dir, annotation_filename + '.csv'))
            print('done.')

            # drop non-faces
            self._annotations = self._annotations[
                self._annotations.expression < 8]

            # Samples in annotation file are somewhat clustered by expression.
            # Shuffle to create a more even distribution.
            # NOTE: deterministic, always creates the same order
            if train:
                from sklearn.utils import shuffle
                self._annotations = shuffle(self._annotations, random_state=2)

                # remove samples with inconsistent expression<->valence/arousal values
                self._remove_outliers()

            poses = []
            confs = []
            landmarks = []
            for cnt, filename in enumerate(
                    self._annotations.subDirectory_filePath):
                if cnt % 1000 == 0:
                    print(cnt)
                filename_noext = os.path.splitext(filename)[0]
                conf, lms, pose = ds_utils.read_openface_detection(
                    os.path.join(self.feature_dir, filename_noext))
                poses.append(pose)
                confs.append(conf)
                landmarks.append(lms)
            self._annotations['pose'] = poses
            self._annotations['conf'] = confs
            self._annotations['landmarks_of'] = landmarks
            # self.annotations.to_csv(path_annotations_mod, index=False)
            self._annotations.to_pickle(path_annotations_mod)

        poses = np.abs(np.stack(self._annotations.pose.values))

        only_good_image_for_training = True
        if train and only_good_image_for_training:
            print(len(self._annotations))

            min_rot_deg = 30
            max_rot_deg = 90
            # print('Limiting rotation to +-[{}-{}] degrees...'.format(min_rot_deg, max_rot_deg))
            # self._annotations = self._annotations[(poses[:, 0] < np.deg2rad(max_rot_deg)) &
            #                                       (poses[:, 1] < np.deg2rad(max_rot_deg)) &
            #                                       (poses[:, 2] < np.deg2rad(max_rot_deg))]
            # self._annotations = self._annotations[(np.deg2rad(min_rot_deg) < poses[:, 0]) |
            #                                       (np.deg2rad(min_rot_deg) < poses[:, 1])]
            # self._annotations = self._annotations[np.deg2rad(min_rot_deg) < poses[:, 1] ]

            print(len(self._annotations))

            # print('Removing OpenFace confs <={:.2f}...'.format(min_conf))
            # self._annotations = self._annotations[self._annotations.conf > cfg.MIN_OPENFACE_CONFIDENCE]
            # print(len(self._annotations))

            # select by Valence/Arousal
            # min_arousal = 0.0
            # print('Removing arousal <={:.2f}...'.format(min_arousal))
            # self._annotations = self._annotations[self._annotations.arousal > min_arousal]
            # print(len(self._annotations))

        # There is (at least) one missing image in the dataset. Remove by checking face width:
        self._annotations = self._annotations[self._annotations.face_width > 0]

        # self._annotations_balanced = self._annotations
        # self.filter_labels(label_dict_exclude={'expression': 0})
        # self.filter_labels(label_dict_exclude={'expression': 1})
        # self._annotations = self._annotations[self._annotations.arousal > 0.2]

        self.rebalance_classes()

        if deterministic is None:
            deterministic = self.mode != TRAIN
        self.transform = ds_utils.build_transform(deterministic, self.color,
                                                  daug)

        transforms = [fp.CenterCrop(cfg.INPUT_SIZE)]
        transforms += [fp.ToTensor()]
        transforms += [fp.Normalize([0.518, 0.418, 0.361],
                                    [1, 1, 1])]  # VGGFace(2)
        self.crop_to_tensor = tf.Compose(transforms)
Ejemplo n.º 7
0
    def __init__(self,
                 root_dir=cfg.VOXCELEB_ROOT,
                 train=True,
                 start=None,
                 max_samples=None,
                 deterministic=True,
                 with_bumps=False,
                 min_of_conf=0.3,
                 min_face_height=100,
                 use_cache=True,
                 **kwargs):

        from utils.face_extractor import FaceExtractor
        self.face_extractor = FaceExtractor()

        self.use_cache = use_cache
        self.root_dir = root_dir
        self.cropped_img_dir = os.path.join(cfg.VOXCELEB_ROOT_LOCAL, 'crops')
        self.fullsize_img_dir = os.path.join(
            root_dir, 'frames/unzippedIntervalFaces/data')
        self.feature_dir = os.path.join(root_dir,
                                        'features/unzippedIntervalFaces/data')
        self.npfeature_dir = os.path.join(
            cfg.VOXCELEB_ROOT_LOCAL, 'features/unzippedIntervalFaces/data')
        self.train = train
        self.with_bumps = with_bumps

        annotation_filename = 'dev' if train else 'test'
        path_annotations_mod = os.path.join(root_dir,
                                            annotation_filename + '.mod.pkl')
        if os.path.isfile(path_annotations_mod) and False:
            self.annotations = pd.read_pickle(path_annotations_mod)
        else:
            print('Reading CSV file...')
            self.annotations = pd.read_csv(
                os.path.join(root_dir, annotation_filename + '.csv'))
            print('done.')

            # self.annotations['of_conf'] = -1
            # self.annotations['landmarks'] = ''
            # self.annotations['pose'] = ''
            # of_confs, poses, landmarks = [], [], []
            #
            #
            #
            # # for cnt, filename in enumerate(self.annotations.fname):
            # for cnt, idx in enumerate(self.annotations.index):
            #     filename = self.annotations.iloc[idx].fname
            #     filename_noext = os.path.splitext(filename)[0]
            #     of_conf, lms, pose = ds_utils.read_openface_detection(os.path.join(self.feature_dir, filename_noext))
            #     str_landmarks = encode_landmarks(lms)
            #     of_confs.append(of_conf)
            #     # poses.append(pose)
            #     landmarks.append(lms)
            #     self.annotations.loc[idx, 'of_conf'] = of_conf
            #     self.annotations.loc[idx, 'landmarks'] = str_landmarks
            #     self.annotations.loc[idx, 'pose'] = encode_landmarks(pose)
            #     if (cnt+1) % 100 == 0:
            #         print(cnt+1)
            #     if (cnt+1) % 1000 == 0:
            #         print('saving annotations...')
            #         self.annotations.to_pickle(path_annotations_mod)
            # # self.annotations.to_csv(path_annotations_mod, index=False)
            # self.annotations.to_pickle(path_annotations_mod)

        path_annotations_mod = os.path.join(root_dir,
                                            annotation_filename + '.lms.pkl')
        lm_annots = pd.read_pickle(os.path.join(root_dir,
                                                path_annotations_mod))

        t = time.time()
        self.annotations = pd.merge(self.annotations,
                                    lm_annots,
                                    on='fname',
                                    how='inner')
        print("Time merge: {:.2f}".format(time.time() - t))

        t = time.time()
        self.annotations['vid'] = self.annotations.fname.map(
            lambda x: x.split('/')[2])
        self.annotations['id'] = self.annotations.uid.map(lambda x: int(x[2:]))
        print("Time vid/id labels: {:.2f}".format(time.time() - t))

        print("Num. faces: {}".format(len(self.annotations)))
        print("Num. ids  : {}".format(self.annotations.id.nunique()))

        # drop bad face detections
        print("Removing faces with conf < {}".format(min_of_conf))
        self.annotations = self.annotations[
            self.annotations.of_conf >= min_of_conf]
        print("Num. faces: {}".format(len(self.annotations)))

        # drop small faces
        print("Removing faces with height < {}px".format(min_face_height))
        self.annotations = self.annotations[
            self.annotations.face_height >= min_face_height]
        print("Num. faces: {}".format(len(self.annotations)))

        fr = 0
        prev_vid = -1
        frame_nums = []
        for n, id in enumerate(self.annotations.vid.values):
            fr += 1
            if id != prev_vid:
                prev_vid = id
                fr = 0
            frame_nums.append(fr)
        self.annotations['FRAME'] = frame_nums

        self.max_frames_per_video = 200
        self.frame_interval = 3
        print('Limiting videos in VoxCeleb to {} frames...'.format(
            self.max_frames_per_video))
        self.annotations = self.annotations[self.annotations.FRAME %
                                            self.frame_interval == 0]
        self.annotations = self.annotations[
            self.annotations.FRAME < self.max_frames_per_video *
            self.frame_interval]
        print("Num. faces: {}".format(len(self.annotations)))

        # limit number of samples
        st, nd = 0, None
        if start is not None:
            st = start
        if max_samples is not None:
            nd = st + max_samples
        self.annotations = self.annotations[st:nd]

        self.transform = ds_utils.build_transform(deterministic=True,
                                                  color=True)
Ejemplo n.º 8
0
    def __init__(self,
                 root_dir=cfg.CELEBA_ROOT,
                 train=True,
                 color=True,
                 start=None,
                 max_samples=None,
                 deterministic=None,
                 crop_type='tight',
                 **kwargs):

        from utils.face_extractor import FaceExtractor
        self.face_extractor = FaceExtractor()

        self.mode = TRAIN if train else TEST

        self.crop_type = crop_type
        self.root_dir = root_dir
        root_dir_local = cfg.CELEBA_ROOT_LOCAL
        assert (crop_type in ['tight', 'loose', 'fullsize'])
        self.cropped_img_dir = os.path.join(root_dir_local, 'crops')
        self.fullsize_img_dir = os.path.join(root_dir, 'img_align_celeba')
        self.feature_dir = os.path.join(root_dir_local, 'features')
        self.color = color
        annotation_filename = 'list_landmarks_align_celeba.txt'

        path_annotations_mod = os.path.join(root_dir_local,
                                            annotation_filename + '.mod.pkl')
        if os.path.isfile(path_annotations_mod):
            self.annotations = pd.read_pickle(path_annotations_mod)
        else:
            print('Reading original TXT file...')
            self.annotations = pd.read_csv(os.path.join(
                self.root_dir, 'Anno', annotation_filename),
                                           delim_whitespace=True)
            print('done.')

            # store OpenFace features in annotation dataframe
            poses = []
            confs = []
            landmarks = []
            for cnt, filename in enumerate(self.annotations.fname):
                if cnt % 1000 == 0:
                    print(cnt)
                filename_noext = os.path.splitext(filename)[0]
                conf, lms, pose = ds_utils.read_openface_detection(
                    os.path.join(self.feature_dir, filename_noext))
                poses.append(pose)
                confs.append(conf)
                landmarks.append(lms)
            self.annotations['pose'] = poses
            self.annotations['conf'] = confs
            self.annotations['landmarks_of'] = landmarks

            # add identities to annotations
            self.identities = pd.read_csv(os.path.join(self.root_dir, 'Anno',
                                                       'identity_CelebA.txt'),
                                          delim_whitespace=True,
                                          header=None,
                                          names=['fname', 'id'])
            self.annotations = pd.merge(self.annotations,
                                        self.identities,
                                        on='fname',
                                        copy=False)

            # save annations as pickle file
            self.annotations.to_pickle(path_annotations_mod)

        # select training or test set (currently not using validation set)
        SPLIT = {
            TRAIN: (0, 162772),
            VAL: (162772, 182639),
            TEST: (182639, 202601)
        }
        self.annotations = self.annotations[
            (self.annotations.index >= SPLIT[self.mode][0])
            & (self.annotations.index < SPLIT[self.mode][1])]

        self.annotations = self.annotations.sort_values(by='id')

        print("Num. faces: {}".format(len(self.annotations)))
        if 'crops_celeba' in self.cropped_img_dir:
            min_of_conf = 0.0
        else:
            min_of_conf = 0.5
        print("Removing faces with conf < {}".format(min_of_conf))
        self.annotations = self.annotations[
            self.annotations.conf >= min_of_conf]
        print("Remaining num. faces: {}".format(len(self.annotations)))

        # max_rot_deg = 1
        # print('Limiting rotation to +-{} degrees...'.format(max_rot_deg))
        # poses = np.abs(np.stack(self.annotations.pose.values))
        # self.annotations = self.annotations[(poses[:, 0] > np.deg2rad(max_rot_deg)) |
        #                                     (poses[:, 1] > np.deg2rad(max_rot_deg)) |
        #                                     (poses[:, 2] > np.deg2rad(max_rot_deg))]
        # print(len(self.annotations))

        # limit number of samples
        st, nd = 0, None
        if start is not None:
            st = start
        if max_samples is not None:
            nd = st + max_samples
        self.annotations = self.annotations[st:nd]
        self._annotations = self.annotations[st:nd].copy()

        if deterministic is None:
            deterministic = self.mode != TRAIN
        self.transform = ds_utils.build_transform(deterministic, self.color)
Ejemplo n.º 9
0
    def __init__(self, root_dir=cfg.W300_ROOT, train=True,
                 transform=None, color=True, start=None, max_samples=None,
                 deterministic=None, align_face_orientation=cfg.CROP_ALIGN_ROTATION,
                 crop_type='tight', test_split='challenging', detect_face=False, use_cache=True,
                 crop_source='bb_detector', daug=0, return_modified_images=False,
                 return_landmark_heatmaps=False, landmark_sigma=3, landmark_ids=range(68), **kwargs):

        assert(crop_type in ['fullsize', 'tight','loose'])
        test_split = test_split.lower()
        assert(test_split in ['common', 'challenging', '300w', 'full'])
        assert(crop_source in W300.CROP_SOURCES)
        lmcfg.config_landmarks('300w')

        self.start = start
        self.max_samples = max_samples
        self.use_cache = use_cache
        self.crop_source = crop_source
        self.return_landmark_heatmaps = return_landmark_heatmaps
        self.return_modified_images = return_modified_images
        self.landmark_sigma = landmark_sigma
        self.landmark_ids = landmark_ids

        self.root_dir = root_dir
        self.local_root_dir = cfg.W300_ROOT_LOCAL
        self.color = color
        self.transform = transform
        self.fullsize_img_dir = os.path.join(self.root_dir, 'images')
        self.align_face_orientation = align_face_orientation
        self.detect_face = detect_face
        self.crop_type = crop_type
        self.cropped_img_dir = os.path.join(cfg.W300_ROOT_LOCAL, 'crops', crop_source)

        self.feature_dir_cnn = os.path.join(cfg.W300_ROOT_LOCAL, 'features_cnn')
        self.feature_dir_of = os.path.join(cfg.W300_ROOT_LOCAL, 'features_of')

        self.bounding_box_dir = os.path.join(cfg.W300_ROOT, 'Bounding Boxes')

        self.split = 'train' if train else test_split
        self.build_annotations(self.split)
        print("Num. images: {}".format(len(self)))

        # limit number of samples
        st,nd = 0, None
        if start is not None:
            st = start
        if max_samples is not None:
            nd = st+max_samples
        self.annotations = self.annotations[st:nd]

        if deterministic is None:
            deterministic = not train
        if self.crop_type == 'tight':
            self.transform = ds_utils.build_transform(deterministic, True, daug)
        elif self.crop_type == 'fullsize':
            self.transform = lambda x:x

        from utils.face_extractor import FaceExtractor
        self.face_extractor = FaceExtractor()

        transforms = [fp.CenterCrop(cfg.INPUT_SIZE)]
        transforms += [fp.ToTensor() ]
        transforms += [fp.Normalize([0.518, 0.418, 0.361], [1, 1, 1])]  # VGGFace(2)
        self.crop_to_tensor = tf.Compose(transforms)
Ejemplo n.º 10
0
    def __init__(self,
                 root_dir=cfg.VGGFACE2_ROOT,
                 train=True,
                 color=True,
                 start=None,
                 max_samples=None,
                 deterministic=None,
                 min_conf=cfg.MIN_OPENFACE_CONFIDENCE,
                 use_cache=True,
                 crop_source='bb_ground_truth',
                 detect_face=False,
                 align_face_orientation=True,
                 return_landmark_heatmaps=False,
                 return_modified_images=False,
                 daug=0,
                 landmark_sigma=None,
                 landmark_ids=None,
                 **kwargs):

        assert (crop_source in [
            'bb_ground_truth', 'lm_ground_truth', 'lm_cnn', 'lm_openface'
        ])

        self.mode = TRAIN if train else VAL

        self.face_extractor = FaceExtractor()
        self.use_cache = use_cache
        self.detect_face = detect_face
        self.align_face_orientation = align_face_orientation
        self.color = color
        self.crop_source = crop_source
        self.return_landmark_heatmaps = return_landmark_heatmaps
        self.return_modified_images = return_modified_images
        self.landmark_sigma = landmark_sigma
        self.landmark_ids = landmark_ids

        self.root_dir = root_dir
        root_dir_local = cfg.VGGFACE2_ROOT_LOCAL
        split_subfolder = 'train' if train else 'test'
        self.cropped_img_dir = os.path.join(root_dir_local, split_subfolder,
                                            'crops', crop_source)
        self.fullsize_img_dir = os.path.join(root_dir, split_subfolder, 'imgs')
        self.feature_dir = os.path.join(root_dir_local, split_subfolder,
                                        'features')
        annotation_filename = 'loose_bb_{}.csv'.format(split_subfolder)
        # annotation_filename = 'loose_landmark_{}.csv'.format(split_subfolder)

        # self.path_annotations_mod = os.path.join(root_dir_local, annotation_filename + '.mod_full_of.pkl')
        self.path_annotations_mod = os.path.join(
            root_dir_local, annotation_filename + '.mod_full.pkl')
        if os.path.isfile(self.path_annotations_mod):
            print('Reading pickle file...')
            self.annotations = pd.read_pickle(self.path_annotations_mod)
            print('done.')
        else:
            print('Reading CSV file...')
            self.annotations = pd.read_csv(
                os.path.join(self.root_dir, 'bb_landmark',
                             annotation_filename))
            print('done.')

            of_confs, poses, landmarks = [], [], []
            self.annotations = self.annotations[0:4000000]
            self.annotations = self.annotations[self.annotations.H > 80]
            print("Number of images: {}".format(len(self)))

            def get_face_height(lms):
                return lms[8, 1] - lms[27, 1]

            read_openface_landmarks = True
            if read_openface_landmarks:
                for cnt, filename in enumerate(self.annotations.NAME_ID):
                    filename_noext = os.path.splitext(filename)[0]

                    bb = self.annotations.iloc[cnt][1:5].values
                    expected_face_center = [
                        bb[0] + bb[2] / 2.0, bb[1] + bb[3] / 2.0
                    ]

                    conf, lms, pose, num_faces = ds_utils.read_openface_detection(
                        os.path.join(self.feature_dir, filename_noext),
                        expected_face_center=expected_face_center,
                        use_cache=True,
                        return_num_faces=True)

                    if num_faces > 1:
                        print("Deleting extracted crop for {}...".format(
                            filename))
                        cache_filepath = os.path.join(self.cropped_img_dir,
                                                      'tight',
                                                      filename + '.jpg')
                        if os.path.isfile(cache_filepath):
                            os.remove(cache_filepath)

                        # numpy_lmfile = os.path.join(self.feature_dir, filename) + '.npz'
                        # if os.path.isfile(numpy_lmfile):
                        #     os.remove(numpy_lmfile)

                    of_confs.append(conf)
                    landmarks.append(lms)
                    poses.append(pose)
                    if (cnt + 1) % 10000 == 0:
                        log.info(cnt + 1)
                    # if (cnt+1) % 1000 == 0:
                    #     print('saving annotations...')
                    #     self.annotations.to_pickle(self.path_annotations_mod)
                self.annotations['pose'] = poses
                self.annotations['of_conf'] = of_confs
                self.annotations['landmarks_of'] = landmarks
                # self.annotations['face_height'] = self.annotations.landmarks_of.map(get_face_height)

            # assign new continuous ids to persons (0, range(n))
            print("Creating id labels...")
            _ids = self.annotations.NAME_ID
            _ids = _ids.map(lambda x: int(x.split('/')[0][1:]))
            self.annotations['ID'] = _ids

            # unique_ids = _ids.unique()
            # uid2idx = dict(zip(unique_ids, range(1,len(unique_ids)+1)))
            # self.annotations['ID'] = _ids.map(uid2idx)

            self.annotations.to_pickle(self.path_annotations_mod)

        select_subset = False
        if select_subset:
            print("Number of images: {}".format(len(self)))
            self.annotations = self.annotations[
                self.annotations.of_conf > min_conf]
            print("Number of images: {}".format(len(self)))
            min_rot_deg = 0
            max_rot_deg = 90
            print('Limiting rotation to +-[{}-{}] degrees...'.format(
                min_rot_deg, max_rot_deg))
            poses = np.abs(np.stack(self.annotations.pose.values))
            self.annotations = self.annotations[
                (poses[:, 0] < np.deg2rad(max_rot_deg))
                & (poses[:, 1] < np.deg2rad(max_rot_deg)) &
                (poses[:, 2] < np.deg2rad(max_rot_deg))]
            # self.annotations = self.annotations[(np.deg2rad(min_rot_deg) < poses[:, 0]) |
            #                                     (np.deg2rad(min_rot_deg) < poses[:, 1])]

        min_face_height = 100
        print(
            'Removing faces with height <={:.2f}px...'.format(min_face_height))
        self.annotations = self.annotations[
            self.annotations.H > min_face_height]
        print("Number of images: {}".format(len(self)))

        # width = self.annotations.W
        # height = self.annotations.H
        # ratio = width / height
        # self.annotations = self.annotations[(ratio > 0.5) & (ratio < 0.60)]
        # self.annotations = self.annotations[ratio < 0.65]
        # self.annotations = self.annotations[ratio > 0.9]

        # FIXME: shuffle for find_similar_images
        # self.annotations = self.annotations[:1000000]
        # from sklearn.utils import shuffle
        # self.annotations = shuffle(self.annotations, random_state=2)
        #############3

        # limit number of samples
        st, nd = 0, None
        if start is not None:
            st = start
        if max_samples is not None:
            nd = st + max_samples
        self.annotations = self.annotations[st:nd]

        if deterministic is None:
            deterministic = self.mode != TRAIN
        self.transform = ds_utils.build_transform(deterministic, self.color,
                                                  daug)

        print("Number of images: {}".format(len(self)))
        print("Number of identities: {}".format(self.annotations.ID.nunique()))