This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun_lbp.py
146 lines (121 loc) · 4.88 KB
/
run_lbp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright (c) 2019-present, Yauheni Kachan. All Rights Reserved.
#
# This source code is licensed under the license found in the LICENSE file in
# the root directory of this source tree.
import os
import pickle
from catalyst.utils.image import imread
import cv2
import fire
import numpy as np
import pandas as pd
from scipy.stats.mstats import gmean
from sklearn import metrics, model_selection
from sklearn.base import clone
from sklearn.linear_model import LogisticRegression
from tqdm import tqdm
from face_cropper import crop_faces, FaceDetectionEngine
from lbp import CropClassificator, LBPFeatureExtractor
from utils import find_items
def infer(
in_csv: str,
in_dir: str,
out_csv: str,
weights_path: str,
n_workers: int = 4,
verbose: bool = False
):
model = CropClassificator(
face_detector=FaceDetectionEngine(weights_path='./models/s3fd_convert.pth'),
feature_extractor=LBPFeatureExtractor(n_features=59, crop_size=(64, 64)),
classificator=load_model(weights_path),
agg_fn=gmean
)
df = pd.read_csv(in_csv)
samples, frames, probabilities = [], [], []
for idx, image_info in tqdm(df.iterrows(), total=df.shape[0], disable=not verbose):
image = imread(image_info['path'], rootpath=in_dir)
probability = model.predict(image)
samples.append(image_info['id'])
frames.append(image_info['frame'])
probabilities.append(probability)
df = pd.DataFrame.from_dict({'id': samples, 'frame': frames, 'prediction': probabilities})
df = df.groupby('id')['prediction'].apply(gmean).reset_index()
df[['id', 'prediction']].to_csv(out_csv, index=False)
def load_model(filepath):
with open(filepath, 'rb') as f:
model = pickle.load(f)
return model
def train(
features_npy: str,
targets_csv: str,
n_splits: int = 5,
n_repeats: int = 10,
logdir: str = '.',
random_seed=82
):
model = LogisticRegression(
penalty='elasticnet',
C=1.0,
class_weight='balanced',
random_state=random_seed,
solver='saga',
max_iter=200,
n_jobs=-1,
l1_ratio=1.0
)
X = np.load(features_npy)
df = pd.read_csv(targets_csv)
y = df['label'].values
logs = []
splitter = model_selection.RepeatedStratifiedKFold(
n_splits=n_splits, n_repeats=n_repeats, random_state=random_seed
)
pbar = tqdm(
splitter.split(X, y, groups=df['id']), desc='folds', total=splitter.get_n_splits()
)
for i, (train_index, valid_index) in enumerate(pbar):
model_ = clone(model)
X_train, X_test = X[train_index], X[valid_index]
y_train, y_test = y[train_index], y[valid_index]
model_.fit(X_train, y_train)
preds = model_.predict_proba(X_test)[:, 1]
logs.append({'auc': metrics.roc_auc_score(y_test, preds)})
pbar.set_postfix(**logs[-1])
auc_ = np.array([it['auc'] for it in logs])
print(f'AUC (mean): {auc_.mean()}\tAUC (str): {auc_.std()}')
with open(os.path.join(logdir, 'logs.pkl'), 'wb') as f:
pickle.dump(logs, f)
# train final model on all data
model.fit(X, y)
with open(os.path.join(logdir, 'model.pkl'), 'wb') as f:
pickle.dump(model, f)
def prepare_lbp_dataset(dirpath: str, features_npy: str, targets_csv: str, verbose: bool = True):
feature_extractor = LBPFeatureExtractor(n_features=59, crop_size=(64, 64))
features, targets = [], []
df = find_items(in_dir=dirpath)
for idx, row in tqdm(df.iterrows(), total=df.shape[0], disable=not verbose):
image = imread(row['path'], rootpath=dirpath)
features.append(feature_extractor(image))
targets.append(row)
np.save(features_npy, np.stack(features, axis=0))
pd.DataFrame(targets).to_csv(targets_csv, index=False)
def prepare_cutout_datasets(in_dir: str, out_dir_crops: str, out_dir_cutout: str, verbose: bool = False):
df = find_items(in_dir=in_dir)
face_detector = FaceDetectionEngine(weights_path='./models/s3fd_convert.pth')
for idx, row in tqdm(df.iterrows(), total=df.shape[0], disable=not verbose):
image = imread(row['path'], rootpath=in_dir)
face_crops, cutout_faces = crop_faces(image, face_detector)
os.makedirs(os.path.join(out_dir_crops, os.path.dirname(row['path'])), exist_ok=True)
for i, (crop, bbox) in enumerate(face_crops):
root, ext = os.path.splitext(row['path'])
cv2.imwrite(os.path.join(out_dir_crops, f'{root}_{i}{ext}'), crop[:, :, ::-1]) # RGB -> BGR
os.makedirs(os.path.join(out_dir_cutout, os.path.dirname(row['path'])), exist_ok=True)
cv2.imwrite(os.path.join(out_dir_cutout, row['path']), cutout_faces[:, :, ::-1]) # RGB -> BGR
if __name__ == '__main__':
fire.Fire({
'infer': infer,
'train': train,
'prepare-lbp-dataset': prepare_lbp_dataset,
'prepare-cutout-datasets': prepare_cutout_datasets
})