{"id":22680525,"url":"https://github.com/andrehrferreira/node-tfjs-image-classifier","last_synced_at":"2025-03-29T13:44:28.138Z","repository":{"id":135824986,"uuid":"268474469","full_name":"andrehrferreira/node-tfjs-image-classifier","owner":"andrehrferreira","description":"Study project to create data set for Tensorflow using Node.js","archived":false,"fork":false,"pushed_at":"2024-08-27T20:40:31.000Z","size":1980,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-04T14:46:34.590Z","etag":null,"topics":["javascript","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andrehrferreira.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-01T09:07:52.000Z","updated_at":"2024-10-11T23:15:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"1515d653-25e4-46f4-aef3-d798d3d7cb0c","html_url":"https://github.com/andrehrferreira/node-tfjs-image-classifier","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrehrferreira%2Fnode-tfjs-image-classifier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrehrferreira%2Fnode-tfjs-image-classifier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrehrferreira%2Fnode-tfjs-image-classifier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrehrferreira%2Fnode-tfjs-image-classifier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andrehrferreira","download_url":"https://codeload.github.com/andrehrferreira/node-tfjs-image-classifier/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246193156,"owners_count":20738450,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","tensorflow"],"created_at":"2024-12-09T19:13:57.032Z","updated_at":"2025-03-29T13:44:28.112Z","avatar_url":"https://github.com/andrehrferreira.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# node-tfjs-image-classifier\n\nStudy project to create data set for Tensorflow using Node.js\n\nWhat does this plugin do?\n\n* Creating binary image blocks (yarn filestodata)\n* Creation of a training model on the VGG16, MNIST, CIFAR10 models\n* Importing Keras models\n* Model training\n* Prediction with pre-trained base\n\n## Instalation\n\n```bash\n$ yarn add node-tfjs-image-classifier --save\n```\n\n## Downloading test files\n\nTo perform the tests of the module used in the Stanford Dogs Dataset available at http://vision.stanford.edu/aditya86/ImageNetDogs/, this base was collected and organized by Aditya Khosla, Nityananda Jayadevaprakash, Bangpeng Yao and Li Fei-Fei at Stanford University. The base has 20.580 dog files classified in 120 categories, ~ 150 images per category.\n\nThe categories are also organized in the ImageNet (http://www.image-net.org/) standard, making it possible to increase pre-trained models.\n\n## Rebuild the native addon module\n\nSo that it is possible to make use of Tensorflow in Nodejs it will be necessary to rebuild the core to add the module, to do so just execute the command below:\n\n```bash\n$ npm rebuild @tensorflow/tfjs-node build-addon-from-source\n```\n\n## Files to binary block \n\nWhen there are many class files for training I recommend turning the base into binary blocks that will be treated and compressed, these blocks can be used in training models and their loading is much more efficient than mapping files.\n\n```bash\n$ yarn filestodata\n```\n\nIf you want to customize the way these files are generated below, follow the explanation of how the conversion process works.\n\n```javascript\nimport \"@babel/polyfill/noConflict\";\n\nimport path from \"path\";\nimport { Model, Data } from \"../src/index\";\n\nconst argv = require(\"minimist\")(process.argv.slice(2));\nconst options = require(`./options/${argv.options || \"default\"}`); //default, cifar10, vgg16, mnist\n\n(async () =\u003e {\n    try{    \n        let data = new Data(options); //Creating dataset\n        console.log(\"Loading files...\");\n        await data.loadFromDirectory(path.resolve(\"./sample/train\")); //Loading files for conversion, where the name of the directories will be the label\n        let model = new Model(options); //Creating model class\n    \n        await model.input(data); // Inputting data for conversion\n        await model.generate(); // Generating the model according to the settings\n\n        console.log(\"Saving data training...\");\n\n        //Converting and saving binary data based on the model\n        await data.saveTrainingData(model.model, path.resolve(\"./sample/data/data.bin\"), path.resolve(\"./sample/data/labels.json\"));\n    }\n    catch(err){\n        console.log(err);\n    }\n})();\n```\n\nAs previously mentioned, the data can be used for training any model, obviously if the data was converted into a model whose image size setting is smaller than the training model, there may be data loss when resizing, so I recommend using the model VGG16 for creating binary files, so all images will have the standard size of 224x224.\n\n## Loading binary data for training\n\nInside the example folder there is a functional script that loads binary data and performs training with model options for training, to use the script just run the command below.\n\n```bash\n$ yarn trainfromdata --options=default\n```\n\nRemembering that the module has support for VGG16, MNIST and CIFAR10. Below is the commented code:\n\n```javascript\nimport \"@babel/polyfill/noConflict\";\n\nimport path from \"path\";\nimport { Model, Data } from \"../src/index\";\n\nconst argv = require(\"minimist\")(process.argv.slice(2));\nconst options = require(`./options/${argv.options || \"default\"}`);\n\n(async () =\u003e {\n    try{    \n        let data = new Data(options); //Creating dataset\n        await data.loadFromData(path.resolve(\"./sample/data/data.bin\"), path.resolve(\"./sample/data/labels.json\")); //Loading binary files and labels\n        let model = new Model(options); //Creating model class\n    \n        await model.input(data); // Inputting data for conversion\n        await model.generate(); // Generating the model according to the settings   \n        let trainResult = await model.train(); // Model training\n\n        console.log(\"Training Complete!\");\n        let losses = trainResult.history.loss;\n        console.log(`Final Loss: ${Number(losses[losses.length - 1]).toFixed(5)}`);\n\n        await model.save(path.resolve(\"./sample/model\")); //Saving pre training model for later use\n    }\n    catch(err){\n        console.log(err);\n    }\n})();\n```\n\n## Training model using files directly\n\nIt is possible to train the model without the need to create binary files just run the command below\n\n```bash\n$ yarn trainfromfiles --options=default\n```\n\n## Create Keras model with Python\n\nThe recommendation for creating more complex models using Keras and using Phyton and not Javascript, for installation follows a tutorial link for Ubuntu 18.04: https://www.pyimagesearch.com/2019/01/30/ubuntu-18-04-install-tensorflow-and-keras-for-deep-learning/. An example of a keras model is available in the /python directory.\n\n## Prediction using pre-trained model\n\nAfter creating the model to perform prediction, just use the script below:\n\n```javascript\nimport \"@babel/polyfill/noConflict\";\n\nimport path from \"path\";\nimport fg from \"fast-glob\";\n\nimport { Model, Data } from \"../src/index\";\n\nconst argv = require(\"minimist\")(process.argv.slice(2));\nconst options = require(`./options/${argv.options || \"default\"}`);\n\n(async () =\u003e {\n    try{\n        let model = new Model(options);\n        await model.load(path.resolve(\"./sample/model\")); // Loading pre-trained model\n\n        let files = await fg(`${path.resolve(\"./sample/validate\")}/*.jpg`); // Loading validation file\n        files.sort(() =\u003e { return 0.5 - Math.random(); }); // Shuffle\n\n        for(let key in files){\n            let result = await model.predictFromFile(files[key]); // Direct prediction by file\n            console.log(path.basename(files[key]), result); // Displaying prediction result\n        }\n    }\n    catch(err){\n        console.log(err);\n    }\n})();\n```\n\n## Prediction using Keras models\n\n```javascript\nimport \"@babel/polyfill/noConflict\";\n\nimport path from \"path\";\nimport fg from \"fast-glob\";\n\nimport { Model, Data } from \"../src/index\";\n\n(async () =\u003e {\n    try{\n        let model = new Model({ type: \"keras\" });\n        await model.load(path.resolve(\"./sample/model-keras.bin\")); // Loading pre-trained model  \n\n        let files = await fg(`${path.resolve(\"./sample/validate\")}/*.jpg`); // Loading validation file\n        files.sort(() =\u003e { return 0.5 - Math.random(); }); // Shuffle\n\n        for(let key in files){\n            let result = await model.predictFromFile(files[key]); // Direct prediction by file\n            console.log(path.basename(files[key]), result); // Displaying prediction result\n        }\n    }\n    catch(err){\n        console.log(err);\n    }\n})();\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrehrferreira%2Fnode-tfjs-image-classifier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrehrferreira%2Fnode-tfjs-image-classifier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrehrferreira%2Fnode-tfjs-image-classifier/lists"}