<jupyter_start><jupyter_text>evaluate for different number of samples<jupyter_code>results_file = os.path.join(cf.out_dir, 'eval_results.pkl')

with open(results_file, "rb") as f:
    results = pickle.load(f)

e_distances = []
e_means = []
samples_column = []
    
for s in [1,4,8,16]:
    e_dist = calc_energy_distances(results['d_matrices'], num_samples=s, probability_weighted=True, label_switches=cf.label_switches)
    e_dist = e_dist[~np.isnan(e_dist)]
    e_distances.extend(e_dist)
    samples_column.extend([s] * len(e_dist))
    e_means.append(np.mean(e_dist))

energy = pd.DataFrame(data={'energy': e_distances, 'num_samples': samples_column})
means = pd.DataFrame(data={'energy': e_means, 'num_samples': [1,4,8,16]})
f = plt.figure(figsize=(5,5), dpi=150)
gs = gridspec.GridSpec(1,1)
ax = plt.subplot(gs[0,0])

sns.stripplot(x="num_samples", y="energy", data=energy, alpha=0.5, s=2, ax=ax)
sns.stripplot(x="num_samples", y="energy", data=means, s=18, marker='^', color='k', ax=ax, jitter=False)
sns.stripplot(x="num_samples", y="energy", data=means, s=14, marker='^', ax=ax, jitter=False)
ax.set_title('Probabilistic U-Net (validation set)', y=1.03)
fs=12
ax.set_ylabel(r'$D_{ged}^{2}$', fontsize=fs)
ax.set_xlabel('# samples', fontsize=fs)
print(results['pixel_frequencies'])
print(results['mode_frequencies'])

stats = get_mode_statistics(cf.label_switches, exp_modes=5)
print(sorted(stats['mode_probs']))

log_mode_frequencies = np.log(results['mode_frequencies'])
log_gt_mode_frequencies = np.log(stats['mode_probs'])
f = plt.figure(figsize=(8,7))
gs = gridspec.GridSpec(2, 2, wspace=0.0, hspace=0.0, height_ratios=[32,6], width_ratios=[3,1])

ax = plt.subplot(gs[0, 0])
sort_ixs = np.argsort(stats['mode_probs'])[::-1]
plt.bar(x=range(len(log_gt_mode_frequencies)), height=results['mode_frequencies'][sort_ixs], align='center', color='g', label='Estimated Frequencies')
plt.bar(x=range(len(log_gt_mode_frequencies)), height=stats['mode_probs'][sort_ixs], align='center', edgecolor='k', fc=(0,0,0,0), label='Ground truth')
plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom=False,      # ticks along the bottom edge are off
    top=False,         # ticks along the top edge are off
    labelbottom=False) # labels along the bottom edge are off

ax.legend()
ax.set_title('Probabilistic U-Net (validation set)', y=1.03)
ax = plt.gca()
ax.set_xlim([-.5,31.5])
ax.set_ylim([0,0.2])
yticks = ax.yaxis.get_major_ticks()
yticks[0].label1.set_visible(False)
ax.set_ylabel('P', fontdict={'size': 15})

rows = list(cf.label_switches.keys())
cell_text = np.transpose(stats['switch'][sort_ixs], axes=[1,0])

ax = plt.subplot(gs[1, 0])
the_table = ax.table(cellText=cell_text,
                      rowLabels=rows,
                      loc='center')

ax.get_yaxis().set_visible(False)
plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom=False,      # ticks along the bottom edge are off
    top=False,         # ticks along the top edge are off
    labelbottom=False) # labels along the bottom edge are off
ax.set_xlabel('Discrete Modes')

ax = plt.subplot(gs[1, 1])
ax.set_title('Pixel-wise Frequency Estimation',fontdict={'size': 6})
plt.barh(y=range(5), width=results['pixel_frequencies'][:,1], align='center', color='g')
plt.barh(y=range(5), width=list(cf.label_switches.values()), align='center', edgecolor='k', fc=(0,0,0,0))
ax.get_yaxis().set_visible(False)
ax.set_xticks([0.1,0.2,0.3,0.4,0.5])
ax.set_xlabel('P')
ax.tick_params(axis='both', which='major', labelsize=6)<jupyter_output><empty_output><jupyter_text>prepare animated gif from samples<jupyter_code>from matplotlib.lines import Line2D
from data.cityscapes.data_loader import map_labels_to_trainId

# get dictionary of image files
data_dir = os.path.join(cf.data_dir, cf.resolution)
data_dict = loadFiles(label_density=cf.label_density, split='val', input_path=data_dir,
                      cities=None, instance=False)

# list of image indices in data_dict
ixs = [0,2,75,85,100,145,350,390,460,470]
num_samples = 16
y_dim = 256
x_dim = 512

img_arr = np.zeros(shape=(y_dim, len(ixs) * x_dim, 3)) 
gt_arr = np.zeros(shape=(y_dim, len(ixs) * x_dim))
sample_arr = np.zeros(shape=(num_samples, y_dim, len(ixs) * x_dim))

for i,ix in enumerate(ixs):

    img_key = list(data_dict.keys())[ix]
    img_path = data_dict[img_key]['data']
    seg_path = data_dict[img_key]['seg']
    
    # load images
    img_arr[:, i * x_dim: (i + 1) * x_dim] = np.transpose(np.load(img_path), axes=[1,2,0]) / 255.
    
    # load gt segmentation
    gt_arr[:, i * x_dim: (i + 1) * x_dim] = map_labels_to_trainId(np.load(seg_path))
    
    # load samples
    for sample_num in range(num_samples):
        sample_path = os.path.join(cf.out_dir, '{}_sample{}_labelIds.npy'.format(img_key, sample_num))
        sample_arr[sample_num, :, i * x_dim: (i + 1) * x_dim] = np.load(sample_path)[0,0]

for sample_num in range(num_samples):
    f = plt.figure(figsize=(len(ixs) * 4, 9))

    arr = np.concatenate([img_arr, to_rgb(gt_arr, cmap=cf.color_map), to_rgb(sample_arr[sample_num], cmap=cf.color_map)], axis=0)
    
    plt.imshow(arr)
    plt.text(-80,170, 'image', fontsize=12, rotation=90, rotation_mode='anchor')
    plt.text(-80,470, 'deterministic', fontsize=12, rotation=90, rotation_mode='anchor')
    plt.text(-40,460, 'groundtruth', fontsize=12, rotation=90, rotation_mode='anchor')
    plt.text(-80,690, 'sample {}'.format(sample_num + 1), fontsize=12, rotation=90, rotation_mode='anchor')
    
    ax = plt.gca()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    
    # custom legend
    trainId2name = cf.trainId2name
    trainId2name[255] = 'ignore'
    legend_handles = [Line2D([0], [0], color=tuple([c / 255. for c in cf.color_map[trainID]]), lw=4, label=trainId2name[trainID]) for trainID in list(cf.color_map.keys())]   
    ax.legend(handles=legend_handles, loc=9, bbox_to_anchor=(0.5, -0.03), ncol=len(legend_handles), frameon=False)
    out_dir = os.path.join(cf.out_dir, 'gif_panel_sample_{}.png'.format(sample_num))
    plt.savefig(out_dir, dpi=200, bbox_inches='tight', pad_inches=0.0)
    plt.close()
    print('Saved to {}'.format(out_dir))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1693130011004559, 'fraction_numerical': 0.03175601320547084, 'mean_word_length': 5.176699029126214, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '642405', 'license': 'Apache-2.0', 'n_tokens_mistral': 2574, 'n_tokens_neox': 2472, 'n_words': 431, 'path': 'evaluation/evaluation_plots.ipynb', 'repo_name': 'giuliano-sider/probabilistic_unet', 'size': '6361'}