윤영빈

mid conv result

......@@ -11,7 +11,7 @@ def softmax(inputA):
normalized_arr = []
for x in inputA:
normalized_arr.append(float(x))
#normalized_arr = normalize(normalized_arr)
normalized_arr = normalize(normalized_arr)
for i in range(0, len(normalized_arr)):
......@@ -36,5 +36,5 @@ def normalize(arrs):
minimum = min(normalized_arr)
denom = float(maximum) - float(minimum)
for i in range(0,len(normalized_arr)):
normalized_arr[i] = (normalized_arr[i] - minimum)/ denom
normalized_arr[i] = ((normalized_arr[i] - minimum)/ denom) * 2 - 1
return normalized_arr
\ No newline at end of file
......
......@@ -10,11 +10,11 @@ import video_util as videoutil
# Define file paths.
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/prevs/vocabulary.csv"
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"
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"
SEGMENT_LABEL_PATH = "/mnt/e/khuhub/2015104192/web/backend/yt8m//prevs/segment_label_ids.csv"
SEGMENT_LABEL_PATH = "/mnt/e/khuhub/2015104192/web/backend/yt8m/segment_label_ids.csv"
# Define parameters.
TAG_TOP_K = 5
......
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
......@@ -2,7 +2,7 @@ import nltk
import gensim
import pandas as pd
from gensim.models import Word2Vec
import sys
def normalize_tag(tag):
if isinstance(tag, str):
new_tag = tag.lower().replace('[^a-zA-Z]', ' ')
......@@ -13,7 +13,7 @@ def normalize_tag(tag):
return new_tag
else:
return tag
'''
# Load files.
nltk.download('stopwords')
vocab = pd.read_csv('E:/khuhub/2015104192/web/backend/yt8m/esot3ria/vocabulary.csv',encoding='utf-8')
......@@ -49,11 +49,14 @@ vocab_phrased = phraser[tokenlist]
# Vectorize tags.
w2v = gensim.models.word2vec.Word2Vec(sentences=vocab_phrased, min_count=1)
w2v.save('E:/khuhub/2015104192/web/backend/yt8m/esot3ria/tag_vectors3.model')
tag_vectors = Word2Vec.load("./tag_vectors3.model").wv
print(tag_vectors['concert'])
w2v.save('E:/khuhub/2015104192/web/backend/yt8m/esot3ria/tag_vectors.model')
'''
tag_vectors = Word2Vec.load("./tag_vectors.model").wv
print(tag_vectors.similarity('koi','koi'))
all_sims = tag_vectors.most_similar('koi', topn=sys.maxsize)
last_10 = list(reversed(all_sims[-10:]))
print(last_10)
# word_vectors = w2v.wv
# vocabs = word_vectors.vocab.keys()
# word_vectors_list = [word_vectors[v] for v in vocabs]
\ No newline at end of file
......
......@@ -22,15 +22,16 @@ def recommend_videos(tags, segments, tag_model_path, video_model_path, top_k):
maxSimilarity = -1
kernel = [np.zeros(100) for i in range(0,5)]
tagKernel = []
#우선은 비교를 뜰 입력 영상의 단일 비디오벡터를 구함
video_vector = np.zeros(100)
tag_preds =[]
videoTagList = []
for (tag, weight) in tags:
tag_preds.append(weight)
videoTagList.append(tag)
#print("tag preds = ",tag_preds)
tag_preds = ac.softmax(tag_preds)
#tag_preds = ac.softmax(tag_preds)
for (tag, weight),pred in zip(tags,tag_preds):
print(tag,pred)
if tag in tag_vectors.vocab:
......@@ -47,7 +48,7 @@ def recommend_videos(tags, segments, tag_model_path, video_model_path, top_k):
for segment in segments:
segment_vector = np.zeros(100)
segTags = [segment[i] for i in range(0,len(segment),2)]
segProbs = ac.softmax([float(segment[i]) for i in range(1,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)])
#print(segProbs)
for tag, weight in zip(segTags,segProbs):
......@@ -80,11 +81,11 @@ def recommend_videos(tags, segments, tag_model_path, video_model_path, top_k):
segment = segments[maxSimilarSegment - math.floor(len(kernel)/2) + k]
segment_vector = np.zeros(100)
segTags = [segment[i] for i in range(0,len(segment),2)]
tagKernel.append(segTags)
segProbs = ac.softmax([float(segment[i]) for i in range(1,len(segment),2)])
print(segTags)
print(segProbs)
normalize(segProbs)
#normalize(segProbs)
for (tag, weight) in zip(segTags,segProbs):
if tag in tag_vectors.vocab:
#float(weight)
......@@ -96,11 +97,22 @@ def recommend_videos(tags, segments, tag_model_path, video_model_path, top_k):
error_tags.append(tag)
kernel[k] = segment_vector
'''
if(k < int(len(kernel)/2)):
kernel[k] = kernel[k] * k
elif(k > int(len(kernel)/2)):
kernel[k] = kernel[k] * (len(kernel) - k)
else:
kernel[k] = kernel[k] * (len(kernel)/2 + 1)
'''
print("TAG kernel")
#tagKernel = tagKernel[1:5]
print(tagKernel)
#여기에서 유사한 벡터들을 뽑아냄
#현재는 비디오id로 영상을 얻을 수 없으므로 반환값으로 비디오 아이디와 태그들, 확률 사용
video_tags = pd.read_csv('/mnt/e/khuhub/2015104192/web/backend/yt8m/esot3ria/segment_tags.csv', encoding='utf8',error_bad_lines=False)
videoVectorList = []
segmentTagList = []
prevVideoId = ""
minimunVideoIds = [["",-1.0] for i in range(0,top_k)]
......@@ -117,7 +129,10 @@ def recommend_videos(tags, segments, tag_model_path, video_model_path, top_k):
#print("====")
#for a in kernel:
# print(len(kernel),norm(a))
maxima, idx = convolution(videoVectorList,kernel,prevVideoId)
convmaxima, convidx = convolution(videoVectorList,kernel,prevVideoId)
maxima, idx = differenceMax(segmentTagList,tagKernel,tag_vectors,videoTagList)
#maxima = maxima + convmaxima
#print(video_id,maxima)
localMinima = 100
localMinimaIndex = -1
......@@ -127,16 +142,21 @@ def recommend_videos(tags, segments, tag_model_path, video_model_path, top_k):
localMinimaIndex = seg
#print(maxima)
if localMinima < maxima:
print(prevVideoId[0:4] + "_" + str(idx),maxima)
print(prevVideoId[0:4] + "_" + str(idx),localMinimaIndex,maxima,convmaxima)
minimunVideoIds[localMinimaIndex] = [prevVideoId[0:4] + "_" + str(idx),maxima]
videoVectorList.clear()
segmentTagList.clear()
prevVideoId = video_id
if video_id == "finished":
break
videoVectorList.append(video_vectors[video_id])
tagList = []
for i in range(1,top_k+1):
tagList.append(row[i].split(":")[0])
segmentTagList.append(tagList)
similar_ids = []
for i in range(0,top_k):
......@@ -155,30 +175,65 @@ def cos_sim(A, B):
return 0
else:
return dot(A, B)/(norm(A)*norm(B))
def shiftKernel(kernel, newValue):
for i in range(0, len(kernel) - 1):
kernel[i] = kernel[i+1]
kernel[len(kernel) - 1] = newValue
def sub_vec_size(A,B):
dir = A-B
return norm(dir)
def convolution(arrs, _kernel,vidId):
s = len(_kernel)
l = len(arrs)
result = []
midpos = math.floor(s/2)
for i in range(0,midpos):
arrs.insert(0,np.zeros(100))
arrs.append(np.zeros(100))
total = 0
for j in range(midpos,len(arrs) - midpos):
convResult = 0
for i in range(0, s):
convResult = convResult + cos_sim(arrs[j - midpos + i],_kernel[i])
if(i == int(len(_kernel)/2)):
convResult = convResult - sub_vec_size(arrs[j - midpos + i],_kernel[i]) + dot(arrs[j - midpos + i],_kernel[i])
result.append(convResult)
total = total + convResult
maxVal = max(result)
index = result.index(maxVal)
return maxVal,index
return total/l,index
def differenceMax(arrs, _kernel, w2v, videoTaglist):
s = len(_kernel)
result = []
midpos = math.floor(s/2)
for i in range(0,midpos):
arrs.insert(0,arrs[0])
arrs.append(arrs[len(arrs)-1])
prevIndex = 0
prevMax = -100
for j in range(midpos,len(arrs) - midpos):
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]):# 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] in w2v.vocab) and (_kernel[i][ind] in w2v.vocab):
convResult = convResult + (w2v.similarity(arrs[j - midpos + i][ind],_kernel[i][ind]))
processed_vocabNum = processed_vocabNum + 1
#convResult = convResult / processed_vocabNum
if prevMax < convResult:
prevMax = convResult
prevIndex = j - midpos
result.append(convResult)
#maxVal = max(result)
#index = result.index(maxVal)
return prevMax,prevIndex
def normalize(arrs):
maximum = max(arrs)
......
......@@ -29,7 +29,10 @@ def getVideoInfo(vid_id, video_tags_path, top_k):
video_tag_tuple = video_tags_info["segment" + str(i)].values[0]# ex: "mobile-phone:0.361"
video_tags.append(video_tag_tuple.split(":")[0])
if video_url == "":
video_url = video_url + ' ' + video_tags
for x in video_tags:
video_url = video_url + ' ' + x
video_url = video_url + '\nThe similar point is : ' + str(float(vid_id[5:]) * 5)
return {
"video_url": video_url,
......
This file is too large to display.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.