-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgorithm.py
204 lines (163 loc) Β· 5.9 KB
/
algorithm.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""
The GPLv3 License (GPLv3)
Copyright (c) 2023 Tushar Maharana
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from PIL import Image as im
import random as ran
import sys
def image_conversion(image, size):
image.convert("RGBA").convert("L")
return image.resize(size)
def getfilename(
shape, var=100, step=5
): # select random files from dataset. (This is possible because they follow a specific naming rule.)
filename = ""
if shape == "circle":
a = ran.randrange(0, var, step)
b = ran.randrange(0, var, step)
r = ran.randrange(0, var, step)
filename = f"circles/({a},{b},{r}).png"
if shape == "rectangle":
a = ran.randrange(0, var, step)
b = ran.randrange(0, var, step)
c = ran.randrange(0, var, step)
d = ran.randrange(0, var, step)
filename = f"rectangles/({a},{b},{c},{d}).png"
return filename
def list_float2int(list): # convert each element of the list into an integer
intlist = []
for i in list:
intlist.append(int(i))
return intlist
def genrandweight(size): # randomly generate weight for first run
weight = []
for _ in range(size[0] * size[1]):
weight.append(ran.randint(-1, 1))
return weight
def createfile(filepath, size):
with open(filepath, "w") as fp:
fp.write(str(genrandweight(size)))
def getfilecontent(filepath, size):
def readfile():
with open(filepath, "r") as fp:
return fp.read()
try:
weights = eval(readfile())
except FileNotFoundError:
print(f'file "{filepath}" does not exists\ncreating "{filepath}"')
createfile(filepath, size)
print("done")
weights = eval(readfile())
except SyntaxError:
print("SyntaxError occurred while parsing file")
choice = input(
"do you want to rewrite file by destroying its content? Y/n [Y]: "
).lower()
while not choice in ("y", "n", ""):
print(f'invalid input "{choice}"')
choice = input(
"do you want to rewrite file by destroying its content? Y/n [Y]: "
).lower()
if choice in ("y", ""):
print(f"rewriting {filepath}")
createfile(filepath, size)
print("done")
weights = eval(readfile())
else:
sys.exit()
return weights
def multiply(weight, image_list, size=(100, 100)):
product = 0
for i in range(size[0] * size[1]):
product += weight[i] * image_list[i]
return product
def addition(weight, image_list, SubOrAdd=1, size=(100, 100)):
sum = []
for i in range(size[0] * size[1]):
sum.append(weight[i] + (image_list[i] * SubOrAdd))
return sum
def normalize(list, factor): # fit it in the range of [0,1]
normalized_list = []
for i in list:
normalized_list.append(i / 255 * factor)
return normalized_list
def getimage_list(image):
image_list = normalize(list((image.getdata())), 1)
return image_list
def guess(imagepath, weight, bias=100, shapes=("circle", "rectangle"), size=(100, 100)):
image_list = getimage_list(image_conversion(im.open(imagepath), size))
product = multiply(weight, image_list)
if product + bias > 0:
predict_shape = shapes[1]
else:
predict_shape = shapes[0]
return predict_shape
def train(
bias=1000,
enouchs=10000,
weightpath="",
size=(100, 100),
shapes=("circle", "rectangle"),
if_image_conversion=True,
no_visualize_weight=False,
verbose=False,
):
# load weight from file or generate randomly
if len(weightpath) == 0:
weight = genrandweight(size)
weightpath = "weight"
else:
weight = getfilecontent(weightpath, size)
correct_guesses = 0
# Read Wikipedia article linked in README.md
for i in range(enouchs):
shape = shapes[ran.randrange(2)]
filename = getfilename(shape)
image = im.open(filename)
if if_image_conversion:
image = image_conversion(image, size)
image_list = getimage_list(image)
product = multiply(weight, image_list)
print(product)
if product + bias > 0:
predict_shape = shapes[0]
if shape != predict_shape:
weight = addition(weight, image_list, +1)
else:
predict_shape = shapes[1]
if shape != predict_shape:
weight = addition(weight, image_list, -1)
if shape == predict_shape:
correct_guesses += 1
if i % 1000 == 0: # log and save every 1000 enouchs
if not no_visualize_weight:
weight_viz = im.new("RGB", size)
weight_viz.putdata(list_float2int(weight))
weight_viz.save("weight.png")
with open(weightpath, "w") as w:
w.write(str(weight))
with open("log", "a") as log:
log.write(
f"{correct_guesses/(1000)}; {i/(enouchs+1) * 100}% := {filename}\n"
)
correct_guesses = 0
if verbose:
print(
i,
correct_guesses,
predict_shape,
shape,
f"\n{correct_guesses/(1000)}; {i/(enouchs+1) * 100}% := {filename}\n",
)
else:
print(i, correct_guesses, predict_shape, shape)