示例#1
0
    def calculate_shrink(self):
        self.__step = 'shrink'
        self.__parameters[1:] = self.__parameters[0] + self.__sigma * \
            (self.__parameters[1:] - self.__parameters[0])
        self.__errors[1:] = [None] * (len(self.__errors) - 1)

        self.__end = self.__parameters[0] + self.__sigma * \
            (self.__end - self.__parameters[0])
        self.end_error = None

        for i in range(1, self._dimensions):
            self.request_face(Face.from_array(self.__parameters[i]), i)
        self.request_face(Face.from_array(self.__end), self._dimensions)
示例#2
0
    def start(self):
        self.__loop = 0
        self.__face = self._initial_face
        self.__parameters = self._initial_face.as_array

        self.request_face(self.__face, 'init')

        self.__errors = {}
        self.__generate_errors()
        self.__indices = [0] * len(self.__levels)
        self.__directions = [1] * len(self.__levels)

        self.__parameters[self.__levels] = [
            self.__get_value(i, self.__indices[i])
            for i in range(len(self.__levels))
        ]

        change_on = 0
        while change_on != -1:
            self.__errors[tuple(
                self.__indices)] = self.__get_parameter(change_on=change_on)
            change_on = self.__inc_index()

        result = self.__convert_parameters()

        parameters = self._initial_face.as_array
        parameters[self.__levels] = result[-1][0]

        face = Face.from_array(parameters)
        self.get_face(face)

        self.finish(face)
示例#3
0
    def __derivative_face(self, param, dx):
        params = self.__face.as_array
        if param >= 0:
            params[param] += dx
        else:
            params[param] += self.__light_dx

        return Face.from_array(params)
示例#4
0
文件: BGDFitter.py 项目: fossabot/mfm
    def start(self):
        face = self._initial_face.as_array

        for _ in range(self.__max_loops):
            derivatives = self.__get_derivatives(face)
            face += derivatives * self.__step

        self.finish(Face.from_array(face))
示例#5
0
    def get_face(self, face):
        """Get face image by face object.

        Accepts both `Face` instance
        and Face representation as `NumPy` `array`.
        """
        if isinstance(face, ndarray):
            face = Face.from_array(face)
        return self.__model.get_image(face)
示例#6
0
 def __initiate_parameters(self):
     self.__step = 'start'
     initial_parameters = concatenate((
         self._initial_face.coefficients,
         [self._initial_face.ambient_light],
         self._initial_face.directed_light
     ))
     for i in range(len(self.__parameters)):
         # print('Initial step {} of {}'.format(i, self._dimensions))
         # self.__parameters[i] = randn(self._dimensions) * 1
         # self.__parameters[i] = zeros(self._dimensions)
         # self.__parameters[i][:i] = self.__offset
         self.__parameters[i] = initial_parameters.copy()
         self.__parameters[i][:i] += self.__offset
         self.request_face(Face.from_array(self.__parameters[i]), i)
     # self.__end = ones(self._dimensions)
     # self.__end = randn(self._dimensions)
     self.__end = initial_parameters + self.__offset
     self.request_face(Face.from_array(self.__end), self._dimensions)
示例#7
0
    def receive_image(self, image, index=None):
        if index == 'init':
            return
        elif index == 'pre':
            self.__get_parameter(self.__current_step + 1)
            return

        shadows = image

        if index is None:
            self.finish(self.__face)
            return

        self.__errors[index] = self.get_image_deviation(shadows)

        if None in self.__errors:
            return

        errors = array(self.__errors)
        max_error = errors.max()
        if self.__loop >= self.__determined_loops:
            X = exp(-errors / max_error).sum()
            v = rand()
            best_index = -1
            t = 0
            for i, error in enumerate(errors):
                e = exp(error / max_error) / X
                t += e
                if v <= e:
                    best_index = i
                    break
                else:
                    v -= e
            if best_index == -1:
                best_index = len(self.__errors) - 1
        else:
            best_index = argmin(self.__errors)

        self.__parameters[self.__current_step] = self.__values[best_index]
        self.__face = Face.from_array(self.__parameters)
        print('{}.{}: Error of the best is {} ({})'.format(
            self.__loop, self.__current_step, self.__errors[best_index],
            min(self.__errors)))

        if self.__current_step + 1 == self._dimensions \
                and self.__loop + 1 >= self.__max_loops:
            self.request_face(self.__face)
            return
        elif self.__current_step + 1 == self._dimensions:
            self.__current_step = 0
            self.__loop += 1
            self.request_face(self.__face, 'pre')
            return

        self.request_face(self.__face, 'pre')
示例#8
0
    def __generate_face_parameters(self):
        """Generate random face.

        Based on needed parameters to iterate by and initial Face.
        """
        parameters = self._initial_face.as_array
        parameters[self.__iterating_parameters] = randn(
            len(self.__iterating_parameters))
        self.__parameters.append(parameters)
        self.__differences.append(None)
        self.__values.append(None)
        return Face.from_array(parameters)
示例#9
0
    def __get_parameter(self, i):
        self.__current_step = i

        if self.__steps[i] % 2 == 0:
            self.__values = linspace(-4, 4, self.__steps[i] + 1, dtype='f')
        else:
            self.__values = linspace(-4, 4, self.__steps[i] + 2, dtype='f')

        self.__errors = [None] * self.__values.size
        for i, value in enumerate(self.__values):
            self.__values[i] = value
            parameters = self.__parameters.copy()
            parameters[self.__current_step] = value
            self.request_face(Face.from_array(parameters), i)
示例#10
0
    def start(self):
        x0 = self._initial_face.as_array.copy()

        final_simplex = x0.copy()
        final_simplex[Face.PCS_SLICE] += self.__offset
        final_simplex[0:-Face.NON_PCS_COUNT:2] = 1
        final_simplex[1:-Face.NON_PCS_COUNT:2] = -1
        final_simplex[Face.NON_PCS_SLICE] = 1

        initial_simplex = array([concatenate((x0[:i], final_simplex[i:]))
                                 for i in range(self._dimensions + 1)], 'f')
        options = {
            'initial_simplex': initial_simplex
        }
        result = minimize(self.get_face_deviation, x0,
                          method='nelder-mead', options=options)
        self.finish(Face.from_array(result))
示例#11
0
    def start(self):
        x0 = self._initial_face.as_array.copy()

        final_simplex = x0.copy()
        final_simplex[Face.PCS_SLICE] += self.__offset
        final_simplex[0:-Face.NON_PCS_COUNT:2] = 1
        final_simplex[1:-Face.NON_PCS_COUNT:2] = -1
        final_simplex[Face.NON_PCS_SLICE] = 1

        initial_simplex = array([
            concatenate((x0[:i], final_simplex[i:]))
            for i in range(self._dimensions + 1)
        ], 'f')
        options = {'initial_simplex': initial_simplex}
        result = minimize(self.get_face_deviation,
                          x0,
                          method='nelder-mead',
                          options=options)
        self.finish(Face.from_array(result))
示例#12
0
    def receive_image(self, image, index=None):
        if index == 'init':
            return
        elif index == 'finish':
            self.finish(self.__face)
            return

        self.__errors[tuple(self.__indices)] = self.get_image_deviation(image)

        change_on = self.__inc_index()
        if change_on == -1:
            result = self.__convert_parameters()
            parameters = self._initial_face.as_array
            parameters[self.__levels] = result[-1][0]
            self.__face = Face.from_array(parameters)
            self.request_face(self.__face, 'finish')
            return

        self.__get_parameter(change_on=change_on)
示例#13
0
    def __calculate_result(self):
        """Get weighted sum of achieved parameters.

        Normalize probabilities as their sum should be 1.
        Then get sum of parameters got during iterations
        multiplied by corresponding probabilities.

        Form parameters of final face and finish the fitting procedure.
        """
        max_difference = max(self.__differences)
        normalized_differences = array(self.__differences) - float(
            sum(
                Decimal(diff - max_difference).exp()
                for diff in self.__differences).ln())
        params = [
            sum(
                Decimal(float(p[i])) * Decimal(diff).exp()
                for diff, p in zip(normalized_differences, self.__parameters))
            for i in self.__estimating_parameters
        ]
        parameters = self._initial_face.as_array
        parameters[self.__estimating_parameters] = params
        self.finish(Face.from_array(parameters))
示例#14
0
 def calculate_contraction(self):
     self.__step = 'contraction'
     self.__contraction = self.__centroid + self.__rho * \
         (self.__parameters[-1] - self.__centroid)
     self.request_face(Face.from_array(self.__contraction))
示例#15
0
 def calculate_expansion(self):
     self.__step = 'expansion'
     self.__expansion = self.__centroid + self.__gamma * \
         (self.__reflection - self.__centroid)
     self.request_face(Face.from_array(self.__expansion))
示例#16
0
 def calculate_reflection(self):
     self.__step = 'reflection'
     self.calculate_centroid()
     self.__reflection = self.__centroid + self.__alpha * \
         (self.__centroid - self.__end)
     self.request_face(Face.from_array(self.__reflection))
示例#17
0
 def start(self):
     parameters = self._initial_face.as_array.copy()
     for _ in range(self.__max_loops):
         parameters = self.__iteration(parameters)
     self.finish(Face.from_array(parameters))
示例#18
0
 def start(self):
     parameters = self._initial_face.as_array.copy()
     for _ in range(self.__max_loops):
         parameters = self.__iteration(parameters)
     self.finish(Face.from_array(parameters))
示例#19
0
 def __get_parameter(self, change_on=0):
     value = self.__get_value(change_on, self.__indices[change_on])
     self.__parameters[self.__levels[change_on]] = value
     self.__face = Face.from_array(self.__parameters)
     return self.get_face_deviation(self.__face)
示例#20
0
 def __get_parameter(self, index=None, change_on=0):
     value = self.__get_value(change_on, self.__indices[change_on])
     self.__parameters[self.__levels[change_on]] = value
     self.__face = Face.from_array(self.__parameters)
     self.request_face(self.__face, index)