File size: 2,261 Bytes
abd6e8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
import numpy as np
from PIL import Image, ImageDraw


def prepare_image(image: Image):
    # convert image
    width, height = image.size
    width = width // 8 * 8
    height = height // 8 * 8
    image = image.crop((0, 0, width, height))
    image = image.convert('L')

    image_array = []

    # image to arrays
    for x in range(width):
        for y in range(height):
            crop = image.crop((x, y, x + 8, y + 8))
            image_array.append(np.reshape(np.asarray(crop) / 255, (1, 64)))

    # save image_array
    image_array = np.asarray(image_array)

    return image_array


def draw_image(map, size):
    # size
    step = 10
    width, height = size
    new_width = width // 8 * 8 * step
    new_height = height // 8 * 8 * step

    # create canvas
    image = Image.new('RGB', (new_width, new_height), (255, 255, 255))
    draw = ImageDraw.Draw(image)

    iter = 0

    # drawing
    for x in range(0, new_width, step):
        for y in range(0, new_height, step):
            if map[iter] == 1:
                xn, yn = x, y + 8
            elif map[iter] == 2:
                xn, yn = x + 8, y
            elif map[iter] == 3:
                xn, yn = x + 8, y - 8
            elif map[iter] == 4:
                xn, yn = x + 8, y + 8
            else:
                iter += 1
                continue
            draw.line(xy=[(x, y), (xn, yn)], fill='black')
            iter += 1

    image = image.resize((width, height), Image.Resampling.LANCZOS)

    return image


def create_map(image_array):
    # Load synapses
    synapses = np.load('./final_synapses.npz')
    W1 = synapses['arr_0']
    b1 = synapses['arr_1']
    W2 = synapses['arr_2']
    b2 = synapses['arr_3']
    W3 = synapses['arr_4']
    b3 = synapses['arr_5']

    def predict(x):
        def relu(t):
            return np.maximum(t, 0)

        def softmax(t):
            out = np.exp(t)
            return out / np.sum(out)

        # Calculate
        t1 = x @ W1 + b1
        h1 = relu(t1)
        t2 = h1 @ W2 + b2
        h2 = relu(t2)
        t3 = h2 @ W3 + b3
        z = softmax(t3)
        return z

    # Form map
    map = []
    for x in image_array:
        z = predict(x)
        y_pred = np.argmax(z)
        map.append(y_pred)
    return map