-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigit_mutator.py
64 lines (54 loc) · 2.5 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
import random
import mutation_manager
import rasterization_tools
import vectorization_tools
from properties import MUTOPPROB, MUTOFPROB, DISTANCE, TSHD_TYPE, DISTANCE_SEED
from utils import get_distance, reshape
from tensorflow import keras
class DigitMutator:
# load the MNIST dataset
mnist = keras.datasets.mnist
(_, _), (x_test, _) = mnist.load_data()
def __init__(self, digit):
self.digit = digit
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