This repo documents my exciting journey into learning machine learning practices for the web. It is featured in the talk of the same name at Devoxx UK 2024 and HalfStack London 2024.
You can play the game and pit yourself against the models at https://is-it-fake.netlify.app/.
This repo contains two applications:
- Is it (F)ake?! game, located in the
cake-appfolder where players can guess if the image is cake or not cake. This application is written using React, Elasticsearch Node.js client and Netlify functions. - The Model Classification application is written in vanilla JavaScript, HTML and CSS. It is present in the
model-playgroundfolder and contains several useful elements:
- A sample webpage showing predictions generated using the COCO-SD and MobileNet model on a sample image.
- The
classify-images.jsscript includes a Node.js implementation generating image classifications of a set of source images for MobileNet and COCO-SSD. These results are persisted in Elasticsearch. - A custom sequential image classification model is created and executed in
custom-model.js. These results are also persisted in Elasticsearch, and the model is saved in themodelfolder. - The script
transfer-learning.jsincludes an example approach combining MobileNet with an additional classification head to determine the presence of cake in our image set. Just like our other scripts, the generated classifications are persisted in Elasticsearch.
See below for how to run the applications and tests.
You need an Elasticsearch cluster to pull the data from. Access is granted using environment variables ELASTIC_URL and ELASTIC_API_KEY, as per the .env-example file at the top of the project:
ELASTIC_URL=https://my-elastic-deployment:port
ELASTIC_API_KEY=my-api-keyStart the application on http://localhost:5173/, ensuring you have Netlify CLI installed:
cd ./cake-game
npm install
npm install netlify-cli -g
netlify dev
Execute the component tests:
cd ./cake-game
npm install
npm run test-ct
To start the model application on http://localhost:3000:
cd ./model-classification-app
npm install
npm run start
Each of the scripts for performing different types of image classification should be executed from the model-classification-app folder. To persist the classifications please ensure you have a simple Elasticsearch cluster available to persist the results, with environment variables ELASTIC_URL, ELASTIC_API_KEY for Elasticsearch and UNSPLASH_ACCESS_KEY for your Unsplash API key exposed to your service as per the .env-example file as the top of the project.
ELASTIC_URL=https://my-elastic-deployment:port
ELASTIC_API_KEY=my-api-key
UNSPLASH_ACCESS_KEY=my-access-unsplash-keyOnce the Elasticsearch and Unsplash API prerequisite has been met you can run the scripts in turn similar to the below:
cd ./model-classification-app
npm install
# Node 22 required for TensorFlow (as of 2025)
nvm install v22.1.0
nvm use v22.1.0
node scripts/classify-images.js
node scripts/custom-model.js
node scripts/grayscale-custom-model.js
node scripts/transfer-learning.js
Alternatively, to persist your results to an alternative store you need to override the respective functions in elasticsearch-util.js to send your classifications to the right place.
- IBM: What are convolutional neural networks?
- Kaggle: Basic Terminologies of Convolutional Neural Networks
- National High Magnetic Field Laboratory: Convolution Kernels
- Deeplizard: Max Pooling in Convolutional Neural Networks
- Govinda Dumane: Introduction to Convolutional Neural Network (CNN) using Tensorflow
- AI Wiki: Accuracy and Loss
- Classification Model On Custom Dataset Using Tensorflow.js Made Simple: Here's What You Need to Know | Garima Nisha
- Machine Learning for Web Developers (WebML) | Google for Developers
- Training an image model with TensorFlow in Node.js | ????
- TensorFlow.js: Make your own "Teachable Machine" using transfer learning with TensorFlow.js | Google Codelab
- Google: Google AI for JavaScript developers with TensorFlow.js
- CORS Issue with TensorFlow.js
- TensorFlow incorrect tensor shape sample error
- Tensor has disposed
- TensorFlow.js TypeError: Cannot read property 'backend' of undefined
- TensorFlow.js Cannot read property 'backend' of undefined in NodeJS
- TensorFlow.js TypeError: Cannot read property 'backend' of undefined - tensor is already disposed when moveData is called
Thanks go to the fantastic bakers out there making hyper-realistic cakes that inspired this talk and have given me great family memories. Specific thanks go to these bakers whose images I used for training and classification:
- https://www.thelondonbaker.com
- https://hemussweetsensation.com
- https://www.everythingjustbaked.com/
- https://ninamariacharles.com
- https://www.thesugardreams.net
- https://juliemcallistercakes.com
- https://www.thesweetstopofrva.com
- https://apriljulian.com/portfolio
- https://cakeyall.com/
- https://www.layerscakestudio.com/
- https://www.apinchofspirit.com/gallery
- https://www.mycakebuds.com
- https://mannandcobakeshop.com/
- https://www.thesculptedslice.com/
Thanks also to Unsplash and their amazing contributors who allowed me to find non-cake images through the JavaScript wrapper unsplash-js of the Unsplash API.
