deepinsight / insightface

State-of-the-art 2D and 3D Face Analysis Project
https://insightface.ai
22.85k stars 5.35k forks source link

Exporting model, using own model error #2433

Open gg22mm opened 1 year ago

gg22mm commented 1 year ago

1、 python torch2onnx.py ./glint360k_cosface_r100_fp16_0.1/backbone.pth --network r100 --output inswapper_128_glint360k_r100.onnx 2、 import cv2 import insightface from insightface.app import FaceAnalysis

face_analyer = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider']) face_analyer.prepare(ctx_id=0, det_size=(640, 640))
face_swapper = insightface.model_zoo.get_model('./models/inswapper_128_glint360k_r100.onnx', download=False, download_zip=False)
source_img = cv2.imread("1.jpg") source_faces = face_analyer.get(source_img) source_faces = sorted(source_faces, key = lambda x : x.bbox[0])
assert len(source_faces)==1 source_face = source_faces[0]
target_img = cv2.imread("obj.jpg")
target_faces = face_analyer.get(target_img) target_faces = sorted(target_faces, key=lambda x: x.bbox[0])
for target_face in target_faces:
target_img = face_swapper.get(target_img, target_face, source_face, paste_back=True)
cv2.imwrite('11.jpg', target_img )

3、error Traceback (most recent call last): File "test.py", line 46, in target_img = face_swapper.get(target_img, target_face, source_face, paste_back=True) TypeError: get() got an unexpected keyword argument 'paste_back'

图片

Sunshine066 commented 12 months ago

The implementation of this method is as follows def get(self, img, face): bbox = face.bbox w, h = (bbox[2] - bbox[0]), (bbox[3] - bbox[1]) center = (bbox[2] + bbox[0]) / 2, (bbox[3] + bbox[1]) / 2 rotate = 0 _scale = self.input_size[0] / (max(w, h)*1.5)

print('param:', img.shape, bbox, center, self.input_size, _scale, rotate)

    aimg, M = face_align.transform(img, center, self.input_size[0], _scale, rotate)
    input_size = tuple(aimg.shape[0:2][::-1])
    #assert input_size==self.input_size
    blob = cv2.dnn.blobFromImage(aimg, 1.0/self.input_std, input_size, (self.input_mean, self.input_mean, self.input_mean), swapRB=True)
    pred = self.session.run(self.output_names, {self.input_name : blob})[0][0]
    if pred.shape[0] >= 3000:
        pred = pred.reshape((-1, 3))
    else:
        pred = pred.reshape((-1, 2))
    if self.lmk_num < pred.shape[0]:
        pred = pred[self.lmk_num*-1:,:]
    pred[:, 0:2] += 1
    pred[:, 0:2] *= (self.input_size[0] // 2)
    if pred.shape[1] == 3:
        pred[:, 2] *= (self.input_size[0] // 2)

    IM = cv2.invertAffineTransform(M)
    pred = face_align.trans_points(pred, IM)
    face[self.taskname] = pred
    if self.require_pose:
        P = transform.estimate_affine_matrix_3d23d(self.mean_lmk, pred)
        s, R, t = transform.P2sRt(P)
        rx, ry, rz = transform.matrix2angle(R)
        pose = np.array( [rx, ry, rz], dtype=np.float32 )
        face['pose'] = pose #pitch, yaw, roll
    return pred

As you can see, there is no paste_back parameter

Sunshine066 commented 12 months ago

You should look at the source code for inssightface to analyze the problem