initial commit
This commit is contained in:
commit
829523cae9
35
aug-ol.py
Normal file
35
aug-ol.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
from PIL import Image
|
||||||
|
from os import listdir as ls
|
||||||
|
from random import randint as r
|
||||||
|
|
||||||
|
src = "../stock-images-part-2"
|
||||||
|
dst = "../stock-images-part-2-ovly"
|
||||||
|
olp = "../cnh-bw.png"
|
||||||
|
|
||||||
|
a = []
|
||||||
|
|
||||||
|
for k, i in enumerate(ls(src)):
|
||||||
|
print(k)
|
||||||
|
a.append(Image.open(f"{src}/{i}").resize((512,512)).convert("RGB"))
|
||||||
|
|
||||||
|
img_ol = Image.open(olp)#.resize((200,200))
|
||||||
|
|
||||||
|
for k, img in enumerate(a):
|
||||||
|
print(k)
|
||||||
|
|
||||||
|
n = img_ol.resize((r(150, 250), r(150, 250)))
|
||||||
|
|
||||||
|
match r(0, 3):
|
||||||
|
case 0:
|
||||||
|
img.paste(n, (r(0, 261), r(0, 261)))
|
||||||
|
case 1:
|
||||||
|
n = n.rotate(90)
|
||||||
|
img.paste(n, (r(0, 261), r(0, 261)))
|
||||||
|
case 2:
|
||||||
|
n = n.rotate(180)
|
||||||
|
img.paste(n, (r(0, 261), r(0, 261)))
|
||||||
|
case 3:
|
||||||
|
n = n.rotate(270)
|
||||||
|
img.paste(n, (r(0, 261), r(0, 261)))
|
||||||
|
|
||||||
|
img.resize((300,300)).save(f"{dst}/{k}.jpg")
|
||||||
46
aug.py
Normal file
46
aug.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
from test2 import *
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from PIL import Image
|
||||||
|
from os import listdir as ls
|
||||||
|
|
||||||
|
a = []
|
||||||
|
|
||||||
|
for i in ls("../pics/yes"):
|
||||||
|
a.append(np.asarray(Image.open(f"../pics/yes/{i}").resize((1024,1024)).convert("RGB")))
|
||||||
|
|
||||||
|
a = np.array(a)
|
||||||
|
|
||||||
|
aug = km.Sequential([
|
||||||
|
kl.Input(shape = (1024, 1024, 3)),
|
||||||
|
kl.RandomRotation((-0.5, 0.5)),
|
||||||
|
kl.RandomSaturation((0.2, 0.6)),
|
||||||
|
kl.RandomContrast((0.6, 0.6)),
|
||||||
|
kl.Resizing(300, 300)
|
||||||
|
])
|
||||||
|
|
||||||
|
aug.compile()
|
||||||
|
aug.summary()
|
||||||
|
|
||||||
|
|
||||||
|
'''
|
||||||
|
from matplotlib import pyplot as plt
|
||||||
|
|
||||||
|
plt.figure()
|
||||||
|
f, g = plt.subplots(3, 3)
|
||||||
|
for i in range(9):
|
||||||
|
g[i//3][i%3].imshow(res[i])
|
||||||
|
|
||||||
|
plt.show()
|
||||||
|
'''
|
||||||
|
|
||||||
|
#print(res[0].numpy().shape)
|
||||||
|
|
||||||
|
#Image.fromarray(res[0].numpy().astype("uint8")).show()
|
||||||
|
|
||||||
|
for i in range(10):
|
||||||
|
print(i)
|
||||||
|
res = aug(a, batch_size = 32, training = True)
|
||||||
|
|
||||||
|
for k, img in enumerate(res):
|
||||||
|
Image.fromarray(img.numpy().astype("uint8")).save(f"../pics-exp/yes/{i:02d}-{k}.jpg")
|
||||||
15
cap.py
Normal file
15
cap.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import cv2 as cv
|
||||||
|
import time
|
||||||
|
import tensorflow
|
||||||
|
from tensorflow.keras.utils import array_to_img as img
|
||||||
|
|
||||||
|
c = cv.VideoCapture(0)
|
||||||
|
|
||||||
|
for i in range(200):
|
||||||
|
print(f"pic {i}")
|
||||||
|
err, frame = c.read()
|
||||||
|
|
||||||
|
img(cv.cvtColor(frame, cv.COLOR_BGR2RGB)).resize((300, 300)).save(f"../cap-2/no/{i:02d}.jpg")
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
c.close()
|
||||||
53
ident-live.py
Normal file
53
ident-live.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#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 test2 import *
|
||||||
|
m.load_weights("model3.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 = m.predict(np.array([pi.astype(float)]))
|
||||||
|
|
||||||
|
if r[0][0] >= 0:
|
||||||
|
if (pi[20][28][0] >= 160):
|
||||||
|
cv.putText(frame, f"Y: {r[0][0]:.9f}", (10, 40), cv.FONT_HERSHEY_SIMPLEX, 1, (0,130,0), 2)
|
||||||
|
else:
|
||||||
|
cv.putText(frame, f"Y: {r[0][0]:.9f}", (10, 40), cv.FONT_HERSHEY_SIMPLEX, 1, (150,255,150), 2)
|
||||||
|
else:
|
||||||
|
if (pi[30][30][0] >= 160):
|
||||||
|
cv.putText(frame, f"N: {r[0][0]:.9f}", (10, 40), cv.FONT_HERSHEY_SIMPLEX, 1, (0,0,130), 2)
|
||||||
|
else:
|
||||||
|
cv.putText(frame, f"N: {r[0][0]:.9f}", (10, 40), cv.FONT_HERSHEY_SIMPLEX, 1, (150,150,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)
|
||||||
38
ident-vid-graph.py
Normal file
38
ident-vid-graph.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#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 test2 import *
|
||||||
|
m.load_weights("model3.keras")
|
||||||
|
|
||||||
|
c = cv.VideoCapture(argv[1])
|
||||||
|
|
||||||
|
frames = []
|
||||||
|
|
||||||
|
while True:
|
||||||
|
ret, frame = c.read()
|
||||||
|
|
||||||
|
if not ret:
|
||||||
|
break
|
||||||
|
|
||||||
|
r = cv.resize(frame, dsize=(300, 300), interpolation=cv.INTER_CUBIC)
|
||||||
|
r = cv.cvtColor(r, cv.COLOR_BGR2RGB)
|
||||||
|
frames.append(r)
|
||||||
|
|
||||||
|
c.release()
|
||||||
|
|
||||||
|
frames = np.array(frames)
|
||||||
|
res = m.predict(frames, batch_size = 64)
|
||||||
|
|
||||||
|
plt.plot([i[0] for i in res])
|
||||||
|
plt.show()
|
||||||
56
ident-vid.py
Normal file
56
ident-vid.py
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
#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 test2 import *
|
||||||
|
m.load_weights("model3.keras")
|
||||||
|
|
||||||
|
c = cv.VideoCapture(argv[1])
|
||||||
|
|
||||||
|
while True:
|
||||||
|
ret, frame = c.read()
|
||||||
|
|
||||||
|
if not ret:
|
||||||
|
break
|
||||||
|
|
||||||
|
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 = m.predict(np.array([pi.astype(float)]))
|
||||||
|
|
||||||
|
if r[0][0] >= 0.5:
|
||||||
|
if (pi[30][30][0] >= 190):
|
||||||
|
cv.putText(frame, f"Y: {r[0][0]:.9f}", (10, 40), cv.FONT_HERSHEY_SIMPLEX, 1, (0,40,0), 2)
|
||||||
|
else:
|
||||||
|
cv.putText(frame, f"Y: {r[0][0]:.9f}", (10, 40), cv.FONT_HERSHEY_SIMPLEX, 1, (200,255,200), 2)
|
||||||
|
else:
|
||||||
|
if (pi[30][30][0] >= 190):
|
||||||
|
cv.putText(frame, f"N: {r[0][0]:.9f}", (10, 40), cv.FONT_HERSHEY_SIMPLEX, 1, (0,40,0), 2)
|
||||||
|
else:
|
||||||
|
cv.putText(frame, f"N: {r[0][0]:.9f}", (10, 40), cv.FONT_HERSHEY_SIMPLEX, 1, (200,255,200), 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)
|
||||||
81
test2.py
Normal file
81
test2.py
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
from tensorflow.keras import layers as kl
|
||||||
|
from tensorflow.keras import models as km
|
||||||
|
from tensorflow.keras import losses as ks
|
||||||
|
from tensorflow.keras import optimizers as ko
|
||||||
|
from tensorflow.keras import callbacks as kc
|
||||||
|
|
||||||
|
def conv(i, k, s, a = None, t = (1, 1)):
|
||||||
|
l1 = kl.Conv2D(k, s, strides = t, padding = 'same')(i)
|
||||||
|
l2 = kl.BatchNormalization()(l1)
|
||||||
|
|
||||||
|
if a:
|
||||||
|
return kl.Activation(a)(l2)
|
||||||
|
else:
|
||||||
|
return l2
|
||||||
|
|
||||||
|
def sepc(i, k, s, a = None, t = (1, 1)):
|
||||||
|
l1 = kl.SeparableConv2D(k, s, strides = t, padding = 'same')(i)
|
||||||
|
l2 = kl.BatchNormalization()(l1)
|
||||||
|
|
||||||
|
if a:
|
||||||
|
return kl.Activation(a)(l2)
|
||||||
|
else:
|
||||||
|
return l2
|
||||||
|
|
||||||
|
def maxp(i, s):
|
||||||
|
return kl.MaxPooling2D(s, padding = 'same')(i)
|
||||||
|
|
||||||
|
def addl(*i):
|
||||||
|
return kl.Add()([*i])
|
||||||
|
|
||||||
|
def gapl(i):
|
||||||
|
return kl.GlobalAveragePooling2D()(i)
|
||||||
|
|
||||||
|
def drpt(i, r):
|
||||||
|
return kl.Dropout(r)(i)
|
||||||
|
|
||||||
|
def dnsl(i, s):
|
||||||
|
return kl.Dense(s)(i)
|
||||||
|
|
||||||
|
def rscl(i, r):
|
||||||
|
return kl.Rescaling(r)(i)
|
||||||
|
|
||||||
|
|
||||||
|
def raise_xception(si = (300, 300), cc = 1):
|
||||||
|
i = kl.Input(shape = (*si, 3))
|
||||||
|
|
||||||
|
b01 = rscl(i, 1/255)
|
||||||
|
|
||||||
|
b11 = conv(b01, 16, (3,3), 'relu')
|
||||||
|
|
||||||
|
b21 = conv(b11, 32, (1,1), t = (2,2))
|
||||||
|
b22 = sepc(b11, 32, (3,3), 'relu')
|
||||||
|
b23 = maxp(b22, (2,2))
|
||||||
|
|
||||||
|
b31 = addl(b21, b23)
|
||||||
|
|
||||||
|
b41 = conv(b31, 64, (1,1), t = (2,2))
|
||||||
|
b42 = sepc(b31, 64, (3,3), 'relu')
|
||||||
|
b43 = maxp(b42, (2,2))
|
||||||
|
|
||||||
|
b51 = addl(b41, b43)
|
||||||
|
|
||||||
|
b61 = sepc(b51, 128, (3,3))
|
||||||
|
b71 = gapl(b61)
|
||||||
|
|
||||||
|
b81 = drpt(b71, 0.3)
|
||||||
|
b91 = dnsl(b81, 64)
|
||||||
|
|
||||||
|
o = dnsl(b91, cc)
|
||||||
|
|
||||||
|
return km.Model(inputs = i, outputs = o)
|
||||||
|
|
||||||
|
|
||||||
|
m = raise_xception()
|
||||||
|
|
||||||
|
m.compile(
|
||||||
|
optimizer = ko.Lion(learning_rate = 0.002),
|
||||||
|
loss = ks.BinaryCrossentropy(from_logits = True),
|
||||||
|
metrics = ['accuracy'])
|
||||||
|
|
||||||
|
m.summary()
|
||||||
22
train.py
Normal file
22
train.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
from test2 import *
|
||||||
|
|
||||||
|
import tensorflow as tf
|
||||||
|
|
||||||
|
td, vd = tf.keras.utils.image_dataset_from_directory(
|
||||||
|
"../dataset-ultimate-1",
|
||||||
|
shuffle = True,
|
||||||
|
seed = 12345,
|
||||||
|
subset = "both",
|
||||||
|
validation_split = 0.1,
|
||||||
|
image_size = (300, 300),
|
||||||
|
labels = "inferred",
|
||||||
|
label_mode = "binary",
|
||||||
|
batch_size = 32)
|
||||||
|
|
||||||
|
ckpt = kc.ModelCheckpoint("model2.keras", save_best_only = True)
|
||||||
|
|
||||||
|
m.fit(td,
|
||||||
|
epochs = 30,
|
||||||
|
verbose = 1,
|
||||||
|
validation_data = vd,
|
||||||
|
callbacks = [ckpt])
|
||||||
25
train2.py
Normal file
25
train2.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
from test2 import *
|
||||||
|
|
||||||
|
import tensorflow as tf
|
||||||
|
|
||||||
|
td, vd = tf.keras.utils.image_dataset_from_directory(
|
||||||
|
"../dataset-ultimate-2",
|
||||||
|
shuffle = True,
|
||||||
|
seed = 12345,
|
||||||
|
subset = "both",
|
||||||
|
validation_split = 0.1,
|
||||||
|
image_size = (300, 300),
|
||||||
|
labels = "inferred",
|
||||||
|
label_mode = "binary",
|
||||||
|
batch_size = 96)
|
||||||
|
|
||||||
|
ckpt = kc.ModelCheckpoint("model4.keras",
|
||||||
|
save_best_only = True,
|
||||||
|
monitor = "val_accuracy")
|
||||||
|
|
||||||
|
m.load_weights("model3.keras")
|
||||||
|
m.fit(td,
|
||||||
|
epochs = 30,
|
||||||
|
verbose = 1,
|
||||||
|
validation_data = vd,
|
||||||
|
callbacks = [ckpt])
|
||||||
59
validate.py
Normal file
59
validate.py
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
from test2 import *
|
||||||
|
|
||||||
|
import tensorflow as tf
|
||||||
|
from sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score
|
||||||
|
from matplotlib import pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
td = tf.keras.utils.image_dataset_from_directory(
|
||||||
|
"../dataset-ultimate-2",
|
||||||
|
image_size = (300, 300),
|
||||||
|
labels = "inferred",
|
||||||
|
label_mode = "binary",
|
||||||
|
batch_size = 96)
|
||||||
|
|
||||||
|
m.load_weights("model3.keras")
|
||||||
|
|
||||||
|
p = [i >= 0 for i in m.predict(td)]
|
||||||
|
r = np.concatenate([y for x, y in td])
|
||||||
|
|
||||||
|
#print(r)
|
||||||
|
#exit()
|
||||||
|
|
||||||
|
def __prep_conf_matr(data):
|
||||||
|
output_matrix = np.zeros([2, 2])
|
||||||
|
|
||||||
|
for p, r in zip(*data):
|
||||||
|
#print(p, r)
|
||||||
|
output_matrix[int(p)][int(r[0])] += 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()
|
||||||
|
|
||||||
|
#p = mod.predict(ds)
|
||||||
|
#r = np.concatenate([y for x, y in ds])
|
||||||
|
|
||||||
|
print(f"Accuracy : {accuracy_score(p, r)}")
|
||||||
|
print(f"Precision : {precision_score(p, r, average = 'binary')}")
|
||||||
|
print(f"Recall : {recall_score(p, r, average = 'binary')}")
|
||||||
|
print(f"F1 Score : {f1_score(p, r, average = 'binary')}")
|
||||||
|
|
||||||
|
__plot_conf_matr([p, r])
|
||||||
Loading…
x
Reference in New Issue
Block a user