Upload vmodel.py
Browse files
vmodel.py
ADDED
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
import torch.nn.functional as F
|
5 |
+
from regress_module import VisProcess, VisTR, VisRes
|
6 |
+
from torchvision import transforms
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
import sys
|
9 |
+
sys.path.append('/home/kejianshi/Desktop/Surgical_Robot/science_robotics/stateregress_back/utils')
|
10 |
+
from general_utils import AttrDict
|
11 |
+
# sys.path.append('/home/kejianshi/Desktop/Surgical_Robot/science_robotics/ar_surrol/surrol/tasks')
|
12 |
+
sys.path.append('/home/kejianshi/Desktop/Surgical_Robot/science_robotics/ar_surrol/surrol/tasks')
|
13 |
+
from depth_anything.dpt import DepthAnything
|
14 |
+
from depth_anything.util.transform import Resize, NormalizeImage, PrepareForNet
|
15 |
+
|
16 |
+
|
17 |
+
class vismodel(nn.Module):
|
18 |
+
def __init__(
|
19 |
+
self,
|
20 |
+
opts
|
21 |
+
|
22 |
+
):
|
23 |
+
super().__init__()
|
24 |
+
self.opts=opts
|
25 |
+
self.device=opts.device
|
26 |
+
|
27 |
+
self.img_size=self.opts.img_size
|
28 |
+
self.obj_num=1
|
29 |
+
self.v_processor=VisRes()
|
30 |
+
if not self.opts.use_exist_depth:
|
31 |
+
self._load_dam()
|
32 |
+
|
33 |
+
def _load_dam(self):
|
34 |
+
encoder = 'vitb' # can also be 'vitb' or 'vitl'
|
35 |
+
self.depth_anything = DepthAnything.from_pretrained('LiheYoung/depth_anything_{:}14'.format(encoder)).eval()
|
36 |
+
#self.depth_anything.to(self.device)
|
37 |
+
|
38 |
+
def _get_depth_with_dam(self, img):
|
39 |
+
'''
|
40 |
+
input: rgb image 1xHxW
|
41 |
+
'''
|
42 |
+
|
43 |
+
#img=self.img_transform({'image': img})['image']
|
44 |
+
#img=torch.from_numpy(img).unsqueeze(0)
|
45 |
+
#img=transforms.Resize((518,518))(img)
|
46 |
+
with torch.no_grad():
|
47 |
+
depth = self.depth_anything(img)
|
48 |
+
|
49 |
+
#print(depth.shape)
|
50 |
+
depth = F.interpolate(depth[None], self.img_size, mode='bilinear', align_corners=False)[0]
|
51 |
+
depth_min = torch.amin(depth, dim=(1, 2), keepdim=True)
|
52 |
+
depth_max = torch.amax(depth, dim=(1, 2), keepdim=True)
|
53 |
+
depth = (depth - depth_min) / (depth_max - depth_min)
|
54 |
+
|
55 |
+
return depth
|
56 |
+
|
57 |
+
|
58 |
+
def normlize_angles(self, x):
|
59 |
+
return np.arctan2(np.sin(x),np.cos(x))
|
60 |
+
|
61 |
+
def get_action(self, state, noise=False, v_action=False):
|
62 |
+
#print("get_action")
|
63 |
+
|
64 |
+
if not v_action:
|
65 |
+
return super().get_action(state, noise)
|
66 |
+
#self.count+=1
|
67 |
+
|
68 |
+
self.v_processor.eval()
|
69 |
+
|
70 |
+
rgb=self.to_torch(state['depth']).unsqueeze(0)
|
71 |
+
|
72 |
+
depth=self._get_depth_with_dam(rgb)[0] #tensor
|
73 |
+
depth_norm=self.depth_norm.normalize(depth.reshape(-1,256*256),device=self.device).reshape(1,256,256)
|
74 |
+
seg=self.to_torch(transitions['seg'])
|
75 |
+
seg_d=torch.concat((seg,depth_norm))
|
76 |
+
|
77 |
+
inputs=seg_d.unsqueeze(0).float().to(self.device) # B 2 256 256
|
78 |
+
#print(inputs.shape)
|
79 |
+
|
80 |
+
with torch.no_grad():
|
81 |
+
v_output=self.v_processor(inputs).squeeze() # 9
|
82 |
+
|
83 |
+
#v_save=v_output.cpu().data.numpy()
|
84 |
+
#np.save('test_record/v_output.npy', v_save)
|
85 |
+
|
86 |
+
o, g = state['observation'], state['desired_goal']
|
87 |
+
g=self.g_norm.normalize(g)
|
88 |
+
#print("g: ",g)
|
89 |
+
g_norm=torch.tensor(g).float().to(self.device)
|
90 |
+
#print("g_norm: ",g_norm)
|
91 |
+
|
92 |
+
if not self.regress_rbt_staet:
|
93 |
+
robot_state=torch.tensor(o[:7]).to(self.device)
|
94 |
+
#pos=v_output[:3]
|
95 |
+
#rel_pos=pos-robot_state[:3]
|
96 |
+
rel_pos=v_output[:3*self.obj_num]
|
97 |
+
new_pos=robot_state[:3]+rel_pos[:3]
|
98 |
+
|
99 |
+
if self.obj_num>1:
|
100 |
+
for i in range(1, self.obj_num):
|
101 |
+
pos=robot_state[:3]+rel_pos[i*3:3*self.obj_num]
|
102 |
+
new_pos=torch.concat((new_pos,pos))
|
103 |
+
|
104 |
+
waypoint_pos_rot=v_output[3*self.obj_num:]
|
105 |
+
|
106 |
+
|
107 |
+
#o=torch.from_numpy(np.concatenate([o[:,7:10].copy(),o[:,13:19]].copy(),axis=1)).to(self.device)
|
108 |
+
|
109 |
+
#o_new=torch.concat((,o),dim=1) # B 19
|
110 |
+
o_new=torch.concat((robot_state, new_pos))
|
111 |
+
o_new=torch.concat((o_new, rel_pos))
|
112 |
+
o_new=torch.concat((o_new, waypoint_pos_rot))
|
113 |
+
o_norm=self.o_norm.normalize(o_new,device=self.device)
|
114 |
+
else:
|
115 |
+
o_norm=self.o_norm.normalize(v_output,device=self.device)
|
116 |
+
o_norm=torch.tensor(o_norm).float().to(self.device)
|
117 |
+
|
118 |
+
input_tensor=torch.concat((o_norm, g_norm), axis=0).to(torch.float32)
|
119 |
+
#save_input=input_tensor.cpu().data.numpy()
|
120 |
+
#np.save('test_record/actor_input.npy', save_input)
|
121 |
+
#exit()
|
122 |
+
|
123 |
+
action = self.actor(input_tensor).cpu().data.numpy().flatten()
|
124 |
+
|
125 |
+
self.v_processor.train()
|
126 |
+
return action
|
127 |
+
|
128 |
+
|
129 |
+
def forward(self, seg, v_gt):
|
130 |
+
# if self.opts.use_exist_depth:
|
131 |
+
# d=rgb
|
132 |
+
# else:
|
133 |
+
# d=self._get_depth_with_dam(rgb)
|
134 |
+
|
135 |
+
# seg_d=torch.concat((seg.unsqueeze(1),seg.unsqueeze(1)),dim=1)
|
136 |
+
seg_d = torch.unsqueeze(seg, 1)
|
137 |
+
# print(seg_d.shape)
|
138 |
+
output=self.v_processor(seg_d)
|
139 |
+
|
140 |
+
|
141 |
+
#print('output: ',type(output))
|
142 |
+
#print('v_gt: ',v_gt.shape)
|
143 |
+
|
144 |
+
pos_loss=F.mse_loss(output[:,:3*self.obj_num],v_gt[:,:3*self.obj_num])
|
145 |
+
|
146 |
+
# w_pos_loss=F.mse_loss(output[:,3*self.obj_num: 3*self.obj_num+3],v_gt[:,3*self.obj_num: 3*self.obj_num+3])
|
147 |
+
# v_loss+=w_pos_loss
|
148 |
+
# w_rot_loss=F.mse_loss(output[:,3*self.obj_num+3:],v_gt[:,3*self.obj_num+3: ])
|
149 |
+
# v_loss+=w_rot_loss
|
150 |
+
metrics = AttrDict(
|
151 |
+
v_pos=pos_loss.item(),
|
152 |
+
# w_pos_loss_loss=w_pos_loss.item(),
|
153 |
+
# w_rot_loss=w_rot_loss.item(),
|
154 |
+
# v_loss=v_loss.item()
|
155 |
+
|
156 |
+
)
|
157 |
+
|
158 |
+
return metrics, pos_loss
|
159 |
+
|
160 |
+
def get_obs(self, seg, rgb):
|
161 |
+
# if self.opts.use_exist_depth:
|
162 |
+
# d=rgb
|
163 |
+
# else:
|
164 |
+
# d=self._get_depth_with_dam(rgb)
|
165 |
+
#d=self._get_depth_with_dam(rgb)
|
166 |
+
#seg_d=torch.concat((seg.unsqueeze(1),d.unsqueeze(1)),dim=1)#.to(self.device)
|
167 |
+
# seg_d=torch.concat((seg.unsqueeze(1),d.unsqueeze(1)),dim=1)#.to(self.device)
|
168 |
+
seg_d = torch.unsqueeze(seg, 1)
|
169 |
+
output=self.v_processor(seg_d)
|
170 |
+
|
171 |
+
#print(output.shape)
|
172 |
+
return output
|