fix face issue:

Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/12256380
This commit is contained in:
ly261666
2023-04-10 20:32:43 +08:00
committed by xingjun.wxj
parent 48fecacf61
commit 509631ca3b
22 changed files with 153 additions and 36 deletions

View File

@@ -48,6 +48,10 @@ class ArcFaceRecognitionPipeline(FaceProcessingBasePipeline):
def preprocess(self, input: Input) -> Dict[str, Any]:
result = super().preprocess(input)
if result is None:
rtn_dict = {}
rtn_dict['img'] = None
return rtn_dict
align_img = result['img']
face_img = align_img[:, :, ::-1] # to rgb
face_img = np.transpose(face_img, axes=(2, 0, 1))
@@ -57,6 +61,8 @@ class ArcFaceRecognitionPipeline(FaceProcessingBasePipeline):
return result
def forward(self, input: Dict[str, Any]) -> Dict[str, Any]:
if input['img'] is None:
return {OutputKeys.IMG_EMBEDDING: None}
img = input['img'].unsqueeze(0)
emb = self.face_model(img).detach().cpu().numpy()
emb /= np.sqrt(np.sum(emb**2, -1, keepdims=True)) # l2 norm

View File

@@ -54,9 +54,15 @@ class FaceAttributeRecognitionPipeline(FaceProcessingBasePipeline):
def preprocess(self, input: Input) -> Dict[str, Any]:
result = super().preprocess(input)
if result is None:
rtn_dict = {}
rtn_dict['img'] = None
return rtn_dict
return result
def forward(self, input: Dict[str, Any]) -> Dict[str, Any]:
if input['img'] is None:
return {OutputKeys.SCORES: None, OutputKeys.LABELS: None}
scores = self.fairface(input['img'])
assert scores is not None
return {OutputKeys.SCORES: scores, OutputKeys.LABELS: self.map_list}

View File

@@ -57,6 +57,10 @@ class FaceLivenessIrPipeline(FaceProcessingBasePipeline):
def preprocess(self, input: Input) -> Dict[str, Any]:
result = super().preprocess(input)
if result is None:
rtn_dict = {}
rtn_dict['input_tensor'] = None
return rtn_dict
orig_img = LoadImage.convert_to_ndarray(input)
orig_img = orig_img[:, :, ::-1]
img = super(FaceLivenessIrPipeline,
@@ -70,6 +74,8 @@ class FaceLivenessIrPipeline(FaceProcessingBasePipeline):
return result
def forward(self, input: Dict[str, Any]) -> Dict[str, Any]:
if input['input_tensor'] is None:
return {OutputKeys.SCORES: None, OutputKeys.BOXES: None}
input_feed = {}
input_feed[
self.input_node_name[0]] = input['input_tensor'].cpu().numpy()

View File

@@ -64,6 +64,10 @@ class FaceLivenessXcPipeline(FaceProcessingBasePipeline):
def preprocess(self, input: Input) -> Dict[str, Any]:
result = super().preprocess(input)
if result is None:
rtn_dict = {}
rtn_dict['input_tensor'] = None
return rtn_dict
img = result['img']
img = (img - 127.5) * 0.0078125
img = np.expand_dims(img, 0).copy()
@@ -74,6 +78,8 @@ class FaceLivenessXcPipeline(FaceProcessingBasePipeline):
return result
def forward(self, input: Dict[str, Any]) -> Dict[str, Any]:
if input['input_tensor'] is None:
return {OutputKeys.SCORES: None, OutputKeys.BOXES: None}
input_feed = {}
input_feed[
self.input_node_name[0]] = input['input_tensor'].cpu().numpy()

View File

@@ -101,12 +101,14 @@ class FaceProcessingBasePipeline(Pipeline):
face_lmks = face_lmks.reshape(5, 2)
align_img, _ = align_face(img, (112, 112), face_lmks)
result = {}
result['img'] = np.ascontiguousarray(align_img)
result['scores'] = [scores]
result['bbox'] = bboxes
result['lmks'] = face_lmks
return result
result = {}
result['img'] = np.ascontiguousarray(align_img)
result['scores'] = [scores]
result['bbox'] = bboxes
result['lmks'] = face_lmks
return result
else:
return None
def align_face_padding(self, img, rect, padding_size=16, pad_pixel=127):
rect = np.reshape(rect, (-1, 4))

View File

@@ -73,6 +73,10 @@ class FaceQualityAssessmentPipeline(FaceProcessingBasePipeline):
def preprocess(self, input: Input) -> Dict[str, Any]:
result = super().preprocess(input)
if result is None:
rtn_dict = {}
rtn_dict['input_tensor'] = None
return rtn_dict
align_img = result['img']
face_img = align_img[:, :, ::-1] # to rgb
face_img = (face_img / 255. - 0.5) / 0.5
@@ -83,12 +87,14 @@ class FaceQualityAssessmentPipeline(FaceProcessingBasePipeline):
return result
def forward(self, input: Dict[str, Any]) -> Dict[str, Any]:
if input['input_tensor'] is None:
return {OutputKeys.SCORES: None, OutputKeys.BOXES: None}
input_feed = {}
input_feed[
self.input_node_name[0]] = input['input_tensor'].cpu().numpy()
result = self.sess.run(self.out_node_name, input_feed=input_feed)
assert result is not None
scores = [result[0][0][0]]
scores = [np.mean(result[0][0])]
boxes = input['bbox'].cpu().numpy()[np.newaxis, :].tolist()
return {OutputKeys.SCORES: scores, OutputKeys.BOXES: boxes}

View File

@@ -66,6 +66,10 @@ class FaceRecognitionOnnxFmPipeline(FaceProcessingBasePipeline):
def preprocess(self, input: Input) -> Dict[str, Any]:
result = super().preprocess(input)
if result is None:
rtn_dict = {}
rtn_dict['input_tensor'] = None
return rtn_dict
align_img = result['img']
face_img = align_img[:, :, ::-1] # to rgb
face_img = (face_img / 255. - 0.5) / 0.5
@@ -76,6 +80,8 @@ class FaceRecognitionOnnxFmPipeline(FaceProcessingBasePipeline):
return result
def forward(self, input: Dict[str, Any]) -> Dict[str, Any]:
if input['input_tensor'] is None:
return {OutputKeys.IMG_EMBEDDING: None}
input_feed = {}
input_feed[
self.input_node_name[0]] = input['input_tensor'].cpu().numpy()

View File

@@ -63,6 +63,10 @@ class FaceRecognitionOnnxIrPipeline(FaceProcessingBasePipeline):
def preprocess(self, input: Input) -> Dict[str, Any]:
result = super().preprocess(input)
if result is None:
rtn_dict = {}
rtn_dict['input_tensor'] = None
return rtn_dict
align_img = result['img']
face_img = align_img[:, :, ::-1] # to rgb
face_img = (face_img / 255. - 0.5) / 0.5
@@ -73,6 +77,8 @@ class FaceRecognitionOnnxIrPipeline(FaceProcessingBasePipeline):
return result
def forward(self, input: Dict[str, Any]) -> Dict[str, Any]:
if input['input_tensor'] is None:
return {OutputKeys.IMG_EMBEDDING: None}
input_feed = {}
input_feed[
self.input_node_name[0]] = input['input_tensor'].cpu().numpy()

View File

@@ -51,6 +51,10 @@ class FaceRecognitionOodPipeline(FaceProcessingBasePipeline):
def preprocess(self, input: Input) -> Dict[str, Any]:
result = super().preprocess(input)
if result is None:
rtn_dict = {}
rtn_dict['img'] = None
return rtn_dict
align_img = result['img']
face_img = align_img[:, :, ::-1] # to rgb
face_img = np.transpose(face_img, axes=(2, 0, 1))
@@ -60,7 +64,8 @@ class FaceRecognitionOodPipeline(FaceProcessingBasePipeline):
return result
def forward(self, input: Dict[str, Any]) -> Dict[str, Any]:
assert input['img'] is not None
if input['img'] is None:
return {OutputKeys.IMG_EMBEDDING: None, OutputKeys.SCORES: None}
img = input['img'].unsqueeze(0)
output = self.face_model(img)
emb = output[0].detach().cpu().numpy()

View File

@@ -49,11 +49,16 @@ class FacialExpressionRecognitionPipeline(FaceProcessingBasePipeline):
]
def preprocess(self, input: Input) -> Dict[str, Any]:
result = super(FacialExpressionRecognitionPipeline,
self).preprocess(input)
result = super().preprocess(input)
if result is None:
rtn_dict = {}
rtn_dict['img'] = None
return rtn_dict
return result
def forward(self, input: Dict[str, Any]) -> Dict[str, Any]:
if input['img'] is None:
return {OutputKeys.SCORES: None, OutputKeys.LABELS: None}
result = self.fer(input)
assert result is not None
scores = result[0].tolist()

View File

@@ -44,12 +44,23 @@ class FacialLandmarkConfidencePipeline(FaceProcessingBasePipeline):
def preprocess(self, input: Input) -> Dict[str, Any]:
result = super().preprocess(input)
if result is None:
rtn_dict = {}
rtn_dict['img'] = None
return rtn_dict
img = LoadImage.convert_to_ndarray(input)
img = img[:, :, ::-1]
result['orig_img'] = img.astype(np.float32)
return result
def forward(self, input: Dict[str, Any]) -> Dict[str, Any]:
if input['img'] is None:
return {
OutputKeys.SCORES: None,
OutputKeys.POSES: None,
OutputKeys.KEYPOINTS: None,
OutputKeys.BOXES: None
}
result = self.flcm(input)
assert result is not None
lms = result[0].reshape(-1, 10).tolist()

View File

@@ -25,8 +25,11 @@ class FaceRecognitionTest(unittest.TestCase, DemoCompatibilityCheck):
Tasks.face_recognition, model=self.model_id)
emb1 = face_recognition(img1)[OutputKeys.IMG_EMBEDDING]
emb2 = face_recognition(img2)[OutputKeys.IMG_EMBEDDING]
sim = np.dot(emb1[0], emb2[0])
print(f'Cos similarity={sim:.3f}, img1:{img1} img2:{img2}')
if emb1 is None or emb2 is None:
print('No Detected Face.')
else:
sim = np.dot(emb1[0], emb2[0])
print(f'Cos similarity={sim:.3f}, img1:{img1} img2:{img2}')
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_demo_compatibility(self):

View File

@@ -29,7 +29,10 @@ class FaceAttributeRecognitionTest(unittest.TestCase):
Tasks.face_attribute_recognition, model=self.model_id)
img_path = 'data/test/images/face_recognition_1.png'
result = fair_face(img_path)
self.show_result(img_path, result)
if result[OutputKeys.SCORES] is None:
print('No Detected Face.')
else:
self.show_result(img_path, result)
if __name__ == '__main__':

View File

@@ -4,6 +4,7 @@ import unittest
import cv2
from modelscope.outputs import OutputKeys
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
from modelscope.utils.cv.image_utils import draw_face_detection_no_lm_result
@@ -25,13 +26,19 @@ class FaceLivenessIrTest(unittest.TestCase):
def test_run_modelhub(self):
face_detection = pipeline(Tasks.face_liveness, model=self.model_id)
result = face_detection(self.img_path)
self.show_result(self.img_path, result)
if result[OutputKeys.SCORES] is None:
print('No Detected Face.')
else:
self.show_result(self.img_path, result)
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_run_default_model(self):
face_detection = pipeline(Tasks.face_liveness)
result = face_detection(self.img_path)
self.show_result(self.img_path, result)
if result[OutputKeys.SCORES] is None:
print('No Detected Face.')
else:
self.show_result(self.img_path, result)
if __name__ == '__main__':

View File

@@ -4,6 +4,7 @@ import unittest
import cv2
from modelscope.outputs import OutputKeys
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
from modelscope.utils.cv.image_utils import draw_face_detection_no_lm_result
@@ -25,13 +26,19 @@ class FaceLivenessRgbTest(unittest.TestCase):
def test_run_modelhub(self):
face_detection = pipeline(Tasks.face_liveness, model=self.model_id)
result = face_detection(self.img_path)
self.show_result(self.img_path, result)
if result[OutputKeys.SCORES] is None:
print('No Detected Face.')
else:
self.show_result(self.img_path, result)
@unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
def test_run_default_model(self):
face_detection = pipeline(Tasks.face_liveness)
result = face_detection(self.img_path)
self.show_result(self.img_path, result)
if result[OutputKeys.SCORES] is None:
print('No Detected Face.')
else:
self.show_result(self.img_path, result)
if __name__ == '__main__':

View File

@@ -4,6 +4,7 @@ import unittest
import cv2
from modelscope.outputs import OutputKeys
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
from modelscope.utils.cv.image_utils import draw_face_detection_no_lm_result
@@ -25,13 +26,19 @@ class FaceLivenessXcTest(unittest.TestCase):
def test_run_modelhub(self):
face_detection = pipeline(Tasks.face_liveness, model=self.model_id)
result = face_detection(self.img_path)
self.show_result(self.img_path, result)
if result[OutputKeys.SCORES] is None:
print('No Detected Face.')
else:
self.show_result(self.img_path, result)
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_run_default_model(self):
face_detection = pipeline(Tasks.face_liveness)
result = face_detection(self.img_path)
self.show_result(self.img_path, result)
if result[OutputKeys.SCORES] is None:
print('No Detected Face.')
else:
self.show_result(self.img_path, result)
if __name__ == '__main__':

View File

@@ -4,6 +4,7 @@ import unittest
import cv2
from modelscope.outputs import OutputKeys
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
from modelscope.utils.cv.image_utils import draw_face_detection_no_lm_result
@@ -14,6 +15,7 @@ class FaceQualityAssessmentTest(unittest.TestCase):
def setUp(self) -> None:
self.model_id = 'damo/cv_manual_face-quality-assessment_fqa'
self.img_path = 'data/test/images/vision_efficient_tuning_test_sunflower.jpg'
self.img_path = 'data/test/images/face_recognition_1.png'
def show_result(self, img_path, detection_result):
@@ -23,16 +25,22 @@ class FaceQualityAssessmentTest(unittest.TestCase):
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_run_modelhub(self):
face_detection = pipeline(
face_quality_assessment = pipeline(
Tasks.face_quality_assessment, model=self.model_id)
result = face_detection(self.img_path)
self.show_result(self.img_path, result)
result = face_quality_assessment(self.img_path)
if result[OutputKeys.SCORES] is None:
print('No Detected Face.')
else:
self.show_result(self.img_path, result)
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_run_default_model(self):
face_detection = pipeline(Tasks.face_quality_assessment)
result = face_detection(self.img_path)
self.show_result(self.img_path, result)
face_quality_assessment = pipeline(Tasks.face_quality_assessment)
result = face_quality_assessment(self.img_path)
if result[OutputKeys.SCORES] is None:
print('No Detected Face.')
else:
self.show_result(self.img_path, result)
if __name__ == '__main__':

View File

@@ -25,8 +25,11 @@ class FmFaceRecognitionTest(unittest.TestCase, DemoCompatibilityCheck):
Tasks.face_recognition, model=self.model_id)
emb1 = face_recognition(img1)[OutputKeys.IMG_EMBEDDING]
emb2 = face_recognition(img2)[OutputKeys.IMG_EMBEDDING]
sim = np.dot(emb1[0], emb2[0])
print(f'Cos similarity={sim:.3f}, img1:{img1} img2:{img2}')
if emb1 is None or emb2 is None:
print('No Detected Face.')
else:
sim = np.dot(emb1[0], emb2[0])
print(f'Cos similarity={sim:.3f}, img1:{img1} img2:{img2}')
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_demo_compatibility(self):

View File

@@ -25,8 +25,11 @@ class IrFaceRecognitionTest(unittest.TestCase, DemoCompatibilityCheck):
Tasks.face_recognition, model=self.model_id)
emb1 = face_recognition(img1)[OutputKeys.IMG_EMBEDDING]
emb2 = face_recognition(img2)[OutputKeys.IMG_EMBEDDING]
sim = np.dot(emb1[0], emb2[0])
print(f'Cos similarity={sim:.3f}, img1:{img1} img2:{img2}')
if emb1 is None or emb2 is None:
print('No Detected Face.')
else:
sim = np.dot(emb1[0], emb2[0])
print(f'Cos similarity={sim:.3f}, img1:{img1} img2:{img2}')
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_demo_compatibility(self):

View File

@@ -24,15 +24,20 @@ class FaceRecognitionOodTest(unittest.TestCase, DemoCompatibilityCheck):
face_recognition = pipeline(self.task, model=self.model_id)
result1 = face_recognition(img1)
emb1 = result1[OutputKeys.IMG_EMBEDDING]
score1 = result1[OutputKeys.SCORES][0][0]
result2 = face_recognition(img2)
emb2 = result2[OutputKeys.IMG_EMBEDDING]
score2 = result2[OutputKeys.SCORES][0][0]
sim = np.dot(emb1[0], emb2[0])
print(f'Cos similarity={sim:.3f}, img1:{img1} img2:{img2}')
print(f'OOD score: img1:{score1:.3f} img2:{score2:.3f}')
if emb1 is None or emb2 is None:
print('No Detected Face.')
else:
sim = np.dot(emb1[0], emb2[0])
score1 = result1[OutputKeys.SCORES][0][0]
score2 = result2[OutputKeys.SCORES][0][0]
print(f'Cos similarity={sim:.3f}, img1:{img1} img2:{img2}')
print(f'OOD score: img1:{score1:.3f} img2:{score2:.3f}')
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_demo_compatibility(self):

View File

@@ -29,7 +29,10 @@ class FacialExpressionRecognitionTest(unittest.TestCase):
Tasks.facial_expression_recognition, model=self.model_id)
img_path = 'data/test/images/facial_expression_recognition.jpg'
result = fer(img_path)
self.show_result(img_path, result)
if result[OutputKeys.SCORES] is None:
print('No Detected Face.')
else:
self.show_result(img_path, result)
if __name__ == '__main__':

View File

@@ -28,7 +28,10 @@ class FacialLandmarkConfidenceTest(unittest.TestCase):
flcm = pipeline(Tasks.face_2d_keypoints, model=self.model_id)
img_path = 'data/test/images/face_recognition_1.png'
result = flcm(img_path)
self.show_result(img_path, result)
if result[OutputKeys.SCORES] is None:
print('No Detected Face.')
else:
self.show_result(img_path, result)
if __name__ == '__main__':