Error in loading the model.json in web application - node.js

I am trying to load the model.json file (converted from python) and it is having the bin files as well. I have imported tfjs-node and tfjs but still getting the error as \atrbtsjsonmodel.json does not exist: loading failed.
Can someone help?
This is the code I have written -
const tf = require ('#tensorflow/tfjs')
const tfn = require ('#tensorflow/tfjs-node')
async function loadModel(){
const handler = tfn.io.fileSystem('atrbts\json\model.json');
const model = await tf.loadLayersModel(handler);
console.log("Model loaded")
}

Related

How to import a JSON object from an AWS Lambda Layer?

This may seem simple but I have had a hard time trying to figure it out. I could not seem to find the solution on the web too.
//nodejs/models.js:
// City = ...
// ...
module.exports = {City, Country, Coupon, Player, Pollfish, Tree};
After zipping nodejs, I uploaded the zip file as an AWS Layer and added the layer to my Lambda function.
When I tried to retrieve the objects in my Lambda function:
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const {Player} = require('./models.js');
It resulted in an error:
2023-01-06T09:19:49.469Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'models.js'\nRequire stack:\n- /var/task/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'models.js'","Require stack:","- /var/task/index.mjs"," at _loadUserApp (file:///var/runtime/index.mjs:1000:17)"," at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1035:21)"," at async start (file:///var/runtime/index.mjs:1200:23)"," at async file:///var/runtime/index.mjs:1206:1"]}
So what should be the proper way to do it?
I got it! It should be:
const {Player} = require('/opt/nodejs/models.js');

could not import mongoose model properly from custom module to main app

Issue is that I am not able to import a piece of my custom module model0.js in my main application app.js.
const connect_db = require("./connect_db.js");
const mongoose_schema = require("./mongoose_schema.js");
const {model} = require("mongoose");
exports.test_model = connect_db.db0.model('test', mongoose_schema.testSchema);
Inside app.js I first import module using const model0 = require("./model0.js") and then I try to assign test_model model from model0 but it does not work const test_model = model0.new_user_model;
However, when I export function as follows:
exports.new_user_model = function (){
return all_db_connections.account_info.model('registered', all_mongoose_schema.registeredSchema);
}
Then, code works as expected on changing app.js code to const test_model = all_models.new_user_model;
MY ASSUMPTION:
I assume this issue happens because without function code executes as soon as module is imported.
PROBLEM:
If it is as i assumed above then I want to know what is proper way to write code in custom module so that it does not executes as soon as module is imported.

Tensorflow JS : Error: Invalid TF_Status: 3 Message: In[0] and In[1] has different ndims: [1,8,8,64,2] vs. [2,1]

I converted a saved model into Tensorflow JS & was trying to get results from it. I was using NodeJS to load the model.json file. I created a endpoint for it and was sending a image url to it. Here is what I did :
const tf = require("#tensorflow/tfjs-node");
var model;
loadModel();
async function loadModel() {
model = await tf.loadGraphModel('https://LinkTo/YourModel/model.json');
console.log("Model Loading Done!!")
}
async function detectcellPhone(imgurl) {
const imgTensor = tf.node.decodeImage(new Uint8Array(fs.readFileSync(imgurl)), 3);
const predictions = await model.executeAsync(imgTensor.expandDims(0));
return predictions;
}
I was able to load the model successfully but when I was trying to get predictions from it, I encountered the following error :
Error: Invalid TF_Status: 3 Message: In[0] and In[1] has different ndims: [1,8,8,64,2] vs. [2,1]
I have answered my own questions below.
After spending a few hours to figure out about the error, I realized that the version of #tensorflow/tfjs-node was 3.13.0. After updating it to the latest version of 3.13.19 the error disappeared. May not be big deal, but I thought I should post it so that others can benefit from this.

fs.existsSync is not a function in Nuxt JS + Lowdb

What I need:
Read a local JSON file inside NuxtJS as the page loads. So I can parse it into a prop within <option> tag.
What I have:
Lowdb (installed as dependency — to read the JSON file) inside a component, with this code:
computed: {
resultFetchCamera: function() {
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('db.json');
const db = low(adapter);
let value = db.get('Size').map('Name').value();
return value;
}
}
}
I got an error This dependency was not found: * fs in ./node_modules/lowdb/adapters/FileSync.js. Fixed with this solution. Which leads me to another error: TypeError: fs.existsSync is not a function. This solution helps out a bit but it also leads to other errors: TypeError: window.require is not a function and TypeError: FileSync is not a constructor. So, I undo the last solution and get back with the fs.existsSync error.
The question:
How to fix the fs.existsSync error (in a NuxtJS environment)?
Did I implement Lowdb to NuxtJS correctly?

What does require('..') mean?

I am new to node.js and have been trying to test some code using yarn. At the moment I am using the following code:
const assert = require('assert')
const username = process.env.HDFS_USERNAME || 'webuser'
const endpoint1 = process.env.HDFS_NAMENODE_1 || 'namenode1.lan'
const endpoint2 = process.env.HDFS_NAMENODE_2 || 'namenode2.lan'
const homeDir = `/user/${username}`
const basePath = process.env.HDFS_BASE_PATH || homeDir
const nodeOneBase = `http://${endpoint1}:9870`
const nodeTwoBase = `http://${endpoint2}:9870`
const webhdfs = require('..')
const should = require('should')
const nock = require('nock')
describe('WebHDFSClient', function () {
const oneNodeClient = new (require('..')).WebHDFSClient({
namenode_host: endpoint1
});
})
that I've got from this repo:
https://github.com/ryancole/node-webhdfs/blob/master/test/webhdfs.js
and when I try to run yarn test I get the following error:
Cannot find module '..'
Require stack:
- myrepo/test/lib/hdfs.js
- myrepo/test/tests.js
- myrepo/node_modules/mocha/lib/mocha.js
- myrepo/node_modules/mocha/index.js
- myrepo/node_modules/mocha/bin/_mocha
Error: Cannot find module '..'
As you can see, require('..') is used couple of times in the code and I can not figure out what it means. I've found posts on require('../') which I think is not exactly the same as this one.
The inbuilt node.js require function uses a quite complex package resolution algorithm. So there are lots of things that may influence it.
A. Defaulting to index.js
node.js implicitly requires a file named index.js if no file name is specified.
So require("..") translates to require("../index.js")
B. The main package property.
If you require is inside a module and points to the root of the module it will read the main property from the packages package.json and require the file specified there.
So given this package definition (package.json)
{
"main": "./fileA.js"
}
The call to require("..") will be translated to require("../fileA.js")
Resources
Good explanatory blog entry
Official Docs

Resources