How to swap x and y in pytorch?

Viewed 52

Given a output tensor as 100 * 6(xmin,ymin,xmax,ymax,conf,class),how can I get a tensor as 100 * 6(ymin,xmin,ymax,xmax,conf,class) in pytorch? For example, given a tensor

x = [[1,2,3,4,5,6],
     [7,8,9,10,11,12]], 

the desired results is

y = [[2,1,4,3,5,6],
     [8,7,10,9,11,12]]
2 Answers

I'm not completely sure about this, but try N6.T or N6.transpose?

You should point clearer for others to understand what is x and y, it can be confused with the axes. But I got your point, you want to swap ymin and xmin position and xmax, ymax position.

Therefore, the easiest way is creating a temporal tensor tempt_x and swap value by slicing.

with x = [[1,2,3,4,5,6],[7,8,9,10,11,12]]

>>> tempt_x = x.copy()
>>> x[:,0] = tempt_x[:,1]
>>> x[:,1] = tempt_x[:,0]
>>> x[:,2] = tempt_x[:,3]
>>> x[:,3] = tempt_x[:,2]
>>> x
array([[ 2,  1,  4,  3,  5,  6],
       [ 8,  7, 10,  9, 11, 12]])
Related