All projects
Java

Animal Drawing Classifier

A Java Swing app that recognises 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.

View on GitHub
Machine Learning Neural Networks Java

Animal Drawing Classifier

Final project for the Advanced Algorithms course at the Universitat de les Illes Balears. A Java Swing application that recognises hand-drawn animals from their pictures using only classic techniques: HSV colour histograms, flood-fill shape descriptors, principal component analysis and one small neural network per class, all trained with backpropagation. The whole neural network is written from scratch — no machine-learning libraries and no convolutional networks — and every stage is exposed in the GUI, from loading the dataset to training, evaluating, classifying and benchmarking.

The application

The window organises 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.

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.

About

The classifier is a one-vs-all ensemble. Each class gets its own network that answers 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. The pipeline runs in stages:

  • Feature extraction — transparent pixels are dropped and the drawing is summarised 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 standardised 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 standardised, 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.

Executing the project

Requires a JDK 17 or newer. The project is plain Java with Swing and has 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.

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