From 8636aef2691b6e32442f24644bf53436140c6db2 Mon Sep 17 00:00:00 2001 From: anon Date: Tue, 10 Dec 2024 20:35:11 +0100 Subject: [PATCH] +basic image recognition --- .../ai/basic_image_classification/is-black.py | 48 +++++++++++++++++++ .../ai/basic_image_classification/mkdata.sh | 6 +++ 2 files changed, 54 insertions(+) create mode 100644 Python/ai/basic_image_classification/is-black.py create mode 100755 Python/ai/basic_image_classification/mkdata.sh diff --git a/Python/ai/basic_image_classification/is-black.py b/Python/ai/basic_image_classification/is-black.py new file mode 100644 index 0000000..ee97804 --- /dev/null +++ b/Python/ai/basic_image_classification/is-black.py @@ -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") diff --git a/Python/ai/basic_image_classification/mkdata.sh b/Python/ai/basic_image_classification/mkdata.sh new file mode 100755 index 0000000..74a1b37 --- /dev/null +++ b/Python/ai/basic_image_classification/mkdata.sh @@ -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