first commit

This commit is contained in:
znetsixe
2025-05-14 10:08:34 +02:00
parent 0e5a2b836b
commit 6df8af593d
11 changed files with 4561 additions and 3 deletions

122
dependencies/monster/modelLoader.js vendored Normal file
View File

@@ -0,0 +1,122 @@
const tf = require('@tensorflow/tfjs');
class ModelLoader {
constructor(logger) {
this.logger = logger || console;
this.model = null;
}
async loadModel(modelUrl, inputShape = [null, 24, 166]) {
try {
this.logger.debug(`Fetching model JSON from: ${modelUrl}`);
const response = await fetch(modelUrl);
const modelJSON = await response.json();
// Fix input shape
this.configureInputLayer(modelJSON, inputShape);
// Extract base path
const baseUrl = this.getBaseUrl(modelUrl);
this.fixWeightPaths(modelJSON, baseUrl);
// Ensure weight specs are there
if (
!modelJSON.weightsManifest ||
!modelJSON.weightsManifest[0].weights ||
modelJSON.weightsManifest[0].weights.length === 0
) {
throw new Error("Model JSON is missing weight specifications.");
}
// Load the binary weight data
const weightUrl = modelJSON.weightsManifest[0].paths[0];
const weightResponse = await fetch(weightUrl);
const weightBuffer = await weightResponse.arrayBuffer();
console.log('modelJSON.weightsManifest:', JSON.stringify(modelJSON.weightsManifest, null, 2));
if (
!modelJSON.weightsManifest ||
!modelJSON.weightsManifest[0].weights ||
modelJSON.weightsManifest[0].weights.length === 0
) {
console.error("❌ modelJSON.weightsManifest is missing weight specs!");
} else {
console.log("✅ Weight specs found:", modelJSON.weightsManifest[0].weights.length);
}
// Create ModelArtifacts object
const artifacts = {
modelTopology: modelJSON.modelTopology,
weightSpecs: modelJSON.weightsManifest[0].weights, // ✅ CORRECT FIELD NAME
weightData: weightBuffer
};
// Load from memory
this.model = await tf.loadLayersModel(tf.io.fromMemory(artifacts));
this.logger.debug('Model loaded successfully');
return this.model;
} catch (error) {
this.logger.error(`Failed to load model: ${error.message}`);
throw error;
}
}
configureInputLayer(modelJSON, inputShape) {
const layers = modelJSON.modelTopology.model_config.config.layers;
if (layers && layers.length > 0) {
const firstLayer = layers[0];
if (firstLayer.class_name === 'InputLayer') {
if (firstLayer.config.batch_shape) {
firstLayer.config.batchInputShape = firstLayer.config.batch_shape;
delete firstLayer.config.batch_shape;
this.logger.debug('Converted batch_shape to batchInputShape:', firstLayer);
} else if (!firstLayer.config.batchInputShape && !firstLayer.config.inputShape) {
firstLayer.config.batchInputShape = inputShape;
this.logger.debug('Configured input layer:', firstLayer);
} else {
this.logger.debug('Input shape already set:', firstLayer.config);
}
}
}
}
getBaseUrl(url) {
return url.substring(0, url.lastIndexOf('/') + 1);
}
fixWeightPaths(modelJSON, baseUrl) {
for (const group of modelJSON.weightsManifest) {
group.paths = group.paths.map(path => {
path = path.replace(/^\/+/, '');
return path.startsWith('http') ? path : `${baseUrl}${path}`;
});
}
}
}
const modelLoader = new ModelLoader();
(async () => {
try {
const localURL = "http://localhost:1880/generalFunctions/datasets/lstmData/tfjs_model/model.json";
const model = await modelLoader.loadModel(localURL);
console.log('Model loaded successfully');
const denseLayer = model.getLayer('dense_8');
const weights = denseLayer.getWeights();
const weightArray = await weights[0].array();
console.log('Dense layer kernel (sample):', weightArray.slice(0, 5));
} catch (error) {
console.error('Failed to load model:', error);
}
})();
module.exports = ModelLoader;

256
dependencies/monster/monsterConfig.json vendored Normal file
View File

@@ -0,0 +1,256 @@
{
"general": {
"name": {
"default": "Monster Configuration",
"rules": {
"type": "string",
"description": "A human-readable name or label for this configuration."
}
},
"id": {
"default": null,
"rules": {
"type": "string",
"nullable": true,
"description": "A unique identifier for this configuration. If not provided, defaults to null."
}
},
"unit": {
"default": "unitless",
"rules": {
"type": "string",
"description": "The unit for this configuration (e.g., 'meters', 'seconds', 'unitless')."
}
},
"logging": {
"logLevel": {
"default": "info",
"rules": {
"type": "enum",
"values": [
{
"value": "debug",
"description": "Log messages are printed for debugging purposes."
},
{
"value": "info",
"description": "Informational messages are printed."
},
{
"value": "warn",
"description": "Warning messages are printed."
},
{
"value": "error",
"description": "Error messages are printed."
}
]
}
},
"enabled": {
"default": true,
"rules": {
"type": "boolean",
"description": "Indicates whether logging is active. If true, log messages will be generated."
}
}
}
},
"functionality": {
"softwareType": {
"default": "monster",
"rules": {
"type": "string",
"description": "Specified software type for this configuration."
}
},
"role": {
"default": "samplingCabinet",
"rules": {
"type": "string",
"description": "Indicates the role this configuration plays (e.g., sensor, controller, etc.)."
}
}
},
"asset": {
"uuid": {
"default": null,
"rules": {
"type": "string",
"nullable": true,
"description": "Asset tag number which is a universally unique identifier for this asset. May be null if not assigned."
}
},
"geoLocation": {
"default": {
"x": 0,
"y": 0,
"z": 0
},
"rules": {
"type": "object",
"description": "An object representing the asset's physical coordinates or location.",
"schema": {
"x": {
"default": 0,
"rules": {
"type": "number",
"description": "X coordinate of the asset's location."
}
},
"y": {
"default": 0,
"rules": {
"type": "number",
"description": "Y coordinate of the asset's location."
}
},
"z": {
"default": 0,
"rules": {
"type": "number",
"description": "Z coordinate of the asset's location."
}
}
}
}
},
"supplier": {
"default": "Unknown",
"rules": {
"type": "string",
"description": "The supplier or manufacturer of the asset."
}
},
"type": {
"default": "sensor",
"rules": {
"type": "enum",
"values": [
{
"value": "sensor",
"description": "A device that detects or measures a physical property and responds to it (e.g. temperature sensor)."
}
]
}
},
"subType": {
"default": "pressure",
"rules": {
"type": "string",
"description": "A more specific classification within 'type'. For example, 'pressure' for a pressure sensor."
}
},
"model": {
"default": "Unknown",
"rules": {
"type": "string",
"description": "A user-defined or manufacturer-defined model identifier for the asset."
}
},
"emptyWeightBucket": {
"default": 3,
"rules": {
"type": "number",
"description": "The weight of the empty bucket in kilograms."
}
}
},
"constraints": {
"samplingtime": {
"default": 0,
"rules": {
"type": "number",
"description": "The time interval between sampling events (in seconds) if not using a flow meter."
}
},
"samplingperiod": {
"default": 24,
"rules": {
"type": "number",
"description": "The fixed period in hours in which a composite sample is collected."
}
},
"minVolume": {
"default": 5,
"rules": {
"type": "number",
"min": 5,
"description": "The minimum volume in liters."
}
},
"maxWeight": {
"default": 23,
"rules": {
"type": "number",
"max": 23,
"description": "The maximum weight in kilograms."
}
},
"subSampleVolume": {
"default": 50,
"rules": {
"type": "number",
"min": 50,
"max": 50,
"description": "The volume of each sub-sample in milliliters."
}
},
"storageTemperature": {
"default": {
"min": 1,
"max": 5
},
"rules": {
"type": "object",
"description": "Acceptable storage temperature range for samples in degrees Celsius.",
"schema": {
"min": {
"default": 1,
"rules": {
"type": "number",
"min": 1,
"description": "Minimum acceptable storage temperature in degrees Celsius."
}
},
"max": {
"default": 5,
"rules": {
"type": "number",
"max": 5,
"description": "Maximum acceptable storage temperature in degrees Celsius."
}
}
}
}
},
"flowmeter": {
"default": true,
"rules": {
"type": "boolean",
"description": "Indicates whether a flow meter is used for proportional sampling."
}
},
"closedSystem": {
"default": false,
"rules": {
"type": "boolean",
"description": "Indicates if the sampling system is closed (true) or open (false)."
}
},
"intakeSpeed": {
"default": 0.3,
"rules": {
"type": "number",
"description": "Minimum intake speed in meters per second."
}
},
"intakeDiameter": {
"default": 12,
"rules": {
"type": "number",
"description": "Minimum inner diameter of the intake tubing in millimeters."
}
}
}
}

656
dependencies/monster/monster_class.js vendored Normal file

File diff suppressed because one or more lines are too long