Repository navigation
Expand file tree
/
Copy pathnetwork_vis.py
More file actions
70 lines (50 loc) · 2.04 KB
/
Copy pathnetwork_vis.py
File metadata and controls
70 lines (50 loc) · 2.04 KB
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
import matplotlib.pyplot as plt
import numpy as np
from sklearn.neighbors import NearestNeighbors, KDTree, BallTree, LSHForest, NearestCentroid
from pyvis.network import Network
from winnow.feature_extraction import SimilarityModel
import yaml
sm = SimilarityModel()
print("Loading config file")
with open("config.yaml", "r") as ymlfile:
cfg = yaml.load(ymlfile)
DISTANCE = float(cfg["match_distance"])
DST_FOLDER = cfg["destination_folder"]
VIDEO_LEVEL_SAVE_FOLDER = cfg["video_level_folder"]
print("Extracting Video Signatures")
sm = SimilarityModel()
video_signatures = sm.predict(VIDEO_LEVEL_SAVE_FOLDER)
video_signatures = np.nan_to_num(video_signatures)
labels = np.array([x.split("_vgg")[0].split("/")[-1] for x in sm.index])
def filter_results(distances, indexes, thr):
results = []
results_distances = []
msk = distances < thr
for i, r in enumerate(msk):
results.append(indexes[i, r])
results_distances.append(distances[i, r])
return results, results_distances
def evaluate_match_based(video_space, estimator, plot=True, thr=0.4):
estimator.fit(video_space)
distances, indexes = estimator.kneighbors(video_space, n_neighbors=20)
if plot == True:
plt.hist(distances[:, 1::].ravel(), bins=100)
results, results_distances = filter_results(distances, indexes, thr)
net = Network(notebook=True, height="1000px", width="1000px")
net.barnes_hut()
for i, l in enumerate(labels):
net.add_node(str(i), label=str(l))
for i, d in enumerate(results):
for j, m in enumerate(d):
if j != 0:
net.add_edge(str(i), str(m), weight=str(results_distances[i][j]))
net.show_buttons(filter_=["physics"])
net.show("mygraph.html")
return net
print("Building Network Graph....")
nearest_neighbor_net = evaluate_match_based(
video_signatures, NearestNeighbors(n_neighbors=50, metric="euclidean", algorithm="kd_tree"), thr=0.3
)
NETWORK_VIS_PATH = DST_FOLDER + "/nn.html"
print("Saving to :", NETWORK_VIS_PATH)
nearest_neighbor_net.show(NETWORK_VIS_PATH)