图像检索的简单实现

首先还是先导入必要的包,代码如下:

1
2
3
4
5
6
7
8
9
10
11
import scipy.io as sio
from scipy.misc import imread, imresize
import tensorflow as tf
import vgg16
import numpy as np
import glob as gb
import os
from matplotlib import pyplot as plt
from sklearn.neighbors import KDTree
import linecache
import time
/usr/local/lib/python3.5/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters

然后导入预训练好的vgg16网络模型,加载到网络里,并准备好所有图像,准备构建图像数据库,代码如下:

1
2
3
4
5
6
7
8
9
10
11
sess = tf.Session()
imgs = tf.placeholder(tf.float32, [None, 224, 224, 3])
vgg = vgg16.Vgg16('vgg16.npy')
images = tf.placeholder("float", [1, 224, 224, 3])
vgg.build(images)

file = 'dataset/'
filenames = os.listdir(file)
filenames.sort()

feature_img = np.zeros(shape=[1,4096])
npy file loaded
build model started
build model finished: 0s

再接着就是将图像加载进预训练的vgg16网络里,这里使用fc7层的特征作为特征,其是大小为4096的向量,遍历所有图像,将各个图像的特征逐行保存如feature.npy文件里,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
f = open('label.txt', 'w')
for filename in filenames:
img_path = gb.glob(file+filename+"/*.jpg")
i = 1
img_path.sort()
for path in img_path:
i = i%51
img = imread(path, mode='RGB')
print(filename + str(i) + '.jpg')
img = imresize(img, (224, 224))
img = np.expand_dims(img,axis=0)
feed_dict = {images: img}
feature = sess.run(vgg.fc7, feed_dict=feed_dict)
feature = np.reshape(feature, [1,4096])
feature_img = np.concatenate([feature_img,feature],axis=0)
# dic = {'features': feature}
# sio.savemat('feature/'+filename+ str(i) + '.mat', dic)
name = path+'\n'
f.write(name)
i+=1
f.close()
np.save('feature.npy',feature_img)

然后就是加载测试图片,获得测试图片特征的代码如下所示:

1
2
3
4
5
6
7
8
def extract_feature(path):
img = imread(path, mode='RGB')
img = imresize(img, (224, 224))
img = np.expand_dims(img, axis=0)
feed_dict = {images: img}
test_feature = sess.run(vgg.fc7, feed_dict=feed_dict)
test_feature = np.reshape(test_feature, [1,4096])
return test_feature

首先测试一张图的评价指标,这里使用了airplanes/image_0001.jpg,图片展示如下:

1
2
3
4
plt.figure()
image = plt.imread('dataset/airplanes/image_0001.jpg')
plt.imshow(image)
plt.show()

然后获得这张图的特征,并和前面已经获得的图像库的特征进行匹配,获得最近的十张图,结果如下:

1
2
3
4
5
6
7
8
9
start = int(round(time.time()* 1000))
test_feature = extract_feature('dataset/airplanes/image_0001.jpg')
feature_lib = np.load('feature.npy')
tree = KDTree(feature_lib, leaf_size=10)
dist, ind = tree.query(test_feature, k=10)
end = int(round(time.time()* 1000))
print("消耗的时间是%.f ms"%(end-start))
print("距离最近的十张图距离是",dist)
print("距离最近的十张图的索引是",ind)
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:2: DeprecationWarning: `imread` is deprecated!
`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``imageio.imread`` instead.

/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:3: DeprecationWarning: `imresize` is deprecated!
`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``skimage.transform.resize`` instead.
  This is separate from the ipykernel package so we can avoid doing imports until


消耗的时间是816 ms
距离最近的十张图距离是 [[    0.         31118.32060277 32061.30867895 33426.1640262
  33756.59609599 33955.5871336  34147.39183701 34280.35701924
  34424.74113314 34482.05957456]]
距离最近的十张图的索引是 [[  51   66   83 1976 1855   57 1764 1897 1768   97]]

获得其中最接近的三张图的地址,展现这些图的图片,如下所示:

1
2
3
4
5
6
7
8
9
theline1=linecache.getline("label.txt",int(ind[0][0]))
theline2=linecache.getline("label.txt",int(ind[0][1]))
theline3=linecache.getline("label.txt",int(ind[0][2]))
for line in [theline1,theline2,theline3]:
plt.figure()
image = plt.imread(line[:-1])
plt.imshow(image)
print(line)
plt.show()
dataset/airplanes/image_0001.jpg
dataset/airplanes/image_0016.jpg
dataset/airplanes/image_0033.jpg

从上面可以看出,展示出的图片和测试图片类别完全一致,其中最接近测试图片的图和测试图片一模一样。

测试一张图片的P-K,R-K,F-K和MRR-K指标

还是以刚才那张图为例,这里先以k=10为例,测试其这四个指标,如下所示:

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
def Get_score(path,ind):
rank = []
corre = 0
for i in range(ind[0].shape[0]):
if int(ind[0][i]) == 2050:
ind[0][i] = 2049
if(path.split('/')[1] == linecache.getline("label.txt",int(ind[0][i])+1).split('/')[1]):
corre+=1
rank.append(i)
# print(linecache.getline("label.txt",int(ind[0][i])).split('/')[1])
rank = np.array(rank)
P_score = corre/ind[0].shape[0]
R_score = corre/50
if P_score!=0 and R_score!=0:
F1_score = 2/(1/P_score+1/R_score)
else:
F1_score = 0
MRR = 0
for j in range(corre):
MRR += 1/(rank[j]+1)
if corre!=0:
MRR_score = MRR*1/corre
else:
MRR_score = 0
return P_score,R_score,F1_score,MRR_score
P_score,R_score,F1_score,MRR_score = Get_score('dataset/airplanes/imagsuose_0001.jpg',ind)
print('precision score',P_score)
print('Recall score',R_score)
print('F1 score',F1_score)
print('MRR score',MRR_score)
precision score 0.5
Recall score 0.1
F1 score 0.16666666666666666
MRR score 0.42000000000000004

可以看出,precision的准确率为百分之50,recall率为百分之10,F1分数为0.166,MRR score为0.42

测试所有图片的P-K,R-K,F-K和MRR-K指标

这里要测试2000张图的这四个指标,先测k=10使,各个类别的这四个指标,代码如下:

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
def evaluate(num):
P_final = 0
R_final = 0
F1_final = 0
MRR_final = 0
for filename in filenames:
if filename == 'clutter':
continue
img_path = gb.glob(file+filename+"/*.jpg")
img_path.sort()
P_total = 0
R_total = 0
F1_total = 0
MRR_total = 0
for path in img_path:
test_feature = extract_feature(path)
tree = KDTree(feature_lib, leaf_size=10)
dist, ind = tree.query(test_feature, k=num)
P_score,R_score,F1_score,MRR_score = Get_score(path,ind)
P_total+=P_score
R_total+=R_score
F1_total+=F1_score
MRR_total+=MRR_score
print("类别是:",filename,"Precision是",P_total/50,"Recall是",R_total/50,"F1是",F1_total/50,"MRR是",MRR_total/50)
P_final += P_total/50
R_final += R_total/50
F1_final += F1_total/50
MRR_final += MRR_total/50
print("全部类别的指标:","Precision是",P_final/40,"Recall是",R_final/40,"F1是",F1_final/40,"MRR是",MRR_final/40)

start = int(round(time.time()))
evaluate(10)
end = int(round(time.time()))
print("总共花了%s"%(end-start))
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:2: DeprecationWarning: `imread` is deprecated!
`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``imageio.imread`` instead.

/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:3: DeprecationWarning: `imresize` is deprecated!
`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``skimage.transform.resize`` instead.
  This is separate from the ipykernel package so we can avoid doing imports until


类别是: accordion Precision是 0.9119999999999996 Recall是 0.1824 F1是 0.3040000000000002 MRR是 0.31084929453262794
类别是: airplanes Precision是 0.7879999999999999 Recall是 0.15759999999999996 F1是 0.2626666666666666 MRR是 0.3427124842529605
类别是: bass Precision是 0.49000000000000005 Recall是 0.09800000000000003 F1是 0.16333333333333333 MRR是 0.4784389928193498
类别是: bonsai Precision是 0.632 Recall是 0.12639999999999998 F1是 0.21066666666666667 MRR是 0.4131869457042076
类别是: brain Precision是 0.748 Recall是 0.14960000000000007 F1是 0.24933333333333332 MRR是 0.3603971844293272
类别是: buddha Precision是 0.6760000000000003 Recall是 0.1352 F1是 0.2253333333333333 MRR是 0.4248558673469389
类别是: butterfly Precision是 0.5000000000000001 Recall是 0.1 F1是 0.16666666666666663 MRR是 0.5168516502897456
类别是: camera Precision是 0.862 Recall是 0.17240000000000003 F1是 0.28733333333333344 MRR是 0.3502130133534896
类别是: car_side Precision是 0.996 Recall是 0.19919999999999993 F1是 0.33200000000000024 MRR是 0.2917294532627866
类别是: cellphone Precision是 0.8819999999999995 Recall是 0.17639999999999997 F1是 0.29400000000000026 MRR是 0.31348964474678753
类别是: chair Precision是 0.618 Recall是 0.12359999999999999 F1是 0.20600000000000002 MRR是 0.4246199987402368
类别是: chandelier Precision是 0.5499999999999997 Recall是 0.11 F1是 0.18333333333333332 MRR是 0.4211596434870244
类别是: cougar_face Precision是 0.8099999999999998 Recall是 0.16200000000000003 F1是 0.27000000000000013 MRR是 0.3646421296296296
类别是: crab Precision是 0.446 Recall是 0.08920000000000002 F1是 0.14866666666666667 MRR是 0.4940006456286218
类别是: crayfish Precision是 0.4620000000000001 Recall是 0.0924 F1是 0.154 MRR是 0.47644240677752564
类别是: crocodile Precision是 0.522 Recall是 0.1044 F1是 0.17400000000000002 MRR是 0.40280735071806495
类别是: crocodile_head Precision是 0.57 Recall是 0.114 F1是 0.19000000000000006 MRR是 0.3809484095490048
类别是: cup Precision是 0.738 Recall是 0.1476 F1是 0.24600000000000005 MRR是 0.3578660777273872
类别是: dalmatian Precision是 0.8879999999999997 Recall是 0.17759999999999998 F1是 0.29600000000000015 MRR是 0.3110384920634921
类别是: dollar_bill Precision是 0.9159999999999995 Recall是 0.18319999999999997 F1是 0.3053333333333336 MRR是 0.31037669753086417
类别是: dolphin Precision是 0.6400000000000001 Recall是 0.12799999999999997 F1是 0.2133333333333333 MRR是 0.4083196995464852
类别是: dragonfly Precision是 0.628 Recall是 0.12559999999999996 F1是 0.20933333333333334 MRR是 0.4042844135802469
类别是: electric_guitar Precision是 0.748 Recall是 0.14960000000000007 F1是 0.24933333333333338 MRR是 0.3705190570672712
类别是: elephant Precision是 0.6859999999999996 Recall是 0.13720000000000002 F1是 0.2286666666666666 MRR是 0.4180562578735197
类别是: emu Precision是 0.7700000000000001 Recall是 0.15400000000000003 F1是 0.25666666666666665 MRR是 0.3568952947845803
类别是: euphonium Precision是 0.848 Recall是 0.1696 F1是 0.2826666666666668 MRR是 0.3418799067775258
类别是: ewer Precision是 0.8559999999999997 Recall是 0.17120000000000005 F1是 0.28533333333333355 MRR是 0.3479175485008819
类别是: faces Precision是 0.9799999999999998 Recall是 0.19599999999999998 F1是 0.32666666666666694 MRR是 0.29173280423280423
类别是: ferry Precision是 0.8919999999999998 Recall是 0.17840000000000003 F1是 0.29733333333333356 MRR是 0.3101462144116906
类别是: flamingo Precision是 0.7120000000000001 Recall是 0.14240000000000003 F1是 0.23733333333333342 MRR是 0.38784880007558575
类别是: gramophone Precision是 0.5519999999999999 Recall是 0.11040000000000001 F1是 0.18399999999999989 MRR是 0.4415120622323005
类别是: grand_piano Precision是 0.8819999999999999 Recall是 0.17639999999999997 F1是 0.2940000000000002 MRR是 0.33017158289241627
类别是: hawksbill Precision是 0.814 Recall是 0.1628 F1是 0.2713333333333334 MRR是 0.3493394935752078
类别是: hedgehog Precision是 0.8639999999999997 Recall是 0.17280000000000004 F1是 0.2880000000000001 MRR是 0.3293413422776518
类别是: helicopter Precision是 0.6 Recall是 0.12000000000000002 F1是 0.19999999999999996 MRR是 0.3974647833207358
类别是: ibis Precision是 0.7920000000000001 Recall是 0.15840000000000004 F1是 0.264 MRR是 0.3453355190224237
类别是: joshua_tree Precision是 0.9199999999999997 Recall是 0.184 F1是 0.30666666666666687 MRR是 0.30409257054673733
类别是: kangaroo Precision是 0.834 Recall是 0.1668 F1是 0.2780000000000001 MRR是 0.3316776549508692
类别是: ketch Precision是 0.9139999999999996 Recall是 0.18279999999999996 F1是 0.30466666666666686 MRR是 0.31019097379692623
类别是: lamp Precision是 0.6719999999999997 Recall是 0.1344 F1是 0.22399999999999995 MRR是 0.4075556311413455
全部类别的指标: Precision是 0.7402499999999999 Recall是 0.14805000000000004 F1是 0.24675000000000002 MRR是 0.37327269982993194
总共花了1586

当k=20时,测试代码和结果如下所示:

1
2
3
4
start = int(round(time.time()))
evaluate(20)
end = int(round(time.time()))
print("总共花了%s"%(end-start))
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:2: DeprecationWarning: `imread` is deprecated!
`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``imageio.imread`` instead.

/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:3: DeprecationWarning: `imresize` is deprecated!
`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``skimage.transform.resize`` instead.
  This is separate from the ipykernel package so we can avoid doing imports until


类别是: accordion Precision是 0.857 Recall是 0.3428 F1是 0.48971428571428577 MRR是 0.20539066307175557
类别是: airplanes Precision是 0.6599999999999998 Recall是 0.264 F1是 0.3771428571428572 MRR是 0.23312569236496805
类别是: bass Precision是 0.3520000000000001 Recall是 0.14079999999999995 F1是 0.20114285714285707 MRR是 0.3725550250665459
类别是: bonsai Precision是 0.509 Recall是 0.20359999999999992 F1是 0.290857142857143 MRR是 0.3002244396294538
类别是: brain Precision是 0.6759999999999999 Recall是 0.2704 F1是 0.3862857142857142 MRR是 0.254187180757293
类别是: buddha Precision是 0.576 Recall是 0.23039999999999997 F1是 0.3291428571428572 MRR是 0.2896625758002827
类别是: butterfly Precision是 0.3960000000000001 Recall是 0.15839999999999999 F1是 0.22628571428571423 MRR是 0.37848773777990524
类别是: camera Precision是 0.8119999999999998 Recall是 0.3248000000000002 F1是 0.46400000000000013 MRR是 0.23347394360475007
类别是: car_side Precision是 0.997 Recall是 0.39879999999999993 F1是 0.5697142857142863 MRR是 0.1792328247913529
类别是: cellphone Precision是 0.843 Recall是 0.3372000000000001 F1是 0.48171428571428593 MRR是 0.20429811849339785
类别是: chair Precision是 0.4710000000000003 Recall是 0.1884 F1是 0.26914285714285713 MRR是 0.33504755371157635
类别是: chandelier Precision是 0.4390000000000001 Recall是 0.17560000000000003 F1是 0.2508571428571427 MRR是 0.2868489698847877
类别是: cougar_face Precision是 0.7630000000000001 Recall是 0.3052 F1是 0.4360000000000001 MRR是 0.2412218469423665
类别是: crab Precision是 0.349 Recall是 0.13959999999999995 F1是 0.19942857142857143 MRR是 0.3480324817189515
类别是: crayfish Precision是 0.36800000000000005 Recall是 0.14719999999999997 F1是 0.2102857142857143 MRR是 0.33955418488231237
类别是: crocodile Precision是 0.42400000000000004 Recall是 0.1696 F1是 0.24228571428571433 MRR是 0.28930749379460363
类别是: crocodile_head Precision是 0.5259999999999999 Recall是 0.21039999999999992 F1是 0.30057142857142854 MRR是 0.2615118611954065
类别是: cup Precision是 0.6459999999999999 Recall是 0.2584 F1是 0.36914285714285705 MRR是 0.24596421538677773
类别是: dalmatian Precision是 0.8470000000000002 Recall是 0.3388000000000002 F1是 0.48400000000000026 MRR是 0.2047244990867052
类别是: dollar_bill Precision是 0.8670000000000004 Recall是 0.34680000000000016 F1是 0.4954285714285718 MRR是 0.1991986558251109
类别是: dolphin Precision是 0.5379999999999999 Recall是 0.21520000000000003 F1是 0.3074285714285714 MRR是 0.28793062262240343
类别是: dragonfly Precision是 0.5370000000000001 Recall是 0.21480000000000005 F1是 0.3068571428571429 MRR是 0.2896271859115977
类别是: electric_guitar Precision是 0.6270000000000001 Recall是 0.2508 F1是 0.3582857142857143 MRR是 0.24706146727997996
类别是: elephant Precision是 0.6129999999999999 Recall是 0.24520000000000003 F1是 0.3502857142857142 MRR是 0.31357805277960843
类别是: emu Precision是 0.66 Recall是 0.26400000000000007 F1是 0.37714285714285706 MRR是 0.2562631926574002
类别是: euphonium Precision是 0.7899999999999998 Recall是 0.316 F1是 0.4514285714285715 MRR是 0.23728301946965857
类别是: ewer Precision是 0.8059999999999998 Recall是 0.3224000000000001 F1是 0.4605714285714288 MRR是 0.22706734081527205
类别是: faces Precision是 0.9530000000000005 Recall是 0.3812000000000001 F1是 0.544571428571429 MRR是 0.18352354358387135
类别是: ferry Precision是 0.8269999999999998 Recall是 0.3308000000000002 F1是 0.47257142857142864 MRR是 0.2019892275742634
类别是: flamingo Precision是 0.6220000000000001 Recall是 0.24880000000000002 F1是 0.35542857142857137 MRR是 0.27999955089261086
类别是: gramophone Precision是 0.428 Recall是 0.17119999999999994 F1是 0.24457142857142852 MRR是 0.321210878485727
类别是: grand_piano Precision是 0.805 Recall是 0.32200000000000023 F1是 0.46000000000000013 MRR是 0.228530350482945
类别是: hawksbill Precision是 0.7440000000000001 Recall是 0.29760000000000014 F1是 0.4251428571428572 MRR是 0.23961314379373044
类别是: hedgehog Precision是 0.7769999999999999 Recall是 0.3108000000000001 F1是 0.444 MRR是 0.22917637257178725
类别是: helicopter Precision是 0.4809999999999999 Recall是 0.19240000000000002 F1是 0.2748571428571429 MRR是 0.26922481396817177
类别是: ibis Precision是 0.685 Recall是 0.2740000000000001 F1是 0.3914285714285714 MRR是 0.2492651484526271
类别是: joshua_tree Precision是 0.8970000000000001 Recall是 0.3588000000000002 F1是 0.5125714285714289 MRR是 0.1905144055309088
类别是: kangaroo Precision是 0.731 Recall是 0.2924000000000001 F1是 0.4177142857142858 MRR是 0.23588099861154674
类别是: ketch Precision是 0.86 Recall是 0.34400000000000014 F1是 0.4914285714285718 MRR是 0.2035065874260235
类别是: lamp Precision是 0.575 Recall是 0.22999999999999993 F1是 0.3285714285714286 MRR是 0.292364211133484
全部类别的指标: Precision是 0.6583500000000001 Recall是 0.2633400000000001 F1是 0.37620000000000015 MRR是 0.259641501945798
总共花了1606

当k=50时,测试代码和结果如下所示:

1
2
3
4
start = int(round(time.time()))
evaluate(50)
end = int(round(time.time()))
print("总共花了%s"%(end-start))
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:2: DeprecationWarning: `imread` is deprecated!
`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``imageio.imread`` instead.

/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:3: DeprecationWarning: `imresize` is deprecated!
`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``skimage.transform.resize`` instead.
  This is separate from the ipykernel package so we can avoid doing imports until


类别是: accordion Precision是 0.6419999999999999 Recall是 0.6419999999999999 F1是 0.6419999999999999 MRR是 0.13524435187251646
类别是: airplanes Precision是 0.42760000000000015 Recall是 0.42760000000000015 F1是 0.42760000000000015 MRR是 0.15611472394371254
类别是: bass Precision是 0.22399999999999992 Recall是 0.22399999999999992 F1是 0.22399999999999987 MRR是 0.2459831025114453
类别是: bonsai Precision是 0.3396 Recall是 0.3396 F1是 0.3396 MRR是 0.16641227976837342
类别是: brain Precision是 0.43079999999999996 Recall是 0.43079999999999996 F1是 0.43079999999999996 MRR是 0.17243015347578314
类别是: buddha Precision是 0.36999999999999994 Recall是 0.36999999999999994 F1是 0.36999999999999994 MRR是 0.19101866884066498
类别是: butterfly Precision是 0.24959999999999996 Recall是 0.24959999999999996 F1是 0.24959999999999996 MRR是 0.2558937748144347
类别是: camera Precision是 0.6276 Recall是 0.6276 F1是 0.6275999999999999 MRR是 0.1525350277841781
类别是: car_side Precision是 0.9231999999999998 Recall是 0.9231999999999998 F1是 0.9231999999999998 MRR是 0.09586743969932714
类别是: cellphone Precision是 0.6244 Recall是 0.6244 F1是 0.6244 MRR是 0.12794734392911913
类别是: chair Precision是 0.28919999999999996 Recall是 0.28919999999999996 F1是 0.28919999999999996 MRR是 0.2598573776970831
类别是: chandelier Precision是 0.31000000000000005 Recall是 0.31000000000000005 F1是 0.31000000000000005 MRR是 0.1808264760517882
类别是: cougar_face Precision是 0.5480000000000002 Recall是 0.5480000000000002 F1是 0.5480000000000002 MRR是 0.16039140243647507
类别是: crab Precision是 0.23800000000000002 Recall是 0.23800000000000002 F1是 0.23800000000000002 MRR是 0.20398167167240952
类别是: crayfish Precision是 0.24239999999999998 Recall是 0.24239999999999998 F1是 0.24239999999999998 MRR是 0.21794387980394123
类别是: crocodile Precision是 0.31880000000000014 Recall是 0.31880000000000014 F1是 0.3188000000000001 MRR是 0.16377485897743327
类别是: crocodile_head Precision是 0.3824000000000001 Recall是 0.3824000000000001 F1是 0.3824000000000001 MRR是 0.15999989480834342
类别是: cup Precision是 0.4528000000000001 Recall是 0.4528000000000001 F1是 0.4528000000000001 MRR是 0.16832274718912343
类别是: dalmatian Precision是 0.6856000000000002 Recall是 0.6856000000000002 F1是 0.6856000000000002 MRR是 0.13038107788414918
类别是: dollar_bill Precision是 0.6992000000000002 Recall是 0.6992000000000002 F1是 0.6992000000000002 MRR是 0.1230311724451443
类别是: dolphin Precision是 0.3472000000000001 Recall是 0.3472000000000001 F1是 0.34720000000000006 MRR是 0.185102586671417
类别是: dragonfly Precision是 0.3636000000000001 Recall是 0.3636000000000001 F1是 0.3636000000000001 MRR是 0.18336640828208575
类别是: electric_guitar Precision是 0.4156 Recall是 0.4156 F1是 0.4156 MRR是 0.16112480696341586
类别是: elephant Precision是 0.4388 Recall是 0.4388 F1是 0.4388 MRR是 0.19226995610666886
类别是: emu Precision是 0.46280000000000016 Recall是 0.46280000000000016 F1是 0.46280000000000016 MRR是 0.1727423329572902
类别是: euphonium Precision是 0.5331999999999999 Recall是 0.5331999999999999 F1是 0.5331999999999999 MRR是 0.16508289859996736
类别是: ewer Precision是 0.6156 Recall是 0.6156 F1是 0.6156 MRR是 0.1484146198122631
类别是: faces Precision是 0.8291999999999997 Recall是 0.8291999999999997 F1是 0.8291999999999997 MRR是 0.1034398902906862
类别是: ferry Precision是 0.5984 Recall是 0.5984 F1是 0.5984 MRR是 0.13050126647346258
类别是: flamingo Precision是 0.4736000000000001 Recall是 0.4736000000000001 F1是 0.4736000000000001 MRR是 0.17999585678937785
类别是: gramophone Precision是 0.27079999999999993 Recall是 0.27079999999999993 F1是 0.27079999999999993 MRR是 0.21110128986305046
类别是: grand_piano Precision是 0.5628 Recall是 0.5628 F1是 0.5628 MRR是 0.16429422826486662
类别是: hawksbill Precision是 0.522 Recall是 0.522 F1是 0.522 MRR是 0.14423908077953893
类别是: hedgehog Precision是 0.568 Recall是 0.568 F1是 0.568 MRR是 0.15742682542564843
类别是: helicopter Precision是 0.33080000000000004 Recall是 0.33080000000000004 F1是 0.33080000000000004 MRR是 0.1642027888118308
类别是: ibis Precision是 0.4879999999999999 Recall是 0.4879999999999999 F1是 0.4879999999999999 MRR是 0.16840193197102116
类别是: joshua_tree Precision是 0.7495999999999999 Recall是 0.7495999999999999 F1是 0.7495999999999999 MRR是 0.10702191326474143
类别是: kangaroo Precision是 0.5000000000000001 Recall是 0.5000000000000001 F1是 0.5000000000000001 MRR是 0.16308041702731674
类别是: ketch Precision是 0.7024000000000001 Recall是 0.7024000000000001 F1是 0.7023999999999999 MRR是 0.12742134275609873
类别是: lamp Precision是 0.38799999999999996 Recall是 0.38799999999999996 F1是 0.38799999999999996 MRR是 0.2064639804390264
全部类别的指标: Precision是 0.4796400000000001 Recall是 0.4796400000000001 F1是 0.4796400000000001 MRR是 0.16759129617888052
总共花了1584

当k=100时,测试代码和结果如下所示:

1
2
3
4
start = int(round(time.time()))
evaluate(100)
end = int(round(time.time()))
print("总共花了%s"%(end-start))
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:2: DeprecationWarning: `imread` is deprecated!
`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``imageio.imread`` instead.

/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:3: DeprecationWarning: `imresize` is deprecated!
`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``skimage.transform.resize`` instead.
  This is separate from the ipykernel package so we can avoid doing imports until


类别是: accordion Precision是 0.37520000000000003 Recall是 0.7504000000000001 F1是 0.5002666666666666 MRR是 0.1089526687599054
类别是: airplanes Precision是 0.28240000000000004 Recall是 0.5648000000000001 F1是 0.3765333333333333 MRR是 0.1169019750148541
类别是: bass Precision是 0.14980000000000004 Recall是 0.2996000000000001 F1是 0.1997333333333334 MRR是 0.17176894130050305
类别是: bonsai Precision是 0.2378 Recall是 0.4756 F1是 0.3170666666666667 MRR是 0.11599950521883272
类别是: brain Precision是 0.25460000000000005 Recall是 0.5092000000000001 F1是 0.3394666666666668 MRR是 0.13976041912502468
类别是: buddha Precision是 0.23559999999999998 Recall是 0.47119999999999995 F1是 0.3141333333333333 MRR是 0.14234849465205957
类别是: butterfly Precision是 0.1656 Recall是 0.3312 F1是 0.22080000000000005 MRR是 0.18328774522576705
类别是: camera Precision是 0.3538 Recall是 0.7076 F1是 0.47173333333333345 MRR是 0.13082315151269644
类别是: car_side Precision是 0.4807999999999996 Recall是 0.9615999999999992 F1是 0.6410666666666662 MRR是 0.09198318332532594
类别是: cellphone Precision是 0.3669999999999999 Recall是 0.7339999999999998 F1是 0.4893333333333334 MRR是 0.10767332455027613
类别是: chair Precision是 0.18839999999999993 Recall是 0.37679999999999986 F1是 0.25120000000000003 MRR是 0.17609480911920505
类别是: chandelier Precision是 0.20939999999999995 Recall是 0.4187999999999999 F1是 0.2792 MRR是 0.13734513592599634
类别是: cougar_face Precision是 0.30920000000000003 Recall是 0.6184000000000001 F1是 0.4122666666666666 MRR是 0.13185920156678382
类别是: crab Precision是 0.1702 Recall是 0.3404 F1是 0.22693333333333332 MRR是 0.13999293047235378
类别是: crayfish Precision是 0.1704 Recall是 0.3408 F1是 0.2272000000000001 MRR是 0.15203942791240327
类别是: crocodile Precision是 0.225 Recall是 0.45 F1是 0.3 MRR是 0.11640589232915156
类别是: crocodile_head Precision是 0.2584000000000001 Recall是 0.5168000000000001 F1是 0.34453333333333336 MRR是 0.11119901333165677
类别是: cup Precision是 0.2852 Recall是 0.5704 F1是 0.38026666666666675 MRR是 0.13574036756164565
类别是: dalmatian Precision是 0.37200000000000005 Recall是 0.7440000000000001 F1是 0.4959999999999999 MRR是 0.1171433385854654
类别是: dollar_bill Precision是 0.4028 Recall是 0.8056 F1是 0.5370666666666666 MRR是 0.10390731615749678
类别是: dolphin Precision是 0.22279999999999997 Recall是 0.44559999999999994 F1是 0.29706666666666676 MRR是 0.14018084092512578
类别是: dragonfly Precision是 0.23659999999999998 Recall是 0.47319999999999995 F1是 0.3154666666666667 MRR是 0.13990322726250679
类别是: electric_guitar Precision是 0.26719999999999994 Recall是 0.5343999999999999 F1是 0.35626666666666656 MRR是 0.12416552351690571
类别是: elephant Precision是 0.27119999999999994 Recall是 0.5423999999999999 F1是 0.36160000000000003 MRR是 0.14391679295668197
类别是: emu Precision是 0.294 Recall是 0.588 F1是 0.39199999999999996 MRR是 0.13093700660434973
类别是: euphonium Precision是 0.30000000000000004 Recall是 0.6000000000000001 F1是 0.4 MRR是 0.12798189953664224
类别是: ewer Precision是 0.36400000000000005 Recall是 0.7280000000000001 F1是 0.48533333333333323 MRR是 0.12132977227837438
类别是: faces Precision是 0.4406000000000001 Recall是 0.8812000000000002 F1是 0.5874666666666665 MRR是 0.09713941810197513
类别是: ferry Precision是 0.3617999999999999 Recall是 0.7235999999999998 F1是 0.48239999999999994 MRR是 0.10755851523534302
类别是: flamingo Precision是 0.3152 Recall是 0.6304 F1是 0.42026666666666673 MRR是 0.13850208527481236
类别是: gramophone Precision是 0.17759999999999998 Recall是 0.35519999999999996 F1是 0.23680000000000004 MRR是 0.15839726580505067
类别是: grand_piano Precision是 0.31919999999999993 Recall是 0.6383999999999999 F1是 0.4256000000000001 MRR是 0.132432426952128
类别是: hawksbill Precision是 0.3161999999999999 Recall是 0.6323999999999999 F1是 0.4216000000000001 MRR是 0.11732087662937214
类别是: hedgehog Precision是 0.3166 Recall是 0.6332 F1是 0.4221333333333336 MRR是 0.13394856501359767
类别是: helicopter Precision是 0.24340000000000003 Recall是 0.48680000000000007 F1是 0.32453333333333334 MRR是 0.11387951623132996
类别是: ibis Precision是 0.31920000000000004 Recall是 0.6384000000000001 F1是 0.4256 MRR是 0.12984272526796853
类别是: joshua_tree Precision是 0.44099999999999995 Recall是 0.8819999999999999 F1是 0.5880000000000003 MRR是 0.0920799595960934
类别是: kangaroo Precision是 0.30080000000000007 Recall是 0.6016000000000001 F1是 0.4010666666666666 MRR是 0.13307640433603413
类别是: ketch Precision是 0.3904000000000001 Recall是 0.7808000000000002 F1是 0.5205333333333333 MRR是 0.11346268784318259
类别是: lamp Precision是 0.24999999999999997 Recall是 0.49999999999999994 F1是 0.3333333333333334 MRR是 0.14451757985277888
全部类别的指标: Precision是 0.29103500000000004 Recall是 0.5820700000000001 F1是 0.3880466666666666 MRR是 0.1292949982716914
总共花了1573

结论

从上面的k=10,20,50,100的结果,可以得到以下一些结论:

1.随着K的增加,这2000张图整体的precision下降,recall是上升的,F1指标也是上升的,MRR指标是下降的。

2.各个类别的分数在上面的结果里已经列出,可以看出car_side的precision是最高的,其recall也是最高的,F1 score也是最高的,MRR最高的是butterfly

3.测试时间上,上述各个k的值最终结果都在1600s前后,可以说很慢了。

0%