-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmultiscanning strategy tool.py
132 lines (110 loc) · 3.33 KB
/
multiscanning strategy tool.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
import numpy as np
import torch
from einops import rearrange
def direction1(input_HW):
h = input_HW.shape[0]
w = input_HW.shape[1]
result = []
for i in range(h):
if (i + 1) % 2 == 1:
result.extend(input_HW[i])
else:
result.extend(input_HW[i][::-1])
return result
def direction2(input_HW):
h = input_HW.shape[0]
w = input_HW.shape[1]
result = []
for i in range(w):
if (i + 1) % 2 == 1:
result.extend(input_HW[:, i])
else:
result.extend(input_HW[:, i][::-1])
return result
def direction3(input_HW):
h = input_HW.shape[0]
w = input_HW.shape[1]
result = []
for i in range(h):
if (i + 1) % 2 == 0:
result.extend(input_HW[h - 1 - i])
else:
result.extend(input_HW[h - 1 - i][::-1])
return result
def direction4(input_HW):
h = input_HW.shape[0]
w = input_HW.shape[1]
result = []
for i in range(w):
if (i + 1) % 2 == 0:
result.extend(input_HW[:, w - 1 - i])
else:
result.extend(input_HW[:, w - 1 - i][::-1])
return result
def direction5(input_HW):
h = input_HW.shape[0]
w = input_HW.shape[1]
result = []
for i in range(h):
if (i + 1) % 2 == 0:
result.extend(input_HW[i])
else:
result.extend(input_HW[i][::-1])
return result
def direction6(input_HW):
h = input_HW.shape[0]
w = input_HW.shape[1]
result = []
for i in range(w):
if (i + 1) % 2 == 1:
result.extend(input_HW[:, w - 1 - i])
else:
result.extend(input_HW[:, w - 1 - i][::-1])
return result
def direction7(input_HW):
h = input_HW.shape[0]
w = input_HW.shape[1]
result = []
for i in range(h):
if (i + 1) % 2 == 1:
result.extend(input_HW[h - 1 - i])
else:
result.extend(input_HW[h - 1 - i][::-1])
return result
def direction8(input_HW):
h = input_HW.shape[0]
w = input_HW.shape[1]
result = []
for i in range(w):
if (i + 1) % 2 == 0:
result.extend(input_HW[:, i])
else:
result.extend(input_HW[:, i][::-1])
return result
method_map = {1: direction1, 2: direction2, 3: direction3, 4: direction4, 5: direction5, 6: direction6, 7: direction7,
8: direction8}
# img_test: batch size,H,W,C
# method_index:方法下标:1-8
def multiscan(img_test, method_index):
input = img_test.numpy()
batchsize = input.shape[0]
h = img_test.shape[1]
w = input.shape[2]
c = input.shape[3]
# print(h, w, c)
sample_all = []
for i in range(batchsize):
sample_pic = []
for j in range(c):
sample_pic.append(method_map[method_index](input[i][:, :, j]))
sample_all.append(sample_pic)
sample_all_array = np.array(sample_all)
sample_all_torch = torch.from_numpy(sample_all_array)
sample_all_rearrange = rearrange(sample_all_torch, 'b c s -> b s c')
return sample_all_rearrange
# batchsize:2 H:2 w:3 c:2
img_test = torch.randn(1, 5, 5, 2)
print(img_test.shape, img_test)
result = multiscan(img_test, 4)
print(result.shape, result)
print("trans shape:", result.shape)