-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
126 lines (100 loc) · 3.31 KB
/
test.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
import torch
import numpy as np
import matplotlib.pyplot as plt
t = torch.tensor([[[[0.0,0,1],
[0,10,0],
[0,0,1]],
[[1,0,3],
[3,0,0],
[2,0,10]],
[[0,0,0],
[0,100,0],
[0,0,0]]
]])
print(t[0].eq(231))
t[0,t[0].eq(231)] = 9999
print(t)
exit()
t = torch.tensor([[[[0.0,0,1],
[0,10,0],
[0,0,1]],
[[1,0,3],
[3,0,0],
[2,0,10]],
[[0,0,0],
[0,100,0],
[0,0,0]]
]])
print(torch.softmax(t[0,[0,1]],dim=1))
exit()
# Predefined sequence of offsets in a clockwise order starting from [1, 0]
dir_kernel = torch.tensor([[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]])
def produce_order(kernel_len):
order = []
ind = 0
i = 0
while i < kernel_len:
order.append(ind)
if ind > 0:
ind = -ind
else:
ind = (-ind + 1)
i += 1
return order
dir_order = produce_order(dir_kernel.shape[0])
print(dir_order)
kernel_len = len(dir_kernel)
rot = 3
max_act_i = 2
# Set up the plot
fig, ax = plt.subplots()
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
import matplotlib.cm as cm
# Plot the origin
ax.plot(0, 0, 'ok') # 'ok' plots a black dot
colors = cm.rainbow(np.linspace(0, 1, dir_kernel.shape[0]))
print(f"Rot: {rot}, Dir: {dir_kernel[rot]}")
print(f"max_act_i: {max_act_i}, max_act_rot: {(rot + dir_order[max_act_i]) % dir_kernel.shape[0]}," +
f"max_act_dir: {dir_kernel[(rot + dir_order[max_act_i]) % dir_kernel.shape[0]]}")
for i in range(dir_kernel.shape[0]):
ind = (rot+dir_order[i]) % dir_kernel.shape[0]
print(f"i {i}, dir_ind: {ind}, dir: {dir_kernel[ind]}")
dx, dy = dir_kernel[ind].numpy()
color = colors[i]
ax.arrow(0, 0, dx, dy, head_width=0.1, head_length=0.2, fc=color, ec=color)
ax.text(dx * 1.1, dy * 1.1, str(i), color='black')
plt.grid(True)
plt.show()
exit()
# Function to map a given direction to its starting index in the offset_sequence
def get_starting_index_for_direction(direction):
# Calculate angle for the given direction
angle = np.arctan2(direction[1], direction[0])
# Normalize the angle to be within [0, 2*pi)
if angle < 0:
angle += 2 * np.pi
# Each segment in the circle corresponds to 45 degrees (pi/4 radians)
# Calculate the index by dividing the angle by the segment size
index = int(round(4 * angle / np.pi)) % len(offset_sequence)
return index
def get_alternating_offsets(offsets):
i = 0
new_offsets = []
ind = 0
while i < len(offsets):
new_offsets.append(offsets[ind])
if ind >= 0:
ind = -(ind + 1)
else:
ind = -ind
i += 1
return new_offsets
new_dir = [-1,-1]
starting_index = get_starting_index_for_direction(new_dir)
def rotate_sequence(starting_index, sequence):
return sequence[starting_index:] + sequence[:starting_index]
rotated_sequence = rotate_sequence(starting_index, offset_sequence)
print(offset_sequence)
print(f"new_dir: {new_dir},\nstarting_index: {starting_index},\nrotated_sequence: {rotated_sequence}")
print(get_alternating_offsets(rotated_sequence))