-
Notifications
You must be signed in to change notification settings - Fork 13
/
plot_result_distribution.py
174 lines (141 loc) · 5.38 KB
/
plot_result_distribution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Plots the distribution of class guesses for an animal instance
import json
import numpy as np
import matplotlib.pyplot as plt
import os
from collections import OrderedDict
from tqdm import tqdm
def plot_all_labels(data, title=None, save=None, showplot=False):
'''
Creates a plot of the distribution of the top label for the images in
the provided data file
'''
# Store the top class and confidence for each image
imgs = data.keys()
all_labels = []
all_conf = []
for i in imgs: # Do all images
# for i in np.random.choice(list(imgs), 50, replace=False): # Select 50 random images
try:
toplabel = data[i]['labels'][-1]
toplabel_conf = float(data[i]['confs'][-1])
all_labels.append(toplabel)
all_conf.append(toplabel_conf)
except:
pass
unique_labels = np.unique(all_labels)
all_labels = np.array(all_labels)
all_conf = np.array(all_conf)
# Plot the chosen labels by their frequency
label_counts = OrderedDict()
label_conf = OrderedDict()
for l in unique_labels: # Count the number of times this label has been selected
label_counts[l] = np.sum(all_labels == l)
conf = all_conf[all_labels==l]
avg_conf = np.mean(conf)
label_conf[l] = avg_conf
# Plot
labels = np.array(list(label_counts.keys())) # Labels
count = np.array(list(label_counts.values())) # Count of each label
freq = count/sum(count) # Frequency of each label
confs = np.array(list(label_conf.values())) # Confidence of each label
assert(label_conf.keys() == label_counts.keys())
to_sort = np.argsort(count)[::-1]
colors = [(x/100, 0, 0) for x in confs[to_sort]]
k = kurtosis(freq[to_sort])
H = entropy(freq[to_sort])
# title += ' (k=' + str(round(k,1)) + ', H=' + str(round(H,1)) + ')'
plotfig(range(len(labels)), freq[to_sort], labels[to_sort], colors, title, save)
return label_counts, label_conf, k, H
def kurtosis(data):
"""
Compute the kurtosis for the given distribution
k = (SUM((Yi-Ybar)^4/N)/s^2)
"""
Y = np.array(data)
s = np.std(Y)
N = len(Y)
Ybar = np.mean(Y)
k = (np.sum(((Y-Ybar)**4)/N)/(1e-15 + s**4))
return k
def entropy(data):
"""
Compute the Shannon entropy for the given distribution
H = -sum(Pi*log2*Pi)
"""
H = -np.sum((data*np.log2(data)))
return H
def plotfig(xs, ys, labels, col, title, savepath):
if len(ys)>50:
plt.figure(figsize=[len(xs)*.15, 7])
else:
plt.figure()
ax = plt.scatter(xs, ys, color=col, zorder=2)
plt.title(title, fontsize=12)
plt.xlabel('Label Categories', fontsize=10)
plt.ylabel('Label frequency', fontsize=10)
plt.ylim(bottom=0, top=1.0)
ylocs, _ = plt.yticks()
plt.hlines(np.arange(0,1.0, 0.1), xmin=0, xmax=len(labels), colors='lightgrey', linestyles='dashed', zorder=1, linewidth=0.5)
plt.xticks(np.arange(len(labels)), labels, rotation='vertical', fontsize=8)
plt.tight_layout()
# Add confidence numbers
# for i, txt in enumerate(confs[to_sort]):
# plt.annotate(str(int(txt))+'%',
# xy=(range(len(keys))[i], vals[to_sort][i]),
# xytext=(range(len(keys))[i]-0.5, vals[to_sort][i]),
# fontsize='x-small',
# rotation=0)
plt.savefig(savepath, dpi=200)
plt.close()
if __name__ == "__main__":
savepath = os.getcwd() + '/plots_label_dist_alexnet/Aves/'
basepath = os.getcwd() + '/alexnet_inat_results/Aves/'
total_count_dict = OrderedDict()
total_conf_dict = OrderedDict()
all_distros = {}
# Plot the results from iNaturalist birds
for fname in tqdm(os.listdir(basepath)):
with open(basepath+fname, 'r') as f:
f = json.load(f)
# Optionally, skip any classes with under 50 images
# if len(f.keys())<50:
# continue
name = fname.split('.')[0]
all_distros[name] = {} # Stores label distribution, kurtosis, and entropy
title = "Label distribution, "+name
label_counts, label_confs, k, H = plot_all_labels(f, save=savepath+'/plots/'+name+'.jpg', title=title)
# Store information about the distribution
order = np.argsort(list(label_counts.values()))[::-1]
all_distros[name]['labels'] = list(np.array(list(label_counts.keys()))[order])
all_distros[name]['counts'] = [int(i) for i in np.array(list(label_counts.values()))[order]]
all_distros[name]['confs'] = [float(i) for i in np.array(list(label_confs.values()))[order]]
all_distros[name]['kurtosis'] = str(k)
all_distros[name]['entropy'] = str(H)
for l in label_counts.keys():
if l not in total_count_dict.keys():
total_count_dict[l] = 0
total_conf_dict[l] = []
total_count_dict[l] += label_counts[l]
total_conf_dict[l].append(label_confs[l])
# Save distribution data as json
with open(savepath+'Aves_distros.json', 'w') as outfile:
json.dump(all_distros, outfile)
# Get the overall distribution for the entire category (e.g. Aves)
total_count = list(total_count_dict.values())
all_labels = list(total_count_dict.keys())
all_freq = total_count/sum(total_count)
# Get average confidence per label
all_conf = []
for c in total_conf_dict.keys():
conflist = total_conf_dict[c]
all_conf.append(sum(conflist)/len(conflist))
# Sort by decreasing frequency
order = np.argsort(all_freq)[::-1]
all_freq_sorted = np.array(all_freq)[order]
all_conf_sorted = np.array(all_conf)[order]
all_labels_sorted = np.array(all_labels)[order]
colors = [[x/100, 0, 0] for x in all_conf_sorted]
# Plot the label distribution for the entire category
title='Top 50 Labels - Aves'
plotfig(range(len(all_freq_sorted))[:50], all_freq_sorted[:50], all_labels_sorted[:50], colors[:50], title, savepath+'Aves.jpg')