forked from ManLiuCoder/PSVMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_attribute_w2v_SUN.py
70 lines (68 loc) · 1.76 KB
/
extract_attribute_w2v_SUN.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 30 12:27:57 2019
@author: war-machince
"""
import os,sys
pwd = os.getcwd()
sys.path.insert(0,pwd)
#%%
print('-'*30)
print(os.getcwd())
print('-'*30)
#%%
import pdb
import pandas as pd
import numpy as np
import gensim.downloader as api
import scipy.io as sio
import pickle
#%%
dataset = 'SUN'
#%%
print('Loading pretrain w2v modeling')
model_name = 'word2vec-google-news-300'#best modeling
model = api.load(model_name)
dim_w2v = 300
print('Done loading modeling')
#%%
replace_word = [('rockstone','rock stone'),('dirtsoil','dirt soil'),('man-made','man-made'),('sunsunny','sun sunny'),
('electricindoor','electric indoor'),('semi-enclosed','semi enclosed'),('far-away','faraway')]
#%%
file_path = 'datasets/attribute/{}/attributes.mat'.format(dataset)
matcontent = sio.loadmat(file_path)
des = matcontent['attributes'].flatten()
#%%
df = pd.DataFrame()
#%% filter
new_des = [''.join(i.item().split('/')) for i in des]
#%% replace out of dictionary words
for pair in replace_word:
for idx,s in enumerate(new_des):
new_des[idx]=s.replace(pair[0],pair[1])
print('Done replace OOD words')
#%%
df['new_des']=new_des
df.to_csv('datasets/attribute/{}/new_des.csv'.format(dataset))
print('Done preprocessing attribute des')
#%%
all_w2v = []
for s in new_des:
print(s)
words = s.split(' ')
if words[-1] == '': #remove empty element
words = words[:-1]
w2v = np.zeros(dim_w2v)
for w in words:
try:
w2v += model[w]
except Exception as e:
print(e)
all_w2v.append(w2v[np.newaxis,:])
#%%
all_w2v=np.concatenate(all_w2v,axis=0)
pdb.set_trace()
#%%
with open('./data/w2v/{}_attribute.pkl'.format(dataset),'wb') as f:
pickle.dump(all_w2v,f)