Beispiel #1
0
	def queryhash(self, qdata):
		Y = np.zeros((len(qdata), self.r), dtype=np.int8)
		for rr in xrange(self.r):
			print rr
			res = self.classifier[rr].predict(qdata)
			Y[:,rr] = np.where(res>0, 1, 0)
		return hash_value(Y)
Beispiel #2
0
	def queryhash(self, qdata):
		if self.type != 'kernel':
			Kdata = np.dot(qdata-self.mean, self.W)
		else:
			Kdata = np.dot(self.kernel_param['kernel'](qdata, self.anchors), self.W)
		Y = np.dot(Kdata, self.R)
		Y = np.where(Y>=0, 1, 0)
		return hash_value(Y)
Beispiel #3
0
	def queryhash(self, qdata):
		if self.classifier == 'CNN':
			return self.cls.predict(qdata)
		Kdata = self.kernel(qdata, self.anchors)
		Kdata -= self.mvec
		Y = np.dot(Kdata, self.W) + self.b
		Y = np.where(Y>=0, 1, 0)
		return hash_value(Y)
Beispiel #4
0
	def basehash(self, data):
		H = np.where(self.B>=0, 1, 0)
		return hash_value(H)
Beispiel #5
0
	def queryhash(self, qdata):
		Kdata = self.kernel(qdata, self.anchors)
		Kdata -= self.mvec
		Y = np.dot(Kdata, self.P)
		Y = np.where(Y>=0, 1, 0)
		return hash_value(Y)
Beispiel #6
0
	def train(self, traindata, trainlabel):
		n = len(traindata)
		# shuffle data
		indexes = np.arange(n, dtype=np.int32)
		np.random.shuffle(indexes)
		traindata = traindata[indexes]
		trainlabel = trainlabel[indexes]

		# determine anchors
		anchoridx = np.copy(indexes)
		np.random.shuffle(anchoridx)
		anchoridx = anchoridx[:self.m]
		self.anchors = traindata[anchoridx]

		# kernel matrix and mean
		KK = self.kernel(traindata, self.anchors)
		self.mvec = np.mean(KK, axis=0).reshape((1, self.m))
		KK = KK - self.mvec

		# pairwise label matrix, rS=PQ^T
		l = self.numlabel + 1

		# PH = [P, +H], QH = [Q, -H]
		PH = np.zeros((n,l+self.r), dtype=np.float32)
		if len(trainlabel.shape) >= 2:
			assert trainlabel.shape[1] == self.numlabel
			PH[:,:self.numlabel] = trainlabel
		else:
			PH[np.arange(n, dtype=np.int32), trainlabel] = 1
		QH = np.copy(PH)
		PH[:,:l-1] *= 2*self.r
		PH[:,l-1] = self.r
		QH[:,l-1] = -1
		S = np.dot(PH[:,:l], QH.T[:l]) / self.r
		print 'Init Obj:', np.sum(np.sum(S*S,axis=1)), np.sum(S)

		# projection optimization
		RM = np.dot(KK.T, KK)
		A = np.zeros((self.m, self.r), dtype=np.float32) # parameter A
		LM = np.dot(np.dot(KK.T, PH[:,:l]), np.dot(QH.T[:l], KK))

		# hash matrix
		H = np.zeros((n, self.r), dtype=np.int8)

		# greedy optimization
		for rr in range(0, self.r):
			print "No:", rr

			# step 1: spectral relaxation
			if rr > 0:
				tmp = np.dot(KK.T, h0.reshape((n,1)))
				LM -= np.dot(tmp, tmp.T)
			(V, U) = eigh(LM, RM, eigvals_only=False)
			A[:,rr] = U[:,self.m-1]
			tmp = np.dot(np.dot(A[:,rr].T, RM), A[:,rr])
			A[:,rr] *= np.sqrt(n/tmp)


			# step 2: sigmoid smoothing
			get_vec, cost = OptProjectionFast(KK, PH[:,:l+rr], QH[:,:l+rr], A[:,rr], 500)

			h0 = np.dot(KK, A[:,rr])
			h0 = np.where(h0>=0, 1, -1)

			h1 = np.dot(KK, get_vec)
			h1 = np.where(h1>=0, 1, -1)

			if np.dot(np.dot(PH.T[:l+rr], h1), np.dot(QH.T[:l+rr], h1)) > \
				np.dot(np.dot(PH.T[:l+rr], h0), np.dot(QH.T[:l+rr], h0)):
				A[:,rr] = get_vec
				h0 = h1

			# update PH and QH
			PH[:,l+rr] = h0
			QH[:,l+rr] = -1 * h0
			Tmp = S + np.dot(PH[:,l:l+rr+1], QH[:,l:l+rr+1].T) / (rr+1)
			print "Obj:", np.sum(np.sum(Tmp*Tmp,axis=1))
			H[:,rr] = (h0 > 0).astype(np.int8)

		self.A = A

		return hash_value(H)
Beispiel #7
0
	def queryhash(self, qdata):
		Y = self.cls.predict(qdata)
		Y = np.where(Y>=0, 1, 0)
		return hash_value(Y)
Beispiel #8
0
	def queryhash(self, qdata):
		Y = np.dot(qdata, self.W)
		Y = np.where(Y>=0, 1, 0)
		return hash_value(Y)
Beispiel #9
0
	def queryhash(self, data):
		(Z, _) = self._Z(data, self.anchors, self.nnanchors, self.sigma)
		print Z.shape
		Y = Z.dot(self.W)
		Y = np.where(Y>=0, 1, 0)
		return hash_value(Y)
Beispiel #10
0
 def queryhash(self, qdata):
     Kdata = self.kernel(qdata, self.anchors)
     Kdata -= self.mvec
     Y = np.dot(Kdata, self.W) + self.b
     Y = np.where(Y >= 0, 1, 0)
     return hash_value(Y)
Beispiel #11
0
	def train(self):
		trainlabel = np.load(root+'trainlabel.npy')
		n = len(trainlabel)
		self.anchors = np.load('anchors.npy')
		mu = self.mu * n

		KK = np.zeros((n, self.m))
		pt = 0
		print 'making kernel vector...'
		for i in xrange(13):
			print 'loading', i+1, '...'
			X = np.load(root+'traindata_fc7_norm_{}.npy'.format(i+1))
			t = X.shape[0]
			KK[pt:pt+t,:] = self.kernel(X, self.anchors)
			pt += t
			del X

		# kernel matrix and mean
		self.mvec = np.mean(KK, axis=0).reshape((1, self.m))
		KK = KK - self.mvec

		np.save('mean_vec.npy', self.mvec)

		# pairwise label matrix, S = 2*P*P.T-1_{n*n}
		if len(trainlabel.shape) >= 2:
			assert trainlabel.shape[1] == self.numlabel
			P = csc_matrix(trainlabel, dtype=np.float32)
			P = P.T
		else:
			P = csc_matrix((np.ones(n),[np.arange(n, dtype=np.int32), trainlabel]), shape=(n,self.numlabel), dtype=np.float32)
			P = P.T
		H = np.zeros((n,self.r))

		print 'preparing step 1...'

		# projection optimization
		RM = np.dot(KK.T, KK)
		W = np.zeros((self.m, self.r), dtype=np.float32) # parameter W
		LM = self.r*(2*np.dot(P.dot(KK).T, P.dot(KK)) - np.dot(np.sum(KK.T, axis=1, keepdims=True), np.sum(KK, axis=0, keepdims=True)))

		# Evaluator

		# step 1: initialize with spectral relaxation
		# step 1.1: batch coordinate optimization
		h0 = np.zeros(n)
		print '\nSTEP 1: Initialize with spectral relaxation...'
		print 'step 1.1...'
		for rr in range(self.r):
			if rr > 0:
				tmp = np.dot(KK.T, h0.reshape((n,1)))
				LM -= np.dot(tmp, tmp.T)
			(V, U) = eigh(LM, RM, eigvals_only=False)
			W[:,rr] = U[:,self.m-1]
			tmp = np.dot(np.dot(W[:,rr].T, RM), W[:,rr])
			W[:,rr] *= np.sqrt(n/tmp)

			h0 = np.where(np.dot(KK, W[:,rr]) >= 0, 1, -1)
			H[:,rr] = h0

		# step 1.2: batch coordinate optimization for some loops
		h1 = np.zeros(n)
		for t in range(5):
			print 'step 1.{}...'.format(t+2)
			for rr in range(self.r):
				h0[:] = H[:,rr]
				tmp = np.dot(KK.T, h0.reshape((n,1)))
				LM += np.dot(tmp, tmp.T)

				(V, U) = eigh(LM, RM, eigvals_only=False)
				W[:,rr] = U[:,self.m-1]
				tmp = np.dot(np.dot(W[:,rr].T, RM), W[:,rr])
				W[:,rr] *= np.sqrt(n/tmp)

				# JUST FOR EXPERIMENT
				h1[:] = np.where(np.dot(KK, W[:,rr]) > 0, 1, -1)
				H[:,rr] = 0
				fun = lambda x: -self.r*(2*np.sum(P.dot(x)**2) - np.sum(x)**2) + np.sum(np.dot(x,H)**2)
				if fun(h1) <= fun(h0):
					h0[:] = h1[:]
				# END FOR EXPERIMENT

				tmp = np.dot(KK.T, h0.reshape((n,1)))
				LM -= np.dot(tmp, tmp.T)

				H[:,rr] = h0

				np.save('hash_value.npy', hash_value(np.where(H>=0, 1, 0)))
				np.save('weight.npy', W)

		# step 2: discrete optimization
		print '\nSTEP 2: Discrete Optimization...'
		RM += self.lmda * np.eye(self.m)
		invRM = np.linalg.inv(RM)
		h = np.zeros(n)
		h1 = np.zeros(n)
		bnds = [(-1,1) for i in xrange(n)]
		for t in range(5):
			print '\nIter No: %d' % t
			# step 2.1: fix Hp, Hq, optimize W
			W = np.dot(invRM, np.dot(KK.T, H))

			# step 2.2: fix W, optimize H
			KK_W = np.dot(KK, W)
			np.save('hash_value.npy', hash_value(np.where(KK_W>=0, 1, 0)))
			np.save('weight.npy', W)
			for rr in range(self.r):
				if (rr+1) % 8 == 0:
					print 'rr:', rr
				h[:] = H[:,rr]
				H[:,rr] = 0
				fun = lambda x: -self.r*(2*np.sum(P.dot(x)**2) - np.sum(x)**2) + np.sum(np.dot(x,H)**2) - mu * np.dot(KK_W[:,rr], x)
				gra = lambda x: -2*self.r*(2*P.T.dot(P.dot(x)) - np.sum(x)) + 2*np.dot(H,np.dot(x,H)) - mu * KK_W[:,rr]
				res = minimize(fun, h, method='L-BFGS-B', jac=gra, bounds=bnds, options={'disp': False, 'maxiter':500, 'maxfun':500})
				h1[:] = np.where(res.x>=0, 1, -1)
				# JUST FOR EXPERIMENT
				if fun(h1) <= fun(h):
					H[:,rr] = h1
				else:
					H[:,rr] = h
				# END FOR EXPERIMENT

		self.W = W
		self.trainlabel = trainlabel

		Y = np.dot(KK, self.W)
		Y = np.where(Y>=0, 1, 0)
		np.save('base_hash_value.npy', hash_value(Y))
		np.save('weight.npy', self.W)