def solve_qp_constrained(P,q,nlabel,x0,**kwargs): import solver_qp_constrained as solver reload(solver) ## constrained solver nvar = q.size npixel = nvar/nlabel F = sparse.bmat([ [sparse.bmat([[-sparse.eye(npixel,npixel) for i in range(nlabel-1)]])], [sparse.eye(npixel*(nlabel-1),npixel*(nlabel-1))], ]) ## quadratic objective objective = solver.ObjectiveAPI(P, q, G=1, h=0, F=F,**kwargs) ## log barrier solver t0 = kwargs.pop('logbarrier_initial_t',1.0) mu = kwargs.pop('logbarrier_mu',20.0) epsilon = kwargs.pop('logbarrier_epsilon',1e-3) solver = solver.ConstrainedSolver( objective, t0=t0, mu=mu, epsilon=epsilon, ) ## remove zero entries in initial guess xinit = x0.reshape((-1,nlabel),order='F') xinit[xinit<1e-10] = 1e-3 xinit = (xinit/np.c_[np.sum(xinit, axis=1)]).reshape((-1,1),order='F') x = solver.solve(xinit, **kwargs) return x
def solve_qp_ground_truth(P, q, list_GT, nlabel, **kwargs): GT = np.asarray(list_GT).T ''' Ax >= b ''' N = P.shape[0] / nlabel S = np.cumsum(GT, axis=1) i_ = np.where(~GT) rows = (i_[1] - S[i_]) * N + i_[0] cols = i_[1] * N + i_[0] G = sparse.coo_matrix( (-np.ones(len(rows)), (rows, cols)), shape=(N * (nlabel - 1), N * nlabel), ).tocsr() G = G + sparse.bmat( [[sparse.spdiags(list_GT[l],0,N,N) for l in range(nlabel)] \ for l2 in range(nlabel-1)] ) h = kwargs.pop('ground_truth_margin', 0.0) n = q.size ## positivity constraint G = sparse.bmat([[G], [sparse.eye(n, n)]]) npixel = n / nlabel F = sparse.bmat([ [ sparse.bmat( [[-sparse.eye(npixel, npixel) for i in range(nlabel - 1)]]) ], [sparse.eye(npixel * (nlabel - 1), npixel * (nlabel - 1))], ]) ## initial guess list_GT_init = kwargs.pop('list_GT_init', None) if list_GT_init is None: xinit = np.asmatrix(np.asarray(list_GT).ravel()).T else: xinit = np.asmatrix(np.asarray(list_GT_init).ravel()).T use_mosek = kwargs.pop('use_mosek', True) if use_mosek: import solver_mosek as solver reload(solver) logger.info('use mosek') objective = solver.ObjectiveAPI(P, q, G=G, h=h, F=F, **kwargs) constsolver = solver.ConstrainedSolver(objective, ) p = xinit.A.reshape((nlabel, -1)) + 1e-8 xinit = np.mat((p / np.sum(p, axis=0)).reshape(xinit.shape)) x = constsolver.solve(xinit) minx = np.min(x) maxx = np.max(x) logger.info( 'Done solver ground truth constrained: x in [{:.3}, {:.3}]'.format( minx, maxx)) logger.info('clipping x in [0,1]') prob = np.clip(x.reshape((nlabel, -1)), 0, 1) prob = prob / np.sum(prob, axis=0) nx = prob.reshape(x.shape) #check validity ndiff = np.sum(np.argmax(prob, axis=0) != np.argmax(list_GT, axis=0)) logger.info('number incorrect pixels: {}'.format(ndiff)) return nx ## else: log barrier import solver_qp_constrained as solver reload(solver) ## quadratic objective #objective = solver.ObjectiveAPI(P, q, G=G, h=h,**kwargs) objective = solver.ObjectiveAPI(P, q, G=G, h=h, F=F, **kwargs) ## log barrier solver t0 = kwargs.pop('logbarrier_initial_t', 1.0) mu = kwargs.pop('logbarrier_mu', 20.0) epsilon = kwargs.pop('logbarrier_epsilon', 1e-4) modified = kwargs.pop('logbarrier_modified', False) maxiter = kwargs.pop('logbarrier_maxiter', 10) solver = solver.ConstrainedSolver( objective, t0=t0, mu=mu, epsilon=epsilon, modified=modified, ) ## internal solver is newton's method newton_a = kwargs.pop('newton_a', 0.4) newton_b = kwargs.pop('newton_b', 0.8) newton_epsilon = kwargs.pop('newton_epsilon', 1e-4) newton_maxiter = kwargs.pop('newton_maxiter', 100) p = xinit.A.reshape((nlabel, -1)) + 1e-8 xinit = np.mat((p / np.sum(p, axis=0)).reshape(xinit.shape)) x = solver.solve( xinit, a=newton_a, b=newton_b, epsilon=newton_epsilon, maxiter=newton_maxiter, ) minx = np.min(x) maxx = np.max(x) logger.info( 'Done solver ground truth constrained: x in [{:.3}, {:.3}]'.format( minx, maxx)) logger.info('clipping x in [0,1]') prob = np.clip(x.reshape((nlabel, -1)), 0, 1) prob = prob / np.sum(prob, axis=0) nx = prob.reshape(x.shape) return nx
def solve_qp_ground_truth(P,q,list_GT,nlabel,**kwargs): GT = np.asarray(list_GT).T ''' Ax >= b ''' N = P.shape[0]/nlabel S = np.cumsum(GT,axis=1) i_ = np.where(~GT) rows = (i_[1]-S[i_])*N + i_[0] cols = i_[1]*N + i_[0] G = sparse.coo_matrix( (-np.ones(len(rows)), (rows,cols)), shape=(N*(nlabel-1),N*nlabel), ).tocsr() G = G + sparse.bmat( [[sparse.spdiags(list_GT[l],0,N,N) for l in range(nlabel)] \ for l2 in range(nlabel-1)] ) h = kwargs.pop('ground_truth_margin',0.0) n = q.size ## positivity constraint G = sparse.bmat([[G], [sparse.eye(n,n)]]) npixel = n/nlabel F = sparse.bmat([ [sparse.bmat([[-sparse.eye(npixel,npixel) for i in range(nlabel-1)]])], [sparse.eye(npixel*(nlabel-1),npixel*(nlabel-1))], ]) ## initial guess list_GT_init = kwargs.pop('list_GT_init',None) if list_GT_init is None: xinit = np.asmatrix(np.asarray(list_GT).ravel()).T else: xinit = np.asmatrix(np.asarray(list_GT_init).ravel()).T use_mosek = kwargs.pop('use_mosek', True) if use_mosek: import solver_mosek as solver reload(solver) logger.info('use mosek') objective = solver.ObjectiveAPI(P, q, G=G, h=h,F=F,**kwargs) constsolver = solver.ConstrainedSolver( objective, ) p = xinit.A.reshape((nlabel,-1)) + 1e-8 xinit = np.mat((p/np.sum(p,axis=0)).reshape(xinit.shape)) x = constsolver.solve(xinit) minx = np.min(x) maxx = np.max(x) logger.info('Done solver ground truth constrained: x in [{:.3}, {:.3}]'.format(minx,maxx)) logger.info('clipping x in [0,1]') prob = np.clip(x.reshape((nlabel,-1)), 0,1) prob = prob / np.sum(prob,axis=0) nx = prob.reshape(x.shape) #check validity ndiff = np.sum(np.argmax(prob, axis=0)!=np.argmax(list_GT,axis=0)) logger.info('number incorrect pixels: {}'.format(ndiff)) return nx ## else: log barrier import solver_qp_constrained as solver reload(solver) ## quadratic objective #objective = solver.ObjectiveAPI(P, q, G=G, h=h,**kwargs) objective = solver.ObjectiveAPI(P, q, G=G, h=h,F=F,**kwargs) ## log barrier solver t0 = kwargs.pop('logbarrier_initial_t',1.0) mu = kwargs.pop('logbarrier_mu',20.0) epsilon = kwargs.pop('logbarrier_epsilon',1e-4) modified = kwargs.pop('logbarrier_modified',False) maxiter = kwargs.pop('logbarrier_maxiter', 10) solver = solver.ConstrainedSolver( objective, t0=t0, mu=mu, epsilon=epsilon, modified=modified, ) ## internal solver is newton's method newton_a = kwargs.pop('newton_a', 0.4) newton_b = kwargs.pop('newton_b', 0.8) newton_epsilon = kwargs.pop('newton_epsilon', 1e-4) newton_maxiter = kwargs.pop('newton_maxiter', 100) p = xinit.A.reshape((nlabel,-1)) + 1e-8 xinit = np.mat((p/np.sum(p,axis=0)).reshape(xinit.shape)) x = solver.solve( xinit, a=newton_a, b=newton_b, epsilon=newton_epsilon, maxiter=newton_maxiter, ) minx = np.min(x) maxx = np.max(x) logger.info('Done solver ground truth constrained: x in [{:.3}, {:.3}]'.format(minx,maxx)) logger.info('clipping x in [0,1]') prob = np.clip(x.reshape((nlabel,-1)), 0,1) prob = prob / np.sum(prob,axis=0) nx = prob.reshape(x.shape) return nx