Skip to content

Commit

Permalink
First commit for iclr
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenevinitsky committed Aug 26, 2020
1 parent 0275e7d commit e35cffa
Show file tree
Hide file tree
Showing 12 changed files with 1,144 additions and 137 deletions.
19 changes: 10 additions & 9 deletions bayesian_inference/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
PED_FRONT = PED_IDX_LST[0]
PED_BACK = PED_IDX_LST[-1]

NOISE_STD = 0.0

def get_filtered_posteriors(env, controller, action, dummy_obs, joint_priors, agent_id, num_locs=4):
def get_filtered_posteriors(env, controller, action, dummy_obs, joint_priors, agent_id, num_locs=4,
noise_std=0.0):
"""Black box predictor of the probability of pedestrian in each of the 4 crosswalk locations
Parameters
Expand Down Expand Up @@ -88,16 +87,18 @@ def get_filtered_posteriors(env, controller, action, dummy_obs, joint_priors, ag

s_all_modified = np.copy(
s_all) # s_all_modified = hypothetical state that an agent observes
s_all_modified[PED_IDX_LST] = lst_comb
int_list = [int(element) for element in lst_comb]
s_all_modified[PED_IDX_LST] = int_list
# _, _, logit = agent.compute_action(s_all_modified, policy_id=policy_map_fn(agent_id), full_fetch=True)
mu = controller.get_action_with_ped(env, s_all_modified, change_speed_mode=False)
mu = controller.get_action_with_ped(env, s_all_modified, ped=int_list, change_speed_mode=False, always_return_action=True)
if mu is not None:
controller.get_action_with_ped(env, s_all_modified, change_speed_mode=False)
sigma = NOISE_STD
controller.get_action_with_ped(env, s_all_modified, ped=int_list, change_speed_mode=False, always_return_action=True)
sigma = noise_std
# noise up your model
if sigma > 0.0:
mu += np.random.normal(loc=0.0, scale=sigma)
joint_likelihood_density = max(min(accel_pdf(mu, sigma, action), 10.0), 0.01)
# joint_likelihood_density = max(min(accel_pdf(mu, sigma, action), 10.0), 0.01)
joint_likelihood_density = accel_pdf(mu, sigma, action)
else:
if mu == action:
joint_likelihood_density = 1
Expand Down Expand Up @@ -171,7 +172,7 @@ def get_filtered_posteriors(env, controller, action, dummy_obs, joint_priors, ag
# now return both the priors and the marginalized estimates
ped_vals = []
for loc_ in range(num_locs):
single_prior_str = f'o_{loc_} = 0'
single_prior_str = f'o_{loc_} = 1'
ped_vals.append(single_priors_filter[single_prior_str][-1])
return ped_vals, joint_priors

Expand Down
112 changes: 74 additions & 38 deletions bayesian_inference/test_bayesian_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@

# Convert constants to uppercase
K = 5
NOISE_STD = 0.0
NOISE_STD = 0.1001
FONT_SIZE = 20
ped_idx_lst = [10, 11, 12, 13]
ped_front = ped_idx_lst[0]
ped_back = ped_idx_lst[-1]
Expand Down Expand Up @@ -260,18 +261,20 @@ def run_env(env, agent, flow_params, is_discrete, rule_based, render):

for str_comb, lst_comb in zip(joint_ped_combos_str, joint_ped_combos_int_list):

int_list = [int(element) for element in lst_comb]
s_all_modified = np.copy(
s_all) # s_all_modified = hypothetical state that an agent observes
s_all_modified[ped_idx_lst] = lst_comb
# _, _, logit = agent.compute_action(s_all_modified, policy_id=policy_map_fn(agent_id), full_fetch=True)
if rule_based:
mu = env.k.vehicle.get_acc_controller(agent_id).get_action_with_ped(env,
s_all_modified)
mu = env.k.vehicle.get_acc_controller(agent_id).get_action_with_ped(env, s_all_modified,
ped=int_list, change_speed_mode=False)
sigma = NOISE_STD
# noise up your model
if sigma > 0.0:
mu += np.random.normal(loc=0.0, scale=sigma)
joint_likelihood_density = max(min(accel_pdf(mu, sigma, action_), 10.0), 0.01)
# joint_likelihood_density = max(min(accel_pdf(mu, sigma, action_), 10.0), 0.01)
joint_likelihood_density = accel_pdf(mu, sigma, action_)
else:
if mu == action_:
joint_likelihood_density = 1
Expand Down Expand Up @@ -470,61 +473,86 @@ def run_env(env, agent, flow_params, is_discrete, rule_based, render):
b_ = single_posteriors_fixed[single_posterior_str]
c_ = single_posteriors_updated_K[single_posterior_str]
d_ = single_posteriors_filter[single_posterior_str]
if val == 1:
title = 'Probability of Pedestrian'
else:
title = 'Probability of No Pedestrian'
plot_2_lines(a_, b_, [f'Pr(ped in grid {loc} = {val}) using updated priors K = all',
f'Pr(ped in grid {loc} = {val}) using fixed priors'],
'figures/all_v_fixed_loc_{}_val_{}'.format(loc, val))
'figures/all_v_fixed_loc_{}_val_{}'.format(loc, val),
fig_title=title)
plot_2_lines(c_, b_, [f'Pr(ped in grid {loc} = {val}) using updated priors K = 5',
f'Pr(ped in grid {loc} = {val}) using fixed priors'],
'figures/5_v_fixed_loc_{}_val_{}'.format(loc, val))
plot_2_lines(d_, b_, [f'Pr(ped in grid {loc} = {val}) using filtered priors',
f'Pr(ped in grid {loc} = {val}) using fixed priors'],
'figures/filter_v_fixed_loc_{}_val_{}'.format(loc, val))
'figures/5_v_fixed_loc_{}_val_{}'.format(loc, val), fig_title=title)
plot_2_lines(d_, c_, [f'Filtered priors',
f'Updated priors'],
'figures/filter_v_updated_loc_{}_val_{}'.format(loc, val), fig_title=title)

fig = plt.figure(figsize=(15, 15))
y0 = visible_pedestrian_dct[0]
y1 = visible_pedestrian_dct[1]
y2 = visible_pedestrian_dct[2]
y3 = visible_pedestrian_dct[3]

vis_ped_0 = plt.subplot(2, 4, 1)
fig = plt.figure(figsize=(12, 12))
vis_ped_0 = plt.subplot(1, 1, 1)
plt.plot(y0, 'o-')
vis_ped_0.set_title('Pedestrian in loc 0')
vis_ped_0.set_xlabel('time (s)')
vis_ped_0.set_ylabel('in loc 1?')
vis_ped_0.set_xlabel('Time (s)')
vis_ped_0.set_ylabel('Pedestrian Boolean Value')
vis_ped_0.set_ylim([-1, 1])
for item in ([vis_ped_0.title, vis_ped_0.xaxis.label, vis_ped_0.yaxis.label] +
vis_ped_0.get_xticklabels() + vis_ped_0.get_yticklabels()):
item.set_fontsize(FONT_SIZE)
plt.savefig('figures/ground_truth_0.png')

vis_ped_1 = plt.subplot(2, 4, 2)
fig = plt.figure(figsize=(12, 12))
vis_ped_1 = plt.subplot(1, 1, 1)
plt.plot(y1, 'o-')
vis_ped_1.set_title('Pedestrian in loc 1')
vis_ped_1.set_xlabel('time (s)')
vis_ped_1.set_ylabel('in loc 1?')
vis_ped_1.set_xlabel('Time (s)')
vis_ped_1.set_ylabel('Pedestrian Boolean Value')
vis_ped_1.set_ylim([-1, 1])

vis_ped_2 = plt.subplot(2, 4, 3)
vis_ped_2.plot(y2, '.-')
for item in ([vis_ped_1.title, vis_ped_1.xaxis.label, vis_ped_1.yaxis.label] +
vis_ped_1.get_xticklabels() + vis_ped_1.get_yticklabels()):
item.set_fontsize(FONT_SIZE)
plt.savefig('figures/ground_truth_1.png')

fig = plt.figure(figsize=(12, 12))
vis_ped_2 = plt.subplot(1, 1, 1)
plt.plot(y2, 'o-')
vis_ped_2.set_title('Pedestrian in loc 2')

vis_ped_2.set_xlabel('time (s)')
vis_ped_2.set_ylabel('in loc 2?')
vis_ped_2.set_xlabel('Time (s)')
vis_ped_2.set_ylabel('Pedestrian Boolean Value')
vis_ped_2.set_ylim([-1, 1])

vis_ped_3 = plt.subplot(2, 4, 4)
vis_ped_3.plot(y3, '.-')
vis_ped_3.set_title('Pedestrian in loc 3')

vis_ped_3.set_xlabel('time (s)')
vis_ped_3.set_ylabel('in loc 3?')
for item in ([vis_ped_2.title, vis_ped_2.xaxis.label, vis_ped_2.yaxis.label] +
vis_ped_2.get_xticklabels() + vis_ped_2.get_yticklabels()):
item.set_fontsize(FONT_SIZE)
plt.savefig('figures/ground_truth_2.png')

fig = plt.figure(figsize=(12, 12))
vis_ped_3 = plt.subplot(1, 1, 1)
plt.plot(y3, 'o-')
vis_ped_3.set_title('Pedestrian in loc 2')
vis_ped_3.set_xlabel('Time (s)')
vis_ped_3.set_ylabel('Pedestrian Boolean Value')
vis_ped_3.set_ylim([-1, 1])
for item in ([vis_ped_3.title, vis_ped_3.xaxis.label, vis_ped_3.yaxis.label] +
vis_ped_3.get_xticklabels() + vis_ped_3.get_yticklabels()):
item.set_fontsize(FONT_SIZE)
plt.savefig('figures/ground_truth_3.png')

fig = plt.figure(figsize=(12, 12))
print(intersection_status, len(intersection_status))
intersection = plt.subplot(2, 4, 5)
intersection = plt.subplot(1, 1, 1)
intersection.plot(intersection_status)
intersection.set_title('-1 = approaching, 0 = on intersection, 1 = past')
intersection.set_xlabel('time (s)')
intersection.set_ylabel('rl car location')
intersection.set_title('Time vs. Car Position')
intersection.set_xlabel('Time (s)')
intersection.set_ylabel('Observed Car Location')
intersection.set_ylim([-1, 1])

plt.savefig('figures/ground_truth.png')
for item in ([intersection.title, intersection.xaxis.label, intersection.yaxis.label] +
intersection.get_xticklabels() + intersection.get_yticklabels()):
item.set_fontsize(FONT_SIZE)
plt.savefig('figures/vehicle_location.png')

# sigma = plt.subplot(2, 4, 6)
# sigma.plot(sigma_vals)
Expand Down Expand Up @@ -745,15 +773,23 @@ def run_transfer(args, action_pairs_lst=[], imitation_model=True, is_discrete=Fa
return action_pairs_lst


def plot_2_lines(y1, y2, legend, title, viewable_ped=False):
plt.figure()
def plot_2_lines(y1, y2, legend, title, fig_title, viewable_ped=False):
plt.figure(figsize=(12, 12))
vis_ped_0 = plt.subplot(1, 1, 1)
x = np.arange(len(y1))
plt.plot(x, y1)
plt.plot(x, y2)
if viewable_ped:
plt.plot(x, viewable_ped)
plt.legend(legend, bbox_to_anchor=(0.5, 1.05), loc=3, borderaxespad=0.)
# plt.legend(legend, bbox_to_anchor=(0.5, 1.05), loc=3, borderaxespad=0.)
plt.legend(legend)
plt.ylim(-0.1, 1.1)
plt.xlabel('Time')
plt.ylabel('Probability')
plt.title(fig_title)
for item in ([vis_ped_0.title, vis_ped_0.xaxis.label, vis_ped_0.yaxis.label] +
vis_ped_0.get_xticklabels() + vis_ped_0.get_yticklabels()):
item.set_fontsize(FONT_SIZE)

# plt.draw()
# plt.pause(0.001)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def make_flow_params():
# support traffic light compliance
vehicles = VehicleParams()

#TODO(klin) make sure the autonomous vehicle being placed here is placed in the right position
vehicles.add(
veh_id="human",
acceleration_controller=(SimCarFollowingController, {}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_flow_params(args=None, pedestrians=True, render=True):
depart_time='0.00',
start='(1.2)--(1.1)',
end='(1.1)--(1.0)',
depart_pos=f'{43 + 0.5 * i}',
depart_pos=f'{47 + 0.5 * i}',
arrival_pos='5')

# we place a sufficient number of vehicles to ensure they confirm with the
Expand Down
Loading

0 comments on commit e35cffa

Please sign in to comment.