+basic image recognition

This commit is contained in:
anon 2024-12-10 20:35:11 +01:00
parent ad2a2aa161
commit 8636aef269
2 changed files with 54 additions and 0 deletions
Python/ai/basic_image_classification

@ -0,0 +1,48 @@
#!/bin/python3
from sys import argv
from tensorflow import keras
my_activation = ( # declared like this to ease commenting/uncommenting
#'sigmoid'
# performs like absolute trash
# requires ~x4 more epochs than relu
#'relu'
# has the tendency to produce such probabilities:
# white.png - 0.00% black : 100.00% white
# black.png - 51.10% black : 48.90% white
# requires roughly 50 epochs and slight luck
'tanh'
# easily adjusts under 10 epochs
# produces reasonable divided probabilites
)
HEIGHT, WIDTH = 20, 20
dataset = keras.utils.image_dataset_from_directory(
"dataset/",
image_size=(HEIGHT, WIDTH),
)
model = keras.Sequential([
keras.layers.Flatten(),
keras.layers.Dense(8, activation=my_activation),
keras.layers.Dense(8, activation=my_activation),
keras.layers.Dense(1, activation='sigmoid')
])
model.compile(
'adam',
loss='binary_crossentropy',
metrics=['accuracy']
)
model.fit(dataset, epochs=10)
img = keras.preprocessing.image.load_img(argv[1], target_size=(HEIGHT, WIDTH))
img = keras.utils.img_to_array(img)
img = keras.ops.expand_dims(img, 0)
score = model.predict(img)[0][0]
print(f"{100 * (1 - score):.2f}% black : {100 * score:.2f}% white")

@ -0,0 +1,6 @@
#!/bin/bash
mkdir dataset
mkdir dataset/white
mkdir dataset/black
convert -size 100x100 xc:white dataset/white/white.png
convert -size 100x100 xc:black dataset/black/black.png