Project scenario:
The python model is converted to onnx model and can be exported successfully, but the following errors occur when loading the model using onnxruntime
InvalidGraph: [ONNXRuntimeError] : 10 : INVALID_ GRAPH : Load model from T.onnx failed:This is an invalid model. Type Error: Type ‘tensor(bool)’ of input parameter (8) of operator (ScatterND) in node (ScatterND_ 15) is invalid.
Problem Description:
import torch
import torch.nn as nn
import onnxruntime
from torch.onnx import export
class Preprocess(nn.Module):
def __init__(self):
super().__init__()
self.max = 1000
self.min = -44
def forward(self, inputs):
inputs[inputs>self.max] = self.max
inputs[inputs<self.min] = self.min
return inputs
x = torch.randint(-1024,3071,(1,1,28,28))
model = Preprocess()
model.eval()
export(
model,
x,
"test.onnx",
input_names=["input"],
output_names=["output"],
opset_version=11,
)
session = onnxruntime.InferenceSession("test.onnx")
Cause analysis:
The same problem can be found in GitHub of pytorch #34054
Solution:
The specific operations are as follows: Mr. Cheng mask
, and then use torch.masked_ Fill()
operation. Instead of using the index to directly assign the entered tensor
class MaskHigh(nn.Module):
def __init__(self, val):
super().__init__()
self.val = val
def forward(self, inputs):
x = inputs.clone()
mask = x > self.val
output = torch.masked_fill(inputs, mask, self.val)
return output
class MaskLow(nn.Module):
def __init__(self, val):
super().__init__()
self.val = val
def forward(self, inputs):
x = inputs.clone()
mask = x < self.val
output = torch.masked_fill(inputs, mask, self.val)
return output
class Clip(nn.Module):
def __init__(self):
super().__init__()
self.high = MaskHigh(1300)
self.low = MaskLow(-44)
def forward(self, inputs):
output = self.high(inputs)
output = self.low(output)
return output
Netron can be used to visualize the calculation diagram generated by the front and rear methods
Index assignment
Read More:
- Pytorch Loading model error: RuntimeError: Error(s) in loading state_dict for Model: Missing key(s) in state_dict
- pytorch RuntimeError: Error(s) in loading state_ Dict for dataparall… Import model error solution
- [Solved] PyTorch Load Model Error: Missing key(s) RuntimeError: Error(s) in loading state_dict for
- [Solved] YOLOv5 Model training error: TypeError: new(): invalid data type ‘str’
- `Model.XXX` is not supported when the `Model` instance was constructed with eager mode enabled
- [Solved] Networkx Error: Attributeerror: ‘graph’ object has no attribute ‘node’
- [Solved] Operator Not Allowed In Graph Error & Attribute Error Tensor object has no attribute numpy
- The lenet model trained by Python failed to predict its own handwritten pictures
- How to Solve keras load_model() Error
- pytorch model.load_state_dict Error [How to Solve]
- To solve the problem that the loss of verification set of resnet50 pre-training model remains unchanged
- [Solved] HTTPError: 404 Client Error: Not Found for url: https://huggingface.co/saved_model
- [Solved] Model training Error: _pickle.PicklingError: Can’t pickle
- [Solved] module ‘numpy.random‘ has no attribute ‘default_rng‘ gensim.model
- [Solved] RuntimeError: Error(s) in loading state_dict for Net:
- YOLOX Model conversion error: [TensorRT] ERROR: runtime.cpp (25) – Cuda Error in allocate: 2 (out of memory)
- [Solved] Error(s) in loading state_dict for GeneratorResNet
- Mxnet Export onnx Symbol and params files provided are invalid
- [Solved] RuntimeError: Error(s) in loading state_dict for BertForTokenClassification
- [Solved] Pytorch Error: RuntimeError: Error(s) in loading state_dict for Network: size mismatch