-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
400 lines (294 loc) · 10.3 KB
/
main.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import torch
import torchvision.models as models
import torchvision.transforms as transforms
from torchvision import datasets
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import math
import os
import random as rnd
# Load the pretrained model
model = models.resnet50(pretrained=True)
# Use the model object to select the desired layer
layer = model._modules.get('avgpool')
# Set model to evaluation mode
model.eval()
# Get the current working directory
current_directory = os.getcwd()
# Specify the relative path to the 'images' folder
relative_path = 'images'
# Construct the absolute path
absolute_path = os.path.join(current_directory, relative_path)
# Transformations
transforms = transforms.Compose([
transforms.Resize(255),
transforms.CenterCrop(224),
transforms.ToTensor()
])
dataset = datasets.ImageFolder(absolute_path, transform=transforms)
dataset = list(dataset)
# Separate images and labels
C,labels = map(list, zip(*dataset))
def get_target_image_indices(target_images_num=5):
"""
Generates a list of unique random indices representing target images.
Args:
total_images (int): Total number of images.
target_images_num (int, optional, default=5): Number of target images to select.
Returns:
list: List of unique random indices.
"""
target_image_indices = []
while len(target_image_indices) < target_images_num:
temp_rnd = rnd.randint(0, len(C))
if (temp_rnd not in target_image_indices):
target_image_indices.append(temp_rnd)
return target_image_indices
def get_vector(image_path):
"""
Extracts a feature vector from the given image using a pre-trained ResNet50 model.
Args:
image_path (str): The file path of the input image.
Returns:
torch.Tensor: The feature vector extracted from the image.
"""
# Load the image with Pillow library
img = Image.open(image_path)
# Create a vector of zeros that will hold our feature vector (the 'avgpool' layer has an output size of 2048)
feature_vector = torch.zeros(2048)
# Define a function that will copy the output of a layer
def copy_data(o):
# Reshape the output tensor to match the shape of the feature_vector
feature_vector.copy_(o.data.view(feature_vector.shape))
# Attach that function to our selected layer
h = layer.register_forward_hook(copy_data)
# Run the model on our transformed image
model(img)
# Detach our copy function from the layer
h.remove()
# Return the feature vector
return feature_vector
def r_similarity(tensor1, tensor2):
"""
Calculates the reciprocal of the Euclidean distance between two tensors.
Args:
tensor1 (torch.Tensor): First input tensor.
tensor2 (torch.Tensor): Second input tensor.
Returns:
float: Reciprocal of the Euclidean distance between two tensors.
"""
euclidian_distance = float((torch.norm(tensor1 - tensor2)).item())
# Avoid division by zero
if euclidian_distance == 0:
return 1
return 1 / euclidian_distance
def create_ranked_lists(features, total_features):
"""
Create ranked lists using the feature similarities.
Args:
features (list of torch.Tensor): List of feature vectors.
total_features (int): Total number of features.
Returns:
2D list: Ranked lists of image indices.
"""
T = []
for target_idx in range(total_features):
tq = []
for query_idx in range(total_features):
similarity = (query_idx, r_similarity(features[target_idx], features[query_idx]))
tq.append(similarity)
# Add tq to T sorted by similarity in descending order
T.append(sorted(tq, key=lambda x: x[1], reverse=True))
return T
def create_hypergraph(ranked_lists, k):
"""
Creates a hypergraph based on ranked lists.
Args:
ranked_lists (2D list): Ranked lists of image indices.
k (int): Number of k-nearest neighbors for each target image.
Returns:
nx.DiGraph: Hypergraph data structure.
"""
hypergraph = nx.DiGraph()
for target_idx in range(len(ranked_lists)):
# Add nodes for each image
hypergraph.add_node(target_idx)
for query_idx in range(k):
# Create hyperedges for the k-nearest neighbors
hypergraph.add_edge(target_idx, ranked_lists[target_idx][query_idx][0], idx=query_idx+1)
return hypergraph
def find_score_of_image(i, v, T):
"""
Finds the score of image index 'v' in the hyperedge 'T[i]'.
Args:
i (int): Index of the target object.
v (int): Index of the query object.
T (2D list): Ranked lists.
Return:
int: Score of 'v' in the ranked list, or -1 if not found.
"""
score = -1
for j in range(len(T[i])):
if (T[i][j][0] == v):
score = T[i][j][1]
break
return score
def create_incidence_matrix(hypergraph, k):
"""
Creates an incidence matrix based on a hypergraph.
Args:
hypergraph (nx.DiGraph): Hypergraph representation.
k (int): Number of k-nearest neighbors.
Returns:
2D list: Incidence matrix.
"""
total_nodes = len(hypergraph.nodes)
H = np.zeros((total_nodes, total_nodes))
for node_idx in hypergraph.nodes:
for neighbor_idx, edge_data in hypergraph[node_idx].items():
H[node_idx][neighbor_idx] = 1 - math.log(edge_data["idx"], k+1)
return H
def pairwise_similarity_rel(eq, vi, vj, H):
"""
Compute the weight assigned to the hyperedge eq and the pairwise similarity relationship (p(eq,vi,vj))
Args:
eq (int): indicate the hyper edge
vi (int): Hyper node
vj (int): Hyper node
H (np array): incident matrix
"""
w_eq = 0
for i in H[eq]:
w_eq += i
return w_eq * H[eq][vi] * H[eq][vj]
def pairwise_cartesian_prod(hypergraph, H):
"""
Computes the similarity measure based on a hypergraph and an incidence matrix.
Args:
hypergraph (hypergraph): The hypergraph with the edges and nodes.
H (np array): Incident matrix.
Returns:
2D List: the similarity measure.
"""
total_nodes = len(hypergraph.nodes)
C1 = np.zeros((total_nodes, total_nodes))
for eq in hypergraph.nodes:
neighbors = [i for i, _ in hypergraph[eq].items()]
for i in neighbors:
for j in neighbors:
C1[i][j] += pairwise_similarity_rel(eq, i, j, H)
return C1
def rank_normalization(L, T):
""""
Creates a symmetric similarity matrix based on ranked lists.
Args:
L (int): Length of the ranked lists.
T (2D list): Ranked lists.
Returns:
2D list: Normalized ranked lists.
"""
normalized_T = []
for i in range(L):
temp = []
for j in range(L):
score = 2 * L - (find_score_of_image(i,j,T) + find_score_of_image(j,i,T))
temp.append((j, score))
normalized_T.append(temp)
# Sort each sublist by similarity score
normalized_T = [sorted(t,key = lambda x: x[1]) for t in normalized_T]
return normalized_T
def hypergraph_manifold_ranking(features, max_iters=15, k=3):
"""
Perform hypergraph manifold ranking.
Args:
features (list): List of features extracted from each image.
max_iters (int): Number of maximum iterations.
k (int): Neighborhood size.
Returns:
2D list: Normalized ranked lists.
"""
L = len(features)
# Initialize ranked lists
T = create_ranked_lists(features, L)
for _ in range(max_iters):
# Perform Rank Normalization
T = rank_normalization(L, T)
# Create the hypergraph
hypergraph = create_hypergraph(T,k)
# Create the incidence matrix 'H'
H = np.array(create_incidence_matrix(hypergraph, k))
# Calculate similarity matrices
Sn = H @ H.T
Sv = H.T @ H
# Calculate Hadamard product (element-wise multiplication)
S = np.multiply(Sn, Sv)
# Compute pairwise cartesian product
C_prod = pairwise_cartesian_prod(hypergraph, H)
# Compute the affinity matrix 'W'
W = np.multiply(C_prod, S)
# Update T based on affinity matrix
for i in range(len(features)):
for j in range(len(features)):
T[i][j] = (T[i][j][0], W[i][T[i][j][0]])
# Sort each sublist
T = [sorted(t,key = lambda x: x[1], reverse=True) for t in T]
return T
def calculate_accuracy(ranked_lists, labels, k, target_images):
""""
Calculates the accuracy of the ranked lists for a set of target images.
Args:
ranked_lists (2D list): Ranked lists of image indices.
labels (list): Labels of the images.
k (int): Number of nearest neighbors.
target_images (list): Indices of target images.
Returns:
list: List of accuracy scores corresponding to each target image.
"""
# Sum of integers from 1 to k
sum = k * (k+1) // 2
accuracy_scores = []
for i in target_images:
m = 0
accuracy = 0
for j in ranked_lists[i][:k]:
if labels[j[0]] == labels[i]:
accuracy += (k-m)/sum
else:
accuracy += 0
m += 1
accuracy_scores.append(accuracy)
return accuracy_scores
def display(ranked_list, accuracy):
"""
Display images along with information in a matplotlib subplot.
Parameters:
- ranked_list (list): A list of tuples containing image indices and scores.
- accuracy (float): The accuracy value.
"""
_, axs = plt.subplots(1, len(ranked_list), figsize=(14, 4))
plt.gcf().canvas.manager.set_window_title(f"Target image {ranked_list[0][0]}")
for ax, (img_idx, score) in zip(axs, ranked_list):
image = np.transpose(C[img_idx], (1, 2, 0))
ax.imshow(image)
title = f"Label: {labels[img_idx]}\nScore: {score:.3f}"
if img_idx == ranked_list[0][0]:
title += "\n(target image)"
ax.set_title(title)
ax.axis('off')
plt.suptitle(f"Accuracy: {accuracy:.3f}")
plt.show()
if __name__ == "__main__":
k = 5
target_images = get_target_image_indices()
print(f"Target images: {target_images}")
final_ranked_lists = hypergraph_manifold_ranking(C, k=k)
accuracy_list = calculate_accuracy(final_ranked_lists, labels, k, target_images)
target_ranked_lists = [final_ranked_lists[i][:k] for i in target_images]
for i in range(len(target_ranked_lists)):
ranked_list = target_ranked_lists[i]
display(ranked_list, accuracy_list[i])
for i in range(len(target_images)):
print(f"t{target_images[i]} = {final_ranked_lists[target_images[i]][0:k]}, accuracy={accuracy_list[i]:.3f}")