def test_filter(env_response): crop_low = np.array([0, 5, 10]) crop_high = np.array([5, 10, 20]) crop_filter = InputFilter() crop_filter.add_observation_filter('observation', 'crop', ObservationCropFilter(crop_low, crop_high)) result = crop_filter.filter(env_response)[0] unfiltered_observation = env_response.next_state['observation'] filtered_observation = result.next_state['observation'] # validate the shape of the filtered observation assert filtered_observation.shape == (5, 5, 10) # validate the content of the filtered observation assert np.all(filtered_observation == unfiltered_observation[0:5, 5:10, 10:20]) # crop with -1 on some axes crop_low = np.array([0, 0, 0]) crop_high = np.array([5, -1, -1]) crop_filter = InputFilter() crop_filter.add_observation_filter('observation', 'crop', ObservationCropFilter(crop_low, crop_high)) result = crop_filter.filter(env_response)[0] unfiltered_observation = env_response.next_state['observation'] filtered_observation = result.next_state['observation'] # validate the shape of the filtered observation assert filtered_observation.shape == (5, 20, 30) # validate the content of the filtered observation assert np.all(filtered_observation == unfiltered_observation[0:5, :, :])
def test_get_filtered_observation_space(): crop_low = np.array([0, 5, 10]) crop_high = np.array([5, 10, 20]) crop_filter = InputFilter() crop_filter.add_observation_filter('observation', 'crop', ObservationCropFilter(crop_low, crop_high)) observation_space = ObservationSpace(np.array([5, 10, 20])) filtered_observation_space = crop_filter.get_filtered_observation_space('observation', observation_space) # make sure the new observation space shape is calculated correctly assert np.all(filtered_observation_space.shape == np.array([5, 5, 10])) # make sure the original observation space is unchanged assert np.all(observation_space.shape == np.array([5, 10, 20])) # crop_high is bigger than the observation space high_error_observation_space = ObservationSpace(np.array([3, 8, 14])) with pytest.raises(ValueError): crop_filter.get_filtered_observation_space('observation', high_error_observation_space) # crop_low is bigger than the observation space low_error_observation_space = ObservationSpace(np.array([3, 3, 10])) with pytest.raises(ValueError): crop_filter.get_filtered_observation_space('observation', low_error_observation_space) # crop with -1 on some axes crop_low = np.array([0, 0, 0]) crop_high = np.array([5, -1, -1]) crop_filter = InputFilter() crop_filter.add_observation_filter('observation', 'crop', ObservationCropFilter(crop_low, crop_high)) observation_space = ObservationSpace(np.array([5, 10, 20])) filtered_observation_space = crop_filter.get_filtered_observation_space('observation', observation_space) # make sure the new observation space shape is calculated correctly assert np.all(filtered_observation_space.shape == np.array([5, 10, 20]))
def test_filter_stacking(): # test that filter stacking works fine by taking as input a transition with: # - an observation of shape 210x160, # - a reward of 100 # filtering it by: # - rescaling the observation to 110x84 # - cropping the observation to 84x84 # - clipping the reward to 1 # - stacking 4 observations to get 84x84x4 env_response = EnvResponse({'observation': np.ones([210, 160])}, reward=100, game_over=False) filter1 = ObservationRescaleToSizeFilter( output_observation_space=ImageObservationSpace(np.array([110, 84]), high=255), ) filter2 = ObservationCropFilter( crop_low=np.array([16, 0]), crop_high=np.array([100, 84]) ) filter3 = RewardClippingFilter( clipping_low=-1, clipping_high=1 ) output_filter = ObservationStackingFilter( stack_size=4, stacking_axis=-1 ) input_filter = InputFilter( observation_filters={ "observation": OrderedDict([ ("filter1", filter1), ("filter2", filter2), ("output_filter", output_filter) ])}, reward_filters=OrderedDict([ ("filter3", filter3) ]) ) result = input_filter.filter(env_response)[0] observation = np.array(result.next_state['observation']) assert observation.shape == (84, 84, 4) assert np.all(observation == np.ones([84, 84, 4])) assert result.reward == 1
def create_agents(self): for agent_name, agent_parameters in self.agents_parameters.items(): agent_parameters.name = agent_name # create agent self.agents[agent_parameters.name] = short_dynamic_import(agent_parameters.path)(agent_parameters, parent=self) self.agents[agent_parameters.name].parent_level_manager = self.parent_level_manager # TODO: this is a bit too specific to be defined here # add an attention cropping filter if the incoming directives are attention boxes if isinstance(self.in_action_space, AttentionActionSpace): attention_size = self.in_action_space.forced_attention_size for agent in self.agents.values(): agent.input_filter.observation_filters['attention'] = \ ObservationCropFilter(crop_low=np.zeros_like(attention_size), crop_high=attention_size) agent.input_filter.observation_filters.move_to_end('attention', last=False) # add the cropping at the beginning
RegressionHeadParameters(), RegressionHeadParameters() ] # agent_params.network_wrappers['main'].num_output_head_copies = 4 # follow lane, left, right, straight agent_params.network_wrappers['main'].rescale_gradient_from_head_by_factor = [1, 1, 1, 1] agent_params.network_wrappers['main'].loss_weights = [1, 1, 1, 1] # TODO: there should be another head predicting the speed which is connected directly to the forward camera embedding agent_params.network_wrappers['main'].batch_size = 120 agent_params.network_wrappers['main'].learning_rate = 0.0002 # crop and rescale the image + use only the forward speed measurement agent_params.input_filter = InputFilter() agent_params.input_filter.add_observation_filter('CameraRGB', 'cropping', ObservationCropFilter(crop_low=np.array([115, 0, 0]), crop_high=np.array([510, -1, -1]))) agent_params.input_filter.add_observation_filter('CameraRGB', 'rescale', ObservationRescaleToSizeFilter( ImageObservationSpace(np.array([88, 200, 3]), high=255))) agent_params.input_filter.add_observation_filter('CameraRGB', 'to_uint8', ObservationToUInt8Filter(0, 255)) agent_params.input_filter.add_observation_filter( 'measurements', 'select_speed', ObservationReductionBySubPartsNameFilter( ["forward_speed"], reduction_method=ObservationReductionBySubPartsNameFilter.ReductionMethod.Keep)) # no exploration is used agent_params.exploration = AdditiveNoiseParameters() agent_params.exploration.noise_percentage_schedule = ConstantSchedule(0) agent_params.exploration.evaluation_noise_percentage = 0 # no playing during the training phase