-
Notifications
You must be signed in to change notification settings - Fork 9
/
digit_mutator.py
89 lines (72 loc) · 3.02 KB
/
digit_mutator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import random
import mutation_manager
import rasterization_tools
import vectorization_tools
from mnist_member import MnistMember
from config import MUTOPPROB
from utils import get_distance
class DigitMutator:
def __init__(self, digit):
self.digit = digit
self.seed = digit.seed
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
distance_inputs = 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
self.digit.predicted_label = None
self.digit.confidence = None
self.digit.correctly_classified = None
return distance_inputs
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
distance_inputs = 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 = MnistMember(v1_xml_desc,
self.digit.expected_label,
self.seed)
second_digit = MnistMember(v2_xml_desc,
self.digit.expected_label,
self.seed)
first_digit.purified = rasterized_digit1
second_digit.purified = rasterized_digit2
return first_digit, second_digit, distance_inputs