Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win_python_keras版本加载模型时报错 #19

Closed
xdqf2128 opened this issue Jun 17, 2021 · 4 comments
Closed

win_python_keras版本加载模型时报错 #19

xdqf2128 opened this issue Jun 17, 2021 · 4 comments

Comments

@xdqf2128
Copy link

简单测试时报错
步骤如下:
1.先用keras保存resnet50的预训练模型

model = ResNet50()
model.save('./resnet50.h5')

2.按照示例,运行

    # 1. 构建 Engine
    builder = fwd.KerasBuilder()
    infer_mode = 'float32'  # Infer Mode: float32 / float16 / int8_calib / int8
    batch_size = 1
    max_workspace_size = 1 << 32

    builder.set_mode(infer_mode)
    engine = builder.build(r'./resnet50.h5', batch_size)

    # need_save = True
    # if need_save:
    #     engine_path = 'path/to/out/engine'
    #     engine.save(engine_path)
    #     engine = fwd.KerasEngine()
    #     engine.load(engine_path)

    # 2. 执行推理
    inputs = np.random.randn(1, 224, 224, 3)
    outputs = engine.forward([inputs])  # list_type output
    print(outputs)

报错如下:

[ERROR] 2021-06-17 09:29:47,713 trt_keras_parser.cpp(90): Load Model failed.
[ERROR] 2021-06-17 09:29:47,713 keras_engine.cpp(129): Parse Keras Graph failed
Traceback (most recent call last):
  File "D:/Projects/tencent_forward/workspace/resnet_forward.py", line 26, in <module>
    outputs = engine.forward([inputs])  # list_type output
AttributeError: 'NoneType' object has no attribute 'forward'

加载模型那一步失败了,还望指导下,感谢~

Environment

TensorRT Version: 7.2.1.6
NVIDIA GPU: RTX 2080 SUPER
NVIDIA Driver Version: 441.22
CUDA Version: 10.2
CUDNN Version: 8.2.0.53
Operating System: Windows 10 专业版
Python Version: 3.8.5
Keras: 2.4.3
h5py: 2.10.0

@yuanzexi
Copy link
Collaborator

@xdqf2128 我按照你的代码尝试复现,但我得机器环境下是执行成功的。
image

我看到你的错误是 Load Model failed.,或许可以确认一下 模型地址是否正确?比如那个模型地址是否能被 with open(filename, 'r') 找到呢?
另外,如果仍有问题,你可以在编译 fwd_keras 的时候,将 ENABLE_LOGGING 改为 ON,然后再用新编译的带日志的包再试试,然后看是因为什么原因导致的 Load Model failed。可以附上日志截图给我们看看。

P.S. 如果 Buid 成功了,注意在推理的时候,输入需要类型转换一下,要不然 numpy randn 生成的默认不是 float32 的数据,会无法通过输入数据校验。

@xdqf2128
Copy link
Author

@yuanzexi 我按照您说的把LOGGING设为了ON
image
但是用新编译出来的包还是只有ERROR级别的日志没有更详细的。
image
然后模型路径没有问题,open可以正常找到
image

@xdqf2128
Copy link
Author

@yuanzexi 我注意到报错是在trt_keras_parser.cpp(90),然后查看了源码,发现是判断模型是否为空,并检查class_name。这里我就想到了新版keras模型的class_name是Functional不是老版的Model,所以我直接替换成了老版本的模型,并添加了日志打印。
image
image
然后我把项目配置设为Debug,添加了log.conf等我觉得可能会影响日志打印的设置
image
结果可以打印详细的日志了,并且之前Load Model failed确实是因为模型class_name的问题
image
替换了老版模型后,错误信息改变了
image
image
看样子是层的类型的问题,请问接下来要怎么解决?

@xdqf2128
Copy link
Author

xdqf2128 commented Jun 22, 2021

@yuanzexi 我注意到tf1.12的模型中BN并不带V1,所以用不带V1的模型试了下,成功完成了build,并且推理成功
image
感谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants