def reset_new(self): print("reset in env15") self.physical_id = self.p self.p.setAdditionalSearchPath(pybullet_data.getDataPath()) self.p.setGravity(0, 0, -9.8) self.p.setTimeStep(1 / 250.) try: self.epoch_num += 1 except: self.epoch_num = 0 self.log_path = safe_path( os.path.join(self.log_root, 'epoch-{}'.format(self.epoch_num))) self.log_info = open( os.path.join(self.log_root, 'epoch-{}.txt'.format(self.epoch_num)), 'w') self.seq_num = 0 self.init_dmp() self.init_motion() self.init_rl() self.reset_obj() self.init_grasp() return self.get_observation()
def init_running(self): self.motion_path = self.dataset_root + '/actions/{}-{}'.format( self.class_id, self.video_id) self.data_q = np.load( self.dataset_root + '/actions/{}-{}/q.npy'.format(self.class_id, self.video_id)) self.data_dq = np.load( self.dataset_root + '/actions/{}-{}/dq.npy'.format(self.class_id, self.video_id)) self.data_gripper = np.load( self.dataset_root + '/actions/{}-{}/gripper.npy'.format(self.class_id, self.video_id)) self.frames_path = cut_frame( self.dataset_root + '/actions/{}-{}/{}-{}.avi'.format( self.class_id, self.video_id, self.class_id, self.video_id), self.dataset_root + '/actions/{}-{}/frames'.format(self.class_id, self.video_id)) self.output_path = safe_path(self.dataset_root + '/actions/{}-{}/simulation_frames'.format( self.class_id, self.video_id)) self.mask_frames_path = safe_path(self.dataset_root + '/actions/{}-{}/masked_frames'. format(self.class_id, self.video_id))
def reset(self, target=None): self.img_buffer = [] try: self.destroy() except: print("initial start!") self.physical_id = self.p self.p.setAdditionalSearchPath(pybullet_data.getDataPath()) self.p.setGravity(0, 0, -9.8) self.p.setTimeStep(1 / 250.) # epoch_max = -1 # for file in os.listdir (self.log_root): # if ('epoch' in file) and ('txt' not in file): # epoch_max = max (epoch_max, int (file.split ('-')[1])) # self.epoch_num = epoch_max + 1 # print ('epoch_max:{}'.format (epoch_max)) self.epoch_num += 1 print('epoch_max:{}'.format(self.epoch_num)) lock = 1 while (lock): try: self.log_path = safe_path( os.path.join(self.log_root, 'epoch-{}'.format(self.epoch_num))) self.log_info = open( os.path.join(self.log_root, 'epoch-{}.txt'.format(self.epoch_num)), 'w') lock = 0 except: self.epoch_num += 1 # self.evaluator.update (img_path=self.log_path, start_id=0) self.seq_num = 0 self.init_dmp() self.init_motion() self.init_rl() self.reset_obj() for j in range(6): index_ = self.robot.activeGripperJointIndexList[j] p.resetJointState(self.robotId, index_, 0, 0) if self.opt.use_embedding: action_p = random.random() action_p = int(action_p * len(self.opt.embedding_list)) if self.opt.fine_tune: fine_tune_list = self.opt.fine_tune_list action_p = random.random() action_p = int(action_p * len(fine_tune_list)) action_p = np.where( np.array(self.opt.embedding_list) == fine_tune_list[action_p])[0][0] if target is not None: action_p = np.where( np.array(self.opt.embedding_list) == target)[0][0] if self.opt.nlp_embedding: self.opt.load_embedding = self.opt.embedding_list[action_p] self.action_embedding = self.embedding_data[ self.opt.load_embedding] else: self.action_embedding = np.array([0] * self.opt.embedding_dim) self.action_embedding[action_p] = 1 self.opt.load_embedding = self.opt.embedding_list[action_p] self.init_grasp() img_info = p.getCameraImage( width=self.w, height=self.h, viewMatrix=self.view_matrix, projectionMatrix=self.proj_matrix, shadow=-1, flags=p.ER_SEGMENTATION_MASK_OBJECT_AND_LINKINDEX, renderer=p.ER_TINY_RENDERER) observation = img_info[2][:, :, :3] if self.opt.add_mask: mask = (img_info[4] > 10000000) mask_id_label = [ 234881025, 301989889, 285212673, 268435457, 318767105, 335544321, 201326593, 218103809, 167772161 ] for item in mask_id_label: mask = mask * (img_info[4] != item) box_mask_label = [ 117440516, 234881028, 184549380, 33554436, 167772164, 50331652, 134217732, 16777220, 67108868, 150994948, 251658244, 234881028, 100663300, 218103812, 83886084, 268435460 ] for item in box_mask_label: mask = mask * (img_info[4] != item) observation = cv2.cvtColor(observation, cv2.COLOR_RGB2BGR) observation[mask] = [127, 151, 182] if self.opt.observation == 'joint_pose': observation = np.array([ p.getJointState(self.robotId, i)[0] for i in range(self.robotEndEffectorIndex) ]) elif self.opt.observation == 'end_pos': observation = np.array(p.getLinkState(self.robotId, 7)[0]) elif self.opt.observation == 'before_cnn': observation = np.array(observation) if self.opt.use_embedding: self.observation = [self.action_embedding, observation] else: self.observation = observation self.info = '' return self.observation
def __init__(self, opt, p_id=None, w_id=None): self.opt = opt if p_id is None: self.p = opt.p else: self.p = p_id if w_id is not None: self.w_id = w_id else: self.w_id = 0 self.class_id = opt.action_id self.video_id = opt.video_id self.test_id = opt.test_id self.cut_frame_num = opt.cut_frame_num self.give_reward_num = opt.give_reward_num self.init_rl() if self.opt.video_reward: self.eval = self.opt.load_video_pred if self.opt.use_cycle: self.cycle = self.opt.load_cycle if self.opt.use_dmp: self.dmp = self.opt.load_dmp # assert (self.opt.video_reward) self.dataset_root = os.path.join(opt.project_root, 'dataset') self.log_root = os.path.join(opt.project_root, 'logs') if self.opt.use_a3c: self.log_root = safe_path(self.log_root + '/a3c_log/test{}'.format(self.test_id)) else: self.log_root = safe_path(self.log_root + '/td3_log/test{}'.format(self.test_id)) self.writer = SummaryWriter(self.log_root) self.env_root = os.path.join(opt.project_root, 'scripts', 'Envs') self.script_root = os.path.join(opt.project_root, 'scripts') self.embedding_data = np.load( os.path.join(self.script_root, 'utils', 'labels', 'label_embedding_uncased.npy')) np.save( os.path.join(self.script_root, 'utils', 'labels', 'label_embedding_uncased_adjusted.npy'), self.embedding_data) # if self.opt.test_id==132: # adjust_list = [86,94,43,45] # adjust_center = np.zeros_like(self.embedding_data[0]) # for adjust_id in adjust_list: # adjust_center += self.embedding_data[adjust_id] # adjust_center /= 4 # for adjust_id in adjust_list: # self.embedding_data[adjust_id] = (self.embedding_data[adjust_id]-adjust_center)*3+adjust_center # np.save(os.path.join (self.script_root, 'utils', 'labels', 'label_embedding_uncased_adjusted.npy'),self.embedding_data) # # if self.opt.test_id==144: # adjust_list = [86,94,43,45] # self.embedding_data[86] = np.array([0.25]*int(self.opt.embedding_dim)) # self.embedding_data[94] = np.array([0.5]*int(self.opt.embedding_dim)) # self.embedding_data[43] = np.array([1.0]*int(self.opt.embedding_dim)) # self.embedding_data[45] = np.array([0.0]*int(self.opt.embedding_dim)) # np.save(os.path.join (self.script_root, 'utils', 'labels', 'label_embedding_uncased_adjusted.npy'),self.embedding_data) self.memory_path = safe_path(os.path.join(self.log_root, 'memory')) backup_code(self.script_root, self.log_root) print('code has been saved!') self.init_plane() self.init_table() self.robot = Robot(pybullet_api=self.p, opt=self.opt) self.robotId = self.robot.robotId self.robotEndEffectorIndex = self.robot.endEffectorIndex self.init_obj() self.view_matrix, self.proj_matrix = get_view(self.opt) self.q_home = np.array( (0., -np.pi / 6., 0., -5. / 6. * np.pi, 0., 2. / 3. * np.pi, 0.)) self.w = self.opt.img_w self.h = self.opt.img_h self.epoch_num = 0
class AutoPhrase: FNAME_POSTAG = safe_path('tags.txt') def __init__(self, omega, quality, length_threshold, memory_size_mb): """AutoPhrase :param omega::Omega :param quality::Quality :param length_threshold::int max length of n-grams """ if not isinstance(omega, Omega): err = 'argument "omega" must be a Omega object, ' \ 'got {}'.format(type(omega)) raise TypeError(err) if not isinstance(quality, Quality): err = 'argument "quality" must be a Quality object, ' \ 'got {}'.format(type(quality)) raise TypeError(err) self.length_threshold = length_threshold self.memory_size_mb = memory_size_mb # 语料库 self.C = omega # 短语质量得分 # 基于AutoPhrase.features self.Q = quality # 状态转移概率: # 从上一个pos标签到当前pos标签的出现概率 self._param_delta = None # 发射概率: # 在确定边界的情况下,生成当前 n-grams组的概率 self._param_theta = None # 跟踪参数收敛 self._trace_delta = list() self._trace_theta = list() self._delta = None self._theta = None self._phrases = None # initialize θ with normalized raw frequencies in the corpus self.init_parameters() @property def phrases(self): if self._phrases is not None: return self._phrases def train(self): """ 基于 Viterbi Training的训练函数,通过前向算法 PGPS 对 theta和 delta进行参数估计 Input: Corpus Ω and phrase quality Q. Output: θu and δ(tx, ty). :return: None """ logging.debug('Viterbi Training.') if self._param_delta is None or self._param_theta is None: raise ValueError('Parameter delta and theta is empty. ' 'Should initial the parameters first.') while decreasing(self._trace_theta): while decreasing(self._trace_delta): current_B = self.PGPS() self.update_delta(current_B) current_B = self.PGPS() self.update_theta(current_B) logging.debug('both theta and delta not converge.') # 获取纠正后的短语 phrases = {} for candidate, idx in self._theta.items(): phrases[candidate] = self._param_theta[idx] if phrases: phrases = pd.Series(phrases).sort_values( ascending=False).reset_index() phrases.columns = ['phrase', 'score'] self._phrases = phrases def PGPS(self): """ POS-Guided Phrasal Segmentation (PGPS) 基于part-of-speech标签的短语分割算法 :return: B::优化后的边界下标序列,每个条目是确定分割短语的边界下标 """ # 设置一个非常大的负数,用它表示无限接近零的小数的对数 # exp(-1e100) ≈ 0 tiny_log_value = -1e100 # 计算整体n-gram序列(以短语为单位的观测序列)的出现概率 # h 中所有关于概率的计算都将转成对数加法(替代乘法) h = collections.defaultdict(lambda: tiny_log_value) # 观测序列从句首开始,句首边界100%一定存在,因此概率为1. # 对1.取对数,得 log(1.) = 0. h[1] = 0. # g <- {右边界: 左边界} g = {} # 通过左右边界下标遍历所有可能出现的n-grams组 bar = tqdm.tqdm(range(1, len(self.C) + 1), ascii=True) for i in bar: bar.set_description('PGPS') for j in range(i + 1, min(i + self.length_threshold + 1, len(self.C) - 1)): # 如果连质量短语都不是,这组 n-grams的出现概率为0 if not self.Q.startswith(self.C.word(i, j)): h[j] = h[i] g[j] = i break # 获取参数:转移概率、发射概率和词频质量得分 T_values = self.pos_quality_scores(i, j) theta_values = self.length_quality_scores(i, j) Q_values = self.freq_quality_scores(i, j) # 计算联合概率 p = np.append( T_values[:-1], # 短语内部概率 ( (1 - T_values[-1]), # 短语外部概率 theta_values, Q_values)) # 用log加法替代乘法计算联合概率 log_p = np.log(p, out=np.zeros_like(p), where=(p != 0)) sum_log_p = log_p.view(np.float64).sum() # 计算当前 n-grams组和已出现 n-gram序列的联合概率 # 在状态转移概率和发射概率的条件下,获取出现概率最大n-grams的边界 if h[i] + sum_log_p > h[j]: h[j] = h[i] + sum_log_p g[j] = i # 回溯前向算法找到出现概率最大的右边界下标 j = len(self.C) + 1 m = 0 b = {} while j > 1: m += 1 b[m] = j try: j = g[j] except KeyError: j = j - 1 B = [1] for i in reversed(range(1, m)): B.append(b[i]) return B def init_parameters(self): """参数初始化""" # 1.theta参数初始化 # 遍历所有可能的候选短语,统计词频 counter = bounter.bounter(size_mb=self.memory_size_mb) for i in range(1, len(self.C) + 1): for j in range(i + 1, min(i + self.length_threshold + 1, len(self.C) - 1)): if not self.Q.startswith(self.C.word(i, j)): break counter.update((self.C.word(i, j), )) # 词频分布 distr = np.array([i for i in counter.values()]) _mean, _std = distr.mean(), distr.std() norm = truncnorm((0. - _mean) / _std, (1. - _mean) / _std, loc=_mean, scale=_std) # θ <- {短语 <- 短语下标} theta = bounter.bounter(size_mb=self.memory_size_mb) idx_theta = 0 # 从零计数 for candidate in counter.keys(): theta[candidate] = idx_theta idx_theta += 1 del counter # 按正态分布随机初始化 θ param_theta = norm.rvs(size=idx_theta) # 2.delta参数初始化 # 获取所有可能的pos标签二元组, delta = self.Tx_Ty() # 按均匀分布随机初始化 δ param_delta = np.random.uniform(size=len(delta)) self._delta = delta self._theta = theta self._param_delta = param_delta self._param_theta = param_theta # 检查参数收敛情况 self._trace_delta.append( compare_arrays(param_delta, np.ones(param_delta.size))) self._trace_theta.append( compare_arrays(param_theta, np.ones(param_theta.size))) def update_theta(self, B): assert (isinstance(B, list)) assert len(B) > 1 pre_param = self._param_theta m = len(B) numerator = bounter.bounter(size_mb=self.memory_size_mb // 2) denominator = collections.defaultdict(int) bar = tqdm.tqdm(range(m - 1), ascii=True) for i in bar: bar.set_description('theta 1/2') numerator.increment(self.C.word(B[i], B[i + 1])) denominator[B[i + 1] - B[i]] += 1 bar = tqdm.tqdm(self._theta.items(), total=len(self._theta), ascii=True) for candidate, idx in bar: bar.set_description('theta 2/2') if numerator[candidate] != 0: u_len = candidate.count(' ') + 1 new_theta_value = numerator[candidate] / denominator[u_len] self._param_theta[idx] = new_theta_value else: self._param_theta[idx] = 0. self._trace_theta.append(compare_arrays(self._param_theta, pre_param)) del pre_param def update_delta(self, B): assert (isinstance(B, list)) assert len(B) > 1 pre_param = self._param_delta m = len(B) n = len(self.C) TxTy_numerator = bounter.bounter(size_mb=self.memory_size_mb // 2) TxTy_denominator = bounter.bounter(size_mb=self.memory_size_mb // 2) bar = tqdm.tqdm(range(1, m - 1), ascii=True) for i in bar: bar.set_description('delta 1/3') for j in range(B[i], B[i + 1] - 1): TxTy_numerator.increment(self.C.tag(j, j + 2)) bar = tqdm.tqdm(range(1, n), ascii=True) for i in bar: bar.set_description('delta 2/3') TxTy_denominator.increment(self.C.tag(i, i + 2)) bar = tqdm.tqdm(self._delta.items(), total=len(self._delta), ascii=True) for txty, idx in bar: bar.set_description('delta 3/3') if TxTy_numerator[txty] != 0: print(txty) new_delta_value = TxTy_numerator[txty] / TxTy_denominator[txty] self._param_delta[idx] = new_delta_value else: self._param_delta[idx] = 0. self._trace_delta.append(compare_arrays(self._param_delta, pre_param)) del pre_param def length_quality_scores(self, start_idx, end_idx): """获取指定的 theta[u] 参数""" # 初始化时从1开始计数,np.array是从0开始计数,因此 u_idx=idx - 1 u_idx = self._theta[self.C.word(start=start_idx, stop=end_idx)] param_val = self._param_theta[u_idx] return param_val def pos_quality_scores(self, start_idx, end_idx): """获取T[u] 参数""" # 候选短语与右邻单词有2种组合情况 # # 1.候选短语和右邻单词都存在:(t1 t2 t3), t4 # 内部概率:(t1 t2), (t2 t3) # 外部概率:(t3 t4) # # 2.候选短语只有一个单词的情况:(t1), t2 # 内部概率:(t1 t1) = 1.0 # 外部概率:(t1 t2) # 有两部分组成: # T_values[:-1]: 内部概率 # T_values[-1]: 外部概率 delta_param_vals = [] if end_idx < len(self.C): if end_idx - start_idx > 1: # 多词词组的情况,遍历内部二元词组和右邻词组 for k in range(start_idx, end_idx + 1): TxTy = self.C.tag(k, k + 2) TxTy_idx = self._delta[TxTy] param_val = self._param_delta[TxTy_idx] delta_param_vals.append(param_val) else: # 单词词组的情况,内部完整性概率为100% TxTy = self.C.tag(start_idx, end_idx + 1) TxTy_idx = self._delta[TxTy] param_val = self._param_delta[TxTy_idx] delta_param_vals.append(1.) delta_param_vals.append(param_val) if delta_param_vals: return np.array(delta_param_vals) def freq_quality_scores(self, start_idx, end_idx, epsilon=1e-3): """initialised by AutoPhrase.features""" try: q = self.Q[self.C.word(start_idx, end_idx)] except KeyError: q = epsilon return q def Tx_Ty(self): """获取所有可能的tx ty part-of-speech by hanlp""" tags = [] f = open(self.FNAME_POSTAG) while True: line = f.readline() if not line: break if line.startswith('#'): continue line = line.rstrip() tags.append(line) if tags: TxTy_dict = {} for idx, (tx, ty) in enumerate(itertools.product(tags, tags)): TxTy_dict['{} {}'.format(tx, ty)] = idx return TxTy_dict else: return {}