Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FaceDetection Library Change #18

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions datasets/vggface2.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from torchvision import transforms

import cv2
import face_detection
import kornia.geometry.transform as GT
from batch_face import RetinaFace
from PIL import Image
from skimage import transform as trans
from tqdm import tqdm
Expand Down Expand Up @@ -99,8 +99,11 @@ def __extract_gt(self):
"""
Extracts the ground truth from the dataset
"""
detector = face_detection.build_detector("RetinaNetResNet50", confidence_threshold=.5,
nms_iou_threshold=.4)
if torch.cuda.is_available():
detector = RetinaFace(gpu_id=torch.cuda.current_device(), network="resnet50")
else:
detector = RetinaFace(gpu_id=-1, network="resnet50")

img_paths = list(glob.glob(os.path.join(self.d_path + '/**/', '*.jpg'), recursive=True))
nf_number = 0
words_count = 0
Expand All @@ -111,22 +114,17 @@ def __extract_gt(self):
boxes = []
image = cv2.imread(jpg)

img_max = max(image.shape[0], image.shape[1])
if img_max > 1320:
continue
bboxes, lndmrks = detector.batched_detect_with_landmarks(np.expand_dims(image, 0))
bboxes = bboxes[0]
lndmrks = lndmrks[0]
faces = detector(image)

if (bboxes.shape[0] == 0) or (lndmrks.shape[0] == 0):
if len(faces) == 0:
nf_number += 1
continue

for box in bboxes:
for face in faces:
box = face[0]
box = np.clip(box[:4], 0, None)
boxes.append(box)

lndmrks = lndmrks[0]
lndmrks = faces[0][1]

dir_name = os.path.dirname(jpg)
lbl = os.path.relpath(dir_name, self.d_path)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Pillow==10.3.0
PyYAML==6.0.1
albumentations==1.4.10
faiss-cpu==1.8.0
face-detection==0.2.2
batch-face>=1.4.0
h5py==3.11.0
kornia==0.7.2
librosa==0.10.2.post1
Expand Down
16 changes: 9 additions & 7 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -1233,8 +1233,14 @@ def _validate(data_loader, model, criterion, loggers, args, epoch=-1, tflogger=N
_, class_preds_batch = torch.max(output, 1)
class_probs.append(class_probs_batch)
class_preds.append(class_preds_batch)
if args.kd_relationbased:
stats = (
'',
OrderedDict([('Loss', losses[OBJECTIVE_LOSS_KEY].mean),
('Overall Loss', losses[OVERALL_LOSS_KEY].mean)])
)

if args.obj_detection:
elif args.obj_detection:
# Only run compute() if there is at least one new update()
if have_mAP:
mAP = map_calculator.compute()['map_50']
Expand Down Expand Up @@ -1343,12 +1349,8 @@ def update_training_scores_history(perf_scores_history, model, top1, top5, mAP,
if args.kd_relationbased:
# Keep perf_scores_history sorted from best to worst based on overall loss
# overall_loss = student_loss*student_weight + distillation_loss*distillation_weight
if not args.sparsity_perf:
perf_scores_history.sort(key=operator.attrgetter('vloss', 'epoch'),
reverse=True)
else:
perf_scores_history.sort(key=operator.attrgetter('params_nnz_cnt', 'vloss', 'epoch'),
reverse=True)
perf_scores_history.sort(key=operator.attrgetter('params_nnz_cnt', 'vloss', 'epoch'),
reverse=True)
for score in perf_scores_history[:args.num_best_scores]:
msglogger.info('==> Best [Overall Loss: %f on epoch: %d]',
-score.vloss, score.epoch)
Expand Down