Spaces:
Configuration error
Configuration error
File size: 21,114 Bytes
8dc9718 |
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# -*- coding: utf-8 -*-
# @Time : 2024/7/13 17:20
# @Author : wenshao
# @Email : wenshaoguo1026@gmail.com
# @Project : FasterLivePortrait
# @FileName: test_models.py
import json
import os, sys
import pdb
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
def test_warping_spade_model():
"""
test warping model in onnx and trt
:return:
"""
import numpy as np
import time
from src.models import WarpingSpadeModel
# tensorrt 模型加载
trt_kwargs = dict(
predict_type="trt",
model_path="./checkpoints/liveportrait_animal_onnx/warping_spade-fix.trt",
)
trt_model = WarpingSpadeModel(**trt_kwargs)
# onnx 模型加载
onnx_kwargs = dict(
predict_type="ort",
model_path="./checkpoints/liveportrait_animal_onnx/warping_spade.onnx",
)
onnx_model = WarpingSpadeModel(**onnx_kwargs)
feature_3d = np.random.randn(1, 32, 16, 64, 64)
kp_source = np.random.randn(1, 21, 3)
kp_driving = np.random.randn(1, 21, 3)
trt_rets = trt_model.predict(feature_3d, kp_source, kp_driving)
onnx_rets = onnx_model.predict(feature_3d, kp_source, kp_driving)
# for i in range(len(trt_rets)):
print(f"output max diff:{np.abs(trt_rets - onnx_rets).max()}")
infer_times = []
for _ in range(30):
t0 = time.time()
trt_rets = trt_model.predict(feature_3d, kp_source, kp_driving)
infer_times.append(time.time() - t0)
print(
"{} tensorrt inference time: min: {}, max: {}, mean: {}".format(WarpingSpadeModel.__name__, np.min(infer_times),
np.max(infer_times), np.median(infer_times)))
infer_times = []
for _ in range(30):
t0 = time.time()
onnx_rets = onnx_model.predict(feature_3d, kp_source, kp_driving)
infer_times.append(time.time() - t0)
print("{} onnx inference time: min: {}, max: {}, mean: {}".format(WarpingSpadeModel.__name__, np.min(infer_times),
np.max(infer_times), np.median(infer_times)))
def test_motion_extractor_model():
"""
test motion_extractor model in onnx and trt
:return:
"""
import numpy as np
import time
import cv2
from src.models import MotionExtractorModel
# tensorrt 模型加载
trt_kwargs = dict(
predict_type="trt",
model_path="./checkpoints/liveportrait_animal_onnx/motion_extractor.trt",
debug=True
)
trt_model = MotionExtractorModel(**trt_kwargs)
# onnx 模型加载
onnx_kwargs = dict(
predict_type="ort",
model_path="./checkpoints/liveportrait_animal_onnx/motion_extractor.onnx",
debug=True
)
onnx_model = MotionExtractorModel(**onnx_kwargs)
img_bgr = cv2.imread("assets/examples/source/s1.jpg")
img_rgb = img_bgr[:, :, ::-1]
input = cv2.resize(img_rgb, (256, 256))
trt_rets = trt_model.predict(input)
onnx_rets = onnx_model.predict(input)
for i in range(len(trt_rets)):
print(f"output {i} max diff:{np.abs(trt_rets[i] - onnx_rets[i]).max()}")
pdb.set_trace()
infer_times = []
for _ in range(30):
t0 = time.time()
trt_rets = trt_model.predict(input)
infer_times.append(time.time() - t0)
print("{} tensorrt inference time: min: {}, max: {}, mean: {}".format(MotionExtractorModel.__name__,
np.min(infer_times),
np.max(infer_times), np.median(infer_times)))
infer_times = []
for _ in range(30):
t0 = time.time()
onnx_rets = onnx_model.predict(input)
infer_times.append(time.time() - t0)
print(
"{} onnx inference time: min: {}, max: {}, mean: {}".format(MotionExtractorModel.__name__, np.min(infer_times),
np.max(infer_times), np.median(infer_times)))
def test_appearance_extractor_model():
"""
test motion_extractor model in onnx and trt
:return:
"""
import numpy as np
import time
import cv2
from src.models import AppearanceFeatureExtractorModel
# tensorrt 模型加载
trt_kwargs = dict(
predict_type="trt",
model_path="./checkpoints/liveportrait_onnx/appearance_feature_extractor.trt",
)
trt_model = AppearanceFeatureExtractorModel(**trt_kwargs)
# onnx 模型加载
onnx_kwargs = dict(
predict_type="ort",
model_path="./checkpoints/liveportrait_onnx/appearance_feature_extractor.onnx",
)
onnx_model = AppearanceFeatureExtractorModel(**onnx_kwargs)
img_bgr = cv2.imread("assets/examples/source/s1.jpg")
img_rgb = img_bgr[:, :, ::-1]
input = cv2.resize(img_rgb, (256, 256))
trt_rets = trt_model.predict(input)
onnx_rets = onnx_model.predict(input)
print(f"output max diff:{np.abs(trt_rets - onnx_rets).max()}")
pdb.set_trace()
infer_times = []
for _ in range(20):
t0 = time.time()
trt_rets = trt_model.predict(input)
infer_times.append(time.time() - t0)
print("{} tensorrt inference time: min: {}, max: {}, mean: {}".format(AppearanceFeatureExtractorModel.__name__,
np.min(infer_times),
np.max(infer_times), np.mean(infer_times)))
# onnx is so slow, don't why, maybe the grid_sample op not implemented well?
infer_times = []
for _ in range(20):
t0 = time.time()
onnx_rets = onnx_model.predict(input)
infer_times.append(time.time() - t0)
print(
"{} onnx inference time: min: {}, max: {}, mean: {}".format(AppearanceFeatureExtractorModel.__name__,
np.min(infer_times),
np.max(infer_times), np.mean(infer_times)))
def test_landmark_model():
"""
test motion_extractor model in onnx and trt
:return:
"""
import numpy as np
import time
import cv2
from src.models import LandmarkModel
# tensorrt 模型加载
trt_kwargs = dict(
predict_type="trt",
model_path="./checkpoints/liveportrait_onnx/landmark.trt",
debug=True
)
trt_model = LandmarkModel(**trt_kwargs)
# onnx 模型加载
onnx_kwargs = dict(
predict_type="ort",
model_path="./checkpoints/liveportrait_onnx/landmark.onnx",
debug=True
)
onnx_model = LandmarkModel(**onnx_kwargs)
img_bgr = cv2.imread("assets/examples/source/s1.jpg")
img_rgb = img_bgr[:, :, ::-1]
input = cv2.resize(img_rgb, (224, 224))
trt_rets = trt_model.predict(input)
onnx_rets = onnx_model.predict(input)
print(f"output max diff:{np.abs(trt_rets - onnx_rets).max()}")
pdb.set_trace()
infer_times = []
for _ in range(30):
t0 = time.time()
trt_rets = trt_model.predict(input)
infer_times.append(time.time() - t0)
print("{} tensorrt inference time: min: {}, max: {}, mean: {}".format(LandmarkModel.__name__,
np.min(infer_times),
np.max(infer_times), np.median(infer_times)))
# onnx is so slow, don't why, maybe the grid_sample op not implemented well?
infer_times = []
for _ in range(30):
t0 = time.time()
onnx_rets = onnx_model.predict(input)
infer_times.append(time.time() - t0)
print(
"{} onnx inference time: min: {}, max: {}, mean: {}".format(LandmarkModel.__name__,
np.min(infer_times),
np.max(infer_times), np.median(infer_times)))
def test_face_analysis_model():
import numpy as np
import cv2
import time
from src.models import FaceAnalysisModel
img_bgr = cv2.imread("assets/examples/source/s1.jpg")
# onnx 模型加载
onnx_kwargs = dict(
predict_type="ort",
model_path=["./checkpoints/liveportrait_onnx/retinaface_det_static.onnx",
"./checkpoints/liveportrait_onnx/face_2dpose_106_static.onnx"],
)
onnx_model = FaceAnalysisModel(**onnx_kwargs)
# tensorrt 模型加载
trt_kwargs = dict(
predict_type="trt",
model_path=["./checkpoints/liveportrait_onnx/retinaface_det_static.trt",
"./checkpoints/liveportrait_onnx/face_2dpose_106_static.trt"],
)
trt_model = FaceAnalysisModel(**trt_kwargs)
trt_rets = trt_model.predict(img_bgr)[0]
onnx_rets = onnx_model.predict(img_bgr)[0]
for key in trt_rets:
print(f"output {key} max diff:{np.abs(trt_rets[key] - onnx_rets[key]).max()}")
pdb.set_trace()
infer_times = []
for _ in range(30):
t0 = time.time()
trt_rets = trt_model.predict(img_bgr)
infer_times.append(time.time() - t0)
print("{} tensorrt inference time: min: {}, max: {}, mean: {}".format(FaceAnalysisModel.__name__,
np.min(infer_times),
np.max(infer_times), np.median(infer_times)))
infer_times = []
for _ in range(30):
t0 = time.time()
onnx_rets = onnx_model.predict(img_bgr)
infer_times.append(time.time() - t0)
print(
"{} onnx inference time: min: {}, max: {}, mean: {}".format(FaceAnalysisModel.__name__, np.min(infer_times),
np.max(infer_times), np.median(infer_times)))
def test_stitching_model():
"""
test stitching model in onnx and trt
:return:
"""
import numpy as np
import time
from src.models import StitchingModel
# tensorrt 模型加载
trt_kwargs = dict(
predict_type="trt",
model_path="./checkpoints/liveportrait_onnx/stitching.trt",
)
trt_model = StitchingModel(**trt_kwargs)
# onnx 模型加载
onnx_kwargs = dict(
predict_type="ort",
model_path="./checkpoints/liveportrait_onnx/stitching.onnx"
)
onnx_model = StitchingModel(**onnx_kwargs)
input = np.random.randn(1, 126)
trt_rets = trt_model.predict(input)
onnx_rets = onnx_model.predict(input)
print(f"output max diff:{np.abs(trt_rets - onnx_rets).max()}")
infer_times = []
for _ in range(20):
t0 = time.time()
trt_rets = trt_model.predict(input)
infer_times.append(time.time() - t0)
print("{} tensorrt inference time: min: {}, max: {}, mean: {}".format(StitchingModel.__name__,
np.min(infer_times),
np.max(infer_times), np.median(infer_times)))
# onnx is so slow, don't why, maybe the grid_sample op not implemented well?
infer_times = []
for _ in range(20):
t0 = time.time()
onnx_rets = onnx_model.predict(input)
infer_times.append(time.time() - t0)
print(
"{} onnx inference time: min: {}, max: {}, mean: {}".format(StitchingModel.__name__,
np.min(infer_times),
np.max(infer_times), np.median(infer_times)))
def test_mediapipe_face():
img_path = ""
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_face_mesh = mp.solutions.face_mesh
os.makedirs('./results/mediapipe_test', exist_ok=True)
# For static images:
IMAGE_FILES = ["assets/examples/source/s9.jpg"]
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
with mp_face_mesh.FaceMesh(
static_image_mode=True,
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5) as face_mesh:
for idx, file in enumerate(IMAGE_FILES):
image = cv2.imread(file)
# Convert the BGR image to RGB before processing.
results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Print and draw face mesh landmarks on the image.
if not results.multi_face_landmarks:
continue
annotated_image = image.copy()
for face_landmarks in results.multi_face_landmarks:
landmarks = []
for landmark in face_landmarks.landmark:
# 提取每个关键点的 x, y, z 坐标
landmarks.append({
'x': landmark.x,
'y': landmark.y,
'z': landmark.z
})
pdb.set_trace()
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_TESSELATION,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles
.get_default_face_mesh_tesselation_style())
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_CONTOURS,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles
.get_default_face_mesh_contours_style())
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_IRISES,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles
.get_default_face_mesh_iris_connections_style())
cv2.imwrite('./results/mediapipe_test/' + os.path.basename(file), annotated_image)
def test_kokoro_model():
import os
os.environ["PHONEMIZER_ESPEAK_LIBRARY"] = r"C:\Program Files\eSpeak NG\libespeak-ng.dll"
os.environ["PHONEMIZER_ESPEAK_PATH"] = r"C:\Program Files\eSpeak NG\espeak-ng.exe"
import torchaudio
from src.models.kokoro.models import build_model
from src.models.kokoro.kokoro import generate
import torch
device = 'cuda' if torch.cuda.is_available() else 'cpu'
MODEL = build_model('checkpoints/Kokoro-82M/kokoro-v0_19.pth', device)
VOICE_NAME = [
'af', # Default voice is a 50-50 mix of Bella & Sarah
'af_bella', 'af_sarah', 'am_adam', 'am_michael',
'bf_emma', 'bf_isabella', 'bm_george', 'bm_lewis',
'af_nicole', 'af_sky',
][0]
VOICEPACK = torch.load(f'checkpoints/Kokoro-82M/voices/{VOICE_NAME}.pt', weights_only=True).to(device)
print(f'Loaded voice: {VOICE_NAME}')
text = "How could I know? It's an unanswerable question. Like asking an unborn child if they'll lead a good life. They haven't even been born."
audio, out_ps = generate(MODEL, text, VOICEPACK, lang=VOICE_NAME[0])
audio_save_path = "./results/kokoro-82m/kokoro_test.wav"
os.makedirs(os.path.dirname(audio_save_path), exist_ok=True)
torchaudio.save(audio_save_path, audio[0], 24000)
print(f"audio save to {audio_save_path}")
def test_kokoro_v1_model():
# import os
# os.environ["PHONEMIZER_ESPEAK_LIBRARY"] = r"C:\Program Files\eSpeak NG\libespeak-ng.dll"
# os.environ["PHONEMIZER_ESPEAK_PATH"] = r"C:\Program Files\eSpeak NG\espeak-ng.exe"
import torchaudio
from kokoro import KPipeline, KModel
import soundfile as sf
import numpy as np
import torch
# 🇺🇸 'a' => American English, 🇬🇧 'b' => British English
# 🇯🇵 'j' => Japanese: pip install misaki[ja]
# 🇨🇳 'z' => Mandarin Chinese: pip install misaki[zh]
voice = 'jf_tebukuro'
with open("checkpoints/Kokoro-82M/config.json", "r", encoding="utf-8") as fin:
model_config = json.load(fin)
model = KModel(config=model_config, model="checkpoints/Kokoro-82M/kokoro-v1_0.pth")
pipeline = KPipeline(lang_code=voice[0], model=model) # <= make sure lang_code matches voice
model.voices = {}
voice_path = "checkpoints/Kokoro-82M/voices"
for vname in os.listdir(voice_path):
pipeline.voices[os.path.splitext(vname)[0]] = torch.load(os.path.join(voice_path, vname), weights_only=True)
# This text is for demonstration purposes only, unseen during training
# text = '''
# The sky above the port was the color of television, tuned to a dead channel.
# "It's not like I'm using," Case heard someone say, as he shouldered his way through the crowd around the door of the Chat. "It's like my body's developed this massive drug deficiency."
# It was a Sprawl voice and a Sprawl joke. The Chatsubo was a bar for professional expatriates; you could drink there for a week and never hear two words in Japanese.
#
# These were to have an enormous impact, not only because they were associated with Constantine, but also because, as in so many other areas, the decisions taken by Constantine (or in his name) were to have great significance for centuries to come. One of the main issues was the shape that Christian churches were to take, since there was not, apparently, a tradition of monumental church buildings when Constantine decided to help the Christian church build a series of truly spectacular structures. The main form that these churches took was that of the basilica, a multipurpose rectangular structure, based ultimately on the earlier Greek stoa, which could be found in most of the great cities of the empire. Christianity, unlike classical polytheism, needed a large interior space for the celebration of its religious services, and the basilica aptly filled that need. We naturally do not know the degree to which the emperor was involved in the design of new churches, but it is tempting to connect this with the secular basilica that Constantine completed in the Roman forum (the so-called Basilica of Maxentius) and the one he probably built in Trier, in connection with his residence in the city at a time when he was still caesar.
#
# [Kokoro](/kˈOkəɹO/) is an open-weight TTS model with 82 million parameters. Despite its lightweight architecture, it delivers comparable quality to larger models while being significantly faster and more cost-efficient. With Apache-licensed weights, [Kokoro](/kˈOkəɹO/) can be deployed anywhere from production environments to personal projects.
# '''
text = '「もしおれがただ偶然、そしてこうしようというつもりでなくここに立っているのなら、ちょっとばかり絶望するところだな」と、そんなことが彼の頭に思い浮かんだ。'
# text = '中國人民不信邪也不怕邪,不惹事也不怕事,任何外國不要指望我們會拿自己的核心利益做交易,不要指望我們會吞下損害我國主權、安全、發展利益的苦果!'
# text = 'Los partidos políticos tradicionales compiten con los populismos y los movimientos asamblearios.'
# text = 'Le dromadaire resplendissant déambulait tranquillement dans les méandres en mastiquant de petites feuilles vernissées.'
# text = 'ट्रांसपोर्टरों की हड़ताल लगातार पांचवें दिन जारी, दिसंबर से इलेक्ट्रॉनिक टोल कलेक्शनल सिस्टम'
# text = "Allora cominciava l'insonnia, o un dormiveglia peggiore dell'insonnia, che talvolta assumeva i caratteri dell'incubo."
# text = 'Elabora relatórios de acompanhamento cronológico para as diferentes unidades do Departamento que propõem contratos.'
# 4️⃣ Generate, display, and save audio files in a loop.
generator = pipeline(
text, voice=voice, # <= change voice here
speed=1, split_pattern=r'\n+'
)
audios = []
for i, (gs, ps, audio) in enumerate(generator):
audios.append(audio)
audios = np.concatenate(audios)
sf.write(f'./results/kokoro-82m/kokoro_v1_0_{voice}.wav', audios, 24000) # save each audio file
print(f'./results/kokoro-82m/kokoro_v1_0_{voice}.wav')
if __name__ == '__main__':
# test_warping_spade_model()
# test_motion_extractor_model()
# test_landmark_model()
# test_face_analysis_model()
# test_appearance_extractor_model()
# test_stitching_model()
# test_mediapipe_face()
# test_kokoro_model()
test_kokoro_v1_model()
|