예제 #1
0
class Tester:
    def __init__(self):
        self.explorer = Explorer(True)

    def test(self):
        file = open('resultsfile.txt', 'w')
        i = 0
        for file_name in os.listdir('labelme-luna16-Data/test'):
            print(file_name)
            image = cv2.imread(
                f'labelme-luna16-Data/test/{file_name}/images/img.png', 0)
            image_label = cv2.imread(
                f'labelme-luna16-Data/test/{file_name}/label_viz.png')
            label = cv2.imread(
                f'labelme-luna16-Data/test/{file_name}/masks/label.png')
            label = cv2.cvtColor(label, cv2.COLOR_BGR2GRAY)
            ret, label = cv2.threshold(label, 20, 255, cv2.THRESH_BINARY)
            boxes, mask = self.explorer.explore(image)
            image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
            if not boxes.size == 0:
                for box in boxes:
                    cls = str(box[0])
                    x1 = int(box[1])
                    y1 = int(box[2])
                    x2 = int(box[3])
                    y2 = int(box[4])
                    image = cv2.rectangle(image, (x1, y1), (x2, y2),
                                          (0, 0, 255), 1)
                    image = cv2.putText(image, cls[:5], (x1, y1),
                                        cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                                        (0, 0, 255))
            file.write(file_name + '\n')
            file.write(str(int(boxes.size // 5)) + '\n')
            if not boxes.size == 0:
                for box in boxes:
                    cls = str(box[0])
                    x1 = int(box[1])
                    y1 = int(box[2])
                    w = int(box[3] - box[1])
                    h = int(box[4] - box[2])
                    file.write(f'{x1} {y1} {w} {h} {cls}\n')
                # image = cv2.resize(image,None,fx=2,fy=2)
            cv2.imshow('JK', image)
            cv2.imshow('mask', mask)
            cv2.imshow('labelviz', image_label)
            cv2.imwrite(f'biaozhu/{i}.png', label)
            cv2.imwrite(f'yuche/{i}.png', mask)
            i += 1
            #if cv2.waitKey(1) == ord('c'):
            #break
        file.close()
예제 #2
0
    def sample(self, keep=None):
        """
        Sample the posterior and return the weighted average result of the
        Model.

        The more sensible result of this method is a SampleList which contains
        samples taken from the posterior distribution.

        Parameters
        ----------
        keep : None or dict of {int:float}
            Dictionary of indices (int) to be kept at a fixed value (float)
            Hyperparameters follow model parameters
            The values will override those at initialization.
            They are only used in this call of fit.

        """
        if keep is None:
            keep = self.keep
        fitlist = self.makeFitlist(keep=keep)

        self.initWalkers(fitlist=fitlist)
        for eng in self.engines:
            eng.walkers = self.walkers

        self.distribution.ncalls = 0  #  reset number of calls

        self.plotData()

        if self.verbose >= 1:
            print("Fit", ("all" if keep is None else fitlist), "parameters of")
            print(" ", self.model._toString("  "))
            print("Using a", self.distribution, "with")
            np = self.model.npchain
            for name, hyp in zip(self.distribution.PARNAMES,
                                 self.distribution.hyperpar):
                print("  %-7.7s   " % name, end="")
                if np in fitlist:
                    print("unknown")
                else:
                    print("  (fixed)  ", hyp.hypar)
#                    print( "%7.2f  (fixed)" % hyp.hypar )
                np += 1
            print("Moving the walkers with")
            for eng in self.engines:
                print(" ", eng)

        if self.verbose >= 2:
            print("Iteration   logZ        H     LowL     npar    parameters")

        explorer = Explorer(self)

        self.logZ = -sys.float_info.max
        self.info = 0

        logWidth = math.log(1.0 -
                            math.exp((-1.0 * self.discard) / self.ensemble))

        if self.optionalRestart():
            logWidth -= self.iteration * (1.0 * self.discard) / self.ensemble

        while self.iteration < self.getMaxIter():

            #  find worst walker(s) in ensemble
            worst = self.findWorst()
            worstLogW = logWidth + self.walkers[worst[-1]].logL

            # Keep posterior samples
            self.storeSamples(worst, worstLogW - math.log(self.discard))

            # Update Evidence Z and Information H
            logZnew = numpy.logaddexp(self.logZ, worstLogW)

            self.info = (math.exp(worstLogW - logZnew) * self.lowLhood +
                         math.exp(self.logZ - logZnew) *
                         (self.info + self.logZ) - logZnew)
            self.logZ = logZnew

            if self.verbose >= 3 or (self.verbose >= 2
                                     and self.iteration % 100 == 0):
                kw = worst[0]
                pl = self.walkers[kw].parlist[self.walkers[kw].fitIndex]
                np = len(pl)
                print(
                    "%8d %8.1f %8.1f %8.1f %6d " %
                    (self.iteration, self.logZ, self.info, self.lowLhood, np),
                    fmt(pl))

                self.plotResult(worst[0], self.iteration)

            self.samples.weed(self.maxsize)  # remove overflow in samplelist

            self.copyWalker(worst)

            # Explore the copied walker(s)
            explorer.explore(worst, self.lowLhood, fitlist)

            # Shrink the interval
            logWidth -= (1.0 * self.discard) / self.ensemble
            self.iteration += 1

            self.optionalSave()

        # End of Sampling
        self.addEnsembleToSamples(logWidth)

        # Calculate weighted average and stdevs for the parameters;
        self.samples.LogZ = self.logZ
        self.samples.info = self.info
        self.samples.normalize()

        # put the info into the model
        self.model.parameters = self.samples.parameters
        self.model.stdevs = self.samples.stdevs

        if self.verbose >= 1:
            self.report()

        return self.samples.average(self.xdata)