All projects

Animal Drawing Classifier

A Java Swing app that recognizes hand-drawn animals using only classic techniques — HSV colour histograms, flood-fill shape descriptors, PCA and a from-scratch neural network trained with backpropagation, one network per class.

Java Machine LearningNeural NetworksJava

The problem

Extra project for the Advanced Algorithms course at the Universitat de les Illes Balears.

Recognise hand-drawn animals with no machine-learning library and no convolutional network — every piece, down to backpropagation itself, written from scratch. That rules out letting a network learn its own features, so the features have to be designed by hand: what separates a dalmatian from a zebra in a drawing is the shape of the markings, not the colours, and nothing in the pipeline will discover that on its own.

The classifier is a one-vs-all ensemble. Each class gets its own network answering a single yes/no question — “does this drawing belong to my class?” — with an output in (0, 1) read as a similarity, and the predicted class is simply the network with the highest output.

How it works

  • Feature extraction — transparent pixels are dropped and the drawing is summarized into a feature vector combining an HSV nine-category colour histogram with flood-fill shape descriptors (region count, mean region size, elongation) that tell a dalmatian’s many small spots from a zebra’s few long stripes, plus edge and Hu-moment descriptors that capture structure the colours alone miss.
  • Dimensionality reduction — the standardized features are projected with a from-scratch PCA (covariance matrix and principal components), and the two leading components drive the scatter plot in the Metrics tab.
  • Training — every network is a single-hidden-layer perceptron with bias terms and a sigmoid output, trained by backpropagation with stochastic epoch shuffling and configurable mini-batches. Training uses Monte-Carlo random restarts — several attempts from different random initialisations, keeping the one that reached the lowest error — and is deterministic, so sequential and parallel runs produce identical weights and only the wall-clock time differs.
  • Classification — a new drawing is standardized, fed forward through every network, and labelled with the winning class. An open-set novelty check measures the input’s distance to the predicted class centroid against the class radius, so a drawing that belongs to none of the trained classes is flagged as unknown instead of forced into a label.
  • Evaluation — a train/test split and k-fold cross-validation report accuracy, precision and recall together with an interactive confusion matrix; trained models can be saved and reloaded so there is no need to retrain on every launch.

The interface

The window organizes the work into four tabs:

  • Train — pick a dataset style and its classes, set the hyperparameters (hidden neurons, learning rate, batch size, error threshold, max epochs, Monte-Carlo restarts, hidden activation), and train. One network per class is trained in parallel, one per core, while the error curve, the network diagram and the concurrency telemetry update live.
  • Metrics — inspect what the features are doing: the 2D PCA scatter of the two leading components coloured by class, per-feature importance, and the feature correlation heatmap.
  • Test — classify a single drawing and show the per-class similarity bars, the foreground overlay, the colour histogram and the raw feature vector; or generate an animated scene of moving drawings, classify every frame and export it as a GIF.
  • Benchmark — sweep a parameter (dataset size, hidden neurons, learning rate…), compare error curves and accuracy, rank the runs on a Pareto front, and extrapolate the training cost of larger problems with Newton’s divided-difference interpolation.

Running it

Requires a JDK 17 or newer — plain Java with Swing, no external dependencies or build tool.

  1. Clone the repository and open the MH/ folder in IntelliJ IDEA.
  2. Run animalclassifier.Main. The dataset/ and presets/ folders sit next to MH/, so they resolve automatically from the module directory.

Four bundled datasets are ready to try after cloning (cartoon-flat, line-art, realista and a frutas set), and the system is generic per class: adding a new animal just means adding a folder of training images.

To run it from the command line instead, compile and launch from the repository root so the relative paths resolve:

javac -d out $(find MH/src -name "*.java")
java -cp out animalclassifier.Main