-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
85 lines (68 loc) · 2.54 KB
/
metrics.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
from image import *
import os
def calc_precision_single_query(resultados, classname):
p = 0
for chave in resultados:
if classname in chave:
p += 1
precision = p / len(resultados)
return precision
import json
def mock_json(resultados, classname, tecnica):
jsonn = []
for arq in resultados:
if classname in arq:
jsonn.append({'img': arq, 'relevant': True, 'irrelevant': False })
else:
jsonn.append({'img': arq, 'relevant': False, 'irrelevant': True })
jsonn.append({'classname': classname})
jsonn.append({'tecnica': tecnica})
return jsonn
import time
def collect_cbir(tecnica):
cont = 0
tic = time.clock()
avg_geral = []
print(f"START {tecnica}")
avg_classes = {'Hors':[], 'Food':[], 'Dino':[], 'Moun':[], 'Elep':[], 'Afri':[], 'Beac':[], 'Buse':[], 'Buil':[], 'Flow':[]}
with open(f'single_results_{tecnica}_20.txt', 'a') as arquivo:
for fil in os.listdir('corel1000'):
#print(f"ARQUIVO {fil}")
avg_file = []
cont+=1
if cont <= 810:
continue
cbir = CBIR()
hists = cbir.run_process(f'corel1000/{fil}')
avg_file.append(calc_precision_single_query(hists, fil[:4]))
for i in range(4):
jsonn = mock_json(hists, fil[:4], tecnica)
tecnica = jsonn.pop()['tecnica']
nomeclasse = jsonn.pop()['classname']
if tecnica == "qpm":
hists = cbir.refilter_imgs(jsonn)
elif tecnica == "multiquery":
hists = cbir.multiple_query_point_search(jsonn)
elif tecnica == "rfra":
hists = cbir.rfra(jsonn)
avg_file.append(calc_precision_single_query(hists, fil[:4]))
#print(avg_file)
arquivo.write(fil + ':' + str(avg_file) + '\n')
avg_geral.append((fil, np.mean(avg_file)))
#cont += 1
for tup in avg_geral:
fil = tup[0][:4]
avg_classes[fil].append(tup[1])
toc = time.clock()
with open('results20.txt', 'a') as f:
f.write(tecnica + "\n")
for classe in avg_classes:
avg_classes[classe] = np.mean(avg_classes[classe])
f.write(classe + ':' + str(avg_classes[classe])+'\n')
f.write("\n")
f.write("TEMPO: " + str(toc - tic))
print(avg_geral)
print(avg_classes)
#collect_cbir("multiquery")
#collect_cbir("rfra")
collect_cbir("qpm")