-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvironment_Scale_Size.py
224 lines (142 loc) · 5.35 KB
/
Environment_Scale_Size.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import matplotlib.pyplot as plt
from data_process import DataProcess
import os
import numpy as np
import random
r = random.random
random.seed(1)
A_0_bound = 35.0
A_1_bound = 30.0
A_1_bias = 45.0
Color_List = ['r', 'g', 'b', 'y', 'w']
train_img_list = os.listdir('train_data/images')
test_img_list = os.listdir('test_data/images')
train_img_num = len(train_img_list)
test_img_num = len(test_img_list)
print('train img num', train_img_num)
print('train img list', train_img_list)
print('test img num', test_img_num)
print("\033[1;34;34mInput\033[0m")
print("\033[4;31;31mInput\033[0m")
class Environment(object):
def __init__(self, is_train=True):
self.reward = 0
self.stop_step = 17
self.step_count = 0
self.start_point = [125, 0]
self.img_idx = 0
self.plt_show = False
self.data_set = 'Train set'
if is_train:
self.is_train = True
self.img_num = train_img_num
self.data_process = DataProcess(
label_path='train_data/labels/',
img_path='train_data/images/',
resized_W=250,
resized_H=750,
patch_size=100,
mask_size=50)
else:
self.is_train = False
self.img_num = test_img_num
print('\n * * * * * * * * * * * * Test Set * * * * * * * * * * * * * *')
self.data_process = DataProcess(
label_path='test_data/labels/',
img_path='test_data/images/',
resized_W=250,
resized_H=750,
patch_size=100,
mask_size=50)
def reset(self, img_idx=0):
"""
:param img_idx: the img_idx
:return: start_point: p_0
img_patch_0: patch_0
"""
if self.is_train:
self.data_set = 'Train Set'
self.img_idx = img_idx
else:
self.data_set = 'Test Set'
self.img_idx = test_img_list[img_idx]
print('\nEnv reset ! ' + self.data_set + ' Img ID: {0}'.format(self.img_idx))
start_point = self.start_point
state_patch = self.data_process.make_state(self.img_idx, start_point)
return start_point, state_patch
def step(self, a, p):
"""
s_, r, done = env.step(a) p --- a ---> p_
:param a:
:return:
"""
action = a[0]
action_x = action[0] * A_0_bound
action_y = action[1] * A_1_bound + A_1_bias
p_ = np.zeros((2,), dtype='float32')
p_[0] = p[0] + action_x
p_[1] = p[1] + action_y
if p_[0] <= 0:
p_[0] = 0.0
elif p_[0] > 245.0:
p_[0] = 245.0
if p_[1] > 745:
p_[1] = 745.0
state_ = self.data_process.make_state(self.img_idx, p_)
return p_, state_
def make_s_input(self, s):
"""
process the state data information
:param s: the output of make_state() (1, 400, 400, 2) uint8
:return: s_input : data input to the network (400, 400, 2) float32
"""
s_scale = s.astype('float32') / 255.0 # (1, 400, 400, 2) float32
s_input = s_scale[0, :, :, :] # (400, 400, 2) float32
return s_input
def get_all_17_label(self):
all_17_points_17 = self.data_process.make_point(self.img_idx)
return all_17_points_17
def get_label_i(self, vb_idx):
'''
get label center point of the vb_idx th VB of current img
:param vb_idx: the VB index
:return: Label: center label of the vb_idx th VB : point_i
'''
all_17_points = self.data_process.make_point(self.img_idx)
point_i = all_17_points[vb_idx]
plt_img = False
if plt_img:
fig3 = plt.figure(3)
_, img = self.data_process.load_img(self.img_idx)
plt.imshow(img)
# plt all 17 VB center points
for vb_idx in range(17):
plt.plot(all_17_points[vb_idx, 0], all_17_points[vb_idx, 1], 'ro-', markersize=2)
# plt current VB points
plt.plot(point_i[0], point_i[1], 'bo-', markersize=4)
print('\npoint_i: ', point_i)
return point_i
def get_landmarks_i(self, vb_idx):
landmarks = self.data_process.make_label_scaled(self.img_idx) # 68 points range: 0-1 [68, 2]
landmark_i = landmarks[4 * vb_idx: 4*(vb_idx+1), :] # 4 points range : 0-1 [4, 2]
landmark_i[:, 0] = landmark_i[:, 0] * 250
landmark_i[:, 1] = landmark_i[:, 1] * 750
return landmark_i
def get_all_landmarks(self):
landmarks = self.data_process.make_label_scaled(self.img_idx) # 68 points range: 0-1 [68, 2]
landmarks[:, 0] = landmarks[:, 0] * 250
landmarks[:, 1] = landmarks[:, 1] * 750
return landmarks
def get_patch_i(self, vb_idx):
'''
get label img patch
:param vb_idx:
:return:
'''
point_i = self.get_label_i(vb_idx)
img_patch_i = self.data_process.make_patch_with_point(self.img_idx, point_i)
fig4 = plt.figure(4)
plt.imshow(img_patch_i[0, :, :, :])
def cal_reward(self, p_gt, p_pre):
reward = p_gt - p_pre
return reward