Caffe 模型#
import sys
sys.path.extend([".."])
import set_env
ROOT: /media/pc/data/lxw/ai/tvm-book
from tvm_book.tools.frontends import Frontend, TrainInputConfig
from tvm_book.tools import display
caffe 前端模型配置:
print(display.Tree("| ")("models/caffe_demo"))
| caffe_demo
| config.toml
| test.caffemodel
| test.prototxt
caffe_demo/
文件夹下存在如下内容:
test.caffemodel
存储 caffe 模型参数的初始化模型test.prototxt
存储 caffe 模型结构config.toml
存储 caffe 模型配置信息[[infer_inputs]] # 推理阶段输入数据信息 name = "input.1" shape = [ 1, 192, 160, 3,] dtype = "uint8" layout = "nhwc" mean = [ 0,] std = [ 256,] [[train_inputs]] # 训练或者量化校准阶段输入数据信息 name = "input.1" shape = [ 1, 3, 192, 160,] dtype = "float32" layout = "nchw" [model] # 前端模型配置信息 model_type = "caffe" # 前端模型框架或者类型 init_net_path = "test.caffemodel" # caffe_pb2.NetParameter,caffemodel,即 caffe 模型参数 predict_net_path = "test.prototxt" # caffe_pb2.NetParameter,caffe prototxt,即 caffe 模型结构
from pathlib import Path
import toml
cfg_path = "models/caffe_demo/config.toml"
cfg_path = Path(cfg_path)
config = toml.load(cfg_path)
frontend = Frontend(config["model"]["model_type"])
if len(config["train_inputs"]) == 1: # "此模型为单输入模型"
input_config = TrainInputConfig(**config["train_inputs"][0])
shape_dict = {input_config.name: input_config.shape}
dtype_dict = {input_config.name: input_config.dtype}
model = frontend.load(
f"{cfg_path.parent}/{config['model']['init_net_path']}",
predict_net_path=f"{cfg_path.parent}/{config['model']['predict_net_path']}",
shape_dict=shape_dict,
dtype_dict=dtype_dict
)
import tvm
from tvm import relay
with tvm.transform.PassContext(opt_level=3, disabled_pass={"AlterOpLayout"}):
mod = relay.quantize.prerequisite_optimize(model.mod, model.params)
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
The Relay type checker is unable to show the following types match:
Tensor[(1), float32]
Tensor[(64), float32]
In particular:
dimension 0 conflicts: 1 does not match 64.
The Relay type checker is unable to show the following types match.
In particular `Tensor[(64), float32]` does not match `Tensor[(1), float32]`
---------------------------------------------------------------------------
DiagnosticError Traceback (most recent call last)
Cell In[5], line 5
2 from tvm import relay
4 with tvm.transform.PassContext(opt_level=3, disabled_pass={"AlterOpLayout"}):
----> 5 mod = relay.quantize.prerequisite_optimize(model.mod, model.params)
File /media/pc/data/lxw/ai/tvm/python/tvm/relay/quantize/quantize.py:329, in prerequisite_optimize(mod, params)
326 if params:
327 mod["main"] = _bind_params(mod["main"], params)
--> 329 mod = optimize(mod)
330 return mod
File /media/pc/data/lxw/ai/tvm/python/tvm/ir/transform.py:238, in Pass.__call__(self, mod)
224 def __call__(self, mod):
225 """Execute the pass. Note that for sequential pass, the dependency among
226 different passes will be resolved in the backend.
227
(...)
236 The updated module after applying this pass.
237 """
--> 238 return _ffi_transform_api.RunPass(self, mod)
File /media/pc/data/lxw/ai/tvm/python/tvm/_ffi/_ctypes/packed_func.py:240, in PackedFuncBase.__call__(self, *args)
228 ret_tcode = ctypes.c_int()
229 if (
230 _LIB.TVMFuncCall(
231 self.handle,
(...)
238 != 0
239 ):
--> 240 raise_last_ffi_error()
241 _ = temp_args
242 _ = args
File /media/pc/data/lxw/ai/tvm/python/tvm/_ffi/base.py:481, in raise_last_ffi_error()
475 # The exception PyObject may contain a large amount of state,
476 # including all stack frames that may be inspected in a later
477 # PDB post-mortem. Therefore, we must make sure to remove the
478 # underlying PyObject* from the C++ side after we retrieve it.
479 _LIB.TVMDropLastPythonError()
--> 481 raise py_err
DiagnosticError: Traceback (most recent call last):
8: tvm::runtime::PackedFuncObj::Extractor<tvm::runtime::PackedFuncSubObj<tvm::runtime::TypedPackedFunc<tvm::IRModule (tvm::transform::Pass, tvm::IRModule)>::AssignTypedLambda<tvm::transform::__mk_TVM9::{lambda(tvm::transform::Pass, tvm::IRModule)#1}>(tvm::transform::__mk_TVM9::{lambda(tvm::transform::Pass, tvm::IRModule)#1}, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)::{lambda(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*)#1}> >::Call(tvm::runtime::PackedFuncObj const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, tvm::runtime::TVMRetValue)
7: tvm::transform::Pass::operator()(tvm::IRModule) const
6: tvm::transform::Pass::operator()(tvm::IRModule, tvm::transform::PassContext const&) const
5: tvm::transform::SequentialNode::operator()(tvm::IRModule, tvm::transform::PassContext const&) const
4: tvm::transform::Pass::operator()(tvm::IRModule, tvm::transform::PassContext const&) const
3: tvm::transform::ModulePassNode::operator()(tvm::IRModule, tvm::transform::PassContext const&) const
2: _ZN3tvm7runtime13PackedFuncObj
1: tvm::runtime::TypedPackedFunc<tvm::IRModule (tvm::IRModule, tvm::transform::PassContext)>::AssignTypedLambda<tvm::relay::transform::InferType()::{lambda(tvm::IRModule, tvm::transform::PassContext const&)#1}>(tvm::relay::transform::InferType()::{lambda(tvm::IRModule, tvm::transform::PassContext const&)#1})::{lambda(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*)#1}::operator()(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*) const
0: tvm::DiagnosticContext::Render()
File "/media/pc/data/lxw/ai/tvm/src/ir/diagnostic.cc", line 131
DiagnosticError: one or more error diagnostics were emitted, please check diagnostic render for output.