Python 学习整理2

python报错 can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

PHPer 2025-08-26 15 0 0

ERROR:app.services.audio_processor:保存音频或其他操作失败: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

ERROR:app.services.audio_processor:处理音频时发生错误: 保存音频或其他操作失败: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

ERROR:app.api.audio_router:处理音频时发生错误:

Traceback (most recent call last):

File "D:\python\fastapi-speaker-extractor\app\services\audio_processor.py", line 677, in process_audio

target_embedding = get_or_create_target_embedding(target_audio_path, CACHE_DIR, verification)

File "D:\python\fastapi-speaker-extractor\app\utils\audio_utils.py", line 110, in get_or_create_target_embedding

np.save(cache_file, embedding.numpy()) # 存为 numpy

TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

During handling of the above exception, another exception occurred:

# =========================================
# 4. 函数:获取目标 embedding(支持缓存)
def get_or_create_target_embedding(audio_path, cache_dir ,verification):
    os.makedirs(cache_dir, exist_ok=True)
    with open(audio_path, 'rb') as f:
        file_hash = hashlib.md5(f.read()).hexdigest()
    cache_file = os.path.join(cache_dir, f"{file_hash}.npy")
    if os.path.exists(cache_file):
        print(f"✅ [缓存命中] 从缓存加载目标说话人 embedding: {cache_file}")
        # 从 .npy 加载的是 numpy,需要转为 Tensor
        embedding_np = np.load(cache_file)
        return torch.from_numpy(embedding_np)  # ✅ 转为 PyTorch Tensor
    else:
        print(f"🔁 [缓存未命中] 正在提取目标说话人音频的 embedding: {audio_path}")
        embedding = extract_audio_embedding(audio_path ,verification)  # Tensor
        # 保存为 numpy 到缓存
        np.save(cache_file, embedding.numpy())  # 存为 numpy
        print(f"💾 [缓存保存] 已保存目标说话人 embedding 到缓存: {cache_file}")
        return embedding  # Tensor

kimi瞬间解决了...

立即注册
更新于:2025-08-26 00:18:51
    您需要登录后才可以评论。 立即注册
    相关内容

    PHP报错:Constants may only evaluate to scalar values

    PHP错误:SQLSTATE[HY000] [2054] The server requested authentica...

    mysql在导入大数据库的时候,报错 Unknown MySQL server host ... ER...

    Xampp安装后apache启动失败报错 www.example.com:443:0 server certificate...

    php报错 Error: Variable 'sql_mode' can't be set to the value of 'M...

    推荐内容

    怎样使用V2Ray代理和SSTap玩如魔兽世界/绝地求生/LOL台服/战地3/黑色沙漠/彩...

    sstap游戏代理教程 从此玩如魔兽世界/绝地求生/LOL台服/战地3/黑色沙漠/彩虹六...

    BT磁力搜索网站汇总和找不到的资源

    什么是磁力链接,您如何使用?

    Z-Library:全球最大的数字图书馆/含打不开的解决方案/镜像

    使用V2Ray的mKCP协议加速游戏

    v2rayN已停止工作

    【车险课堂】什么是无赔款优待系数ncd,你“造”吗?