Help find dependencies, python 3.9 problem with conversion Converting ONNX -> TensorFlow

未关闭
#2,004 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
技术栈
python, pytorch, tensorflow

调研方向

从提供的 Python 脚本开始,复现 convert_onnx_to_tf 步骤,并记录所使用的 Python 和依赖项版本。当确定了兼容的依赖项集合,并将其记录为可正常工作的 requirements 文件或等效的环境规范时,工作即完成。

由索引模型根据 Issue 内容生成。

描述

Please help me resolve dependency issues.

I'm using Python 3.9 and encountering problems during the conversion step Converting ONNX -> TensorFlow. No matter what I try, I keep running into conflicts or other errors. I cannot find a proper set of dependencies or at least a working requirements.txt.

Some sources say Python 3.9 is required, others say 3.8, and some recommend 3.10. Honestly, I’m exhausted trying to figure this out.

import os
import subprocess
import torch
import onnx
import onnxslim
import sys
import types
from pathlib import Path
import urllib.request
import zipfile
import shutil

def download_yolov5_repo(target_dir):
    zip_url = "https://github.com/ultralytics/yolov5/archive/refs/heads/master.zip"
    zip_path = os.path.join(target_dir, "yolov5.zip")
    print("📥 Downloading YOLOv5 repository...")
    urllib.request.urlretrieve(zip_url, zip_path)

    with zipfile.ZipFile(zip_path, 'r') as zip_ref:
        zip_ref.extractall(target_dir)
    os.remove(zip_path)

    extracted_path = os.path.join(target_dir, "yolov5-master")
    yolov5_path = os.path.join(target_dir, "yolov5")
    if os.path.exists(yolov5_path):
        shutil.rmtree(yolov5_path)
    shutil.move(extracted_path, yolov5_path)
    print("✅ YOLOv5 repository downloaded and prepared.")

def convert_pt_to_onnx(pt_path, onnx_path):
    yolov5_dir = os.path.join(Path(pt_path).parent, "yolov5")
    yolov5_model_path = os.path.join(yolov5_dir, "models", "experimental.py")

    if not os.path.exists(yolov5_model_path):
        print("⚠️ YOLOv5 repo not found, downloading it...")
        download_yolov5_repo(Path(pt_path).parent)

    if yolov5_dir not in sys.path:
        sys.path.insert(0, yolov5_dir)

    try:
        from models.experimental import attempt_load
    except ImportError as e:
        print("❌ ERROR: Could not import YOLOv5 modules. Ensure YOLOv5 repo is in the correct path.")
        raise e

    model = attempt_load(pt_path, device=torch.device('cpu'))
    model.eval()

    dummy_input = torch.randn(1, 3, 640, 640)

    torch.onnx.export(
        model,
        dummy_input,
        onnx_path,
        export_params=True,
        opset_version=12,
        do_constant_folding=True,
        input_names=['images'],
        output_names=['output'],
        dynamic_axes={'images': {0: 'batch_size'}, 'output': {0: 'batch_size'}}
    )

    onnxslim.slim(onnx_path, onnx_path)

def convert_onnx_to_tf(onnx_path, tf_path):
    subprocess.run(['onnx-tf', 'convert', '-i', onnx_path, '-o', tf_path], check=True)

def convert_tf_to_tfjs(tf_path, tfjs_path):
    try:
        print("⚙️  Importing TensorFlow.js converter inside Python...")
        sys.modules['tensorflow_decision_forests'] = types.SimpleNamespace()
        sys.modules['tensorflow_decision_forests.keras'] = types.SimpleNamespace()
        sys.modules['tensorflow_decision_forests.keras.core'] = types.SimpleNamespace()
        sys.modules['tensorflow_decision_forests.keras.core_inference'] = types.SimpleNamespace()
        sys.modules['tensorflow_decision_forests.tensorflow'] = types.SimpleNamespace()
        sys.modules['tensorflow_decision_forests.tensorflow.ops'] = types.SimpleNamespace()
        sys.modules['tensorflow_decision_forests.tensorflow.ops.inference'] = types.SimpleNamespace()
        sys.modules['tensorflow_decision_forests.tensorflow.ops.inference.api'] = types.SimpleNamespace()
        sys.modules['tensorflow_decision_forests.tensorflow.ops.inference.op'] = types.SimpleNamespace()
        sys.modules['tensorflow_decision_forests.tensorflow.ops.inference.op_dynamic'] = types.SimpleNamespace()

        import tensorflowjs as tfjs

        tfjs.converters.convert_tf_saved_model(
            saved_model_dir=tf_path,
            output_dir=tfjs_path,
            skip_op_check=True,
            strip_debug_ops=True
        )
        print(f"✅ TensorFlow.js conversion complete! Files saved to: {tfjs_path}")
    except Exception as e:
        print("\n❌ ERROR: TensorFlow.js conversion failed unexpectedly.")
        raise e

def main():
    folder_path = input("Enter the folder path to search for PyTorch (.pt) models: ")
    pt_files = [f for f in os.listdir(folder_path) if f.endswith(".pt")]

    if not pt_files:
        print("No PyTorch (.pt) models found.")
        return

    print("\nFound PyTorch models:")
    for i, f in enumerate(pt_files, start=1):
        print(f"{i}. {f}")

    idx = int(input("Enter the number of the model you want to convert: ")) - 1
    pt_path = os.path.join(folder_path, pt_files[idx])
    model_name = os.path.splitext(pt_files[idx])[0]

    onnx_path = os.path.join(folder_path, f"{model_name}.onnx")
    tf_path = os.path.join(folder_path, "model_tf")
    tfjs_path = os.path.join(folder_path, f"{model_name}WebModel")

    print("\nConverting PyTorch -> ONNX using YOLOv5...")
    convert_pt_to_onnx(pt_path, onnx_path)

    print("\nConverting ONNX -> TensorFlow...")
    convert_onnx_to_tf(onnx_path, tf_path)

    print("\nConverting TensorFlow -> TensorFlow.js...")
    convert_tf_to_tfjs(tf_path, tfjs_path)

if __name__ == '__main__':
    main()
主要语言
Jupyter Notebook
星标
4.4k
派生
1.1k
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

tensorflow/probability 的其他 Issue

查看 tensorflow/probability 的全部 Issue

相似的 Issue

更多 Machine Learning Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。