2

I am writing code for Kernel Matching pursuit.In that, I have used to cvxpy for convex optimization.I have to minimize following objective which is based on this paper:http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=6815769 and code is as follows:

import os
import cv2
import numpy as np
import cvxpy as cp
import cvxopt
from sklearn.datasets import make_sparse_coded_signal
from sklearn.linear_model import OrthogonalMatchingPursuit

rootdir = 'F:/face train image'

image=list()
#newimg=list()
for subdir, dirs, files in os.walk(rootdir):
 for file in files:
    img=cv2.imread(os.path.join(subdir, file),0)
    img1=cv2.resize(img,(50,50))
    img2=np.reshape(img1,(2500,1))
    image.append(img2)

for i in range(1,len(image)):
if i == 1:
    Y=image[0]
Y=np.append(Y,image[i],1)

[r,c]=Y.shape
for i in range(0,c):
a=np.linalg.norm(Y[:,i])
for j in range(0,r):
    Y[j,i]=Y[j,i]/a

yt=cv2.imread( "F:/face test image/s5/8.pgm",0)
yt=cv2.resize(yt,(50,50))
yt=np.reshape(yt,(2500,1))

Ytr=np.transpose(Y)
print Ytr.shape

ytr=np.transpose(yt)
print ytr.shape
#Kernel functions using dot product.Here only linear kernel is used.
KYY=np.dot(Ytr,Y)
Kytyt=np.dot(ytr,yt)
KytY=np.dot(ytr,Y)

lam=0.2
xt=cp.Variable(25,1,name="xt")
xtr=xt.T

epirk=Kytyt+xt.T*KYY*xt-KytY*xt
objective= cp.Minimize(epirk+lam*cp.norm1(xt))
constraints=[]
prob=cp.Problem(objective,constraints)
result=prob.solve()

But,the code is not working and throws error:cannot multiply two non-constants.I think it is isssue of multiplying three terms in 'epirk'.But,I havn't got any solution on this.Please Help.

Rodrigo de Azevedo
  • 1,097
  • 9
  • 17
Gunjan naik
  • 501
  • 1
  • 7
  • 18

1 Answers1

1

You're right. In the line where you form epirk need to use quad_form(xt, KYY) instead of xt.T*KYY*xt.

In CVXPY you're not allowed to multiply two non-constant values. You have to use an equivalent CVXPY function.

steven
  • 671
  • 4
  • 8
  • It is throwing an error as follows: CvxPyDomainError: P has both positive and negative eigenvalues. – Gunjan naik Jul 03 '15 at 08:26
  • It's complaining that ``KYY`` isn't positive semidefinite. ``KYY`` needs to be positive semidefinite or the problem isn't convex. It should be PSD if it's a kernel, right? – steven Sep 14 '15 at 03:38