def __init__(self, trainCollection, tpp="lemm", feature="color64+dsift", k=1000, rootpath=ROOT_PATH): self.trainCollection = trainCollection self.k = k self.name = "%s(%s,%s,%s,%d)" % (self.__class__.__name__, self.trainCollection, tpp, feature, k) vobfile = os.path.join(rootpath, trainCollection, "TextData", "wn.%s.txt" % trainCollection) self.vob = set(map(str.strip, open(vobfile).readlines())) printStatus( INFO, 'the vocabulary of %s contains %d tags' % (trainCollection, len(self.vob))) self.gamma = (1.0 / MEDIAN_DISTANCE[feature])**2 self.feat_dir = os.path.join(rootpath, trainCollection, 'FeatureIndex', feature) self.dim = FEATURE_TO_DIM[feature] self.fcs = FlickrContextSim(trainCollection, rootpath) printStatus(INFO, self.name + ' okay')
def __init__(self, trainCollection, tpp="lemm", feature="color64+dsift", k=1000, rootpath=ROOT_PATH): self.trainCollection = trainCollection self.k = k self.name = "%s(%s,%s,%s,%d)" % (self.__class__.__name__, self.trainCollection, tpp, feature, k) vobfile = os.path.join(rootpath, trainCollection, "TextData", "wn.%s.txt"%trainCollection) self.vob = set(map(str.strip, open(vobfile).readlines())) printStatus(INFO, 'the vocabulary of %s contains %d tags' % (trainCollection, len(self.vob))) self.gamma = (1.0/MEDIAN_DISTANCE[feature])**2 self.feat_dir = os.path.join(rootpath, trainCollection, 'FeatureIndex', feature) self.dim = FEATURE_TO_DIM[feature] self.fcs = FlickrContextSim(trainCollection,rootpath) printStatus(INFO, self.name + ' okay')
class TagRanking: def __init__(self, trainCollection, tpp="lemm", feature="color64+dsift", k=1000, rootpath=ROOT_PATH): self.trainCollection = trainCollection self.k = k self.name = "%s(%s,%s,%s,%d)" % (self.__class__.__name__, self.trainCollection, tpp, feature, k) vobfile = os.path.join(rootpath, trainCollection, "TextData", "wn.%s.txt" % trainCollection) self.vob = set(map(str.strip, open(vobfile).readlines())) printStatus( INFO, 'the vocabulary of %s contains %d tags' % (trainCollection, len(self.vob))) self.gamma = (1.0 / MEDIAN_DISTANCE[feature])**2 self.feat_dir = os.path.join(rootpath, trainCollection, 'FeatureIndex', feature) self.dim = FEATURE_TO_DIM[feature] self.fcs = FlickrContextSim(trainCollection, rootpath) printStatus(INFO, self.name + ' okay') def getName(self): return self.name def computePxt(self, qry_vec, tag, uniqueUser=False): assert (self.k <= 1000) feat_file = os.path.join(self.feat_dir, tag[:2], tag, 'feature.bin') vecs = np.fromfile(feat_file, dtype=np.float32) nr_of_images = len(vecs) / self.dim A = vecs.reshape((nr_of_images, self.dim)) weights = [] #s_time = time.time() # accelerate pxt computation by matrix operations squared_distance = np.linalg.norm(qry_vec)**2 + np.linalg.norm( A, axis=1)**2 - 2 * A.dot(qry_vec) assert (len(squared_distance) == nr_of_images) #print squared_distance #squared_distance = squared_distance.tolist()[0] #t1 = time.time() - s_time weights = [math.exp(-self.gamma * x) for x in squared_distance] ''' compute distance per pair, much slower than the matrix based approach s_time = time.time() old_d = [0] * len(vecs) i = 0 for name,x in zip(renamed, vecs): old_d[i] = math.sqrt(sum([(x[j]-qry_vec[j])**2 for j in range(self.dim)])) i += 1 t2 = time.time() - s_time for i in range(len(vecs)): diff = old_d[i]**2 - squared_distance[i] assert(abs(diff)<1e-8) print '%.6f %.6f' % (t1, t2) ''' return np.mean(weights) def estimate(self, qry_vec, qry_tags, uniqueUser=0, doRandomwalk=1): alpha = 0.5 epsilon = 1e-6 tagSeq = str.split(qry_tags) tagSeq = [t for t in tagSeq if t in self.vob] n = len(tagSeq) v = [self.computePxt(qry_vec, tag, uniqueUser) for tag in tagSeq] #print sorted(zip(tagSeq,v), key=lambda v:(v[1]), reverse=True) if not doRandomwalk: tag2score = dict(zip(tagSeq, v)) return sorted(tag2score.iteritems(), key=lambda v: (v[1]), reverse=True) ''' ----------------------------------------- Compute the nxn transition matrix ----------------------------------------- ''' p = np.zeros((n, n)) for i in range(n): for j in range(n): if i == j: p[i, j] = 1.0 elif i < j: p[i, j] = self.fcs.compute(tagSeq[i], tagSeq[j], gamma=1) else: p[i, j] = p[j, i] ''' ----------------------------------------- normalize row s.t. row sum is 1 ----------------------------------------- ''' for i in range(n): rowsum = p[i, :].sum() p[i, :] /= float(rowsum) #print qry_tags #print p ''' ----------------------------------------- do random walk ----------------------------------------- ''' r = [0] * n r0 = [(1 - alpha) * v[j] for j in range(n)] for T in range(1, 1000): for j in range(n): r[j] = alpha * sum([r0[i] * p[i, j] for i in range(n)]) + (1 - alpha) * v[j] diff = sum([abs(r[j] - r0[j]) for j in range(n)]) #print T, diff if diff < epsilon: break r0 = [r[j] for j in range(n)] tag2score = dict(zip(tagSeq, r)) return sorted(tag2score.iteritems(), key=lambda v: v[1], reverse=True)
class TagRanking: def __init__(self, trainCollection, tpp="lemm", feature="color64+dsift", k=1000, rootpath=ROOT_PATH): self.trainCollection = trainCollection self.k = k self.name = "%s(%s,%s,%s,%d)" % (self.__class__.__name__, self.trainCollection, tpp, feature, k) vobfile = os.path.join(rootpath, trainCollection, "TextData", "wn.%s.txt"%trainCollection) self.vob = set(map(str.strip, open(vobfile).readlines())) printStatus(INFO, 'the vocabulary of %s contains %d tags' % (trainCollection, len(self.vob))) self.gamma = (1.0/MEDIAN_DISTANCE[feature])**2 self.feat_dir = os.path.join(rootpath, trainCollection, 'FeatureIndex', feature) self.dim = FEATURE_TO_DIM[feature] self.fcs = FlickrContextSim(trainCollection,rootpath) printStatus(INFO, self.name + ' okay') def getName(self): return self.name def computePxt(self, qry_vec, tag, uniqueUser=False): assert(self.k<=1000) feat_file = os.path.join(self.feat_dir, tag[:2], tag, 'feature.bin') vecs = np.fromfile(feat_file, dtype=np.float32) nr_of_images = len(vecs)/self.dim A = vecs.reshape( (nr_of_images, self.dim)) weights = [] #s_time = time.time() # accelerate pxt computation by matrix operations squared_distance = np.linalg.norm(qry_vec)**2 + np.linalg.norm(A,axis=1)**2 - 2*A.dot(qry_vec) assert(len(squared_distance) == nr_of_images) #print squared_distance #squared_distance = squared_distance.tolist()[0] #t1 = time.time() - s_time weights = [math.exp(-self.gamma*x) for x in squared_distance] ''' compute distance per pair, much slower than the matrix based approach s_time = time.time() old_d = [0] * len(vecs) i = 0 for name,x in zip(renamed, vecs): old_d[i] = math.sqrt(sum([(x[j]-qry_vec[j])**2 for j in range(self.dim)])) i += 1 t2 = time.time() - s_time for i in range(len(vecs)): diff = old_d[i]**2 - squared_distance[i] assert(abs(diff)<1e-8) print '%.6f %.6f' % (t1, t2) ''' return np.mean(weights) def estimate(self, qry_vec, qry_tags, uniqueUser=0, doRandomwalk=1): alpha = 0.5 epsilon = 1e-6 tagSeq = str.split(qry_tags) tagSeq = [t for t in tagSeq if t in self.vob] n = len(tagSeq) v = [self.computePxt(qry_vec,tag,uniqueUser) for tag in tagSeq] #print sorted(zip(tagSeq,v), key=lambda v:(v[1]), reverse=True) if not doRandomwalk: tag2score = dict(zip(tagSeq,v)) return sorted(tag2score.iteritems(), key=lambda v:(v[1]), reverse=True) ''' ----------------------------------------- Compute the nxn transition matrix ----------------------------------------- ''' p = np.zeros((n,n)) for i in range(n): for j in range(n): if i == j: p[i,j] = 1.0 elif i < j: p[i,j] = self.fcs.compute(tagSeq[i],tagSeq[j],gamma=1) else: p[i,j] = p[j,i] ''' ----------------------------------------- normalize row s.t. row sum is 1 ----------------------------------------- ''' for i in range(n): rowsum = p[i,:].sum() p[i,:] /= float(rowsum) #print qry_tags #print p ''' ----------------------------------------- do random walk ----------------------------------------- ''' r = [0] * n r0 = [(1-alpha)*v[j] for j in range(n)] for T in range(1, 1000): for j in range(n): r[j] = alpha * sum([r0[i]*p[i,j] for i in range(n)]) + (1-alpha)*v[j] diff = sum([abs(r[j]-r0[j]) for j in range(n)]) #print T, diff if diff < epsilon: break r0 = [r[j] for j in range(n)] tag2score = dict(zip(tagSeq,r)) return sorted(tag2score.iteritems(), key=lambda v:v[1], reverse=True)
def __init__(self, collection, useWnVob=1, rootpath=ROOT_PATH): SemanticTagrelLearner.__init__(self, collection, useWnVob, rootpath) self.engine = FlickrContextSim(collection, rootpath)