def load(self): if os.path.isfile( f'{file_name}.data-00000-of-00001'): # search for actual file self.network.load_weights(file_name) logger.debug( f'Successfully loaded {file_name} model in KerasModel.load()') else: logger.error(f'{file_name} file not found in KerasModel.load()')
def __connect(self): try: logger.info(f'Connecting to {HOST}:{PORT}...') self.client = icarla.client(HOST, PORT) # Even on a local machine, CARLA server takes about 2-3 seconds to respond - especially loading maps is long self.client.set_timeout(10.0) self.world = self.client.get_world() logger.info(f'Connected successfully') except RuntimeError as r: logger.error(f'Could not connect to server: {r}')
def loop(self): if self.spectator is None or self.vehicle is None or self.path is None: time.sleep(1.0) else: try: self.update() except RuntimeError as r: logger.error(f'Error: {r}') logger.warning(f'Setting vehicle to None') self.vehicle = None
def predict(self, state): if state is None: return None, None action = self.model.predict(state) try: # Copy value, not reference choice = self.choices[action][:] return action, choice except RuntimeError: logger.error(f'Error when trying to find right value for {action}') return None, None
def load(self, path=NETWORKAGENT_MODEL_PATH): logger.info(f'Loading model from {path}') try: self.model.load_model(path, dim_features=get_feature_dimension, image_height=AGENT_IM_HEIGHT, image_width=AGENT_IM_WIDTH, n_actions=choices_count, model=self.model_class) except FileNotFoundError as f: logger.error(f'Failed to find file at {path} - {f}') except RuntimeError as r: logger.critical(f'Failed to load agent from {path} - {r}')
def loop(self): if self.vehicle is None: time.sleep(1.0) else: try: self.poll_acceleration() self.poll_position_and_direction() self.poll_angular_acceleration() self.poll_velocity() time.sleep(POLL_TIME) except RuntimeError as r: logger.error(f'Error: {r}') logger.warning(f'Setting vehicle to None') self.vehicle = None
def control(self, throttle, steering): if self.vehicle is not None: if throttle >= 0.0: try: self.vehicle.apply_control(icarla.vehicle_control(throttle=throttle, steer=steering)) except RuntimeError as r: logger.error(f'Error: {r}') logger.warning(f'Setting vehicle to None') self.vehicle = None else: try: throttle *= -1.0 self.vehicle.apply_control(icarla.vehicle_control(reverse=True, throttle=throttle, steer=steering)) except RuntimeError as r: logger.error(f'Error: {r}') logger.warning(f'Setting vehicle to None') self.vehicle = None else: raise RuntimeError('Vehicle not set in ControllerThread')
def move(actor, loc, safe=True): """ Moves the actor to a given location | If safe mode is enabled, move becomes a blocking function which retries if it failed""" actor.set_location(loc) if safe: time.sleep(0.5) # for some reason we must wait for the simulator to process this d = actor.get_location().distance(loc) if d > 1.0: logger.warning(f'Failed moving {actor.type_id} to {loc}, retry...') logger.info(f'Actors transform is {actor.get_transform()}') actor.set_transform(carla.Transform(add_locations(loc, location(0, 0, 1)), rotation([0, 0, 0]))) time.sleep(0.5) d = actor.get_location().distance(loc) if d > 3.0: logger.error(f'Failed moving {actor.type_id} to {loc}') logger.info(f'Actors transform is {actor.get_transform()}') return
def train_model(self, data=None): if data is None: val = None train = None # val = get_validation_data() # train = get_train_data() logger.error('Missing implementation in train_model') pass else: trainables = states_to_trainables(data) random.shuffle(trainables) trainables = balance(trainables) random.shuffle(trainables) val_len = len(trainables) // 10 val = trainables[:val_len] train = trainables[val_len:] if len(train) > 0: self.model.train(train=train, test=val, epochs=5) else: logger.error('Train has length 0')
def optimize(self, new_state, prev_state=None, action=None): try: return self.model.optimize(new_state, prev_state, action) except RuntimeError as r: logger.error(f'Error in model.optimize: {r}')