Image reconstruction experiments using a small header-only multilayer perceptron (MLP).
The project provides two programs:
src/live.cpp— live reconstruction viewer using Raylib.src/file.cpp— offline training that saves reconstruction outputs as PNG files.
Shared model, activation, image, and training logic lives in include/.
mkdir bin
g++ src/live.cpp -std=c++17 -O3 -march=native -o bin/live.exe -lraylibFor abstract or wallpaper-style results, do not use Fourier encoding or coordinate shuffling:
.\bin\live.exe --image .\images\<image-name>.png --params parameters\wallpaper.txt --rate 0.01For a closer reconstruction of the original image, use Fourier encoding and shuffle the coordinates:
.\bin\live.exe --image .\images\<image-name>.png --params parameters\exact.txt --fourier 6 --shuffle --rate 0.01Replace <image-name> with the name of an image stored in images/.
Visual-Experiments/
├── images/ # Input images
├── include/ # Shared model, activation, and image helpers
├── parameters/ # Network configuration files
├── src/
│ ├── file.cpp # Offline reconstruction
│ └── live.cpp # Live Raylib viewer
└── bin/ # Compiled executables
- C++17-compatible compiler
- Raylib
stb_image.hstb_image_write.h
Install GCC and Raylib:
pacman -S --needed mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-raylibThe live viewer links against Raylib using:
-lraylib
If your installation requires different linker flags, adjust the build command for your toolchain.
The project uses:
stb_image.hfor loading images.stb_image_write.hfor writing PNG files.
The source expects the headers to be available as:
#include <stb/stb_image.h>
#include <stb/stb_image_write.h>A typical directory layout is:
third_party/
└── stb/
├── stb_image.h
└── stb_image_write.h
If stb is stored locally under third_party/, add it to the compiler include path:
-Ithird_partyFor example:
g++ src/live.cpp -std=c++17 -O3 -march=native -Ithird_party -o bin/live.exe -lraylibCreate the output directory:
mkdir bing++ src/live.cpp -std=c++17 -O3 -march=native -o bin/live.exe -lraylibg++ src/file.cpp -std=c++17 -O3 -march=native -o bin/app.exe -lraylibAdd any required include or linker flags depending on where Raylib and stb are installed.
Wallpaper mode is intended for more abstract, stylized, or smooth reconstructions.
Recommended configuration:
- Use
parameters/wallpaper.txt. - Do not use
--fourier. - Do not use
--shuffle. - A lower learning rate, such as
0.01, can produce more colorful results.
.\bin\live.exe --image .\images\<image-name>.png --params parameters\wallpaper.txt --rate 0.01.\bin\app.exe --image .\images\<image-name>.png --outdir output --epochs 20 --params parameters\wallpaper.txt --rate 0.01Exact reconstruction is intended to reproduce the source image more closely.
Recommended configuration:
- Use
parameters/exact.txt. - Use
--fourier 6. - Use
--shuffle.
.\bin\live.exe --image .\images\<image-name>.png --params parameters\exact.txt --fourier 6 --shuffle --rate 0.01.\bin\app.exe --image .\images\<image-name>.png --outdir output --epochs 20 --params parameters\exact.txt --fourier 6 --shuffle --rate 0.01| Flag | Description |
|---|---|
--image |
Path to the input image |
--outdir |
Output directory for generated PNG files |
--epochs |
Number of training epochs |
--fourier |
Number of Fourier bands used for positional encoding |
--shuffle |
Shuffle samples before training |
--rate |
Learning rate |
--params |
Path to a network configuration file |
Some options are specific to one executable. For example, --outdir is used by the offline reconstruction program.
Files in parameters/ define the network architecture and activation functions.
Hidden layers use the format:
<width> <activation>
The final line defines the output activation.
64 sin
32 sin
16 sin
8 sin
sigmoid
Some patterns observed during reconstruction experiments:
- Wallpaper-style results: avoid Fourier encoding and coordinate shuffling.
- Exact reconstruction: use coordinate shuffling with around
6Fourier bands. - Lower learning rates tended to produce more colorful outputs.
- Funnel-shaped networks often worked better than constant-width networks.
sinperformed well for some wallpaper-style reconstructions.- A
sigmoidoutput activation keeps output values in the[0, 1]range. - Increasing the number of Fourier bands beyond
6often produced worse results.
These are experimental observations rather than fixed rules. Results vary depending on the input image, network architecture, and training configuration.
- Some of the code in
mlp.hppwas optimised by using ChatGPT to vectorise matrices resulting in better performance