윤영빈

almost done

......@@ -12,8 +12,10 @@ import video_util as videoutil
MODEL_PATH = "/mnt/e/khuhub/2015104192/web/backend/yt8m/esot3ria/model/inference_model/segment_inference_model"
VOCAB_PATH = "/mnt/e/khuhub/2015104192/web/backend/yt8m/vocabulary.csv"
VIDEO_TAGS_PATH = "/mnt/e/khuhub/2015104192/web/backend/yt8m/esot3ria/segment_tags.csv"
VIDEO_IDS_PATH = "/mnt/e/khuhub/2015104192/web/backend/yt8m/esot3ria/videoIds.csv"
TAG_VECTOR_MODEL_PATH = "/mnt/e/khuhub/2015104192/web/backend/yt8m/esot3ria/tag_vectors.model"
VIDEO_VECTOR_MODEL_PATH = "/mnt/e/khuhub/2015104192/web/backend/yt8m/esot3ria/video_vectors.model"
VIDEO_ID_MODEL_PATH = "/mnt/e/khuhub/2015104192/web/backend/yt8m/esot3ria/videoId_vectors.model"
SEGMENT_LABEL_PATH = "/mnt/e/khuhub/2015104192/web/backend/yt8m/segment_label_ids.csv"
# Define parameters.
......@@ -228,8 +230,8 @@ def inference_pb(file_path, threshold):
# 5. Create recommend videos info, Combine results.
recommend_video_ids = recommender.recommend_videos(tag_result, inputVideoTagResults, TAG_VECTOR_MODEL_PATH,
VIDEO_VECTOR_MODEL_PATH, VIDEO_TOP_K)
video_result = [videoutil.getVideoInfo(ids, VIDEO_TAGS_PATH, TAG_TOP_K) for ids in recommend_video_ids]
VIDEO_VECTOR_MODEL_PATH, VIDEO_ID_MODEL_PATH, VIDEO_TOP_K)
video_result = [videoutil.getVideoInfo(ids, VIDEO_TAGS_PATH, TAG_TOP_K,False) for ids in recommend_video_ids]
inference_result = {
"tag_result": tag_result,
......
This file is too large to display.
This diff could not be displayed because it is too large.
......@@ -6,7 +6,7 @@ import pandas as pd
import math
import activation as ac
def recommend_videos(tags, segments, tag_model_path, video_model_path, top_k):
def recommend_videos(tags, segments, tag_model_path, video_model_path, video_id_model, top_k):
# 이 함수에서 모든걸 다 함
# tags는 label val 로 묶인 문자열 리스트임
# tags의 길이는 segment의 길이
......@@ -17,11 +17,12 @@ def recommend_videos(tags, segments, tag_model_path, video_model_path, top_k):
#segments는 클래스 확률 클래스 확률... 일케 저장되어 있음
tag_vectors = Word2Vec.load(tag_model_path).wv
video_vectors = Word2Vec().wv.load(video_model_path)
video_ids = Word2Vec().wv.load(video_id_model)
error_tags = []
maxSimilarSegment = 0
maxSimilarity = -1
kernel = [np.zeros(100) for i in range(0,5)]
print('prev len',len(segments))
kernel = [np.zeros(100) for i in range(0,9)]
tagKernel = []
#우선은 비교를 뜰 입력 영상의 단일 비디오벡터를 구함
video_vector = np.zeros(100)
......@@ -41,27 +42,39 @@ def recommend_videos(tags, segments, tag_model_path, video_model_path, top_k):
error_tags.append(tag)
#각 세그먼트마다 비교를 떠서 인덱스를 저장
currentIndex = 0
for segment in segments:
segment_vector = np.zeros(100)
segTags = [segment[i] for i in range(0,len(segment),2)]
segProbs = [float(segment[i]) for i in range(1,len(segment),2)]#ac.softmax([float(segment[i]) for i in range(1,len(segment),2)])
midpos = math.floor(len(kernel)/2)
for i in range(0,midpos):
segments.insert(0,segments[0])
segments.append(segments[len(segments)-1])
for tag, weight in zip(segTags,segProbs):
if tag in tag_vectors.vocab:
segment_vector = segment_vector + (tag_vectors[tag] * float(weight))
else:
# Pass if tag is unknown
if tag not in error_tags:
error_tags.append(tag)
#비디오 벡터와 세그먼트 벡터 비교
similarity = cos_sim(video_vector, segment_vector) #cos_sim(video_vector, segment_vector)#
currentIndex = midpos
for si in range(midpos,len(segments) - midpos - 1):
similarity = 0
for segi in range(-1,2):
segment = segments[si + segi]
segment_vector = np.zeros(100)
segTags = [segment[i] for i in range(0,len(segment),2)]
segProbs = [float(segment[i]) for i in range(1,len(segment),2)]#ac.softmax([float(segment[i]) for i in range(1,len(segment),2)])
for tag, weight in zip(segTags,segProbs):
if tag in tag_vectors.vocab:
segment_vector = segment_vector + (tag_vectors[tag] * float(weight))
else:
# Pass if tag is unknown
if tag not in error_tags:
error_tags.append(tag)
#비디오 벡터와 세그먼트 벡터 비교
#similarity = similarity + cos_sim(video_vector, segment_vector) #cos_sim(video_vector, segment_vector)#
for currentSegmentTag, videoVectorTag in zip(segTags,videoTagList):
if(currentSegmentTag in tag_vectors.vocab) and (videoVectorTag in tag_vectors.vocab):
similarity = similarity + tag_vectors.similarity(currentSegmentTag,videoVectorTag)
for currentSegmentTag, videoVectorTag,videoVectorTagPred in zip(segTags,videoTagList,tag_preds):
if(currentSegmentTag in tag_vectors.vocab) and (videoVectorTag in tag_vectors.vocab):
prob = float(videoVectorTagPred)
if videoVectorTag not in segTags:
prob = 0
similarity = similarity + (tag_vectors.similarity(currentSegmentTag,videoVectorTag) * prob)
if similarity >= maxSimilarity:
maxSimilarSegment = currentIndex
maxSimilarity = similarity
......@@ -71,7 +84,7 @@ def recommend_videos(tags, segments, tag_model_path, video_model_path, top_k):
maxSimilarSegment = len(segments) - int(len(kernel)/2) - 1
#세그먼트 인덱스 증가
currentIndex = currentIndex + 1
print('maxSimilarSegment',maxSimilarSegment,'len',len(segments))
#커널 생성
for k in range (0,len(kernel)):
segment = segments[maxSimilarSegment - math.floor(len(kernel)/2) + k]
......@@ -139,11 +152,11 @@ def recommend_videos(tags, segments, tag_model_path, video_model_path, top_k):
tagList.append([row[i].split(":")[0],row[i].split(":")[1]])
segmentTagList.append(tagList)
similar_ids = []
for i in range(0,top_k):
similar_ids.append(minimunVideoIds[i][0])
#similar_ids = []
#for i in range(0,top_k):
# similar_ids.append(minimunVideoIds[i][0])
#similar_ids = [x[0] for x in video_vectors.similar_by_vector(video_vector, top_k)]
similar_ids = [x[0] for x in video_ids.similar_by_vector(video_vector, top_k)]
print('results =' ,similar_ids)
return similar_ids
......@@ -199,12 +212,14 @@ def differenceMax(arrs, _kernel, w2v, videoTaglist):
convResult = 0
processed_vocabNum = 1
for i in range(0, s):
#if i == midpos:
if(_kernel[i][0] not in arrs[j - midpos + i][0:2][0]):# and ((videoTaglist[0] not in arrs[j - midpos + i][0:2])) and ((videoTaglist[1] not in arrs[j - midpos + i][0:5])):
if(_kernel[i][0] not in arrs[j - midpos + i][0]):# and ((videoTaglist[0] not in arrs[j - midpos + i][0:2])) and ((videoTaglist[1] not in arrs[j - midpos + i][0:5])):
continue
for ind in range(0,5):
if(arrs[j - midpos + i][ind][0] in w2v.vocab) and (_kernel[i][ind] in w2v.vocab):
convResult = convResult + (w2v.similarity(arrs[j - midpos + i][ind][0],_kernel[i][ind]) * float(arrs[j - midpos + i][ind][1]))
prob = float(arrs[j - midpos + i][ind][1])
if arrs[j - midpos + i][ind][0] not in videoTaglist:
prob = 0
convResult = convResult + (w2v.similarity(arrs[j - midpos + i][ind][0],_kernel[i][ind]) * prob)
processed_vocabNum = processed_vocabNum + 1
if prevMax < convResult:
......
......@@ -16,7 +16,7 @@ def getURL(vid_id):
print(getURL('nzwW'))
def getVideoInfo(vid_id, video_tags_path, top_k):
def getVideoInfo(vid_id, video_tags_path, top_k, isPerVideo):
print("vid_id = ",vid_id)
video_url = getURL(vid_id[0:4])
......@@ -31,8 +31,9 @@ def getVideoInfo(vid_id, video_tags_path, top_k):
if video_url == "":
for x in video_tags:
video_url = video_url + ' ' + x
video_url = video_url + '\nThe similar point is : ' + str(float(vid_id[5:]) * 5)
if isPerVideo == False:
video_url = video_url + '\nThe similar point is : ' + str(float(vid_id[5:]) * 5)
return {
"video_url": video_url,
......
......@@ -24,8 +24,8 @@ if __name__ == '__main__':
video_vectors = Word2Vec().wv # Empty model
# Load video recommendation tags.
video_tags = pd.read_csv('./segment_tags.csv', encoding='utf-8',error_bad_lines=False)
#video_tags = pd.read_csv('./segment_tags.csv', encoding='utf-8',error_bad_lines=False)
video_tags = pd.read_csv('./videoIds.csv', encoding='utf-8',error_bad_lines=False)
# Define batch variables.
batch_video_ids = []
batch_video_vectors = []
......@@ -64,7 +64,7 @@ if __name__ == '__main__':
print(error_tags)
print(len(error_tags))
video_vectors.save("video_vectors.model")
video_vectors.save("videoId_vectors.model")
# Usage
# video_vectors = Word2Vec().wv.load("video_vectors.model")
......
......@@ -395,6 +395,197 @@ def inference(reader, train_dir, data_pattern, out_file_location, batch_size,
coord.join(threads)
sess.close()
def inference2(reader, train_dir, data_pattern, out_file_location, batch_size,
top_k):
"""Inference function."""
with tf.Session(config=tf.ConfigProto(
allow_soft_placement=True)) as sess, gfile.Open(out_file_location,
"w+") as out_file:
video_id_batch, video_batch, num_frames_batch = get_input_data_tensors(
reader, data_pattern, batch_size)
inference_model_name = "segment_inference_model" if FLAGS.segment_labels else "inference_model"
checkpoint_file = os.path.join(train_dir, "inference_model",
inference_model_name)
if not gfile.Exists(checkpoint_file + ".meta"):
raise IOError("Cannot find %s. Did you run eval.py?" % checkpoint_file)
meta_graph_location = checkpoint_file + ".meta"
logging.info("loading meta-graph: " + meta_graph_location)
if FLAGS.output_model_tgz:
with tarfile.open(FLAGS.output_model_tgz, "w:gz") as tar:
for model_file in glob.glob(checkpoint_file + ".*"):
tar.add(model_file, arcname=os.path.basename(model_file))
tar.add(os.path.join(train_dir, "model_flags.json"),
arcname="model_flags.json")
print("Tarred model onto " + FLAGS.output_model_tgz)
with tf.device("/cpu:0"):
saver = tf.train.import_meta_graph(meta_graph_location,
clear_devices=True)
logging.info("restoring variables from " + checkpoint_file)
saver.restore(sess, checkpoint_file)
input_tensor = tf.get_collection("input_batch_raw")[0]
num_frames_tensor = tf.get_collection("num_frames")[0]
predictions_tensor = tf.get_collection("predictions")[0]
# Workaround for num_epochs issue.
def set_up_init_ops(variables):
init_op_list = []
for variable in list(variables):
if "train_input" in variable.name:
init_op_list.append(tf.assign(variable, 1))
variables.remove(variable)
init_op_list.append(tf.variables_initializer(variables))
return init_op_list
sess.run(
set_up_init_ops(tf.get_collection_ref(tf.GraphKeys.LOCAL_VARIABLES)))
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
num_examples_processed = 0
start_time = time.time()
whitelisted_cls_mask = None
if FLAGS.segment_labels:
final_out_file = out_file
out_file = tempfile.NamedTemporaryFile()
logging.info(
"Segment temp prediction output will be written to temp file: %s",
out_file.name)
if FLAGS.segment_label_ids_file:
whitelisted_cls_mask = np.zeros((predictions_tensor.get_shape()[-1],),
dtype=np.float32)
segment_label_ids_file = FLAGS.segment_label_ids_file
if segment_label_ids_file.startswith("http"):
logging.info("Retrieving segment ID whitelist files from %s...",
segment_label_ids_file)
segment_label_ids_file, _ = urllib.request.urlretrieve(
segment_label_ids_file)
with tf.io.gfile.GFile(segment_label_ids_file) as fobj:
for line in fobj:
try:
cls_id = int(line)
whitelisted_cls_mask[cls_id] = 1.
except ValueError:
# Simply skip the non-integer line.
continue
out_file.write(u"VideoId,LabelConfidencePairs\n".encode("utf8"))
#=========================================
#open vocab csv file and store to dictionary
#=========================================
voca_dict = {}
vocabs = codecs.open('./vocabulary.csv', 'r','utf-8')
while True:
line = vocabs.readline()
if not line: break
vocab_dict_item = line.split(",")
if vocab_dict_item[0] != "Index":
voca_dict[vocab_dict_item[0]] = vocab_dict_item[3]
vocabs.close()
try:
while not coord.should_stop():
video_id_batch_val, video_batch_val, num_frames_batch_val = sess.run(
[video_id_batch, video_batch, num_frames_batch])
if FLAGS.segment_labels:
results = get_segments(video_batch_val, num_frames_batch_val, 5)
video_segment_ids = results["video_segment_ids"]
video_id_batch_val = video_id_batch_val[video_segment_ids[:, 0]]
video_id_batch_val = np.array([
"%s:%d" % (x.decode("utf8"), y)
for x, y in zip(video_id_batch_val, video_segment_ids[:, 1])
])
video_batch_val = results["video_batch"]
num_frames_batch_val = results["num_frames_batch"]
if input_tensor.get_shape()[1] != video_batch_val.shape[1]:
raise ValueError("max_frames mismatch. Please re-run the eval.py "
"with correct segment_labels settings.")
predictions_val, = sess.run([predictions_tensor],
feed_dict={
input_tensor: video_batch_val,
num_frames_tensor: num_frames_batch_val
})
now = time.time()
num_examples_processed += len(video_batch_val)
elapsed_time = now - start_time
logging.info("num examples processed: " + str(num_examples_processed) +
" elapsed seconds: " + "{0:.2f}".format(elapsed_time) +
" examples/sec: %.2f" %
(num_examples_processed / elapsed_time))
for line in format_lines(video_id_batch_val, predictions_val, top_k,
whitelisted_cls_mask):
out_file.write(line)
out_file.flush()
except tf.errors.OutOfRangeError:
logging.info("Done with inference. The output file was written to " +
out_file.name)
finally:
coord.request_stop()
if FLAGS.segment_labels:
# Re-read the file and do heap sort.
# Create multiple heaps.
logging.info("Post-processing segment predictions...")
segment_id_list = []
segment_classes = []
cls_result_arr = []
cls_score_dict = {}
out_file.seek(0, 0)
old_seg_name = '0000'
counter = 0
for line in out_file:
counter += 1
if counter / 5000 == 0:
print(counter, " processed")
segment_id, preds = line.decode("utf8").split(",")
if segment_id == "VideoId":
# Skip the headline.
continue
preds = preds.split(" ")
pred_cls_ids = [int(preds[idx]) for idx in range(0, len(preds), 2)]
pred_cls_scores = [float(preds[idx]) for idx in range(1, len(preds), 2)]
#=======================================
segment_id = str(segment_id.split(":")[0])
if segment_id not in segment_id_list:
segment_id_list.append(str(segment_id))
segment_classes.append("")
index = segment_id_list.index(segment_id)
if old_seg_name != segment_id:
cls_score_dict[segment_id] = {}
old_seg_name = segment_id
for classes in range(0,len(pred_cls_ids)):#pred_cls_ids:
segment_classes[index] = str(segment_classes[index]) + str(pred_cls_ids[classes]) + " " #append classes from new segment
if pred_cls_ids[classes] in cls_score_dict[segment_id]:
cls_score_dict[segment_id][pred_cls_ids[classes]] = cls_score_dict[segment_id][pred_cls_ids[classes]] + pred_cls_scores[classes]
else:
cls_score_dict[segment_id][pred_cls_ids[classes]] = pred_cls_scores[classes]
for segs,item in zip(segment_id_list,segment_classes):
print('====== R E C O R D ======')
cls_arr = item.split(" ")[:-1]
cls_arr = list(map(int,cls_arr))
cls_arr = sorted(cls_arr) #클래스별로 정렬
result_string = ""
temp = cls_score_dict[segs]
temp= sorted(temp.items(), key=operator.itemgetter(1), reverse=True) #밸류값 기준으로 정렬
demoninator = float(temp[0][1] + temp[1][1] + temp[2][1] + temp[3][1] + temp[4][1])
#for item in temp:
for itemIndex in range(0, top_k):
# Normalize tag name
segment_tag = str(voca_dict[str(temp[itemIndex][0])])
normalized_tag = normalize_tag(segment_tag)
result_string = result_string + normalized_tag + ":" + format(temp[itemIndex][1]/demoninator,".3f") + ","
cls_result_arr.append(result_string[:-1])
logging.info(segs + " : " + result_string[:-1])
#=======================================
final_out_file.write("vid_id,segment1,segment2,segment3,segment4,segment5\n")
for seg_id, class_indcies in zip(segment_id_list, cls_result_arr):
final_out_file.write("%s,%s\n" %(seg_id, str(class_indcies)))
final_out_file.close()
out_file.close()
coord.join(threads)
sess.close()
def main(unused_argv):
logging.set_verbosity(tf.logging.INFO)
if FLAGS.input_model_tgz:
......@@ -431,7 +622,7 @@ def main(unused_argv):
raise ValueError("'input_data_pattern' was not specified. "
"Unable to continue with inference.")
inference(reader, FLAGS.train_dir, FLAGS.input_data_pattern,
inference2(reader, FLAGS.train_dir, FLAGS.input_data_pattern,
FLAGS.output_file, FLAGS.batch_size, FLAGS.top_k)
def normalize(arrs):
......@@ -443,3 +634,4 @@ def normalize(arrs):
if __name__ == "__main__":
app.run()
......