Articles
Software
Gallery
Other works
Contacts

#Basic image classification example.html



        -set up all the data we need:
            >one black image
            >one white image
        {@begin=sh@
            #!/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
        @end=sh@}
        >make our model
        {@begin=python@
            #!/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
                # which is not surprising considering its inherent asymmetry
                # 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([
                # normally we would use convolutional layers
                #  before flattening and adding a few dense layers,
                #  however the example is so simple we dont care about spacial information
                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")
        @end=python@}