-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrainer_step1.py
192 lines (172 loc) · 7.11 KB
/
trainer_step1.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
import random
#import time
import pybullet as p
import numpy as np
import sim_class
from PIL import Image
import concurrent.futures
import tool
from sys import argv
import gc
#import multiprocessing
loop_id = argv[1]
comp_id = argv[2]
example_number_need_collect = int(argv[3])
#loop_id = 3
#comp_id = 'l'
#example_number_need_collect = 1
img_save_dir = './data'+str(loop_id)+'/train/input/'
label_save_dir = img_save_dir.replace("input", "label")
state_save_dir = img_save_dir.replace("input", "state")
random_para_save_dir = img_save_dir.replace("input", "random_para")
tqdm_dir = img_save_dir.replace("input", "tqdm_p")
predict_save_dir = img_save_dir.replace("input", "predict_save")
seg_map_dir = img_save_dir.replace("input", "seg_save")
sec_input_dir = img_save_dir.replace("input", "sec_input")
tool.create_dir_not_exist(img_save_dir)
tool.create_dir_not_exist(label_save_dir)
tool.create_dir_not_exist(state_save_dir)
tool.create_dir_not_exist(random_para_save_dir)
tool.create_dir_not_exist(tqdm_dir)
tool.create_dir_not_exist(predict_save_dir)
tool.create_dir_not_exist(seg_map_dir)
tool.create_dir_not_exist(sec_input_dir)
img_save_dir = img_save_dir+str(comp_id)
label_save_dir = label_save_dir +str(comp_id)
state_save_dir = state_save_dir+str(comp_id)
random_para_save_dir = random_para_save_dir +str(comp_id)
tqdm_dir = tqdm_dir+str(comp_id)
predict_save_dir = predict_save_dir+str(comp_id)
seg_map_dir = seg_map_dir+str(comp_id)
sec_input_dir = sec_input_dir+str(comp_id)
#%%
image_pixel_before = 320
image_pixel_after = 240
#%%
def custom_method_saveimg(floder_id):
np.random.seed()
object_type = random.randint(0,1)
if object_type == 0:
object_path = './objurdf/duomi/duomi.urdf'
num_obj = 140+random.randint(-5,10)
mass_random = random.uniform(0.005,0.0065)
elif object_type == 1:
object_path = './objurdf/cy/cy.urdf'
num_obj = 150+random.randint(-5,10)
mass_random = random.uniform(0.004,0.005)
elif object_type == 2:
object_path = './objurdf/sj/sj.urdf'
num_obj = 90+random.randint(-10,10)
#%%
GUI = False
yaw_times = 6
aps = 4
pitch_times = 3
roll_times = 3
fl_times = 4
EyePosition=[0,0,0.46+random.uniform(-0.01,0.01)]
TargetPosition=[0,0,0]
fov_d = 69.25+random.uniform(-0.25,0.25)
near = 0.001
far = EyePosition[2]+0.05
state_save_path = state_save_dir+str(int(floder_id))+'.bullet'
robotStartOrn = p.getQuaternionFromEuler([0, 0, 0])
lateralFriction_random = random.uniform(0.25,0.35)
globalScaling_random = random.uniform(0.95,1)
#%%
random_para=[]
random_para.append(GUI)
random_para.append(num_obj)
random_para.append(yaw_times)
random_para.append(EyePosition)
random_para.append(TargetPosition)
random_para.append(fov_d)
random_para.append(near)
random_para.append(far)
random_para.append(state_save_path)
random_para.append(object_path)
random_para.append(aps)
random_para.append(pitch_times)
random_para.append(roll_times)
random_para.append(fl_times)
#%%
rot_step_size = 360 / yaw_times
y_ws = np.array([rot_step_size * i for i in range(yaw_times)]).tolist()
# 0: 0.02 1:0.03 2:0.04
ap_ws = [0, 1, 2, 3]
p_ws = [0,10,20]
r_ws = [0,-10,10]
fl_ws = [0,1,2,3]
selected_yaw = random.sample(y_ws, 5)
selected_pitch = random.sample(p_ws, 3)
selected_roll= random.sample(r_ws, 3)
selected_ap= random.sample(ap_ws, 3)
selected_fl = random.sample(fl_ws, 3)
random_para.append(selected_yaw)
random_para.append(selected_pitch)
random_para.append(selected_roll)
random_para.append(selected_ap)
random_para.append(selected_fl)
random_para.append(mass_random)
random_para.append(lateralFriction_random)
random_para.append(globalScaling_random)
random_para.append(object_type)
random_para = np.array(random_para,dtype=object)
np.save(random_para_save_dir+str(floder_id)+'.npy',random_para)
#%%
#_init_ sim_env
sim = sim_class.Sim(state_save_path, num_obj, GUI, image_pixel_before,
EyePosition,TargetPosition,fov_d,far,near,
robotStartOrn,object_path,mass_random,
lateralFriction_random,globalScaling_random)
#build env_sim
sim.build_e()
#render
rgbImg, depthImg, segImg = sim.render()
img_d, float_depth, poke_pos_map = sim.after_render()
img_d[np.where(segImg==0)] = 255
#%%
for yt in selected_yaw:
for pt in selected_pitch:
for rt in selected_roll:
for ap_ind in selected_ap:
for fl_ind in selected_fl:
#grasp_paras = np.zeros(5).reshape(1,1,5)
#grasp_paras[0][0][0]=ap_ind
#grasp_paras[0][0][1]=yt
#grasp_paras[0][0][2]=pt
#grasp_paras[0][0][3]=rt
#grasp_paras[0][0][4]=fl_ind
#grasp_paras_save_path = sec_input_dir+'num_'+str(floder_id)+'_yaw_'+str(int(yt)) \
#+'_ap_'+str(int(ap_ind))+'_pitch_'+str(int(pt)) \
#+'_roll_'+str(int(rt))+'_fl_'+str(int(fl_ind))+'.npy'
#np.save(grasp_paras_save_path, grasp_paras.astype(np.int32))
#
tmp_img_d = img_d.copy()
tmp_img_d = tmp_img_d.astype(np.uint8)
tmp_img_d = Image.fromarray(tmp_img_d)
tmp_img_d = tmp_img_d.rotate(angle=int(yt), fillcolor = (255,255,255))
tmp_img_d_save_path = img_save_dir+'num_'+str(floder_id)+'_yaw_'+str(int(yt)) \
+'_ap_'+str(int(ap_ind))+'_pitch_'+str(int(pt)) \
+'_roll_'+str(int(rt))+'_fl_'+str(int(fl_ind))+'.png'
tmp_img_d.save(tmp_img_d_save_path)
tmp_seg = segImg.copy()
tmp_seg = tmp_seg.astype(np.uint8)
tmp_seg = Image.fromarray(tmp_seg)
tmp_seg = tmp_seg.rotate(angle=int(yt), fillcolor = (0))
tmp_seg_save_path = seg_map_dir+'num_'+str(floder_id)+'_yaw_'+str(int(yt)) \
+'_ap_'+str(int(ap_ind))+'_pitch_'+str(int(pt)) \
+'_roll_'+str(int(rt))+'_fl_'+str(int(fl_ind))+'.png'
tmp_seg.save(tmp_seg_save_path)
p.disconnect()
gc.collect()
if __name__ == '__main__':
# print(time.localtime(time.time()))
with concurrent.futures.ProcessPoolExecutor() as executor:
futures = [executor.submit(custom_method_saveimg,floder_id) for floder_id in range(example_number_need_collect)]
for future in concurrent.futures.as_completed(futures):
try:
print(future.result())
except Exception as exc:
print(f'Generated an exception: {exc}')
# print(time.localtime(time.time()))