torch.index_select(input_tensor, 切片維度, 切片索引)
注意:切完之后,轉onnx時會生成Gather節點;
torch自帶切片操作:
start : end : step:
范圍前閉后開,將其放在哪個維度上,就對那個維度起作用
torch.cat((a, b) , dim)
在已有的軸上拼接矩陣,默認軸為0,給定軸的維度可以不同,其余軸的維度必須相同
?
三個操作的組合使用例子如下:
import torchx = torch.randn(1, 18, 4, 4)# print("x:",x)
print("x.shape:",x.shape)indices_cls = torch.tensor([2, 5, 8, 11, 14, 17])
indices_point = torch.tensor([0,1, 3,4, 6,7, 9,10, 12,13, 15,16])kpt_point = torch.index_select(x, 1, indices_point)
kpt_cls = torch.index_select(x, 1, indices_cls)print("kpt_point.shape:",kpt_point.shape)
print("kpt_cls.shape:",kpt_cls.shape)x_2 = torch.cat([kpt_point[:,0:2:1,],kpt_cls[:,0:1:1,],kpt_point[:,2:4:1,],kpt_cls[:,1:2:1,],kpt_point[:,4:6:1,],kpt_cls[:,2:3:1,],kpt_point[:,6:8:1,],kpt_cls[:,3:4:1,],kpt_point[:,8:10:1,],kpt_cls[:,4:5:1,],kpt_point[:,10:12:1,],kpt_cls[:,5:6:1,]],1)# print("x_2:",x_2)
print("x_2.shape:",x_2.shape)
打印組合前后tensor的輸出形狀和內容發現,前后一致:
x.shape: torch.Size([1, 18, 4, 4])
kpt_point.shape: torch.Size([1, 12, 4, 4])
kpt_cls.shape: torch.Size([1, 6, 4, 4])
x_2.shape: torch.Size([1, 18, 4, 4])