-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphRec-kfashion_Inference.py
272 lines (206 loc) · 10.2 KB
/
GraphRec-kfashion_Inference.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
'''
paper titles "Attribute-Aware Non-Linear Co-Embeddings of Graph Features" Accepted in RecSys 2019
This code was implemented using python 3.5 and TensorFlow 1.7
We would like to thank "Guocong Song" because we utilized parts of his code from "songgc/TF-recomm" in our implementation
'''
import os
import sys
import numpy as np
import pandas as pd
import time
from collections import deque
import argparse
import matplotlib.pyplot as plt
import tensorflow as tf
from six import next
from sklearn import preprocessing
from sklearn import preprocessing
from scipy.sparse import lil_matrix
from scipy.sparse import coo_matrix
from utils.iterator import *
from utils.load_dataset import *
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import warnings
warnings.filterwarnings(action='ignore')
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
tf.logging.set_verbosity(tf.logging.ERROR)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
os.environ['CUDA_VISIBLE_DEVICES']=''
tf.Session(config=tf.ConfigProto(device_count={"GPU": 0, "CPU": 1}))
def get_data_HR(opt):
global PERC
df_test = read_process(f"{opt.HR_PATH}")
return df_test
def get_data_Entire(opt):
global PERC
df_test = read_process(f"{opt.TEST_PATH}")
return df_test
class ShuffleIterator(object):
def __init__(self, inputs, batch_size=10):
self.inputs = inputs
self.batch_size = batch_size
self.num_cols = len(self.inputs)
self.len = len(self.inputs[0])
self.inputs = np.transpose(np.vstack([np.array(self.inputs[i]) for i in range(self.num_cols)]))
def __len__(self):
return self.len
def __iter__(self):
return self
def __next__(self):
return self.next()
def next(self):
ids = np.random.randint(0, self.len, (self.batch_size,))
out = self.inputs[ids, :]
return [out[:, i] for i in range(self.num_cols)]
class OneEpochIterator(ShuffleIterator):
def __init__(self, inputs, batch_size=10):
super(OneEpochIterator, self).__init__(inputs, batch_size=batch_size)
if batch_size > 0:
self.idx_group = np.array_split(np.arange(self.len), np.ceil(self.len / batch_size))
else:
self.idx_group = [np.arange(self.len)]
self.group_id = 0
def next(self):
if self.group_id >= len(self.idx_group):
self.group_id = 0
raise StopIteration
out = self.inputs[self.idx_group[self.group_id], :]
self.group_id += 1
return [out[:, i] for i in range(self.num_cols)]
def inferenceDense(phase,user_batch, item_batch,idx_user,idx_item, user_num, item_num,UReg=0.05,IReg=0.1):
with tf.device(opt.DEVICE):
user_batch = tf.nn.embedding_lookup(idx_user, user_batch, name="embedding_user")
item_batch = tf.nn.embedding_lookup(idx_item, item_batch, name="embedding_item")
ul1mf=tf.layers.dense(inputs=user_batch, units=opt.MFSIZE,activation=tf.nn.crelu, kernel_initializer=tf.random_normal_initializer(stddev=0.01))
il1mf=tf.layers.dense(inputs=item_batch, units=opt.MFSIZE,activation=tf.nn.crelu, kernel_initializer=tf.random_normal_initializer(stddev=0.01))
InferInputMF=tf.multiply(ul1mf, il1mf)
infer=tf.reduce_sum(InferInputMF, 1, name="inference")
regularizer = tf.add(opt.UW*tf.nn.l2_loss(ul1mf), opt.IW*tf.nn.l2_loss(il1mf), name="regularizer")
return infer, regularizer
def clip(x):
return np.clip(x, 1.0, 5.0)
def GraphRec_test(opt, test, ItemData=False,UserData=False,Graph=False):
iter_test = OneEpochIterator([test["user"],test["item"],test["rate"]],batch_size=opt.BATCH_SIZE)
phase = tf.placeholder(tf.bool, name='phase')
init_op = tf.global_variables_initializer()
# Load model
with tf.Session() as sess:
sess.run(init_op)
saver = tf.train.import_meta_graph(f'./{opt.SAVE_NAME}/{opt.WEIGHT_NAME}.meta', clear_devices=True)
saver.restore(sess, f'./{opt.SAVE_NAME}/{opt.WEIGHT_NAME}')
user_batch = tf.get_collection('train_var')[0]
item_batch = tf.get_collection('train_var')[1]
infer = tf.get_collection('train_var')[2]
test_err2 = np.array([])
degreelist=list()
predlist=list()
for users, items, rates in iter_test:
pred_batch = sess.run(infer, feed_dict={user_batch: users,
item_batch: items,
phase:False})
pred_batch = clip(pred_batch)
test_err2 = np.append(test_err2, np.power(pred_batch - rates, 2))
test_err = np.sqrt(np.mean(test_err2))
finalerror=test_err
print('\033[31m \033[43m' + 'RMSE Score' + " : " + str(finalerror) + '\033[0m')
def GraphRec_hr(opt, test, ItemData=False,UserData=False,Graph=False):
iter_test = OneEpochIterator([test["user"],test["item"],test["rate"]],batch_size=1)
phase = tf.placeholder(tf.bool, name='phase')
init_op = tf.global_variables_initializer()
# Load model
dt_ = []
hit_correct = 0
user_number = 0
with tf.Session() as sess:
sess.run(init_op)
saver = tf.train.import_meta_graph(f'./{opt.SAVE_NAME}/{opt.WEIGHT_NAME}.meta', clear_devices=True)
saver.restore(sess, f'./{opt.SAVE_NAME}/{opt.WEIGHT_NAME}')
user_batch = tf.get_collection('train_var')[0]
item_batch = tf.get_collection('train_var')[1]
infer = tf.get_collection('train_var')[2]
for users, items, rates in iter_test:
pred_batch = sess.run(infer, feed_dict={user_batch: users,
item_batch: items,
phase:False})
pred_batch = clip(pred_batch)
dt_.append([users[0], items[0], pred_batch[0]])
#if pred_batch[0] > 2.7 :
# hit_correct += 1
#user_number += 1
#HRscore = hit_correct/user_number
#print('\033[31m \033[42m' + 'HR@10 Score' + " : " + str(HRscore) + '\033[0m')
predicted_df = pd.DataFrame(dt_, columns=["user", "item", "rate"])
predicted_df.to_csv("./kdeepfashion/pred_df.csv")
def GraphRec_infer(opt, test, ItemData=False,UserData=False,Graph=False):
iter_test = OneEpochIterator([test["user"],test["item"],test["rate"]],batch_size=1)
phase = tf.placeholder(tf.bool, name='phase')
init_op = tf.global_variables_initializer()
# Load model
dt_ = []
with tf.Session() as sess:
sess.run(init_op)
saver = tf.train.import_meta_graph(f'./{opt.SAVE_NAME}/{opt.WEIGHT_NAME}.meta', clear_devices=True)
saver.restore(sess, f'./{opt.SAVE_NAME}/{opt.WEIGHT_NAME}')
user_batch = tf.get_collection('train_var')[0]
item_batch = tf.get_collection('train_var')[1]
infer = tf.get_collection('train_var')[2]
for users, items, rates in iter_test:
pred_batch = sess.run(infer, feed_dict={user_batch: users,
item_batch: items,
phase:False})
pred_batch = clip(pred_batch)
dt_.append([users[0], items[0], pred_batch[0]])
predicted_df = pd.DataFrame(dt_, columns=["user", "item", "rate"])
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--TEST_PATH', default="./kdeepfashion/rate_data.csv", help="test dataset path")
parser.add_argument('--HR_PATH', default="./kdeepfashion/HR_test.csv", help="test dataset path")
parser.add_argument('--WEIGHT_NAME', default="trained_ckpt-6226", help="saved weighted file name")
parser.add_argument('--EPOCH_MAX', type=int, default=196)
parser.add_argument('--BATCH_SIZE', type=int, default=1, help='total batch size for all GPUs')
parser.add_argument('--DEVICE', default="/gpu:0", help="/gpu:0")
parser.add_argument('--SAVE_NAME', default="model_kfashion_add_externel", help="save folder name")
parser.add_argument('--MFSIZE', type=int, default=50)
parser.add_argument('--UW', type=float, default=0.05)
parser.add_argument('--IW', type=float, default=0.02)
parser.add_argument('--LR', type=float, default=0.00003, help='Learning Rate')
parser.add_argument('--PERC', type=float, default=0.9, help='Training dataset Rate')
parser.add_argument('--RUN', default="test", help="test+HR / infer")
opt = parser.parse_args()
DEVICE="/cpu"
################################################
if opt.RUN == "test":
_, df_test = get_data(opt)
print()
print('\033[31m' + '----------------------------------' + '\033[0m')
print(f'■ Size of Test Dataset : {df_test.shape}')
tf.compat.v1.reset_default_graph()
GraphRec_test(opt, df_test,
ItemData=True, UserData=True, Graph=True)
df_test = get_data_HR(opt)
df_entire = get_data_Entire(opt)
tf.compat.v1.reset_default_graph()
GraphRec_hr(opt, df_entire,
ItemData=True, UserData=True, Graph=True)
predicted_df = read_process("./kdeepfashion/pred_df.csv")
hr_correct = 0
user_num = 0
for i in df_test["user"].unique().tolist():
z = df_test.loc[(df_test["user"] == i), "item"].values[0]
bool_ = z in predicted_df.loc[(predicted_df["user"] == i)].sort_values("rate", ascending=False).head(10)["item"].tolist()
user_num += 1
if bool_:
hr_correct+=1
else:
pass
HRscore = hr_correct / user_num
print()
print('\033[31m' + '----------------------------------' + '\033[0m')
print(f'■ Number of Test Users used for HR@10 : {user_num}')
print('\033[31m \033[42m' + 'HR@10 Score' + " : " + str(HRscore) + '\033[0m')
if opt.RUN == "infer":
df_test = get_data_HR(opt)
tf.compat.v1.reset_default_graph()
GraphRec_infer(opt, df_test,
ItemData=True, UserData=True, Graph=True)