目录
Slice切片
snpe报错 Node Slice_37: ERROR_SLICE_DYNAMIC_INPUTS: Invalid Slice inputs 164, only constant inputs supported
查看onnx节点
Invalid Slice inputs 4, only constant inputs supported
Slice切片
在神经网络中,Slice
操作通常用于从输入张量中提取一个子张量。这个操作需要指定开始的索引、结束的索引以及步长,然后从输入张量中提取一个子张量。这类似于Python中的切片操作。
例如,假设你有一个形状为[10, 20, 30]的张量,你可以使用Slice
操作来提取一个形状为[5, 10, 15]的子张量。
这个操作是非常基础的操作,因此在大多数情况下,你不能直接用其他操作来替换它。然而,你可能可以通过改变你的模型结构或者使用其他操作来实现相同的效果。这取决于你的具体模型和用例。
例如,如果你的模型中的Slice
操作只是用来提取输入张量的一部分,那么你可能可以通过改变输入数据来避免使用Slice
操作。也就是说,你可以在输入数据中只包含你需要的部分,这样你就不需要在模型中使用Slice
操作来提取子张量。
另一方面,如果Slice
操作是用来实现某种特定的功能,如切分特征图,那么你可能需要找到其他的方式来实现这个功能。这可能包括使用其他的操作,如Split
,或者改变你的模型结构。
import numpy as np
# 创建一个形状为[5, 5]的二维数组
arr = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]])
# 使用切片操作来提取一个子数组
sub_arr = arr[1:4, 2:5]
print(sub_arr)
Node Slice_37: ERROR_SLICE_DYNAMIC_INPUTS: Invalid Slice inputs 164, only constant inputs supported
这是是说Slice操作 支持constant inputs,不支持动态inputs
意思是支持 Slice,比如 sub_arr = arr[1:4, 2:5] ,不支持1 4 2 5 是动态变量
import onnx
from onnx import shape_inference
# Load the ONNX model
model = onnx.load(r"D:\yolov8s.onnx")
inferred_model = shape_inference.infer_shapes(model)
# Iterate over the nodes in the graph
for node in model.graph.node:
# Check if the node name is "Slice_37"
if node.name == "Slice_37":
# Print the node information
print("Node Name: ", node.name)
print("Op Type: ", node.op_type)
print("Inputs: ", node.input)
print("Outputs: ", node.output)
for attr in node.attribute:
print("Attribute: ", attr.name, " Value: ", attr)
for node in model.graph.node:
# Check if the node name is "Slice_37"
if node.output[0] == "164":
# Print the node information
print("Node Name: ", node.name)
print("Op Type: ", node.op_type)
print("Inputs: ", node.input)
print("Outputs: ", node.output)
for attr in node.attribute:
print("Attribute: ", attr.name, " Value: ", attr)
for node in inferred_model.graph.node:
# Check if the node name is "Slice_37"
if node.name == "Mul_36":
# Iterate over the inputs of the node
for input in node.input:
# Find the corresponding value info in the graph input or initializer
for value_info in inferred_model.graph.value_info:
if value_info.name == input:
print("Input: ", input)
for dim in value_info.type.tensor_type.shape.dim:
print("Dimension: ", dim.dim_value) # Iterate over the outputs of the node
# Iterate over the outputs of the node
for output in node.output:
# Find the corresponding value info in the graph output
for value_info in inferred_model.graph.value_info:
if value_info.name == output:
print("Output: ", output)
for dim in value_info.type.tensor_type.shape.dim:
print("Dimension: ", dim.dim_value)