Skip to content

Commit 542d368

Browse files
committed
Updated the transform size and added num workers params
1 parent 43407a2 commit 542d368

File tree

7 files changed

+44
-43
lines changed

7 files changed

+44
-43
lines changed

libreface/AU_Detection/inference.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def detect_action_units(image_path:str,
5858
'weights_download_id': '17v_vxQ09upLG3Yh0Zlx12rpblP7uoA8x',
5959
'data': 'BP4D',
6060
'fold': 'all',
61-
'num_workers': 0,
6261
'image_size': 256,
6362
'crop_size': 224,
6463
'num_labels': 12,
@@ -94,6 +93,7 @@ def detect_action_units(image_path:str,
9493
def detect_action_units_video(aligned_frames_path_list:list,
9594
device:str = "cpu",
9695
batch_size:int = 256,
96+
num_workers:int = 2,
9797
weights_download_dir = "./weights_libreface")->dict:
9898

9999
opts = ConfigObject({'seed': 0,
@@ -102,7 +102,7 @@ def detect_action_units_video(aligned_frames_path_list:list,
102102
'weights_download_id': '17v_vxQ09upLG3Yh0Zlx12rpblP7uoA8x',
103103
'data': 'BP4D',
104104
'fold': 'all',
105-
'num_workers': 0,
105+
'num_workers': num_workers,
106106
'image_size': 256,
107107
'crop_size': 224,
108108
'num_labels': 12,

libreface/AU_Recognition/inference.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ def get_au_intensities_and_detect_aus(image_path, device="cpu",
6363
'fer_data_root': '',
6464
'fer_data': 'AffectNet',
6565
'fold': 'all',
66-
'num_workers': 4,
6766
'image_size': 256,
6867
'crop_size': 224,
6968
'au_recognition_num_labels': 12,
@@ -102,7 +101,7 @@ def get_au_intensities_and_detect_aus(image_path, device="cpu",
102101
return format_output(detected_aus, task="au_detection"), format_output(au_intensities, task = "au_recognition")
103102

104103
def get_au_intensities_and_detect_aus_video(aligned_frames_path, device="cpu",
105-
batch_size = 256,
104+
batch_size = 256, num_workers=2,
106105
weights_download_dir="./weights_libreface"):
107106
opts = ConfigObject({'seed':0,
108107
'ckpt_path': f'{weights_download_dir}/AU_Recognition/weights/combined_resnet.pt',
@@ -117,7 +116,7 @@ def get_au_intensities_and_detect_aus_video(aligned_frames_path, device="cpu",
117116
'fer_data_root': '',
118117
'fer_data': 'AffectNet',
119118
'fold': 'all',
120-
'num_workers': 4,
119+
'num_workers': num_workers,
121120
'image_size': 256,
122121
'crop_size': 224,
123122
'au_recognition_num_labels': 12,
@@ -170,7 +169,6 @@ def get_au_intensities(image_path, device="cpu",
170169
'weights_download_id': '14qEnWRew2snhdMdOVyqKFJ5rq5VZrfAX',
171170
'data': 'DISFA',
172171
'fold': 'all',
173-
'num_workers': 0,
174172
'image_size': 256,
175173
'crop_size': 224,
176174
'num_labels': 12,
@@ -204,7 +202,7 @@ def get_au_intensities(image_path, device="cpu",
204202
return format_output(au_intensities)
205203

206204
def get_au_intensities_video(aligned_frames_path, device="cpu",
207-
batch_size = 256,
205+
batch_size = 256, num_workers = 2,
208206
weights_download_dir = "./weights_libreface"):
209207

210208
# # Path to the YAML config file
@@ -218,7 +216,7 @@ def get_au_intensities_video(aligned_frames_path, device="cpu",
218216
'weights_download_id': '14qEnWRew2snhdMdOVyqKFJ5rq5VZrfAX',
219217
'data': 'DISFA',
220218
'fold': 'all',
221-
'num_workers': 0,
219+
'num_workers': num_workers,
222220
'image_size': 256,
223221
'crop_size': 224,
224222
'num_labels': 12,

libreface/Facial_Expression_Recognition/inference.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def get_facial_expression(image_path, device = "cpu",
4949
'ckpt_path': f'{weights_download_dir}/Facial_Expression_Recognition/weights/resnet.pt',
5050
'weights_download_id': '1PeoPj8rga4vU2nuh_PciyX3HqaXp6LP7',
5151
'data': 'AffectNet',
52-
'num_workers': 8,
5352
'image_size': 224,
5453
'num_labels': 8,
5554
'dropout': 0.1,
@@ -93,7 +92,7 @@ def get_facial_expression(image_path, device = "cpu",
9392
return facial_expr_idx_to_class(facial_expression)
9493

9594
def get_facial_expression_video(aligned_frames_path, device = "cpu",
96-
batch_size = 256,
95+
batch_size = 256, num_workers = 2,
9796
weights_download_dir = "./weights_libreface"):
9897
opts = ConfigObject({'seed': 0,
9998
'train_csv': 'training_filtered.csv',
@@ -102,7 +101,7 @@ def get_facial_expression_video(aligned_frames_path, device = "cpu",
102101
'ckpt_path': f'{weights_download_dir}/Facial_Expression_Recognition/weights/resnet.pt',
103102
'weights_download_id': '1PeoPj8rga4vU2nuh_PciyX3HqaXp6LP7',
104103
'data': 'AffectNet',
105-
'num_workers': 8,
104+
'num_workers': num_workers,
106105
'image_size': 224,
107106
'num_labels': 8,
108107
'dropout': 0.1,

libreface/__init__.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def get_facial_attributes_video(video_path,
5858
temp_dir="./tmp",
5959
device="cpu",
6060
batch_size = 256,
61+
num_workers = 2,
6162
weights_download_dir:str = "./weights_libreface"):
6263
print(f"Using device: {device} for inference...")
6364
frame_extraction_start = time.time()
@@ -83,15 +84,18 @@ def get_facial_attributes_video(video_path,
8384
detected_aus, au_intensities = get_au_intensities_and_detect_aus_video(aligned_frames_path_list,
8485
device = device,
8586
batch_size=batch_size,
87+
num_workers=num_workers,
8688
weights_download_dir=weights_download_dir)
8789
elif model_choice == "separate_prediction_heads":
8890
detected_aus = detect_action_units_video(aligned_frames_path_list,
8991
device=device,
9092
batch_size=batch_size,
93+
num_workers=num_workers,
9194
weights_download_dir=weights_download_dir)
9295
au_intensities = get_au_intensities_video(aligned_frames_path_list,
9396
device=device,
9497
batch_size=batch_size,
98+
num_workers=num_workers,
9599
weights_download_dir=weights_download_dir)
96100
else:
97101
print(f"Undefined model_choice = {model_choice} for get_facial_attributes_video()")
@@ -117,12 +121,14 @@ def save_facial_attributes_video(video_path,
117121
temp_dir="./tmp",
118122
device="cpu",
119123
batch_size = 256,
124+
num_workers = 2,
120125
weights_download_dir:str = "./weights_libreface"):
121126
frames_df = get_facial_attributes_video(video_path,
122127
model_choice=model_choice,
123128
temp_dir=temp_dir,
124129
device=device,
125130
batch_size=batch_size,
131+
num_workers=num_workers,
126132
weights_download_dir=weights_download_dir)
127133
save_path = uniquify_file(output_save_path)
128134
frames_df.to_csv(save_path, index=False)
@@ -157,6 +163,7 @@ def get_facial_attributes(file_path,
157163
temp_dir="./tmp",
158164
device="cpu",
159165
batch_size = 256,
166+
num_workers = 2,
160167
weights_download_dir:str = "./weights_libreface"):
161168
file_type = check_file_type(file_path)
162169
if file_type == "image":
@@ -175,13 +182,14 @@ def get_facial_attributes(file_path,
175182
if output_save_path is None:
176183
return get_facial_attributes_video(file_path, model_choice=model_choice,
177184
temp_dir=temp_dir, device=device,
178-
batch_size=batch_size, weights_download_dir=weights_download_dir)
185+
batch_size=batch_size,
186+
num_workers=num_workers, weights_download_dir=weights_download_dir)
179187
else:
180188
try:
181189
return save_facial_attributes_video(file_path, output_save_path=output_save_path,
182190
model_choice=model_choice, temp_dir=temp_dir,
183191
device=device, batch_size=batch_size,
184-
weights_download_dir=weights_download_dir)
192+
num_workers=num_workers, weights_download_dir=weights_download_dir)
185193
except Exception as e:
186194
print(e)
187195
print("Some error in saving the results.")

libreface/commandline.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import argparse
2-
import os
32

43
from libreface import get_facial_attributes
54

@@ -10,14 +9,16 @@ def main_func():
109
parser.add_argument("--output_path", type=str, default="sample_results.csv", help="Path to the csv where results should be saved. Defaults to 'sample_results.csv'")
1110
parser.add_argument("--device", type=str, default="cpu", help="Device to use while inference. Can be 'cpu', 'cuda:0', 'cuda:1', ... Defaults to 'cpu'")
1211
parser.add_argument("--temp", type=str, default="./tmp", help="Path where the temporary results for facial attributes can be saved.")
12+
parser.add_argument("--num_workers", type=int, default=2, help="Number of workers to be used in the dataloader while doing inference on a video.")
1313

1414
args = parser.parse_args()
1515

1616
get_facial_attributes(args.input_path,
1717
output_save_path=args.output_path,
1818
model_choice="joint_au_detection_intensity_estimator",
1919
temp_dir=args.temp,
20-
device=args.device)
20+
device=args.device,
21+
num_workers=args.num_workers)
2122

2223
if __name__ == "__main__":
2324
main_func()

libreface/detect_mediapipe_image.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
import scipy.ndimage
66
from PIL import Image, ImageDraw
77
from tqdm import tqdm
8-
from multiprocessing import Process
98
import mediapipe as mp
10-
import pickle
11-
import time
129
import argparse
1310

1411
from libreface.utils import uniquify_file, restructure_landmark_mediapipe
@@ -93,9 +90,10 @@ def image_align(img, face_landmarks, output_size=256,
9390
quad += pad[:2]
9491

9592
img = img.transform((transform_size, transform_size), Image.Transform.QUAD,
96-
(quad + 0.5).flatten(), Image.Resampling.NEAREST)
93+
(quad + 0.5).flatten(), Image.Resampling.BILINEAR)
9794

98-
out_image = img.resize((output_size, output_size), Image.Resampling.NEAREST)
95+
out_image = img.resize((output_size, output_size), Image.Resampling.LANCZOS)
96+
out_image = img
9997

10098
return out_image
10199

test_libreface.py

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
import libreface
22
import time
33

4-
## inference on single image and store results to a variable
5-
# detected_facial_attributes = libreface.get_facial_attributes_image(image_path = "sample_disfa.png",
6-
# temp_dir = "./temp",
7-
# device = "cuda")
4+
# inference on single image and store results to a variable
5+
detected_facial_attributes = libreface.get_facial_attributes_image(image_path = "sample_disfa.png",
6+
temp_dir = "./temp",
7+
device = "cpu")
88

9-
# ## inference on a single image and save results in a csv file
10-
# libreface.save_facial_attributes_image(image_path = "sample_disfa.png",
11-
# output_save_path = "sample_image_results.csv",
12-
# temp_dir = "./temp",
13-
# device = "cuda")
9+
# inference on a single image and save results in a csv file
10+
libreface.save_facial_attributes_image(image_path = "sample_disfa.png",
11+
output_save_path = "sample_image_results.csv",
12+
temp_dir = "./temp",
13+
device = "cpu")
1414

15-
# ## inference on a video and store the results to a pandas dataframe
16-
# detected_facial_attributes_df = libreface.get_facial_attributes_video(video_path = "sample_disfa.avi",
17-
# temp_dir = "./temp",
18-
# device = "cuda")
15+
# inference on a video and store the results to a pandas dataframe
16+
detected_facial_attributes_df = libreface.get_facial_attributes_video(video_path = "sample_disfa.avi",
17+
temp_dir = "./temp",
18+
device = "cpu")
1919

2020
# ## inference on a video and save the results framewise in a csv file
21-
start = time.time()
22-
libreface.save_facial_attributes_video(video_path = "sample_disfa_long.avi",
21+
libreface.save_facial_attributes_video(video_path = "sample_disfa.avi",
2322
output_save_path = "sample_video_results.csv",
2423
temp_dir = "./temp",
25-
device = "cuda")
26-
end = time.time()
27-
print(f"{end-start:.4f}s - {600 / (end-start):.4f}")
24+
device = "cpu")
2825

29-
# ## inference on any image or video type and store results accordingly to a variable or save results
30-
# detected_facial_attributes = libreface.get_facial_attributes(file_path = "sample_disfa.avi",
31-
# output_save_path = "sample_results.csv",
32-
# temp_dir = "./temp",
33-
# device = "cuda")
26+
## inference on any image or video type and store results accordingly to a variable or save results
27+
detected_facial_attributes = libreface.get_facial_attributes(file_path = "sample_disfa.avi",
28+
output_save_path = "sample_results.csv",
29+
temp_dir = "./temp",
30+
device = "cpu")

0 commit comments

Comments
 (0)