-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphysical_shift.py
172 lines (161 loc) · 7.14 KB
/
physical_shift.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import numpy as np
import torch
def numpy2torch(x:np.ndarray):
if len(x.shape) == 2:
x = torch.tensor(x)
return torch.tensor(x).unsqueeze(0).unsqueeze(0)
if len(x.shape) == 3:
x = torch.tensor(x)
x = np.transpose(x, (2,0,1))
return torch.tensor(x).unsqueeze(0)
if len(x.shape) == 4:
x = np.transpose(x, (0, 3, 1, 2))
x = torch.tensor(x)
return x
else:
return torch.tensor(x)
class ShiftDirecttion:
UP_DOWN = 0
LEFT_RIGHT = 1
def get_shift(default_direction:ShiftDirecttion=ShiftDirecttion.LEFT_RIGHT):
# default_direction = ShiftDirecttion.LEFT_RIGHT
if default_direction == ShiftDirecttion.UP_DOWN:
def shift(inputs, step):
row, col, L = inputs.shape
new_row = row + abs(step) * (L - 1)
output = np.zeros((new_row, col , L))
for i in range(L):
if step < 0:
output[new_row-i*abs(step)-row:new_row-i*abs(step),:, i] = inputs[:, :, i]
else:
output[new_row - i * abs(step) - row:new_row - i * abs(step), :, L-i-1] = inputs[:, :, L-i-1]
return output
def shift_back(inputs, step):
row, col, L = inputs.shape
new_row = row - abs(step) * (L - 1)
output = np.zeros((new_row, col, L))
for i in range(L):
if step < 0:
output[:, :, i] = inputs[row-i*abs(step)-new_row:row-i*abs(step),:, i]
else:
output[:, :, L-i-1] = inputs[row - i * abs(step) - new_row:row - i * abs(step), :, L-i-1]
return output
def shift_torch(inputs, step=1,index_tensor=None,output=None):
"""
:param inputs: b,c,h,w x_cube
:param step: 1 down,-1 up
:param index_tensor: shift location index
:param output: output variable handle
:return: output
"""
[bs, nC, row, col] = inputs.shape
new_row = row + abs(step) * (nC - 1)
if output is None:
output = torch.zeros(bs, nC, new_row,col).cuda()
else:
output = output * 0
if index_tensor is None:
for i in [j for j in range(nC)]:
if step < 0:
output[:, i, new_row-i*abs(step)-row:new_row-i*abs(step), :] = inputs[:, i, :, :]
else:
output[:, nC-i-1, new_row-i*abs(step)-row:new_row-i*abs(step), :] = inputs[:, nC-i-1, :, :]
else:
output[index_tensor] = inputs.reshape(-1)
return output
def shift_back_torch(inputs, step=1,index_tensor=None,output=None):
"""
:param inputs: b,c,h,w x_cube
:param step: 1 down,-1 up
:param index_tensor: shift back location index
:param output: output variable handle
:return: output
"""
[bs, nC, row, col] = inputs.shape
new_row = row - abs(step) * (nC - 1)
if output is None:
output = torch.zeros(bs, nC, new_row, col).cuda()
else:
output = output * 0
if index_tensor is None:
for i in range(nC):
if step < 0:
output[:, i, :, :] = inputs[:, i, row-i*abs(step)-new_row:row-i*abs(step), :]
else:
output[:, nC-i-1, :, :] = inputs[:, nC-i-1, row - i * abs(step) - new_row:row - i * abs(step), :]
else:
return inputs[index_tensor].reshape(output.shape)
output[:,:,:,:] = inputs[index_tensor].reshape(output.shape)
return output
else:
#LEFT RIGHT
def shift(inputs, step):
row, col, L = inputs.shape
new_col = col + abs(step) * (L - 1)
output = np.zeros((row, new_col, L))
for i in range(L):
if step < 0:
output[:, new_col - i * abs(step) - col:new_col - i * abs(step), i] = inputs[:, :, i]
else:
output[:, new_col - i * abs(step) - col:new_col - i * abs(step), L - i - 1] = inputs[:, :, L - i - 1]
return output
def shift_back(inputs, step):
row, col, L = inputs.shape
new_col = col - abs(step) * (L - 1)
output = np.zeros((row, new_col, L))
for i in range(L):
if step < 0:
output[:, :, i] = inputs[:, col - i * abs(step) - new_col:col - i * abs(step), i]
else:
output[:, :, L - i - 1] = inputs[:, col - i * abs(step) - new_col:col - i * abs(step), L - i - 1]
return output
def shift_torch(inputs, step=1,index_tensor=None,output=None):
"""
:param inputs: b,c,h,w x_cube
:param step: 1 right,-1 left
:param index_tensor: shift location index
:param output: output variable handle
:return: output
"""
[bs, nC, row, col] = inputs.shape
new_col= col + abs(step) * (nC - 1)
if output is None:
output = torch.zeros(bs, nC, row,new_col).cuda()
else:
output = output * 0
if index_tensor is None:
for i in [j for j in range(nC)]:
if step < 0:
output[:, i, : ,new_col-i*abs(step)-col:new_col-i*abs(step)] = inputs[:, i, :, :]
else:
output[:, nC-i-1,:, new_col-i*abs(step)-col:new_col-i*abs(step)] = inputs[:, nC-i-1, :, :]
else:
output[index_tensor] = inputs.reshape(-1)
return output
def shift_back_torch(inputs, step=1, index_tensor=None, output=None):
"""
:param inputs: b,c,h,w x_cube
:param step: 1 right,-1 left
:param index_tensor: shift back location index
:param output: output variable handle
:return: output
"""
[bs, nC, row, col] = inputs.shape
# print("debug",bs, nC, row, col)
new_col = col - abs(step) * (nC - 1)
if output is None:
output = torch.zeros(bs, nC, row, new_col).cuda()
else:
output = output * 0
# print("debug output",output.shape)
# print("debug index", index_tensor[0].shape)
if index_tensor is None:
for i in range(nC):
if step < 0:
output[:, i, :, :] = inputs[:, i, : ,col-i*abs(step)-new_col:col-i*abs(step)]
else:
output[:, nC - i - 1, :, :] = inputs[:, nC-i-1,:, col-i*abs(step)-new_col:col-i*abs(step)]
else:
return inputs[index_tensor].reshape(output.shape)
return output
return shift,shift_back,shift_torch,shift_back_torch