def modelLogLikelihoodRobustRegionCh(image, template, testMask,
                                     backgroundModel, layerPriors, variances):
    likelihood = pixelLikelihoodRobustRegionCh(image, template, testMask,
                                               backgroundModel, layerPriors,
                                               variances)
    liksum = ch.sum(ch.log(likelihood))

    return liksum
    def logProb(self):
        visibility = self.sqeRenderer.visibility_image
        # visible = visibility != 4294967295

        visible = np.array(self.sqeRenderer.image_mesh_bool(
            [0])).copy().astype(np.bool)

        return ch.log(
            pixelLikelihoodRobustSQErrorCh(self.sqeRenderer, visible, 'MASK',
                                           self.foregroundPrior,
                                           self.variances))
Esempio n. 3
0
def logPixelLikelihoodErrorCh(sqerrors, testMask, backgroundModel, variances):
    sigma = ch.sqrt(variances)
    # sum = np.sum(np.log(layerPrior * scipy.stats.norm.pdf(image, location = template, scale=np.sqrt(variances) ) + (1 - repPriors)))
    mask = testMask
    if backgroundModel == 'FULL':
        mask = np.ones(sqerrors.shape[0:2])
    # mask = np.repeat(mask[..., np.newaxis], 3, 2)
    uniformProbs = np.ones(sqerrors.shape[0:2])
    logprobs =   (-(sqerrors) / (2. * variances))  - ch.log((sigma * np.sqrt(2.0 * np.pi)))
    pixelLogProbs = logprobs[:,:,0] + logprobs[:,:,1] + logprobs[:,:,2]
    return pixelLogProbs * mask
Esempio n. 4
0
    def logProb(self):
        # visibility = self.renderer.visibility_image
        # visible = visibility != 4294967295
        try:
            self.useMask
        except:
            self.useMask = False

        if self.useMask:
            visible = self.renderer.indices_image != 0
        else:
            visible = np.ones_like(self.renderer.indices_image.astype(np.bool))

        # visible = np.array(self.renderer.image_mesh_bool([0])).copy().astype(np.bool)

        return ch.log(pixelLikelihoodRobustCh(self.groundtruth, self.renderer, visible, 'MASK', self.foregroundPrior, self.variances))
    def logProb(self):

        fgProb = ch.exp(-(self.renderer - self.groundtruth.r)**2 /
                        (2 * self.variances.r)) * (
                            1. /
                            (np.sqrt(self.variances.r) * np.sqrt(2 * np.pi)))

        h = self.renderer.r.shape[0]
        w = self.renderer.r.shape[1]

        occProb = np.ones([h, w])
        bgProb = np.ones([h, w])

        fgMask = np.array(self.renderer.image_mesh_bool([0])).astype(np.bool)

        errorFun = ch.log(fgMask[:, :, None] * (
            (self.Q[0][:, :, None] * fgProb) +
            (self.Q[1] * occProb + self.Q[2] * bgProb)[:, :, None]) +
                          (1 - fgMask[:, :, None]))

        return errorFun
Esempio n. 6
0
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]),
              chPZComp.reshape([5, numVars]))
Esempio n. 7
0
    def objFun(vs):

        vs = np.array(vs)
        res = []
        for vs_it, vs_i in enumerate(vs):
            changevars(vs_i, free_variables)

            import densecrf_model

            vis_im = np.array(renderer.indices_image == 1).copy().astype(
                np.bool)
            bound_im = renderer.boundarybool_image.astype(np.bool)

            segmentation, Q = densecrf_model.crfInference(
                rendererGT.r, vis_im, bound_im, [0.75, 0.25, 0.01],
                resultDir + 'imgs/crf/Q_' + str(test_i) + '_it' + str(vs_it))
            vColor = color
            if updateColor:
                if np.sum(segmentation == 0) > 5:
                    segmentRegion = segmentation == 0
                    vColor = np.median(rendererGT.reshape(
                        [-1, 3])[segmentRegion.ravel()],
                                       axis=0) * 1.4
                    vColor = vColor / max(np.max(vColor), 1.)

            chVColors[:] = vColor
            chSHLightCoeffs[:] = lightCoeffs

            variances = stds**2

            fgProb = ch.exp(-(renderer - rendererGT)**2 /
                            (2 * variances)) * (1. /
                                                (stds * np.sqrt(2 * np.pi)))

            h = renderer.r.shape[0]
            w = renderer.r.shape[1]

            occProb = np.ones([h, w])
            bgProb = np.ones([h, w])

            errorFun = -ch.sum(
                ch.log(vis_im[:, :, None] *
                       ((Q[0].reshape([h, w, 1]) * fgProb) +
                        (Q[1].reshape([h, w]) * occProb +
                         Q[2].reshape([h, w]) * bgProb)[:, :, None]) +
                       (1 - vis_im[:, :, None]))) / (h * w)

            if minAppLight:
                options = {'disp': False, 'maxiter': 10}

                def cb(_):
                    print("Error: " + str(errorFun.r))

                ch.minimize({'raw': errorFun},
                            bounds=None,
                            method=method,
                            x0=free_variables_app_light,
                            callback=cb,
                            options=options)

            res = res + [errorFun.r.reshape([1, 1])]

        return np.vstack(res)