def on_received_get_commands(self, context: Context, data: GetCommands): # context.info(f'on_received_get_commands') if not self.agent.initialized: pwm_left, pwm_right = [0, 0] else: # TODO: let's use a queue here. Performance suffers otherwise. # What you should do is: *get the last command*, if available # otherwise, wait for one command. t0 = time.time() while not self.agent.updated: dt = time.time() - t0 if dt > 2.0: context.info(f"agent not ready since {dt:.1f} s") time.sleep(0.5) if dt > 60: msg = "I have been waiting for commands from the ROS part" f" since {int(dt)} s" context.error(msg) raise Exception(msg) time.sleep(0.02) dt = time.time() - t0 if dt > 2.0: context.info(f"obtained agent commands after {dt:.1f} s") time.sleep(0.2) pwm_left, pwm_right = self.agent.action self.agent.updated = False grey = RGB(0.5, 0.5, 0.5) led_commands = LEDSCommands(grey, grey, grey, grey, grey) pwm_commands = PWMCommands(motor_left=pwm_left, motor_right=pwm_right) commands = DB20Commands(pwm_commands, led_commands) context.write("commands", commands)
def init(self, context: Context): context.info("Check GPU...") self.check_gpu_available(context) # Model predicts linear and angular velocity but we need to issue # wheel velocity commands to robot, this wrapper converts to the latter. self.convertion_wrapper = SteeringToWheelVelWrapper() context.info('init()') model_lin = NvidiaModel() model_lin.load_state_dict( torch.load( 'saved_nvidia_model_lin.pth', map_location=self.device, )) model_lin.eval() self.model_lin = model_lin.to(self.device) model_ang = NvidiaModel() model_ang.load_state_dict( torch.load( 'saved_nvidia_model_ang.pth', map_location=self.device, )) model_ang.eval() self.model_ang = model_ang.to(self.device) self.current_image = np.zeros(self.expect_shape) self.input_image = np.zeros((150, 200, 3)) self.to_predictor = np.expand_dims(self.input_image, axis=0)
def on_received_set_params(self, context: Context, data: PlanningSetup): context.info("initialized") self.params = data # This is the interval of allowed linear velocity # Note that min_velocity_x_m_s and max_velocity_x_m_s might be different. # Note that min_velocity_x_m_s may be 0 in advanced exercises (cannot go backward) max_velocity_x_m_s: float = self.params.max_linear_velocity_m_s min_velocity_x_m_s: float = self.params.min_linear_velocity_m_s # This is the max curvature. In earlier exercises, this is +inf: you can turn in place. # In advanced exercises, this is less than infinity: you cannot turn in place. max_curvature: float = self.params.max_curvature # these have the same meaning as the collision exercises body: List[PlacedPrimitive] = self.params.body environment: List[PlacedPrimitive] = self.params.environment # these are the final tolerances - the precision at which you need to arrive at the goal tolerance_theta_deg: float = self.params.tolerance_theta_deg tolerance_xy_m: float = self.params.tolerance_xy_m # For convenience, this is the rectangle that contains all the available environment, # so you don't need to compute it bounds: Rectangle = self.params.bounds
def update_observations(self, blur_time: float, context: Context): context.info(f'update_observations() at {self.current_time}') assert self.render_observations to_average = [] n = len(self.render_observations) # context.info(str(self.render_timestamps)) # context.info(f'now {self.current_time}') for i in range(n): ti = self.render_timestamps[i] if math.fabs(self.current_time - ti) < blur_time: to_average.append(self.render_observations[i]) obs0 = to_average[0].astype('float32') context.info(str(obs0.shape)) for obs in to_average[1:]: obs0 += obs obs = obs0 / len(to_average) obs = obs.astype('uint8') # context.info(f'update {obs.shape} {obs.dtype}') jpg_data = rgb2jpg(obs) camera = JPGImage(jpg_data) obs = Duckiebot1Observations(camera) self.ro = DB18RobotObservations(self.robot_name, self.current_time, obs) self.last_observations_time = self.current_time
def init(self, context: Context): context.info("Check GPU...") self.check_gpu_available(context) # Model predicts linear and angular velocity but we need to issue # wheel velocity commands to robot, this wrapper converts to the latter. self.convertion_wrapper = SteeringToWheelVelWrapper() context.info('init()') model = Modelv1() model.load_state_dict( torch.load( "modelv1_maserati_bill_simulated_amadobot_base_epoch_200.pth", # "modelv1_maserati_bill_simulated_amadobot_base.pth", # 'modelv1_maserati_plus_simulated.pth', map_location=self.device, )) model.eval() self.model = model.to(self.device) self.current_image = np.zeros(self.expect_shape) self.input_image = np.zeros((150, 200, 3)) self.to_predictor = np.expand_dims(self.input_image, axis=0)
def check_tensorflow_gpu(self, context: Context): import tensorflow as tf name = tf.test.gpu_device_name() context.info(f"gpu_device_name: {name!r} ") if not name: # None or '' no_hardware_GPU_available(context)
def init(self, context: Context): context.info("init()") self.rgb = None self.l_max = -math.inf self.r_max = -math.inf self.l_min = math.inf self.r_min = math.inf self.left = None self.right = None
def init(self, context: Context): print(time.time(), 'init') context.info('init()') frozen_model_filename = "frozen_graph.pb" # We use our "load_graph" function to load the graph self.graph = load_graph(frozen_model_filename) self.session = tf.Session(graph=self.graph)
def on_received_observations(self, context: Context, data: DB20ObservationsWithTimestamp): profiler = context.get_profiler() camera: JPGImageWithTimestamp = data.camera odometry: DB20OdometryWithTimestamp = data.odometry context.info(f"camera timestamp: {camera.timestamp}") context.info(f"odometry timestamp: {odometry.timestamp}") with profiler.prof("jpg2rgb"): _rgb = jpg2rgb(camera.jpg_data)
def init(self, context: Context): context.info('init()') self.image_processor = DTPytorchWrapper() self.action_processor = ActionWrapper(FakeWrap()) from model import DDPG self.check_gpu_available(context) self.model = DDPG(state_dim=self.image_processor.shape, action_dim=2, max_action=1, net_type="cnn") self.current_image = np.zeros((640, 480, 3)) self.model.load("model", directory="./models")
def on_received_get_robot_performance(self, context: Context, data: RobotName): context.info(f"get_robot_interface_description()") metrics = {} metrics["reward"] = Metric( higher_is_better=True, cumulative_value=self.current_time, description="Dummy reward equal to survival time.", ) pm = PerformanceMetrics(metrics) rid = RobotPerformance(robot_name=data, t_effective=self.current_time, performance=pm) context.write("robot_performance", rid)
def init(self, context: Context): context.info("Check GPU...") limit_gpu_memory() self.check_tensorflow_gpu() from frankModel import FrankNet from helperFncs import SteeringToWheelVelWrapper self.convertion_wrapper = SteeringToWheelVelWrapper() context.info('init()') self.model = FrankNet.build(200, 150) self.model.load_weights("FrankNet.h5") self.current_image = np.zeros(self.expect_shape) self.input_image = np.zeros((150, 200, 3)) self.to_predictor = np.expand_dims(self.input_image, axis=0)
def init(self, context: Context): context.info("Check GPU...") limit_gpu_memory() test_payload() self.check_tensorflow_gpu() from cbcNet import cbcNet from helperFncs import SteeringToWheelVelWrapper self.convertion_wrapper = SteeringToWheelVelWrapper() context.info('init()') self.model = cbcNet.get_inference("cbcNet.h5") self.current_image = np.zeros(self.expect_shape) self.input_image = np.zeros((150, 200, 3)) self.to_predictor = np.expand_dims(self.input_image, axis=0)
def init(self, context: Context): context.info("Checking GPU availability...") limit_gpu_memory() self.check_tensorflow_gpu(context) from model import TfInference # this is the folder where our models are graph_location = "tf_models/" # define observation and output shapes self.model = TfInference( observation_shape=(1, ) + self.expect_shape, # this is the shape of the image we get. action_shape=(1, 2), # we need to output v, omega. graph_location=graph_location, ) # stored. self.current_image = np.zeros(self.expect_shape)
def check_gpu_available(self, context: Context): import torch available = torch.cuda.is_available() context.info(f"torch.cuda.is_available = {available!r}") context.info("init()") if available: i = torch.cuda.current_device() count = torch.cuda.device_count() name = torch.cuda.get_device_name(i) context.info(f"device {i} of {count}; name = {name!r}") else: no_hardware_GPU_available(context)
def check_gpu_available(self, context: Context): available = torch.cuda.is_available() req = os.environ.get('AIDO_REQUIRE_GPU', None) context.info(f'torch.cuda.is_available = {available!r} AIDO_REQUIRE_GPU = {req!r}') context.info('init()') self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') if available: i = torch.cuda.current_device() count = torch.cuda.device_count() name = torch.cuda.get_device_name(i) context.info(f'device {i} of {count}; name = {name!r}') else: if req is not None: msg = 'No GPU found' context.error(msg)
def check_gpu_available(self, context: Context): import torch available = torch.cuda.is_available() req = os.environ.get('AIDO_REQUIRE_GPU', None) context.info(f'torch.cuda.is_available = {available!r} AIDO_REQUIRE_GPU = {req!r}') context.info('init()') if available: i = torch.cuda.current_device() count = torch.cuda.device_count() name = torch.cuda.get_device_name(i) context.info(f'device {i} of {count}; name = {name!r}') else: if req is not None: msg = 'I need a GPU; bailing.' context.error(msg) raise RuntimeError(msg)
def on_received_episode_start(self, context: Context, data: EpisodeStart): context.info("Starting episode %s." % data)
def finish(self, context: Context): context.info("finish()")
def finish(self, context: Context): context.info('finish()')
def init(self, context: Context): context.info("init()") # Start the ROSAgent, which handles publishing images and subscribing to action self.agent = ROSAgent() context.info("inited")
def init(self, context: Context): context.info('init()')
def on_updated_config(self, context: Context, key: str, value): context.info(f"Config was updated: {key} = {value!r}")
def on_received_seed(self, context: Context, data: int): context.info(f'seed({data})')
def init(self, context: Context): context.info("init()")
def on_received_episode_start(self, context: Context, data: EpisodeStart): context.info(f"Starting episode {data.episode_name}.") yaml_payload = getattr(data, "yaml_payload", "{}") self.agent.publish_episode_start(data.episode_name, yaml_payload)
def on_received_episode_start(self, context: Context, data: EpisodeStart): context.info(f'Starting episode "{data.episode_name}".')
def on_received_seed(self, context: Context, data: int): context.info(f'seed({data})') np.random.seed(data) random.seed(data) self._create_scenarios(context)
def on_received_observations(self, context: Context, data: DB20Observations): camera: JPGImage = data.camera if self.rgb is None: context.info("received first observations") self.rgb = dcu.bgr_from_rgb(dcu.bgr_from_jpg(camera.jpg_data))
def init(self, context: Context): self.n = 0 context.info("init()")