initial commit
This commit is contained in:
commit
a5c85e5060
103
f.py
Normal file
103
f.py
Normal file
@ -0,0 +1,103 @@
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
from matplotlib import pyplot as plt
|
||||
from PIL import Image, ImageTk
|
||||
|
||||
|
||||
def __prep_data():
|
||||
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
|
||||
|
||||
x_train = x_train.astype("float32") / 255.
|
||||
x_test = x_test.astype("float32") / 255.
|
||||
|
||||
x_train = x_train.reshape(-1, 784)
|
||||
x_test = x_test.reshape(-1, 784)
|
||||
|
||||
y_train = tf.keras.utils.to_categorical(y_train, 10)
|
||||
y_test = tf.keras.utils.to_categorical(y_test, 10)
|
||||
|
||||
return (x_train, y_train), (x_test, y_test)
|
||||
|
||||
|
||||
def __prep_conf_matr(m):
|
||||
output_matrix = np.zeros([10, 10])
|
||||
|
||||
(_, _), (x_test, y_test) = __prep_data()
|
||||
pred = m.predict(x_test)
|
||||
|
||||
for i, v in enumerate(pred):
|
||||
output_matrix[np.argmax(v)][np.argmax(y_test[i])] += 1
|
||||
|
||||
return output_matrix
|
||||
|
||||
|
||||
def __plot_conf_matr(m):
|
||||
matr = __prep_conf_matr(m)
|
||||
|
||||
_, ax = plt.subplots()
|
||||
|
||||
ax.matshow(matr, cmap = plt.cm.Blues)
|
||||
|
||||
for i, x in enumerate(matr):
|
||||
for j, y in enumerate(x):
|
||||
ax.text(i,
|
||||
j,
|
||||
str(round(y)),
|
||||
va = "center",
|
||||
ha = "center")
|
||||
|
||||
plt.show()
|
||||
|
||||
|
||||
def __plot_acc_rate(h):
|
||||
plt.plot(h.history['accuracy'], label = 'train_acc')
|
||||
plt.plot(h.history['val_accuracy'], label = 'valid_acc')
|
||||
|
||||
plt.legend()
|
||||
|
||||
plt.show()
|
||||
|
||||
|
||||
def train(m, label):
|
||||
(x_train, y_train), (x_test, y_test) = __prep_data()
|
||||
|
||||
m.compile(optimizer = "adam",
|
||||
loss = "categorical_crossentropy",
|
||||
metrics = ["accuracy"])
|
||||
|
||||
h = m.fit(x_train,
|
||||
y_train,
|
||||
epochs = 30,
|
||||
batch_size = 512,
|
||||
validation_data = (x_test, y_test))
|
||||
|
||||
m.save_weights(f"save-{label}.weights.h5")
|
||||
|
||||
__plot_acc_rate(h)
|
||||
__plot_conf_matr(m)
|
||||
|
||||
|
||||
def classify(m, label, imgfn):
|
||||
m.compile(optimizer = "adam",
|
||||
loss = "categorical_crossentropy",
|
||||
metrics = ["accuracy"])
|
||||
|
||||
m.load_weights(f"save-{label}.weights.h5")
|
||||
|
||||
img = Image.open(imgfn).convert("L")
|
||||
flat_img = np.array(img).reshape(-1, 784)
|
||||
|
||||
res = m.predict(flat_img)
|
||||
|
||||
plt.imshow(flat_img.reshape(28, 28),
|
||||
cmap = "gray")
|
||||
|
||||
plt.title(np.argmax(res))
|
||||
plt.show()
|
||||
|
||||
put_active = 0
|
||||
take_active = 0
|
||||
|
||||
def classify_live(m, label):
|
||||
import lv
|
||||
lv.classify_live(m, label)
|
102
lv.py
Normal file
102
lv.py
Normal file
@ -0,0 +1,102 @@
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
from matplotlib import pyplot as plt
|
||||
from PIL import Image, ImageTk
|
||||
import tkinter as tk
|
||||
import math
|
||||
import time
|
||||
|
||||
__put_active = 0
|
||||
__take_active = 0
|
||||
|
||||
__img = None
|
||||
__cw = None
|
||||
|
||||
def classify_live(m, label):
|
||||
global __img, __cw
|
||||
|
||||
m.compile(optimizer = "adam",
|
||||
loss = "categorical_crossentropy",
|
||||
metrics = ["accuracy"])
|
||||
|
||||
m.load_weights(f"save-{label}.weights.h5")
|
||||
|
||||
r = tk.Tk()
|
||||
r.title("Draw!")
|
||||
|
||||
canvas = np.zeros([28, 28])
|
||||
|
||||
__img = Image.fromarray(np.uint8(canvas * 255), "L")
|
||||
__cw = ImageTk.PhotoImage(
|
||||
__img.resize(size = (504, 504),
|
||||
resample = Image.NEAREST))
|
||||
|
||||
l = tk.Label(r, image = __cw)
|
||||
lt = tk.Label(r, text = "", font = ("Liberation Sans", 48))
|
||||
|
||||
lt.pack()
|
||||
l.pack()
|
||||
|
||||
def clear_array():
|
||||
canvas[:][:] = np.zeros([28, 28])
|
||||
|
||||
def mouse_down(ev):
|
||||
global __put_active, __take_active
|
||||
|
||||
if ev.num == 1:
|
||||
__put_active = 1
|
||||
elif ev.num == 2:
|
||||
__take_active = 1
|
||||
|
||||
|
||||
def mouse_up(ev):
|
||||
global __put_active, __take_active
|
||||
|
||||
if ev.num == 1:
|
||||
__put_active = 0
|
||||
elif ev.num == 2:
|
||||
__take_active = 0
|
||||
|
||||
|
||||
def update_img():
|
||||
global __img, __cw
|
||||
|
||||
__img = Image.fromarray(np.uint8(canvas * 255), "L")
|
||||
__cw = ImageTk.PhotoImage(
|
||||
__img.resize(size = (504, 504),
|
||||
resample = Image.NEAREST))
|
||||
|
||||
l.configure(image = __cw)
|
||||
|
||||
r.after(50, update_img)
|
||||
|
||||
|
||||
def update_pred():
|
||||
pred = m.predict(canvas.reshape(-1, 784),
|
||||
verbose = 0)
|
||||
|
||||
lt.configure(text = np.argmax(pred))
|
||||
|
||||
r.after(300, update_pred)
|
||||
|
||||
|
||||
def change_pix(ev):
|
||||
x = math.floor(ev.x / 18)
|
||||
y = math.floor(ev.y / 18)
|
||||
|
||||
if __put_active:
|
||||
canvas[y, x] = 1
|
||||
elif __take_active:
|
||||
canvas[x, y] = 0
|
||||
|
||||
l.bind("<Motion>", change_pix)
|
||||
l.bind("<ButtonPress>", mouse_down)
|
||||
l.bind("<ButtonRelease>", mouse_up)
|
||||
|
||||
tk.Button(text = "Clear",
|
||||
command = clear_array).pack()
|
||||
|
||||
r.after(300, update_pred)
|
||||
r.after(50, update_img)
|
||||
|
||||
r.mainloop()
|
19
nn1.py
Normal file
19
nn1.py
Normal file
@ -0,0 +1,19 @@
|
||||
import tensorflow as tf
|
||||
import tensorflow.keras.layers as l
|
||||
from sys import argv
|
||||
|
||||
import f
|
||||
|
||||
m = tf.keras.models.Sequential([
|
||||
l.Input(shape = (784,)),
|
||||
l.Dense(128, activation = "relu"),
|
||||
l.Dense(64, activation = "relu"),
|
||||
l.Dense(10, activation = "softmax")
|
||||
])
|
||||
|
||||
#f.train(m, "1")
|
||||
|
||||
if len(argv) == 2:
|
||||
f.classify(m, "1", argv[1])
|
||||
else:
|
||||
f.classify_live(m, "1")
|
19
nn2.py
Normal file
19
nn2.py
Normal file
@ -0,0 +1,19 @@
|
||||
import tensorflow as tf
|
||||
import tensorflow.keras.layers as l
|
||||
from sys import argv
|
||||
|
||||
import f
|
||||
|
||||
m = tf.keras.models.Sequential([
|
||||
l.Input(shape = (784,)),
|
||||
l.Dense(64, activation = "relu"),
|
||||
l.Dense(20, activation = "relu"),
|
||||
l.Dense(10, activation = "softmax")
|
||||
])
|
||||
|
||||
#f.train(m, "2")
|
||||
|
||||
if len(argv) == 2:
|
||||
f.classify(m, "2", argv[1])
|
||||
else:
|
||||
f.classify_live(m, "2")
|
20
nn3.py
Normal file
20
nn3.py
Normal file
@ -0,0 +1,20 @@
|
||||
import tensorflow as tf
|
||||
import tensorflow.keras.layers as l
|
||||
from sys import argv
|
||||
|
||||
import f
|
||||
|
||||
m = tf.keras.models.Sequential([
|
||||
l.Input(shape = (784,)),
|
||||
l.Dense(4096, activation = "relu"),
|
||||
l.Dense(4096, activation = "relu"),
|
||||
l.Dense(4096, activation = "relu"),
|
||||
l.Dense(10, activation = "softmax")
|
||||
])
|
||||
|
||||
#f.train(m, "3")
|
||||
|
||||
if len(argv) == 2:
|
||||
f.classify(m, "3", argv[1])
|
||||
else:
|
||||
f.classify_live(m, "3")
|
Loading…
x
Reference in New Issue
Block a user