Пример #1
0
    def generate(self):
        # Select mutation operator.
        rand_mutation_probability = random.uniform(0, 1)
        if rand_mutation_probability >= MUTOPPROB:
            mutation = 1
        else:
            mutation = 2

        condition = True
        counter_mutations = 0
        while condition:
            counter_mutations += 1
            vector1, vector2 = mutation_manager.generate(
                self.digit.xml_desc, mutation)
            v1_xml_desc = vectorization_tools.create_svg_xml(vector1)
            rasterized_digit1 = rasterization_tools.rasterize_in_memory(
                v1_xml_desc)

            v2_xml_desc = vectorization_tools.create_svg_xml(vector2)
            rasterized_digit2 = rasterization_tools.rasterize_in_memory(
                v2_xml_desc)

            distance_inputs = get_distance(rasterized_digit1,
                                           rasterized_digit2)

            if distance_inputs != 0:
                condition = False

        first_digit = Digit(v1_xml_desc, EXPECTED_LABEL)
        second_digit = Digit(v2_xml_desc, EXPECTED_LABEL)
        first_digit.purified = rasterized_digit1
        second_digit.purified = rasterized_digit2
        return first_digit, second_digit, distance_inputs
Пример #2
0
    def mutate(self, reference=None):
        # Select mutation operator.
        rand_mutation_probability = random.uniform(0, 1)
        if rand_mutation_probability >= MUTOPPROB:
            mutation = 1
        else:
            mutation = 2

        condition = True
        counter_mutations = 0
        while condition:
            counter_mutations += 1
            mutant_vector = mutation_manager.mutate(self.digit.xml_desc,
                                                    mutation,
                                                    counter_mutations / 20)
            mutant_xml_desc = vectorization_tools.create_svg_xml(mutant_vector)
            rasterized_digit = rasterization_tools.rasterize_in_memory(
                mutant_xml_desc)

            distance_inputs = get_distance(self.digit.purified,
                                           rasterized_digit)

            if distance_inputs != 0:
                if reference is not None:
                    distance_inputs = get_distance(reference.purified,
                                                   rasterized_digit)
                    if distance_inputs != 0:
                        condition = False
                else:
                    condition = False

        self.digit.xml_desc = mutant_xml_desc
        self.digit.purified = rasterized_digit

        return distance_inputs
Пример #3
0
 def __init__(self, desc, label):
     self.xml_desc = desc
     self.expected_label = label
     self.predicted_label = None
     self.purified = rasterization_tools.rasterize_in_memory(self.xml_desc)
     self.P_class = None
     self.P_notclass = None
    def mutate(self):
        condition = True
        counter_mutations = 0
        while condition:
            # Select mutation operator.
            rand_mutation_probability = random.uniform(0, 1)
            rand_mutation_prob = random.uniform(0, 1)
            if rand_mutation_probability >= MUTOPPROB:            
                if rand_mutation_prob >= MUTOFPROB:
                    mutation = 1
                else:
                    mutation = 2
            else:
                if rand_mutation_prob >= MUTOFPROB:
                    mutation = 3
                else:
                    mutation = 4

            counter_mutations += 1
            mutant_vector = mutation_manager.mutate(self.digit.xml_desc, mutation, counter_mutations/20)
            mutant_xml_desc = vectorization_tools.create_svg_xml(mutant_vector)
            rasterized_digit = rasterization_tools.rasterize_in_memory(mutant_xml_desc)

            distance_inputs = get_distance(self.digit.purified, rasterized_digit)

            if (TSHD_TYPE == '0'):
                if distance_inputs != 0:
                    condition = False
            elif (TSHD_TYPE == '1'):
                seed_image = DigitMutator.x_test[int(self.digit.seed)]
                xml_desc = vectorization_tools.vectorize(seed_image)
                seed = rasterization_tools.rasterize_in_memory(xml_desc)
                distance_seed = get_distance(seed, rasterized_digit)
                if distance_inputs != 0 and distance_seed <= DISTANCE and distance_seed != 0:
                    condition = False
            elif (TSHD_TYPE == '2'):
                seed = reshape(DigitMutator.x_test[int(self.digit.seed)])
                distance_seed = get_distance(seed, rasterized_digit)
                if distance_inputs != 0 and distance_seed <= DISTANCE_SEED and distance_seed != 0:
                    condition = False

        self.digit.xml_desc = mutant_xml_desc
        self.digit.purified = rasterized_digit
        self.digit.predicted_label = None
        self.digit.confidence = None
def apply_rast_tshd(image, seed, tshd_val):
    seed_image = x_test[int(seed)]
    xml_desc = vectorization_tools.vectorize(seed_image)
    seed = rasterization_tools.rasterize_in_memory(xml_desc)
    distance_seed = get_distance(seed, image)

    print("RAST %s" % distance_seed)

    if distance_seed < tshd_val:
        return True
    else:
        return False
Пример #6
0
 def __init__(self, desc, label, seed):
     self.timestamp, self.elapsed_time = Timer.get_timestamps()
     self.id = Digit.COUNT
     self.run = RUN
     self.seed = seed
     # TODO
     self.features = FEATURES
     self.tool = "DeepHyperion"
     self.xml_desc = desc
     self.purified = rasterization_tools.rasterize_in_memory(self.xml_desc)
     self.expected_label = label
     self.predicted_label = None
     self.confidence = None
     Digit.COUNT += 1