Example #1
0
    def test_pca_validity(self, pca_postfix):
        cnn_model = CNNModel()
        pca_utility = PCAUtility()
        tf_record_utility = TFRecordUtility()
        image_utility = ImageUtility()

        eigenvalues, eigenvectors, meanvector = pca_utility.load_pca_obj(
            dataset_name=DatasetName.ibug, pca_postfix=pca_postfix)

        lbl_arr, img_arr, pose_arr = tf_record_utility.retrieve_tf_record(
            tfrecord_filename=IbugConf.tf_train_path,
            number_of_records=30,
            only_label=False)
        for i in range(20):
            b_vector_p = self.calculate_b_vector(lbl_arr[i], True, eigenvalues,
                                                 eigenvectors, meanvector)
            lbl_new = meanvector + np.dot(eigenvectors, b_vector_p)

            labels_true_transformed, landmark_arr_x_t, landmark_arr_y_t = image_utility. \
                create_landmarks_from_normalized(lbl_arr[i], 224, 224, 112, 112)

            labels_true_transformed_pca, landmark_arr_x_pca, landmark_arr_y_pca = image_utility. \
                create_landmarks_from_normalized(lbl_new, 224, 224, 112, 112)

            image_utility.print_image_arr(i, img_arr[i], landmark_arr_x_t,
                                          landmark_arr_y_t)
            image_utility.print_image_arr(i * 1000, img_arr[i],
                                          landmark_arr_x_pca,
                                          landmark_arr_y_pca)
Example #2
0
    def test_pca_validity(self, dataset_name, pca_postfix):
        image_utility = ImageUtility()

        eigenvalues = load('pca_obj/' + dataset_name + self.eigenvalues_prefix + str(pca_postfix)+".npy")
        eigenvectors = load('pca_obj/' + dataset_name + self.eigenvectors_prefix + str(pca_postfix)+".npy")
        meanvector = load('pca_obj/' + dataset_name + self.meanvector_prefix + str(pca_postfix)+".npy")

        '''load data: '''
        lbl_arr = []
        img_arr = []
        path = None

        if dataset_name == DatasetName.ibug:
            path = IbugConf.rotated_img_path_prefix  # rotated is ok, since advs_aug is the same as rotated
            num_of_landmarks = IbugConf.num_of_landmarks
        elif dataset_name == DatasetName.cofw:
            path = CofwConf.rotated_img_path_prefix
            num_of_landmarks = CofwConf.num_of_landmarks
        elif dataset_name == DatasetName.wflw:
            path = WflwConf.rotated_img_path_prefix
            num_of_landmarks = WflwConf.num_of_landmarks

        for file in tqdm(os.listdir(path)):
            if file.endswith(".pts"):
                pts_file = os.path.join(path, file)
                img_file = pts_file[:-3] + "jpg"
                if not os.path.exists(img_file):
                    continue

                points_arr = []
                with open(pts_file) as fp:
                    line = fp.readline()
                    cnt = 1
                    while line:
                        if 3 < cnt <= num_of_landmarks+3:
                            x_y_pnt = line.strip()
                            x = float(x_y_pnt.split(" ")[0])
                            y = float(x_y_pnt.split(" ")[1])
                            points_arr.append(x)
                            points_arr.append(y)
                        line = fp.readline()
                        cnt += 1
                lbl_arr.append(points_arr)
                img_arr.append(Image.open(img_file))

        for i in range(30):
            b_vector_p = self.calculate_b_vector(lbl_arr[i], True, eigenvalues, eigenvectors, meanvector)
            lbl_new = meanvector + np.dot(eigenvectors, b_vector_p)
            lbl_new = lbl_new.tolist()

            labels_true_transformed, landmark_arr_x_t, landmark_arr_y_t = image_utility. \
                create_landmarks(lbl_arr[i], 1, 1)

            labels_true_transformed_pca, landmark_arr_x_pca, landmark_arr_y_pca = image_utility. \
                create_landmarks(lbl_new, 1, 1)

            image_utility.print_image_arr(i, img_arr[i], landmark_arr_x_t, landmark_arr_y_t)
            image_utility.print_image_arr(i * 1000, img_arr[i], landmark_arr_x_pca, landmark_arr_y_pca)