-
Notifications
You must be signed in to change notification settings - Fork 3
/
HashRetrieval.py
277 lines (211 loc) · 9.35 KB
/
HashRetrieval.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# -*- coding:utf-8 -*-
'''
@Updatingtime: 2021/9/29 15:26
@Author : Yilan Zhang
@Filename : HashRetrieval.py
@Email : [email protected]
'''
import torch
from torch.autograd import Variable
from torchvision import transforms
import torch.backends.cudnn as cudnn
import torch.utils.data as data
from PIL import Image
import argparse
import os
import time
import numpy as np
'''import model'''
from network import DH_ResNet18,Attention_ResNet18
'''generate model'''
model=Attention_ResNet18.resnet18(pretrained=False)
# print model structure and parameters
print(model)
for name, param in model.named_parameters():
print(name)
print(param)
cudnn.benchmark = True
model.cuda()
model.eval()
'''set test parameters'''
parser = argparse.ArgumentParser(description='my code for classification task')
parser.add_argument('--classNum', default=4, help='number of classes')
parser.add_argument('--samplesNumOfTestset', default=[452,1287,332,262], help='number of classes')
args = parser.parse_args()
# use gpu 0 as cuda device
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
'''build a logger of testing process and show it'''
testLogPath = "/"
File=open(testLogPath+'TestingLog.txt','w')
'''get filename path list of training data and test data'''
trainDataFilenamePath = "/"
testDataFilenamePath = "/"
trainDataFilename = open(trainDataFilenamePath)
trainDataFilenameList = []
for line in trainDataFilename:
trainDataFilenameList.append(line.rstrip('\n'))
print('number of training samples:',len(trainDataFilenameList))
File.write('number of training samples:{}'.format(len(trainDataFilenameList))+'\n')
testDataFilename=open(testDataFilenamePath)
testDataFilenameList=[]
for line in testDataFilename:
testDataFilenameList.append(line.rstrip('\n'))
print('number of test samples:',len(testDataFilenameList))
File.write('number of test samples:{}'.format(len(testDataFilenameList))+'\n')
'''data augmentation'''
trainDataTransformation=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
testDataTransformation=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
'''data loader'''
class trainDataLoader(data.Dataset):
def __init__(self, dataFilenameList, transform=None):
self.dataFilenameList = dataFilenameList
self.transform = transform
def __getitem__(self, index):
path = self.dataFilenameList[index]
data=Image.open(path)
label = int(self.dataFilenameList[index].split('/')[-2])
if self.transform is not None:
data=self.transform(data)
label = torch.LongTensor([label])
return data, label, path
def __len__(self):
return len(self.dataFilenameList)
class testDataLoader(data.Dataset):
def __init__(self, dataFilenameList, transform=None):
self.dataFilenameList = dataFilenameList
self.transform = transform
def __getitem__(self, index):
path = self.dataFilenameList[index]
data=Image.open(path)
label = int(self.dataFilenameList[index].split('/')[-2])
if self.transform is not None:
data=self.transform(data)
label = torch.LongTensor([label])
return data, label, path
def __len__(self):
return len(self.dataFilenameList)
'''load data'''
trainData = data.DataLoader(trainDataLoader(dataFilenameList=trainDataFilenameList, transform=trainDataTransformation), batch_size=1,shuffle=False, num_workers=4)
testData = data.DataLoader(testDataLoader(dataFilenameList=testDataFilenameList,transform=testDataTransformation), batch_size=1,shuffle=False,num_workers=2)
'''build hashtable & continuous feature database'''
print('Build hashtable and continuous feature database...')
hashTable = {}
featureDatabase = {}
# 0.5 for deephash, 0.0 for others
threshhold = 0.5
if threshhold == 0.5:
print('for deep hash')
for data, target, path in trainData:
target = torch.squeeze(target, 1)
data, target = data.cuda(), target.cuda()
data, target = Variable(data), Variable(target)
_,continuousHashCode = model(data)
feature = continuousHashCode
if not hashTable:
print("The dimension of the hash key:", continuousHashCode.shape[1])
File.write("The dimension of the hash key:{}".format(continuousHashCode.shape[1])+'\n')
print("The dimension of the feature:", feature.shape[1])
File.write("The dimension of the feature:{}".format(feature.shape[1])+'\n')
File.flush()
# continuous feature database
featureDatabase[path] = [feature.cpu().detach().numpy()]
# hash table
hashCode=(torch.sign(continuousHashCode - threshhold)).cpu().detach().numpy()
hashKey=tuple(hashCode[0])
if hashKey not in hashTable.keys():
hashTable[hashKey]=[path]
else:
hashTable[hashKey].append(path)
'''test image retrieval'''
print('Strat testing image retrieval...')
topNumOfRetrievalResults = 10
#mAP
retrievalResultsAccuracyOfEachSampleOfDifferentClass={}
for category in range(args.classNum):
retrievalResultsAccuracyOfEachSampleOfDifferentClass[category]=[]
numOfDoneImages=0
# mRR
ReciprocalRankofEachSampleofDiffentCalss={}
for category in range(args.classNum):
ReciprocalRankofEachSampleofDiffentCalss[category]=[]
startTime = time.time()
for data, target, path in testData:
target = torch.squeeze(target, 1)
label = int(target.cpu().numpy())
data, target = data.cuda(), target.cuda()
data, target = Variable(data), Variable(target)
_,continuousHashCode=model(data)
# get feature
feature=continuousHashCode.cpu().detach().numpy()
# get hashcode
hashCode=torch.sign(continuousHashCode - threshhold).cpu().detach().numpy()
hashKey = tuple(hashCode[0])
rawRetrievalResult={}
for keyValue in hashTable.keys():
hammingDistance = 0
for i in range(0, len(hashKey)):
if hashKey[i] != keyValue[i]:
hammingDistance = hammingDistance + 1
if hammingDistance <=4:
for imgpath in hashTable[keyValue]:
# for Euclidean distance
# rawRetrievalResult[imgpath]=np.sqrt(np.sum(np.square(feature - featureDatabase[imgpath])))
# for Cosine distance
rawRetrievalResult[imgpath] = 1.0 - np.dot(feature[0],featureDatabase[imgpath][0][0])/(np.linalg.norm(feature[0],ord=2)*np.linalg.norm(featureDatabase[imgpath][0][0],ord=2))
# for Manhattan distance
# rawRetrievalResult[imgpath] = np.sum(np.linalg.norm(feature[0] - featureDatabase[imgpath][0][0], ord=1))
preciseRetrievalResult = zip(rawRetrievalResult.values(), rawRetrievalResult.keys())
preciseRetrievalResult = sorted(preciseRetrievalResult) #默认为距离升序
correctRetrievalSamplesOfEachResult = 0
num=0
firstcorrectresult=0
for euclideanDistance, imgpath in list(preciseRetrievalResult)[0:topNumOfRetrievalResults]:
imgpath = ''.join(str(i) for i in imgpath)
resultLabel = int(imgpath.split('/')[-2])
num += 1
if label == resultLabel:
correctRetrievalSamplesOfEachResult = correctRetrievalSamplesOfEachResult + 1
if firstcorrectresult==0:
firstcorrectresult=1/num
else:
continue
#这里加一些评价指标
if len(preciseRetrievalResult) == 0:
accuracy = 0
elif len(preciseRetrievalResult) < topNumOfRetrievalResults:
accuracy=correctRetrievalSamplesOfEachResult * 1.0 / len(preciseRetrievalResult)
else:
accuracy=correctRetrievalSamplesOfEachResult * 1.0 / topNumOfRetrievalResults
#mAP
retrievalResultsAccuracyOfEachSampleOfDifferentClass[label].append(accuracy)
#mRR
ReciprocalRankofEachSampleofDiffentCalss[label].append(firstcorrectresult)
# show percent of done images
numOfDoneImages+=1
print("\r",'{:.2f}% done'.format(100*numOfDoneImages/len(testData)),end="",flush=True)
print("\n")
totalTestingTime = time.time() - startTime
'''test result of image retrieval'''
averagePrecision=[0.0 for cols in range(args.classNum)]
averageRR=[0.0 for cols in range(args.classNum)]
for category in range(args.classNum):
averagePrecision[category]=np.mean(retrievalResultsAccuracyOfEachSampleOfDifferentClass[category])
averageRR[category]=np.mean(ReciprocalRankofEachSampleofDiffentCalss[category])
print('AP of Class '+str(category)+': '+str(averagePrecision[category]))
File.write('AP of Class {}:{}'.format(str(category),str(averagePrecision[category]))+'\n')
print('RR of Class '+str(category)+': '+str(averageRR[category]))
File.write('RR of Class {}:{}'.format(str(category),str(averageRR[category]))+'\n')
print('mAP of all classes: '+str(np.mean(averagePrecision)))
File.write('mAP of all classes:{} '.format(str(np.mean(averagePrecision)))+'\n')
print('mRR of all classes: '+str(np.mean(averageRR)))
File.write('mRR of all classes:{} '.format(str(np.mean(averageRR)))+'\n')
perTestingTime = totalTestingTime / len(testDataFilenameList)
print("Per testing time: ", perTestingTime)
File.write("Per testing time:{}".format(perTestingTime))
File.close()