import paddle
import paddle.fluid as fluid
import numpy as np
from paddle.jit import to_static
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn import Layer, Linear
from paddle.nn.initializer import TruncatedNormal, Constant, Normal
from model.vit import VitModel, ImageRank, Mlp, QuickGELU
from paddle.inference import Config
from paddle.inference import create_predictor
from paddle.inference import PrecisionType
import paddle
import paddle.fluid as fluid
import numpy as np
paddle.disable_static()
model_path = "my_path/model/vit_v1.2_static/vit_1.2_static_visual"
model = paddle.jit.load(model_path)
paddle.enable_static()
# 设置使用CPU还是GPU
place = fluid.CUDAPlace(1) # 或者 fluid.CUDAPlace(0) 如果使用GPU
# 创建一个执行器
exe = fluid.Executor(place)
# 创建Program
startup_program = fluid.Program()
infer_program = fluid.Program()
with fluid.program_guard(infer_program, startup_program):
# 定义输入数据
input_shape = [None, 3, 224, 224]
input_dtype = 'float32'
input_data = fluid.data(name='img_emb', shape=input_shape, dtype=input_dtype)
# 实例化模型并获取输出
model = model
output = model(input_data)
# 创建一个随机的数据集作为示例
def create_dummy_dataset(batch_size, num_samples):
for _ in range(num_samples):
yield np.random.rand(batch_size, 3, 224, 224).astype('float32')
# 获取推理数据
batch_size = 1
num_samples = 10
dummy_data = create_dummy_dataset(batch_size, num_samples)
# 初始化参数
exe.run(startup_program)
# 进行推理
fetch_list = [output.name]
for batch_data in dummy_data:
result = exe.run(infer_program,
feed={'input_data': batch_data},
fetch_list=fetch_list)
print("Inference result:", result)
运行后报错:
InvalidArgumentError: The Tensor in the assign Op's Input Variable X(img_emb) is not initialized.
[Hint: Expected t->IsInitialized() == true, but received t->IsInitialized():0 != true:1.] (at /paddle/paddle/fluid/framework/operator.cc:1595)
[operator < assign > error]
具体错误信息:
mypath/py37/lib/python3.7/site-packages/paddle/fluid/backward.py:1697: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
return list(x) if isinstance(x, collections.Sequence) else [x]
W1126 20:07:28.164589 139296 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 8.0, Driver API Version: 12.0, Runtime API Version: 11.2
W1126 20:07:28.166074 139296 device_context.cc:465] device: 0, cuDNN Version: 90.0.
mypath/py37/lib/python3.7/site-packages/paddle/fluid/executor.py:1307: UserWarning: There are no operators in the program to be executed. If you pass Program manually, please use fluid.program_guard to ensure the current Program is being used.
warnings.warn(error_info)
mypath/py37/lib/python3.7/site-packages/paddle/fluid/executor.py:773: UserWarning: The variable input_data is not found in program. It is not declared or is pruned.
% name)
Traceback (most recent call last):
File "inference_error.py", line 62, in <module>
fetch_list=fetch_list)
File "mypath/py37/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1262, in run
six.reraise(*sys.exc_info())
File "mypath/py37/lib/python3.7/site-packages/six.py", line 703, in reraise
raise value
File "mypath/py37/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1260, in run
return_merged=return_merged)
File "mypath/py37/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1402, in _run_impl
use_program_cache=use_program_cache)
File "mypath/py37/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1492, in _run_program
[fetch_var_name])
ValueError: In user code:
File "inference_error.py", line 43, in <module>
output = model(input_data)
File "mypath/py37/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py", line 914, in __call__
outputs = self.forward(*inputs, **kwargs)
File "mypath/py37/lib/python3.7/site-packages/paddle/fluid/dygraph/io.py", line 1282, in __i_m_p_l__
return _run_static_graph(input, program_holder, p.desc)
File "mypath/py37/lib/python3.7/site-packages/paddle/fluid/dygraph/io.py", line 871, in _run_static_graph
dict_rename_var_old_new)
File "mypath/py37/lib/python3.7/site-packages/paddle/fluid/dygraph/io.py", line 939, in _append_block
outputs={'Out': [out_name]})
File "mypath/py37/lib/python3.7/site-packages/paddle/fluid/framework.py", line 3184, in append_op
attrs=kwargs.get("attrs", None))
File "mypath/py37/lib/python3.7/site-packages/paddle/fluid/framework.py", line 2224, in __init__
for frame in traceback.extract_stack():
InvalidArgumentError: The Tensor in the assign Op's Input Variable X(img_emb) is not initialized.
[Hint: Expected t->IsInitialized() == true, but received t->IsInitialized():0 != true:1.] (at /paddle/paddle/fluid/framework/operator.cc:1595)
[operator < assign > error]
说是Input Variable X(img_emb) is not initialized,但是我在input_data = fluid.data(name='img_emb', shape=input_shape, dtype=input_dtype)中initialized过了呀。
bug描述 Describe the Bug
通过paddle.jit.save将动态图模型转为静态图保存后,希望把它放入静态图推理代码框架(即建立program,run.exe那种推理格式)里进行推理,原因是想要和其他已在静态图框架下写好的程序及模型联动运行,没有找到符合需求的文档/教学,按照当前代码梳理了一份最小代码集,但是在执行时始终会报错,望协助解决:
运行后报错:
具体错误信息:
说是Input Variable X(img_emb) is not initialized,但是我在
input_data = fluid.data(name='img_emb', shape=input_shape, dtype=input_dtype)
中initialized过了呀。其他补充信息 Additional Supplementary Information
No response