From a5c85e5060562b00d21397c85a73e78321631c84 Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Sat, 11 Oct 2025 21:57:26 +0300 Subject: [PATCH] initial commit --- f.py | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lv.py | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ nn1.py | 19 +++++++++++ nn2.py | 19 +++++++++++ nn3.py | 20 +++++++++++ 5 files changed, 263 insertions(+) create mode 100644 f.py create mode 100644 lv.py create mode 100644 nn1.py create mode 100644 nn2.py create mode 100644 nn3.py diff --git a/f.py b/f.py new file mode 100644 index 0000000..97cc546 --- /dev/null +++ b/f.py @@ -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) diff --git a/lv.py b/lv.py new file mode 100644 index 0000000..e05ff57 --- /dev/null +++ b/lv.py @@ -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("", change_pix) + l.bind("", mouse_down) + l.bind("", mouse_up) + + tk.Button(text = "Clear", + command = clear_array).pack() + + r.after(300, update_pred) + r.after(50, update_img) + + r.mainloop() diff --git a/nn1.py b/nn1.py new file mode 100644 index 0000000..5bc74cd --- /dev/null +++ b/nn1.py @@ -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") diff --git a/nn2.py b/nn2.py new file mode 100644 index 0000000..a753888 --- /dev/null +++ b/nn2.py @@ -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") diff --git a/nn3.py b/nn3.py new file mode 100644 index 0000000..6ca2ee8 --- /dev/null +++ b/nn3.py @@ -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")