def cvcompute(mat, weights, foldid, nlams):
if len(weights.shape) > 1:
weights = scipy.reshape(weights, [weights.shape[0], ])
wisum = scipy.bincount(foldid, weights = weights)
nfolds = scipy.amax(foldid) + 1
outmat = scipy.ones([nfolds, mat.shape[1]])*scipy.NaN
good = scipy.zeros([nfolds, mat.shape[1]])
mat[scipy.isinf(mat)] = scipy.NaN
for i in range(nfolds):
tf = foldid == i
mati = mat[tf, ]
wi = weights[tf, ]
outmat[i, :] = wtmean(mati, wi)
good[i, 0:nlams[i]] = 1
N = scipy.sum(good, axis = 0)
cvcpt = dict()
cvcpt['cvraw'] = outmat
cvcpt['weights'] = wisum
cvcpt['N'] = N
return(cvcpt)
# end of cvcompute
#=========================
python类amax()的实例源码
def cvcompute(mat, weights, foldid, nlams):
if len(weights.shape) > 1:
weights = scipy.reshape(weights, [weights.shape[0], ])
wisum = scipy.bincount(foldid, weights = weights)
nfolds = scipy.amax(foldid) + 1
outmat = scipy.ones([nfolds, mat.shape[1]])*scipy.NaN
good = scipy.zeros([nfolds, mat.shape[1]])
mat[scipy.isinf(mat)] = scipy.NaN
for i in range(nfolds):
tf = foldid == i
mati = mat[tf, ]
wi = weights[tf, ]
outmat[i, :] = wtmean(mati, wi)
good[i, 0:nlams[i]] = 1
N = scipy.sum(good, axis = 0)
cvcpt = dict()
cvcpt['cvraw'] = outmat
cvcpt['weights'] = wisum
cvcpt['N'] = N
return(cvcpt)
# end of cvcompute
#=========================
def lambda_interp(lambdau, s):
# lambda is the index sequence that is produced by the model
# s is the new vector at which evaluations are required.
# the value is a vector of left and right indices, and a vector of fractions.
# the new values are interpolated bewteen the two using the fraction
# Note: lambda decreases. you take:
# sfrac*left+(1-sfrac*right)
if len(lambdau) == 1:
nums = len(s)
left = scipy.zeros([nums, 1], dtype = scipy.integer)
right = left
sfrac = scipy.zeros([nums, 1], dtype = scipy.float64)
else:
s[s > scipy.amax(lambdau)] = scipy.amax(lambdau)
s[s < scipy.amin(lambdau)] = scipy.amin(lambdau)
k = len(lambdau)
sfrac = (lambdau[0] - s)/(lambdau[0] - lambdau[k - 1])
lambdau = (lambdau[0] - lambdau)/(lambdau[0] - lambdau[k - 1])
coord = scipy.interpolate.interp1d(lambdau, range(k))(sfrac)
left = scipy.floor(coord).astype(scipy.integer, copy = False)
right = scipy.ceil(coord).astype(scipy.integer, copy = False)
#
tf = left != right
sfrac[tf] = (sfrac[tf] - lambdau[right[tf]])/(lambdau[left[tf]] - lambdau[right[tf]])
sfrac[~tf] = 1.0
#if left != right:
# sfrac = (sfrac - lambdau[right])/(lambdau[left] - lambdau[right])
#else:
# sfrac[left == right] = 1.0
result = dict()
result['left'] = left
result['right'] = right
result['frac'] = sfrac
return(result)
# end of lambda_interp
# =========================================
def cvcompute(mat, weights, foldid, nlams):
if len(weights.shape) > 1:
weights = scipy.reshape(weights, [weights.shape[0], ])
wisum = scipy.bincount(foldid, weights = weights)
nfolds = scipy.amax(foldid) + 1
outmat = scipy.ones([nfolds, mat.shape[1]])*scipy.NaN
good = scipy.zeros([nfolds, mat.shape[1]])
mat[scipy.isinf(mat)] = scipy.NaN
for i in range(nfolds):
tf = foldid == i
mati = mat[tf, ]
wi = weights[tf, ]
outmat[i, :] = wtmean(mati, wi)
good[i, 0:nlams[i]] = 1
N = scipy.sum(good, axis = 0)
cvcpt = dict()
cvcpt['cvraw'] = outmat
cvcpt['weights'] = wisum
cvcpt['N'] = N
return(cvcpt)
# end of cvcompute
#=========================
def __MR_W_D_matrix(self,img,labels):
s = sp.amax(labels)+1
vect = self.__MR_superpixel_mean_vector(img,labels)
adj = self.__MR_get_adj_loop(labels)
W = sp.spatial.distance.squareform(sp.spatial.distance.pdist(vect))
W = sp.exp(-1*W / self.weight_parameters['delta'])
W[adj.astype(np.bool)] = 0
D = sp.zeros((s,s)).astype(float)
for i in range(s):
D[i, i] = sp.sum(W[i])
return W,D
def __MR_boundary_indictor(self,labels):
s = sp.amax(labels)+1
up_indictor = (sp.zeros((s,1))).astype(float)
right_indictor = (sp.zeros((s,1))).astype(float)
low_indictor = (sp.zeros((s,1))).astype(float)
left_indictor = (sp.zeros((s,1))).astype(float)
upper_ids = sp.unique(labels[0,:]).astype(int)
right_ids = sp.unique(labels[:,labels.shape[1]-1]).astype(int)
low_ids = sp.unique(labels[labels.shape[0]-1,:]).astype(int)
left_ids = sp.unique(labels[:,0]).astype(int)
up_indictor[upper_ids] = 1.0
right_indictor[right_ids] = 1.0
low_indictor[low_ids] = 1.0
left_indictor[left_ids] = 1.0
return up_indictor,right_indictor,low_indictor,left_indictor
def lambda_interp(lambdau, s):
# lambda is the index sequence that is produced by the model
# s is the new vector at which evaluations are required.
# the value is a vector of left and right indices, and a vector of fractions.
# the new values are interpolated bewteen the two using the fraction
# Note: lambda decreases. you take:
# sfrac*left+(1-sfrac*right)
if len(lambdau) == 1:
nums = len(s)
left = scipy.zeros([nums, 1], dtype = scipy.integer)
right = left
sfrac = scipy.zeros([nums, 1], dtype = scipy.float64)
else:
s[s > scipy.amax(lambdau)] = scipy.amax(lambdau)
s[s < scipy.amin(lambdau)] = scipy.amin(lambdau)
k = len(lambdau)
sfrac = (lambdau[0] - s)/(lambdau[0] - lambdau[k - 1])
lambdau = (lambdau[0] - lambdau)/(lambdau[0] - lambdau[k - 1])
coord = scipy.interpolate.interp1d(lambdau, range(k))(sfrac)
left = scipy.floor(coord).astype(scipy.integer, copy = False)
right = scipy.ceil(coord).astype(scipy.integer, copy = False)
#
tf = left != right
sfrac[tf] = (sfrac[tf] - lambdau[right[tf]])/(lambdau[left[tf]] - lambdau[right[tf]])
sfrac[~tf] = 1.0
#if left != right:
# sfrac = (sfrac - lambdau[right])/(lambdau[left] - lambdau[right])
#else:
# sfrac[left == right] = 1.0
result = dict()
result['left'] = left
result['right'] = right
result['frac'] = sfrac
return(result)
# end of lambda_interp
# =========================================
def lambda_interp(lambdau, s):
# lambda is the index sequence that is produced by the model
# s is the new vector at which evaluations are required.
# the value is a vector of left and right indices, and a vector of fractions.
# the new values are interpolated bewteen the two using the fraction
# Note: lambda decreases. you take:
# sfrac*left+(1-sfrac*right)
if len(lambdau) == 1:
nums = len(s)
left = scipy.zeros([nums, 1], dtype = scipy.integer)
right = left
sfrac = scipy.zeros([nums, 1], dtype = scipy.float64)
else:
s[s > scipy.amax(lambdau)] = scipy.amax(lambdau)
s[s < scipy.amin(lambdau)] = scipy.amin(lambdau)
k = len(lambdau)
sfrac = (lambdau[0] - s)/(lambdau[0] - lambdau[k - 1])
lambdau = (lambdau[0] - lambdau)/(lambdau[0] - lambdau[k - 1])
coord = scipy.interpolate.interp1d(lambdau, range(k))(sfrac)
left = scipy.floor(coord).astype(scipy.integer, copy = False)
right = scipy.ceil(coord).astype(scipy.integer, copy = False)
#
tf = left != right
sfrac[tf] = (sfrac[tf] - lambdau[right[tf]])/(lambdau[left[tf]] - lambdau[right[tf]])
sfrac[~tf] = 1.0
#if left != right:
# sfrac = (sfrac - lambdau[right])/(lambdau[left] - lambdau[right])
#else:
# sfrac[left == right] = 1.0
result = dict()
result['left'] = left
result['right'] = right
result['frac'] = sfrac
return(result)
# end of lambda_interp
# =========================================
def lambda_interp(lambdau, s):
# lambda is the index sequence that is produced by the model
# s is the new vector at which evaluations are required.
# the value is a vector of left and right indices, and a vector of fractions.
# the new values are interpolated bewteen the two using the fraction
# Note: lambda decreases. you take:
# sfrac*left+(1-sfrac*right)
if len(lambdau) == 1:
nums = len(s)
left = scipy.zeros([nums, 1], dtype = scipy.integer)
right = left
sfrac = scipy.zeros([nums, 1], dtype = scipy.float64)
else:
s[s > scipy.amax(lambdau)] = scipy.amax(lambdau)
s[s < scipy.amin(lambdau)] = scipy.amin(lambdau)
k = len(lambdau)
sfrac = (lambdau[0] - s)/(lambdau[0] - lambdau[k - 1])
lambdau = (lambdau[0] - lambdau)/(lambdau[0] - lambdau[k - 1])
coord = scipy.interpolate.interp1d(lambdau, range(k))(sfrac)
left = scipy.floor(coord).astype(scipy.integer, copy = False)
right = scipy.ceil(coord).astype(scipy.integer, copy = False)
#
tf = left != right
sfrac[tf] = (sfrac[tf] - lambdau[right[tf]])/(lambdau[left[tf]] - lambdau[right[tf]])
sfrac[~tf] = 1.0
#if left != right:
# sfrac = (sfrac - lambdau[right])/(lambdau[left] - lambdau[right])
#else:
# sfrac[left == right] = 1.0
result = dict()
result['left'] = left
result['right'] = right
result['frac'] = sfrac
return(result)
# end of lambda_interp
# =========================================
def __MR_superpixel_mean_vector(self,img,labels):
s = sp.amax(labels)+1
vec = sp.zeros((s,3)).astype(float)
for i in range(s):
mask = labels == i
super_v = img[mask].astype(float)
mean = sp.mean(super_v,0)
vec[i] = mean
return vec
def __MR_affinity_matrix(self,img,labels):
W,D = self.__MR_W_D_matrix(img,labels)
aff = pinv(D-self.weight_parameters['alpha']*W)
aff[sp.eye(sp.amax(labels)+1).astype(bool)] = 0.0 # diagonal elements to 0
return aff
def __MR_second_stage_indictor(self,saliency_img_mask,labels):
s = sp.amax(labels)+1
# get ids from labels image
ids = sp.unique(labels[saliency_img_mask]).astype(int)
# indictor
indictor = sp.zeros((s,1)).astype(float)
indictor[ids] = 1.0
return indictor
def __MR_fill_superpixel_with_saliency(self,labels,saliency_score):
sa_img = labels.copy().astype(float)
for i in range(sp.amax(labels)+1):
mask = labels == i
sa_img[mask] = saliency_score[i]
return cv2.normalize(sa_img,None,0,255,cv2.NORM_MINMAX)