/
cnet.py
98 lines (80 loc) · 2.84 KB
/
cnet.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
import json
import divisi2
import numpy as np
from csc.divisi2.blending import blend
from csc.divisi.flavors import ConceptByFeatureMatrix
annotations = json.load(open('/home/salvation/Desktop/annotations.json'))
objects = json.load(open('/home/salvation/Desktop/objects.json'))
predicates = json.load(open('/home/salvation/Desktop/predicates.json'))
imgs = list()
for keys in annotations:
imgs.append(keys)
p = np.load('/home/salvation/visual_genome_relations.npy')
q = np.load('/home/salvation/vg_relationships/visual_genome_relations.npy')
# For divisi
# weighted_triples = list()
# for img in imgs:
# for annotation in annotations[img]:
# relation = predicates[annotation['predicate']]
# obj = objects[annotation['object']['category']]
# subject = objects[annotation['subject']['category']]
# rel_triple = (obj, relation, subject)
# weight = 4.0
# weighted_triple = (rel_triple, weight)
# weighted_triples.append(weighted_triple)
# matrix = ConceptByFeatureMatrix.from_triples(weighted_triples)
# normalized_matrix = matrix.normalized()
# print(type(normalized_matrix))
# For divisi2
weighted_relations = list()
# for img in imgs:
# for annotation in annotations[img]:
# # relation = predicates[annotation['predicate']]
# obj = objects[annotation['object']['category']]
# subject = objects[annotation['subject']['category']]
# weight = 4.0
# rel_triple = (weight, obj, subject)
# # weighted_triple = (rel_triple, weight)
# weighted_relations.append(rel_triple)
obj_list = []
for idx in range(len(p)):
obj = p[idx][2]
subj = p[idx][0]
weight = 4.0
rel_triple = (weight, obj, subj)
obj_list.append(obj)
# weighted_triple = (rel_triple, weight)
weighted_relations.append(rel_triple)
for idx in range(len(q)):
obj = q[idx][2]
subj = q[idx][0]
weight = 4.0
rel_triple = (weight, obj, subj)
obj_list.append(obj)
# weighted_triple = (rel_triple, weight)
weighted_relations.append(rel_triple)
print len(weighted_relations)
#print len(obj_list)
obj_list = set(obj_list)
print len(obj_list)
matrix = divisi2.make_sparse(weighted_relations)
#print matrix
# ConceptNet Matrix
A = divisi2.network.conceptnet_matrix('en')
A_concept_axes, A_axis_weights, A_feature_axes = A.svd(k=100)
blended_matrix = blend([matrix, A])
concept_axes, axis_weights, feature_axes = blended_matrix.svd(k=100)
common_objects = list(set(obj_list).intersection(A.row_labels))
print len(A.row_labels)
# Save embeddings for ConceptNet
cnet_object_embeddings = np.array(
[A_concept_axes.row_named(obj) for obj in common_objects])
np.save('cnet_object_embeddings.npy', cnet_object_embeddings)
# Save embeddings for Blended Matrix
blended_object_embeddings = np.array(
[concept_axes.row_named(obj) for obj in common_objects])
np.save('blended_object_embeddings.npy', blended_object_embeddings)
v = []
for idx in obj_list:
#print idx
v.append(concept_axes.row_named(i