def pixelLikelihoodRobustRegionCh(image, template, testMask, backgroundModel, layerPrior, variances): sigma = ch.sqrt(variances) mask = testMask if backgroundModel == 'FULL': mask = np.ones(image.shape[0:2]) # mask = np.repeat(mask[..., np.newaxis], 3, 2) repPriors = ch.tile(layerPrior, image.shape[0:2]) # sum = np.sum(np.log(layerPrior * scipy.stats.norm.pdf(image, location = template, scale=np.sqrt(variances) ) + (1 - repPriors))) # uniformProbs = np.ones(image.shape) imshape = image.shape from opendr.filters import filter_for from opendr.filters import GaussianKernel2D blur_mtx = filter_for(imshape[0], imshape[1], imshape[2] if len(imshape) > 2 else 1, kernel=GaussianKernel2D(3, 1)) blurred_image = MatVecMult(blur_mtx, image).reshape(imshape) blurred_template = MatVecMult(blur_mtx, template).reshape(imshape) probs = ch.exp(-(blurred_image - template)**2 / (2 * variances)) * (1. / (sigma * np.sqrt(2 * np.pi))) foregroundProbs = (probs[:, :, 0] * probs[:, :, 1] * probs[:, :, 2]) * layerPrior + (1 - repPriors) return foregroundProbs * mask + (1 - mask)
def pixelLikelihoodRobustCh(image, template, testMask, backgroundModel, layerPrior, variances): sigma = ch.sqrt(variances) mask = testMask if backgroundModel == 'FULL': mask = np.ones(image.shape[0:2]) # mask = np.repeat(mask[..., np.newaxis], 3, 2) repPriors = ch.tile(layerPrior, image.shape[0:2]) # sum = np.sum(np.log(layerPrior * scipy.stats.norm.pdf(image, location = template, scale=np.sqrt(variances) ) + (1 - repPriors))) # uniformProbs = np.ones(image.shape) probs = ch.exp( - (image - template)**2 / (2 * variances)) * (1./(sigma * np.sqrt(2 * np.pi))) foregroundProbs = (probs[:,:,0] * probs[:,:,1] * probs[:,:,2]) * layerPrior + (1 - repPriors) return foregroundProbs * mask + (1-mask)
def layerPosteriorsRobustCh(image, template, testMask, backgroundModel, layerPrior, variances): sigma = ch.sqrt(variances) mask = testMask if backgroundModel == 'FULL': mask = np.ones(image.shape[0:2]) # mask = np.repeat(mask[..., np.newaxis], 3, 2) repPriors = ch.tile(layerPrior, image.shape[0:2]) probs = ch.exp( - (image - template)**2 / (2 * variances)) * (1/(sigma * np.sqrt(2 * np.pi))) foregroundProbs = probs[:,:,0] * probs[:,:,1] * probs[:,:,2] * layerPrior backgroundProbs = np.ones(image.shape) outlierProbs = ch.Ch(1-repPriors) lik = pixelLikelihoodRobustCh(image, template, testMask, backgroundModel, layerPrior, variances) # prodlik = np.prod(lik, axis=2) # return np.prod(foregroundProbs*mask, axis=2)/prodlik, np.prod(outlierProbs*mask, axis=2)/prodlik return foregroundProbs*mask/lik, outlierProbs*mask/lik
def on_changed(self, which): if 'w' in which or 'A' in which: A = np.abs(self.A.tocoo()) self.wmat = A.tocsc().dot(sp.diags([self.w], [0])) A9_data = np.tile(A.data / 2., 9) A9_row = np.hstack([A.row * 9 + i for i in range(9)]) A9_col = np.hstack([A.col * 9 + i for i in range(9)]) self.A9 = sp.csc_matrix((A9_data, (A9_row, A9_col)), shape=(A.shape[0] * 9, A.shape[1] * 9)) #wmatcoo = self.wmat.tocoo() #wmat3_data = np.tile(wmatcoo.data, 3) #wmat3_row = np.hstack([wmatcoo.row*3 + i for i in range(3)]) #wmat3_col = np.hstack([wmatcoo.col*3 + i for i in range(3)]) #self.wmat3 = sp.csc_matrix((wmat3_data, (wmat3_row, wmat3_col)), # shape=(wmatcoo.shape[0]*3, # wmatcoo.shape[1]*3)) if 'reg_e' in which or 'model_e' in which: T = np.einsum('ij,jk->ikj', self.model_e.T, self.reg_e).reshape(9, -1) # compute rotation per vertex rv through SVD T_per_v = (self.wmat.dot(T.T)).reshape(-1, 3, 3) u_s_v = np.linalg.svd(T_per_v) col3 = np.asarray([1., 1., -1.]) # negate third column of u if det is negative # The real rotation, according to Sorkine, would be v.T.dot(u.T), # because numpy returns usv instead of usv.T. However, we're applying # e.dot(rot), so we need rot.T which is u.dot(v) self.rv = np.asarray([ u.dot(v) if np.linalg.det(u.dot(v)) > 0 # np.prod(s) > 0# else (u * col3).dot(v) for u, s, v in zip(*u_s_v) ]) # average vertex rotations for each edge self.rf = MatVecMult(mtx=self.A9.T, vec=self.rv.ravel()).reshape(-1, 3) w3 = ch.tile(self.w, (3, 1)).T.ravel() self.err = (LoopProd(rot=self.rf, edge=self.model_e).ravel() - self.reg_e.ravel()) * w3
numPixels = win * 2 * win * 2 chInput = ch.Ch(colors) numVars = chInput.size recSoftmaxW = ch.Ch(np.random.uniform(0, 1, [nRecComps, numVars]) / numVars) chRecLogistic = ch.exp(ch.dot(recSoftmaxW, chInput.reshape([numVars, 1]))) chRecSoftmax = chRecLogistic.ravel() / ch.sum(chRecLogistic) chZRecComps = ch.zeros([numVars, nRecComps]) chZ = ch.zeros([numVars]) recMeans = ch.Ch(np.random.uniform(0, 1, [3, nRecComps])) recCovars = 0.2 chRecLogLikelihoods = -0.5 * (chZ.reshape([numPixels, 3, 1]) - ch.tile( recMeans, [numPixels, 1, 1]))**2 - ch.log( (2 * recCovars) * (1 / (ch.sqrt(recCovars) * np.sqrt(2 * np.pi)))) genZCovars = 0.2 chGenComponentsProbs = ch.Ch(gmm.weights_) chCompMeans = ch.zeros([nComps, 3]) for comp in range(nComps): chCompMeans[comp, :] = gmm.means_[comp] chPZComp = ch.exp( -(ch.tile(chZ.reshape([numPixels, 3, 1]), [1, 1, nComps]) - chCompMeans.reshape([1, 3, nComps]))**2 / (2 * genZCovars)) * (1 / (ch.sqrt(genZCovars) * np.sqrt(2 * np.pi))) chPZ = ch.dot(chGenComponentsProbs.reshape([1, nComps]),