Beispiel #1
0
        for i in range(data_.shape[1]):
            beam_labels_numpy[i * beam_width: (i + 1) * beam_width, :] = beam_labels[i]

        # find expression from these predicted beam labels
        expressions = [""] * config.batch_size * beam_width
        for i in range(config.batch_size * beam_width):
            for j in range(max_len):
                expressions[i] += generator.unique_draw[beam_labels_numpy[i, j]]
        for index, prog in enumerate(expressions):
            expressions[index] = prog.split("$")[0]

        #Predicted_expressions += expressions
        target_expressions = parser.labels2exps(labels, k)
        Target_expressions += target_expressions

        target_stacks = parser.expression2stack(target_expressions)

        target_voxels = target_stacks[-1,:,0,:,:,:].astype(dtype=bool)
        target_voxels_new = np.repeat(target_voxels, axis=0,
                                      repeats=beam_width)
        predicted_stack = stack_from_expressions(parser, expressions)

        beam_R = np.sum(np.logical_and(target_voxels_new, predicted_stack), (1, 2,
                                                                             3)) / \
                 (np.sum(np.logical_or(target_voxels_new, predicted_stack), (1, 2,
                                                                             3)) + 1)
        axis = glm.vec3(1,1,1)
        transfer_matrix = axis_view_matrix(axis=axis)
        center = np.dot( transfer_matrix,np.array([32,32,32],dtype=float) )
        
        # choose an output whose projection is the most similar to the input 2D image
            expressions = [""] * test_batch_size * beam_width
            for i in range(test_batch_size * beam_width):
                for j in range(maxx_len):
                    expressions[i] += generator.unique_draw[beam_labels_numpy[
                        i, j]]
            for index, p in enumerate(expressions):
                expressions[index] = p.split("$")[0]

            programs_tar[jit] += targ_prog
            programs_pred[jit] += expressions

            pred_images = []
            for index, exp in enumerate(expressions):
                program = parser.Parser.parse(exp)
                if validity(program, len(program), len(program) - 1):
                    stack = parser.expression2stack([exp])
                    pred_images.append(stack[-1, -1, 0, :, :])
                else:
                    pred_images.append(np.zeros(config.canvas_shape))
            pred_images = np.stack(pred_images, 0).astype(dtype=np.bool)
            target_images = data_[-1, :, 0, :, :].astype(dtype=bool)

            # repeat the target_images beamwidth times
            target_images_new = np.repeat(target_images,
                                          axis=0,
                                          repeats=beam_width)
            beam_CD = chamfer(target_images_new, pred_images)

            CD = np.zeros((test_batch_size, 1))
            for r in range(test_batch_size):
                CD[r, 0] = min(beam_CD[r * beam_width:(r + 1) * beam_width])
Beispiel #3
0
class Optimize:
    """
    Post processing visually guided search using Powell optimizer.
    """
    def __init__(self, query_expression, metric="iou", stack_size=7, steps=15):
        """
        Post processing visually guided search.
        :param query_expression: expression to be optimized
        :param metric: metric to be minimized, like chamfer
        :param stack_size: max stack size required in any program
        :param steps: max tim step of any program
        """
        self.parser = ParseModelOutput(canvas_shape=[64, 64],
                                       stack_size=stack_size,
                                       unique_draws=None,
                                       steps=steps)
        self.query_expression = query_expression
        self.get_graph_structure(query_expression)
        self.metric = metric
        self.errors = []

    def get_target_image(self, image: np.ndarray):
        """
        Gets the target image.
        :param image: target image
        :return: 
        """
        self.target_image = image

    def get_graph_structure(self, expression):
        """
        returns the nodes (terminals) of the program
        :param expression: input query expression
        :return: 
        """
        program = self.parser.Parser.parse(expression)
        self.graph_str = []
        for p in program:
            self.graph_str.append(p["value"])

    def make_expression(self, x: np.ndarray):
        expression = ""
        index = 0
        for e in self.graph_str:
            if e in ["c", "s", "t"]:
                expression += e + "({},{},{})".format(x[index], x[index + 1],
                                                      x[index + 2])
                index += 3
            else:
                expression += e
        return expression

    def objective(self, x: np.ndarray):
        """
        Objective to minimize.
        :param x: input program parameters in numpy array format
        :return: 
        """
        x = x.astype(np.int)
        x = np.clip(x, 8, 56)

        query_exp = self.make_expression(x)
        query_image = self.parser.expression2stack([query_exp])[-1, 0, 0, :, :]
        if self.metric == "iou":
            error = -np.sum(np.logical_and(
                self.target_image, query_image)) / np.sum(
                    np.logical_or(self.target_image, query_image))
        elif self.metric == "chamfer":
            error = chamfer(np.expand_dims(self.target_image, 0),
                            np.expand_dims(query_image, 0))
        return error