1
0

Compare commits

..

10 Commits

Author SHA1 Message Date
5d54ccd751 stats making and reading files 2025-11-20 16:13:05 +02:00
2b6d093c0d update training method 2025-11-20 11:32:33 +02:00
347f6a2483 add identification scripts 2025-11-20 11:31:48 +02:00
0f4ebce8f6 updates from colab [fix model, reduce augmentation] 2025-11-20 11:31:24 +02:00
4285df1657 slow augmenter 2025-11-19 12:09:32 +02:00
316513b6e3 save 1 2025-11-19 11:15:01 +02:00
d6a8daedea save 2025-11-18 17:00:51 +02:00
3c485a9c9a rename 2025-11-18 16:08:40 +02:00
a1155f5d4e save 3 [built] 2025-11-18 16:08:01 +02:00
d93925d196 save 2 2025-11-18 16:06:17 +02:00
11 changed files with 403 additions and 116 deletions

33
aug.py Normal file
View File

@ -0,0 +1,33 @@
import tensorflow as tf
import numpy as np
from PIL import Image
from os import listdir as ls
from sys import argv, exit
from tensorflow.keras.utils import array_to_img as img
from tensorflow.keras import layers as kl
if len(argv) != 3:
exit(1)
files = ls(argv[1])
for v, fn in enumerate(files):
i = np.array(Image.open(f"{argv[1]}/{fn}").convert('RGB').resize((300,300))) / 255.0
print(f"Processing {v+1:03d}/{len(files)}: {fn}")
for x1 in range(3):
i1 = tf.image.random_brightness(i, 0.6)
for x2 in range(3):
i2 = tf.image.random_saturation(i1, 0.1, 2.0)
for x3 in range(3):
i3 = tf.image.random_contrast(i2, 0.2, 1.9)
img(i3).save(f"{argv[2]}/{fn.rsplit('.', 1)[0]}_{x1}_{x2}_{x3}_1.png")
i3 = tf.image.flip_left_right(i3)
img(i3).save(f"{argv[2]}/{fn.rsplit('.', 1)[0]}_{x1}_{x2}_{x3}_2.png")
i3 = tf.image.flip_up_down(i3)
img(i3).save(f"{argv[2]}/{fn.rsplit('.', 1)[0]}_{x1}_{x2}_{x3}_3.png")
i3 = tf.image.flip_left_right(i3)
img(i3).save(f"{argv[2]}/{fn.rsplit('.', 1)[0]}_{x1}_{x2}_{x3}_4.png")

45
ident-live.py Normal file
View File

@ -0,0 +1,45 @@
#from PIL import Image
#from sys import argv, exit
import numpy as np
import cv2 as cv
from tensorflow.keras.utils import array_to_img as img
from matplotlib import pyplot as plt
#if len(argv) != 2:
# exit(1)
#i = np.array(Image.open(argv[1]).convert('RGB').resize((300,300)))
from m import *
mod.load_weights("model3-val-acc-0.74.model.keras")
c = cv.VideoCapture(0)
while True:
ret, frame = c.read()
pi = np.array(img(cv.cvtColor(frame, cv.COLOR_BGR2RGB)).resize((300, 300)).convert("RGB"))
#plt.imshow(pi)
#plt.show()
#print(pi.astype(float))
r = mod.predict(np.array([pi.astype(float)]))
cv.putText(frame, f"Y: {r[0][1]:.9f}", (10, 40), cv.FONT_HERSHEY_SIMPLEX, 1, (200,255,200), 2)
cv.putText(frame, f"N: {r[0][0]:.9f}", (10, 70), cv.FONT_HERSHEY_SIMPLEX, 1, (200,200,255), 2)
cv.imshow('me', frame)
if cv.waitKey(1) == ord('q'):
break
#print(frame)
c.release()
cv.destroyAllWindows()
#r = mod.predict(np.array([i]))
#print(r)

15
ident.py Normal file
View File

@ -0,0 +1,15 @@
from m import *
from PIL import Image
from sys import argv, exit
import numpy as np
if len(argv) != 2:
exit(1)
i = np.array(Image.open(argv[1]).convert('RGB').resize((300,300)))
mod.load_weights("model3.model.keras")
r = mod.predict(np.array([i]))
print(r)

160
m.py Normal file
View File

@ -0,0 +1,160 @@
import tensorflow as tf
import numpy as np
from tensorflow import keras as k
from tensorflow.keras import layers as kl
from tensorflow.keras import models as km
from tensorflow.keras import optimizers as ko
from tensorflow.keras import callbacks as kc
CONV_SIZE = 96
MPPT_SIZE = 256
DRPT_RATE = 0.3
TOUT_AMNT = 2
def conv(i, n, s, t = (1, 1)):
c = kl.Conv2D(n, s, padding = 'same', strides = t)(i)
b = kl.BatchNormalization()(c)
return kl.Activation('relu')(b)
def avg(i, s):
return kl.AveragePooling2D(s, padding = 'same', strides = (1, 1))(i)
def max(i, s):
return kl.MaxPooling2D(s, padding = 'same', strides = (1, 1))(i)
def generate_start(i):
s = kl.Rescaling(1./255.)(i)
r1 = conv(s, CONV_SIZE, (3, 3))
r2 = conv(r1, CONV_SIZE, (3, 3))
r3 = conv(r2, CONV_SIZE, (3, 3))
m1 = kl.MaxPooling2D((3, 3))(r3)
r4 = conv(m1, CONV_SIZE, (1, 1))
r5 = conv(r4, CONV_SIZE, (3, 3))
return kl.MaxPooling2D((3, 3))(r5)
def generate_type_a(i):
a11 = conv(i, CONV_SIZE, (1, 1))
a21 = avg(i, (3, 3))
a22 = conv(a21, CONV_SIZE, (1, 1))
a31 = conv(i, CONV_SIZE, (1, 1))
a32 = conv(a31, CONV_SIZE, (3, 3))
a41 = conv(i, CONV_SIZE, (1, 1))
a42 = conv(a41, CONV_SIZE, (3, 3))
a43 = conv(a42, CONV_SIZE, (3, 3))
return kl.Concatenate()([a11, a22, a32, a43])
def generate_ab_bridge(i):
ab11 = conv(i, CONV_SIZE, (1, 1))
ab12 = conv(ab11, CONV_SIZE, (3, 3))
ab13 = conv(ab12, CONV_SIZE, (3, 3))
ab21 = conv(i, CONV_SIZE, (3, 3))
ab31 = max(i, (3, 3))
return kl.Concatenate()([ab13, ab21, ab31])
def generate_type_b(i):
b11 = conv(i, CONV_SIZE, (1, 1))
b12 = conv(b11, CONV_SIZE, (7, 1))
b13 = conv(b12, CONV_SIZE, (1, 7))
b14 = conv(b13, CONV_SIZE, (7, 1))
b15 = conv(b14, CONV_SIZE, (1, 7))
b21 = conv(i, CONV_SIZE, (1, 1))
b22 = conv(b21, CONV_SIZE, (1, 7))
b23 = conv(b22, CONV_SIZE, (7, 1))
b31 = conv(i, CONV_SIZE, (1, 1))
b41 = avg(i, (3, 3))
b42 = conv(b41, CONV_SIZE, (1, 1))
return kl.Concatenate()([b15, b23, b31, b42])
def generate_bc_bridge(i):
bc11 = conv(i, CONV_SIZE, (1, 1))
bc12 = conv(bc11, CONV_SIZE, (7, 1))
bc13 = conv(bc12, CONV_SIZE, (1, 7))
bc14 = conv(bc13, CONV_SIZE, (3, 3))
bc21 = conv(i, CONV_SIZE, (1, 1))
bc22 = conv(bc21, CONV_SIZE, (1, 1))
bc31 = max(i, (3, 3))
return kl.Concatenate()([bc14, bc22, bc31])
def generate_aux(i):
u1 = kl.AveragePooling2D((5, 5))(i)
u2 = conv(u1, CONV_SIZE, (1, 1))
u3 = kl.Flatten()(u2)
u4 = kl.Dropout(DRPT_RATE)(u3)
u5 = kl.Dense(MPPT_SIZE)(u4)
u6 = kl.Dense(TOUT_AMNT)(u5)
return kl.Activation('softmax')(u6)
def generate_type_c(i):
c11 = conv(i, CONV_SIZE, (1, 1))
c12 = conv(c11, CONV_SIZE, (3, 3))
c13 = conv(c12, CONV_SIZE, (1, 3))
c14 = conv(c12, CONV_SIZE, (3, 1))
c21 = conv(i, CONV_SIZE, (1, 1))
c22 = conv(c21, CONV_SIZE, (1, 3))
c23 = conv(c21, CONV_SIZE, (3, 1))
c31 = max(i, (3, 3))
c32 = conv(c31, CONV_SIZE, (1, 1))
c41 = conv(i, CONV_SIZE, (1, 1))
return kl.Concatenate()([c14, c23, c32, c41])
def generate_finish(i):
f1 = kl.AveragePooling2D((5, 5))(i)
f2 = conv(f1, CONV_SIZE, (1, 1))
f3 = kl.Flatten()(f2)
f4 = kl.Dropout(DRPT_RATE)(f3)
f5 = kl.Dense(MPPT_SIZE)(f4)
f6 = kl.Dense(TOUT_AMNT)(f5)
return kl.Activation('softmax')(f6)
gi = kl.Input((300, 300, 3))
ds = generate_start(gi)
for _ in range(3):
ds = generate_type_a(ds)
ds = generate_ab_bridge(ds)
for _ in range(4):
ds = generate_type_b(ds)
uo = generate_aux(ds)
go = generate_bc_bridge(ds)
for _ in range(2):
go = generate_type_c(go)
go = generate_finish(go)
mod = k.Model(inputs = gi, outputs = go)
mod.summary()
mod.compile(optimizer = ko.Lion(learning_rate = 0.0001),
loss = 'categorical_crossentropy',
metrics = ['accuracy'])

47
stats-cm.py Normal file
View File

@ -0,0 +1,47 @@
from m import *
from matplotlib import pyplot as plt
def __prep_conf_matr(data):
output_matrix = np.zeros([2, 2])
for p, r in zip(*data):
print(p, r)
output_matrix[np.argmax(p)][np.argmax(r)] += 1
return output_matrix
def __plot_conf_matr(data):
matr = __prep_conf_matr(data)
_, 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()
ds = tf.keras.preprocessing.image_dataset_from_directory(
'../dataset-orig-aug-1-mini-1/',
labels = 'inferred',
label_mode = 'categorical',
color_mode = 'rgb',
image_size = (300, 300),
batch_size = 32,
verbose = True
)
#ds_short = ds.take(1)
p = mod.predict(ds)
r = np.concatenate([y for x, y in ds])
__plot_conf_matr([p, r])

20
stats-generic.py Normal file
View File

@ -0,0 +1,20 @@
from m import *
from matplotlib import pyplot as plt
ds = tf.keras.preprocessing.image_dataset_from_directory(
'../dataset-orig-aug-1-mini-1/',
labels = 'inferred',
label_mode = 'categorical',
color_mode = 'rgb',
image_size = (300, 300),
batch_size = 32,
verbose = True
)
p = mod.predict(ds)
r = np.concatenate([y for x, y in ds])
with open('results.txt', 'w') as f:
for i in zip(p, r):
f.write(f"{i[0][0]} {i[0][1]} {i[1][0]} {i[1][1]}\n")

30
stats-other.py Normal file
View File

@ -0,0 +1,30 @@
from m import *
from sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score
ds = tf.keras.preprocessing.image_dataset_from_directory(
'../dataset-orig-aug-1-mini-1/',
labels = 'inferred',
label_mode = 'categorical',
color_mode = 'rgb',
image_size = (300, 300),
batch_size = 32,
verbose = True
)
p = []
r = []
with open('results.txt', 'r') as f:
for i in f.read().split('\n'):
if i == '':
continue
res = tuple(map(float, i.split()))
p.append([res[0] > 0.5, res[1] > 0.5])
r.append([res[2], res[3]])
print(f"Accuracy : {accuracy_score(p, r)}")
print(f"Precision : {precision_score(p, r, average = 'micro')}")
print(f"Recall : {recall_score(p, r, average = 'micro')}")
print(f"F1 Score : {f1_score(p, r, average = 'micro')}")

13
stats-plot-1.py Normal file
View File

@ -0,0 +1,13 @@
from matplotlib import pyplot as plt
ta = [0.5396, 0.6266, 0.7137, 0.6768, 0.7114, 0.7821, 0.7373, 0.6866, 0.5987, 0.6861, 0.7236, 0.7147]
va = [0.4273, 0.4714, 0.4273, 0.4229, 0.4317, 0.4229, 0.5022, 0.4449, 0.5947, 0.7489, 0.5815, 0.7048]
x = [i+1 for i in range(12)]
plt.plot(x, ta)
plt.plot(x, va)
plt.legend(["train_acc", "valid_acc"])
plt.show()

116
test.py
View File

@ -1,116 +0,0 @@
import tensorflow as tf
import numpy as np
from tensorflow.keras import layers as kl
from tensorflow.keras import models as km
from tensorflow.keras import optimizers as ko
def generate_inception_model(a = 5, b = 4, c = 2):
# start
i = kl.Input(shape = (300, 300, 3))
r1 = kl.Conv2D(1024, (3, 3), padding = 'same', activation = 'relu')(i)
r2 = kl.Conv2D(1024, (5, 5), padding = 'same', activation = 'relu')(r1)
r3 = kl.Conv2D(1024, (7, 7), padding = 'same', activation = 'relu')(r2)
m1 = kl.MaxPooling2D((3, 3))(r3)
r4 = kl.Conv2D(1024, (3, 3), padding = 'same', activation = 'relu')(m1)
r5 = kl.Conv2D(1024, (5, 5), padding = 'same', activation = 'relu')(r4)
ia0 = kl.MaxPooling2D((2, 2))(r5)
# a types
for k in range(a):
exec(f"ia{k}_1_1 = kl.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(ia{k})")
exec(f"ia{k}_3_1 = kl.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(ia{k})")
exec(f"ia{k}_3_2 = kl.Conv2D(1024, (3, 1), padding = 'same', activation = 'relu')(ia{k}_3_1)")
exec(f"ia{k}_5_1 = kl.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(ia{k})")
exec(f"ia{k}_5_2 = kl.Conv2D(1024, (3, 1), padding = 'same', activation = 'relu')(ia{k}_5_1)")
exec(f"ia{k}_5_3 = kl.Conv2D(1024, (5, 1), padding = 'same', activation = 'relu')(ia{k}_5_2)")
exec(f"ia{k}_7_1 = kl.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(ia{k})")
exec(f"ia{k}_7_2 = kl.Conv2D(1024, (3, 1), padding = 'same', activation = 'relu')(ia{k}_7_1)")
exec(f"ia{k}_7_3 = kl.Conv2D(1024, (5, 1), padding = 'same', activation = 'relu')(ia{k}_7_2)")
exec(f"ia{k}_7_4 = kl.Conv2D(1024, (7, 1), padding = 'same', activation = 'relu')(ia{k}_7_3)")
exec(f"ia{k+1} = kl.Concatenate()([ia{k}_1_1, ia{k}_3_2, ia{k}_5_3, ia{k}_7_4])")
# grid size reductor 1
iab_1 = kl.Conv2D(1024, (5, 1), padding = 'same', activation = 'relu')(eval(f"ia{a}"))
iab_2 = kl.Conv2D(1024, (5, 1), padding = 'same', activation = 'relu')(eval(f"ia{a}"))
iab_3 = kl.Conv2D(1024, (3, 1), padding = 'same', activation = 'relu')(iab_2)
iab_4 = kl.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(iab_3)
iab_6 = kl.Concatenate()([iab_1, iab_4, iab_5])
# b types
for k in range(b):
exec(f"ib{k}_1_1 = kl.MaxPooling2D((2, 2), padding = 'same')(iab_6)")
exec(f"ib{k}_1_2 = kl.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(i)")
exec(f"ib{k}_3_1 = kl.Conv2D(1024, (3, 1), padding = 'same', activation = 'relu')(iab_6)")
exec(f"ib{k}_5_1 = kl.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(iab_6)")
exec(f"ib{k}_5_2 = kl.Conv2D(1024, (3, 1), padding = 'same', activation = 'relu')(ib{k}_5_1)")
exec(f"ib{k}_5_3 = kl.Conv2D(1024, (5, 1), padding = 'same', activation = 'relu')(ib{k}_5_2)")
exec(f"ib{k}_7_1 = kl.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(iab_6)")
exec(f"ib{k}_7_2 = kl.Conv2D(1024, (3, 1), padding = 'same', activation = 'relu')(ib{k}_7_1)")
exec(f"ib{k}_7_3 = kl.Conv2D(1024, (5, 1), padding = 'same', activation = 'relu')(ib{k}_7_2)")
exec(f"ib{k}_7_4 = kl.Conv2D(1024, (7, 1), padding = 'same', activation = 'relu')(ib{k}_7_3)")
exec(f"ib{k+1} = kl.Concatenate()([ib{k}_1_2, ib{k}_3_2, ib{k}_5_3, ib{k}_7_4])")
# grid size reductor 2
# c types
for k in range(c):
exec(f"ic{k}_1_1 = kl.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(i{'b' if k else 'a'}{k if k else a})")
exec(f"ic{k}_3_1 = kl.Conv2D(1024, (3, 1), padding = 'same', activation = 'relu')(i{'b' if k else 'a'}{k if k else a})")
exec(f"ic{k}_5_1 = kl.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(i{'b' if k else 'a'}{k if k else a})")
exec(f"ic{k}_5_2 = kl.Conv2D(1024, (3, 1), padding = 'same', activation = 'relu')(ib{k}_5_1)")
exec(f"ic{k}_5_3 = kl.Conv2D(1024, (5, 1), padding = 'same', activation = 'relu')(ib{k}_5_2)")
exec(f"ic{k}_7_1 = kl.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(i{'b' if k else 'a'}{k if k else a})")
exec(f"ic{k}_7_2 = kl.Conv2D(1024, (3, 1), padding = 'same', activation = 'relu')(ib{k}_7_1)")
exec(f"ic{k}_7_3 = kl.Conv2D(1024, (5, 1), padding = 'same', activation = 'relu')(ib{k}_7_2)")
exec(f"ic{k}_7_4 = kl.Conv2D(1024, (7, 1), padding = 'same', activation = 'relu')(ib{k}_7_3)")
exec(f"ib{k+1} = kl.Concatenate()([ib{k}_1_1, ib{k}_3_2, ib{k}_5_3, ib{k}_7_4])")
# mppt
o = eval(f"ic{c}")
return tf.keras.Model(inputs = i, outputs = o)
'''
a_i = l.Input(shape = (300, 300, 3))
a_1_1 = l.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(a_i)
a_3_1 = l.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(a_i)
a_3_2 = l.Conv2D(1024, (3, 1), padding = 'same', activation = 'relu')(a_3_1)
a_5_1 = l.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(a_i)
a_5_2 = l.Conv2D(1024, (5, 1), padding = 'same', activation = 'relu')(a_5_1)
a_7_1 = l.Conv2D(1024, (1, 1), padding = 'same', activation = 'relu')(a_i)
a_7_2 = l.Conv2D(1024, (7, 1), padding = 'same', activation = 'relu')(a_7_1)
a_o = l.Concatenate()([a_1_1, a_3_2, a_5_2, a_7_2])
inception_type_a = [a_i, a_o]
tf.keras.Model(*inception_type_a)
'''
mod = generate_inception_model()
mod.summary()
mod.compile(optimizer = ko.Lion(learning_rate = 0.001))

15
train_l.py Normal file
View File

@ -0,0 +1,15 @@
import tensorflow as tf
ds_train, ds_valid = tf.keras.preprocessing.image_dataset_from_directory(
'../dataset-orig',
labels = 'inferred',
label_mode = 'categorical',
color_mode = 'rgb',
image_size = (300, 300),
shuffle = False,
validation_split = 0.05,
subset = 'both',
verbose = True
)
from m import mod

25
train_l_v2.py Normal file
View File

@ -0,0 +1,25 @@
import tensorflow as tf
ds_train, ds_valid = tf.keras.preprocessing.image_dataset_from_directory(
'/mnt/tmpfs1/ds-mini-1',
labels = 'inferred',
label_mode = 'categorical',
color_mode = 'rgb',
batch_size = 16,
image_size = (300, 300),
shuffle = False,
validation_split = 0.05,
subset = 'both',
verbose = True
)
from m import *
ckpt = kc.ModelCheckpoint("model3.model.keras",
monitor = 'val_accuracy',
save_best_only = True)
h = mod.fit(ds_train,
epochs = 9,
validation_data = ds_valid,
callbacks = [ckpt])