forked from ManLiuCoder/PSVMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_attribute_w2v_CUB.py
72 lines (69 loc) · 1.9 KB
/
extract_attribute_w2v_CUB.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
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 4 13:43:05 2019
@author: badat
"""
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 pickle
#%%
# _DEFAULT_BASE_DIR = os.path.expanduser('~/gensim-data')
# BASE_DIR = os.environ.get('GENSIM_DATA_DIR')
# print(BASE_DIR)
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 = [('spatulate','broad'),('upperparts','upper parts'),('grey','gray')]
#%%
# path = 'datasets/Attribute/attribute/{}/attributes.txt'.format('CUB')
path = 'datasets/attribute/{}/attributes.txt'.format('CUB')
df=pd.read_csv(path,sep=' ',header = None, names = ['idx','des'])
des = df['des'].values
#%% filter
new_des = [' '.join(i.split('_')) for i in des]
new_des = [' '.join(i.split('-')) for i in new_des]
new_des = [' '.join(i.split('::')) for i in new_des]
new_des = [i.split('(')[0] for i in new_des]
new_des = [i[4:] for i in new_des] # moveout 'has '
#%% 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/CUB/new_des.csv')
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('datasets/Attribute/w2v/CUB_attribute.pkl','wb') as f:
pickle.dump(all_w2v,f)