How knetic0/mnist-live-learning works
Project type and purpose
• This is a lightweight full-stack web application for on-the-fly training and prediction of MNIST digits.
• The front end is a browser UI (canvas + basic controls) built with HTML/JS/CSS.
• The backend is a Node.js/Express server that handles two REST endpoints (/train, /predict) and uses TensorFlow.js (in train.js) to build, train, predict, and persist a CNN model.
File structure
• index.js – Express server setup, routes and middleware.
• train.js – Model architecture definition (CNN layers), train & predict functions, model load/save logic.
• public/ – Front-end assets: index.html (UI), app.js (canvas logic, mode toggling, API calls), styles.css.
• model/ (auto-created) – Filesystem persistence for the TensorFlow.js model.
• package.json / package-lock.json – Dependency and script definitions.
• .env (via .env.example) – Configuration for PORT and MODEL_DIR.
• Scripts: npm start / npm run dev (nodemon).
README for insights
• Two main UX flows: Training Mode (POST /train with image+label) and Prediction Mode (POST /predict with image).
• Automatic model persistence every hour and at shutdown.
• TensorFlow.js used both in browser (for canvas preprocessing?) and on server.
Main components & interactions
a. Front End
– Browser Canvas UI
– app.js handles user input, mode selection, HTTP calls.
b. API Layer (Express server)
– index.js sets up /train and /predict endpoints
– Receives multipart/form-data (image blob, label)
c. ML Engine (train.js)
– Defines CNN model
– Exposes trainSample() and predictSample()
– On train request: preprocess image → call model.fit()
– On predict request: preprocess → call model.predict()
d. Persistent Storage
– model directory on disk (MODEL_DIR)
– loadModel() at startup if exists
– saveModel() hourly and on shutdown
e. External/Infrastructure
– Environment variables (PORT, MODEL_DIR)
– Node.js runtime
– npm modules (Express, TensorFlow.js)
Browser UI → HTTP POST /train → Express → train.js.trainSample → TensorFlow.js → Model in memory → save to disk
Browser UI → HTTP POST /predict → Express → train.js.predictSample → TensorFlow.js → JSON response → Browser UI
Architectural patterns & principles
• Monolithic two-tier design (client/server).
• Separation of concerns:
– UI rendering & event handling (public/)
– HTTP routing & middleware (index.js)
– ML logic & persistence (train.js + model/).
• RESTful endpoints for decoupled client interactions.
• File-based model persistence (no external database).