async def listen_socket(self, websocket, path): print(f"Connect {path}") self.client_id_count += 1 room: Optional[Room] = None client = Client(id=self.client_id_count, socket=websocket) stats = Stats("Incoming") try: async for message_raw in websocket: message = decode(message_raw) if message["type"] == "join": room_key = message["room"] if not room_key in self.rooms: room = Room(key=room_key) self.rooms[room_key] = room room.future = asyncio.ensure_future( Server.listen_room(room)) else: room = self.rooms[room_key] room.new_clients.append(client) await websocket.send( encode({ "type": "joined", "client_id": client.id })) elif room: message["client_id"] = client.id await room.event_queue.put(message) else: await websocket.send(encode(message)) stats.incr() except websockets.ConnectionClosed: pass except Exception as e: print(e) pass client.disconnected = True if room is not None: if room.client_count() == 0: await room.event_queue.put(None) del self.rooms[room.key] await room.future print(f"Cleaned Room {room.key}") print(f"Disconnect {self.rooms}")
async def listen_room(room): if room.listening: raise Exception(f"Already listening to {room.key}") room.listening = True print(f"Listen Room {room.key}") stats = Stats(f"Outgoing {room.key}") while True: qevent = await room.event_queue.get() if qevent == None: break if len(room.new_clients) > 0: for client in room.new_clients: room.clients[client.id] = client room.new_clients = [] room.msg_id += 1 qevent["msg_id"] = room.msg_id count = 0 disconnected: List[int] = [] for client in room.clients.values(): if client.disconnected: disconnected.append(client.id) continue count += 1 try: await client.socket.send(encode(qevent)) except websockets.ConnectionClosed: print("Lost client in send") client.disconnected = True stats.incr(count) for d in disconnected: if room.clients[d]: del room.clients[d] print(f"Unlisten Room {room.key}") room.listening = False
# 1. load param file try: exp_dir = import_module(sys.argv[1]) except: raise ValueError('Please specify a valid experiment name as first argument.') params = exp_dir.param # 2. add experiment specific parameters params.get_par() params.get_aux() params.aux.display = True try: tag = sys.argv[2] except: raise ValueError('Please specify a valid experiment tag.') params.aux.experiment_tag = '_{}'.format(tag) # 3. initialize experiment, sorn, and stats objects # the experiment class keeps a copy of the initial sorn main parameters experiment = exp_dir.experiment.Experiment(params) experiment.files_tosave = ['params', 'stats', 'scripts'] sorn = Sorn(params, experiment.inputsource) stats = Stats(experiment.stats_cache, params) # 4. run one experiment once experiment.run(sorn, stats) # 5. save initial sorn parameters and stats objects backup_pickle(experiment, stats)
def main(): sl = Stats() use_lstm = False if use_lstm: # lstm net # keypoint_network_path = "/home/monti/Desktop/pdc/lstm_2021-02-28/checkpoint_0386.pth" # keypoint_network_path = "/home/monti/Desktop/pdc/lstm_2021-02-29/checkpoint_0346.pth" # keypoint_network_path = "/home/monti/Desktop/pdc/lstm_2021-02-26/checkpoint_0546.pth" # keypoint_network_path = "/home/monti/Desktop/pdc/lstm_2021-03-02/checkpoint_0246.pth" # keypoint_network_path = "/home/monti/Desktop/pdc/lstm_2021-03-02_cbrc/checkpoint_0296.pth" keypoint_network_path = "/home/monti/Desktop/pdc/lstm_2021-03-02_cbrc/checkpoint_0316.pth" keypoint_inference = MankeyKeypointDetectionLSTM(keypoint_network_path) else: # standard net # keypoint_network_path = "/home/monti/Desktop/pdc/resnet18_2021-01-30/checkpoint_0118.pth" keypoint_network_path = "/home/monti/Desktop/pdc/resnet18_2021-02-01/checkpoint_0236.pth" keypoint_inference = MankeyKeypointDetection(keypoint_network_path) C = ry.Config() C.addFile("common/single_world.g") C.addFrame("occ_frame") use_side_cam = True if use_side_cam: # this camera is located at the side of the robot video_cam_frame = C.frame("video_camera") video_cam_45 = [1.3, 1.3, 1.35, 0.245372, 0.193942, 0.548013, 0.775797] video_cam_more_side = [1.3, 1.1, 1.35, 0.401579, 0.298422, 0.537793, 0.67857] video_cam_X = video_cam_more_side video_cam_frame.setPosition(video_cam_X[:3]) video_cam_frame.setQuaternion(video_cam_X[3:]) video_cam = C.cameraView() video_cam.addSensorFromFrame("video_camera") video_cam.selectSensor("video_camera") video_cam.updateConfig(C) else: video_cam = C.cameraView() video_cam.addSensorFromFrame("camera") video_cam.selectSensor("camera") video_cam.updateConfig(C) viewer = C.view() jn = "R_panda_joint6" C.setJointState([C.getJointState([jn])[0] + 0.3], [jn]) # Set stick so that it's in the robot's gripper. C.frame("stick").setRelativePosition([0, 0, 0]) C.frame("stick").setRelativeQuaternion(euler_to_quaternion(0, 0, radians(90))) # attach_stick_to_EE(C) # random_stick_pos_in_gripper(C, "endeffR") set_stick_color(C, random_rgb()) randomize_stick_length(C) # setup_stick(C) set_stick_color(C, (0.3, 0.9, 0.1)) randomize_stick_length(C) cf = C.frame("camera") camera_front_facing = True if camera_front_facing: # cf.setPosition([0.5, 1.5, 1.7]) # too far. Depth estimation not reliable cf.setPosition([0.5, 1.1, 1.7]) cf.setQuaternion(euler_to_quaternion(radians(-110), radians(180), -radians(0))) else: # top down view cf.setPosition([0.5, 0.5, 1.4]) cf.setRotationRad(*[ 0, 0, 0.7071068, 0.7071068 ]) camera = C.cameraView() camera.addSensorFromFrame('camera') camera.selectSensor('camera') camera.updateConfig(C) plan_path(C, camera, keypoint_inference, sl, video_cam, use_lstm)
def __init__(self, config, elffile, debug_elffile=None): self.config = config self.name = self.config.BINARY_NAME self.path = self.config.BINARY_PATH self.elffile = ELFFile(elffile) self.entry_point = self.elffile.header[ 'e_entry'] if 'e_entry' in self.elffile.header else None self.binary_type = self.elffile.header[ 'e_type'] if 'e_type' in self.elffile.header else None self.init_pc = None self.fini_pc = None self.insns = dict() self.flags = dict() self.giv_regs = dict() self.giv_offsets = dict() self.int_consts = dict() self.string_consts = dict() self.code_offsets = dict() self.switch_tables = dict() self.virtual_elms = dict() self.temp_offsets = dict() self.node_types = dict() self.othervar_nodes = dict() self.size_nodes = dict() self.op_nodes = dict() self.unknown_node = None self.direct_offsets = dict() self.predicted = set() self.types = dict() set_global_machine_arch(self.elffile.get_machine_arch()) utils.set_global_machine_arch(self.elffile.get_machine_arch(), self) self.sections = Sections(binary=self) if self.config.BAP_FILE_PATH == '' or not os.path.exists( self.config.BAP_FILE_PATH): if self.config.BYTEWEIGHT_SIGS_PATH == '': if self.sections.has_sec(SYMTAB): bap_result = subprocess.getoutput( 'bap {} --pass=loc --symbolizer=objdump --rooter=internal' .format(self.path)) else: bap_result = subprocess.getoutput( 'bap {} --pass=loc --symbolizer=objdump'.format( self.path)) else: bap_result = subprocess.getoutput( 'bap {} --pass=loc --symbolizer=objdump --byteweight-sigs={}' .format(self.path, self.config.BYTEWEIGHT_SIGS_PATH)) bap_json = json.loads(bap_result) else: bap_json = json.load(open(self.config.BAP_FILE_PATH)) self.insn_map = InsnMap(**bap_json) self.bap = Prog(**bap_json, binary=self, has_symtab=self.sections.has_sec(SYMTAB)) self.functions = Functions(bap=self.bap.subs, binary=self) self.functions.initialize() self.low_pc = self.functions.low_pc self.high_pc = self.functions.high_pc self.sections.init_dynsym_offsets() self.nodes = Nodes(binary=self) self.edges = Edges(binary=self) self.factors = Factors(binary=self) self.constraints = Constraints(binary=self) self.string_table = None self.symbol_table = None self.debug_loc = None if self.config.MODE == self.config.TRAIN: self.stats = Stats(self) self.debug_info = DebugInfo(binary=self, debug_elffile=debug_elffile) self.debug_info.binary_train_info() self.nodes.initialize() self.edges.initialize()
class Binary: def __init__(self, config, elffile, debug_elffile=None): self.config = config self.name = self.config.BINARY_NAME self.path = self.config.BINARY_PATH self.elffile = ELFFile(elffile) self.entry_point = self.elffile.header[ 'e_entry'] if 'e_entry' in self.elffile.header else None self.binary_type = self.elffile.header[ 'e_type'] if 'e_type' in self.elffile.header else None self.init_pc = None self.fini_pc = None self.insns = dict() self.flags = dict() self.giv_regs = dict() self.giv_offsets = dict() self.int_consts = dict() self.string_consts = dict() self.code_offsets = dict() self.switch_tables = dict() self.virtual_elms = dict() self.temp_offsets = dict() self.node_types = dict() self.othervar_nodes = dict() self.size_nodes = dict() self.op_nodes = dict() self.unknown_node = None self.direct_offsets = dict() self.predicted = set() self.types = dict() set_global_machine_arch(self.elffile.get_machine_arch()) utils.set_global_machine_arch(self.elffile.get_machine_arch(), self) self.sections = Sections(binary=self) if self.config.BAP_FILE_PATH == '' or not os.path.exists( self.config.BAP_FILE_PATH): if self.config.BYTEWEIGHT_SIGS_PATH == '': if self.sections.has_sec(SYMTAB): bap_result = subprocess.getoutput( 'bap {} --pass=loc --symbolizer=objdump --rooter=internal' .format(self.path)) else: bap_result = subprocess.getoutput( 'bap {} --pass=loc --symbolizer=objdump'.format( self.path)) else: bap_result = subprocess.getoutput( 'bap {} --pass=loc --symbolizer=objdump --byteweight-sigs={}' .format(self.path, self.config.BYTEWEIGHT_SIGS_PATH)) bap_json = json.loads(bap_result) else: bap_json = json.load(open(self.config.BAP_FILE_PATH)) self.insn_map = InsnMap(**bap_json) self.bap = Prog(**bap_json, binary=self, has_symtab=self.sections.has_sec(SYMTAB)) self.functions = Functions(bap=self.bap.subs, binary=self) self.functions.initialize() self.low_pc = self.functions.low_pc self.high_pc = self.functions.high_pc self.sections.init_dynsym_offsets() self.nodes = Nodes(binary=self) self.edges = Edges(binary=self) self.factors = Factors(binary=self) self.constraints = Constraints(binary=self) self.string_table = None self.symbol_table = None self.debug_loc = None if self.config.MODE == self.config.TRAIN: self.stats = Stats(self) self.debug_info = DebugInfo(binary=self, debug_elffile=debug_elffile) self.debug_info.binary_train_info() self.nodes.initialize() self.edges.initialize() def dump_debug(self): with open(self.config.DEBUG_PATH, 'w') as w: for f in self.functions.functions: w.write('function {}\n'.format(repr(f))) w.write('function {}\n'.format(str(f))) for blk_tid in sorted(f.blks.keys()): blk = f.blks[blk_tid] w.write('blk {}\n'.format(blk_tid)) for stmt in blk.stmts: pc = format(stmt.pc, '02x') if stmt.pc is not None else None w.write('stmt: {} {}\n'.format(pc, stmt.insn)) w.write('raw: {}\n'.format(repr(stmt))) w.write('debug: {}\n'.format(str(stmt))) w.write('\n') w.write('\n') def dump_stat(self): self.stats.stat() self.stats.dump() def dump_corrects(self): self.stats.stat() self.stats.dump_corrects() def dump_errors(self): self.stats.stat() self.stats.dump_errors() def dump_edges(self): self.edges.dump() def to_json(self, clear=False): assign = self.nodes.to_json(clear) query = self.edges.to_json() query += self.factors.to_json() query += self.constraints.to_json() return OrderedDict([('assign', assign), ('query', query)]) def dump_graph(self): j = json.dumps(self.to_json()) with open(self.config.GRAPH_PATH, 'w') as w: w.write(j) w.write('\n') def dump_predicted(self): predicted = sorted(list(self.predicted)) with open(self.config.PREDICTEDS_PATH, 'w') as w: w.write('\n'.join(predicted)) def set_test_result_from_path(self, result_path): with open(result_path, 'r') as result: j = json.load(result) self.set_test_result(j) def set_test_result_from_server(self, clear=False): utils.write_progress('Making Prediction...', self) url = self.config.N2P_SERVER_URL params = self.to_json(clear) data = { 'method': 'infer', 'params': params, 'jsonrpc': '2.0', 'id': 0, } response = requests.post(url, data=json.dumps(data)).json() self.set_test_result(response['result']) def set_test_result(self, j): nodes_json = j['assign'] if 'assign' in j else j # nodes_json = j['result'] if 'result' in j else j for node_json in nodes_json: if 'inf' in node_json: node = self.nodes.nodes[node_json['v']] node.test_name = node_json['inf'] # if node.test_name not in TTYPES and node.test_name != UNKNOWN_LABEL: # self.predicted.add(node.test_name) if self.config.MODE == self.config.TRAIN \ and self.config.STAT_PATH != '': self.stats.stat_result(nodes_json) def get_features(self): reg_x = [] reg_y = [] off_x = [] off_y = [] for node in self.nodes.nodes.values(): if isinstance(node, Reg) and node.function.init_run: reg_x.append(list(node.features)) if node.train_name != UNKNOWN_LABEL: reg_y.append(1) else: reg_y.append(0) elif isinstance(node, IndirectOffset) and node.function.init_run: off_x.append(list(node.features)) if node.train_name != UNKNOWN_LABEL: off_y.append(1) else: off_y.append(0) return reg_x, reg_y, off_x, off_y def get_debug_abbrev(self): bs = bytearray() bs.append(ENUM_ABBREV_CODE['COMPILE_UNIT']) bs.append(ENUM_DW_TAG['DW_TAG_compile_unit']) bs.append(ENUM_DW_CHILDREN['DW_CHILDREN_yes']) bs.append(ENUM_DW_AT['DW_AT_language']) bs.append(ENUM_DW_FORM['DW_FORM_data1']) bs.append(ENUM_DW_AT['DW_AT_low_pc']) bs.append(ENUM_DW_FORM['DW_FORM_addr']) bs.append(ENUM_DW_AT['DW_AT_high_pc']) bs.append(ENUM_DW_FORM['DW_FORM_addr']) bs.append(0x00) bs.append(0x00) bs.append(ENUM_ABBREV_CODE['SUBPROGRAM']) bs.append(ENUM_DW_TAG['DW_TAG_subprogram']) bs.append(ENUM_DW_CHILDREN['DW_CHILDREN_yes']) bs.append(ENUM_DW_AT['DW_AT_name']) bs.append(ENUM_DW_FORM['DW_FORM_string']) bs.append(ENUM_DW_AT['DW_AT_type']) bs.append(ENUM_DW_FORM['DW_FORM_ref4']) bs.append(ENUM_DW_AT['DW_AT_low_pc']) bs.append(ENUM_DW_FORM['DW_FORM_addr']) bs.append(ENUM_DW_AT['DW_AT_high_pc']) if self.config.ADDRESS_BYTE_SIZE == 4: bs.append(ENUM_DW_FORM['DW_FORM_data4']) elif self.config.ADDRESS_BYTE_SIZE == 8: bs.append(ENUM_DW_FORM['DW_FORM_data8']) bs.append(0x00) bs.append(0x00) bs.append(ENUM_ABBREV_CODE['SUBPROGRAM_VOID']) bs.append(ENUM_DW_TAG['DW_TAG_subprogram']) bs.append(ENUM_DW_CHILDREN['DW_CHILDREN_yes']) bs.append(ENUM_DW_AT['DW_AT_name']) bs.append(ENUM_DW_FORM['DW_FORM_string']) bs.append(ENUM_DW_AT['DW_AT_low_pc']) bs.append(ENUM_DW_FORM['DW_FORM_addr']) bs.append(ENUM_DW_AT['DW_AT_high_pc']) if self.config.ADDRESS_BYTE_SIZE == 4: bs.append(ENUM_DW_FORM['DW_FORM_data4']) elif self.config.ADDRESS_BYTE_SIZE == 8: bs.append(ENUM_DW_FORM['DW_FORM_data8']) bs.append(0x00) bs.append(0x00) bs.append(ENUM_ABBREV_CODE['VARIABLE']) bs.append(ENUM_DW_TAG['DW_TAG_variable']) bs.append(ENUM_DW_CHILDREN['DW_CHILDREN_no']) bs.append(ENUM_DW_AT['DW_AT_name']) bs.append(ENUM_DW_FORM['DW_FORM_string']) bs.append(ENUM_DW_AT['DW_AT_location']) bs.append(ENUM_DW_FORM['DW_FORM_exprloc']) bs.append(ENUM_DW_AT['DW_AT_type']) bs.append(ENUM_DW_FORM['DW_FORM_ref4']) bs.append(0x00) bs.append(0x00) bs.append(ENUM_ABBREV_CODE['LOC_VARIABLE']) bs.append(ENUM_DW_TAG['DW_TAG_variable']) bs.append(ENUM_DW_CHILDREN['DW_CHILDREN_no']) bs.append(ENUM_DW_AT['DW_AT_name']) bs.append(ENUM_DW_FORM['DW_FORM_string']) bs.append(ENUM_DW_AT['DW_AT_location']) bs.append(ENUM_DW_FORM['DW_FORM_sec_offset']) bs.append(ENUM_DW_AT['DW_AT_type']) bs.append(ENUM_DW_FORM['DW_FORM_ref4']) bs.append(0x00) bs.append(0x00) bs.append(ENUM_ABBREV_CODE['FUN_ARG']) bs.append(ENUM_DW_TAG['DW_TAG_formal_parameter']) bs.append(ENUM_DW_CHILDREN['DW_CHILDREN_no']) bs.append(ENUM_DW_AT['DW_AT_name']) bs.append(ENUM_DW_FORM['DW_FORM_string']) bs.append(ENUM_DW_AT['DW_AT_location']) bs.append(ENUM_DW_FORM['DW_FORM_exprloc']) bs.append(ENUM_DW_AT['DW_AT_type']) bs.append(ENUM_DW_FORM['DW_FORM_ref4']) bs.append(0x00) bs.append(0x00) bs.append(ENUM_ABBREV_CODE['LOC_FUN_ARG']) bs.append(ENUM_DW_TAG['DW_TAG_formal_parameter']) bs.append(ENUM_DW_CHILDREN['DW_CHILDREN_no']) bs.append(ENUM_DW_AT['DW_AT_name']) bs.append(ENUM_DW_FORM['DW_FORM_string']) bs.append(ENUM_DW_AT['DW_AT_location']) bs.append(ENUM_DW_FORM['DW_FORM_sec_offset']) bs.append(ENUM_DW_AT['DW_AT_type']) bs.append(ENUM_DW_FORM['DW_FORM_ref4']) bs.append(0x00) bs.append(0x00) bs += self.types.debug_abbrev() bs.append(0x00) return bs def get_debug_info(self): bs = bytearray() # version bs.append(0x02) bs.append(0x00) # abbrev offset bs.append(0x00) bs.append(0x00) bs.append(0x00) bs.append(0x00) # pointer size bs.append(int(self.config.ADDRESS_BYTE_SIZE)) # compile unit bs.append(ENUM_ABBREV_CODE['COMPILE_UNIT']) # DW_AT_language bs.append(ENUM_DW_AT_language['DW_LANG_C89']) bs += utils.encode_address(self.sections.sections[TEXT].addr, self) bs += utils.encode_address(self.sections.sections[TEXT].end_addr, self) self.string_table = StringTable(binary=self) self.symbol_table = SymbolTable(binary=self) self.debug_loc = DebugLoc(binary=self) self.types = Types(binary=self, offset=len(bs)) bs_rest = bytearray() for off in sorted(self.direct_offsets.keys()): direct_offset = self.direct_offsets[off] if direct_offset.test_name is not None \ and direct_offset.test_name != UNKNOWN_LABEL: bs_rest += direct_offset.debug_info() for low_pc in sorted(self.functions.functions_by_lowpc.keys()): f = self.functions.functions_by_lowpc[low_pc] if f.is_run_init \ and f.test_name is not None \ and f.test_name != UNKNOWN_LABEL: bs_rest += f.debug_info() bs_rest.append(0x00) bs = bs + self.types.content + bs_rest length = len(bs) bs = length.to_bytes(4, byteorder='little') + bs return bs def modify_elf(self, binary_without_symtab=None): utils.write_progress('Preparing Output...', self) modify_elf = ctypes.cdll.LoadLibrary( self.config.MODIFY_ELF_LIB_PATH).modify_elf modify_elf.argtypes = [ ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_char_p ] info = self.get_debug_info() abbrev = self.get_debug_abbrev() loc = self.debug_loc.content self.symbol_table.debug_info() strtab = self.string_table.content symtab = self.symbol_table.content symtab_info = self.symbol_table.num_entries if binary_without_symtab is not None: binary_path = binary_without_symtab.encode('ascii') len_symtab = len(symtab) else: binary_path = self.config.BINARY_PATH.encode('ascii') len_symtab = 0 if self.sections.has_sec(SYMTAB) else len(symtab) modify_elf(binary_path, self.config.OUTPUT_BINARY_PATH.encode('ascii'), len(info), bytes(info), len(abbrev), bytes(abbrev), len(loc), bytes(loc), len(strtab), bytes(strtab), len_symtab, self.config.ADDRESS_BYTE_SIZE * 2 + 8, symtab_info, bytes(symtab)) utils.write_progress('Output Prepared...', self)