NodeJS tensorflow.js:无法加载保存在tfjs中的模型

iswrvxsc  于 2023-02-21  发布在  Node.js
关注(0)|答案(1)|浏览(136)

我无法将使用@tensorflow/tfjs-node创建的顺序模型加载回node。

import * as tf from '@tensorflow/tfjs-node';
      
model = tf.sequential();

// Add a single input layer
model.add(
   tf.layers.dense({ inputShape: [1.0], units: 1, useBias: true })
);

model.add(
   tf.layers.dense({
      inputShape: [1],
      units: 10,
      activation: 'relu',
      kernelRegularizer: tf.regularizers.l2({ l2: 0.01 }),
      useBias: true
   })
);

// Add additional hidden layers with L2 regularization
model.add(
   tf.layers.dense({
      units: 8,
      activation: 'relu',
      kernelRegularizer: tf.regularizers.l2({ l2: 0.01 }),
      useBias: true
   })
);

model.add(
   tf.layers.dense({
      units: 6,
      activation: 'relu',
      kernelRegularizer: tf.regularizers.l2({ l2: 0.01 }),
      useBias: true
   })
);

// Add an output layer with linear activation
model.add(
   tf.layers.dense({ units: 1, activation: 'linear', useBias: true })
);

// Add an output layer
model.add(tf.layers.dense({ units: 1, useBias: true }));

我是这样保存模型的(目前有点粗糙,但看起来可以用其他方法):

const createSavableModel = async () => {
   await model.save(tf.io.withSaveHandler(artifacts => {
      modelArtifacts = serialize.serialize(artifacts);
      return Promise.resolve({
         modelArtifactsInfo: {
            dateSaved: new Date()
         } as any
      });
   }));
};

我把modelArtifacts藏在MongoDB里。
然后,在尝试加载模型时,如下所示:

model = await tf.loadLayersModel(tf.io.fromMemory(existingModelArtifacts));

我得到一个很长的错误:
错误:未知层:{"模型拓扑":{"类名称..."
1.该层是用Python定义的,在这种情况下,需要将其移植到TensorFlow.js或JavaScript代码。
1.自定义层是在JavaScript中定义的,但未使用tf. serialization. registerClass()正确注册。
现在,模型完全是在tensorflow.js中创建的,错误并没有真正告诉我它对哪一层不满意。
可能是什么原因造成的?

编辑

我已减少模型层:

model = tf.sequential();

// Add a single input layer
model.add(
   tf.layers.dense({ inputShape: [1.0], units: 1, useBias: true })
);

model.add(tf.layers.dense({ units: 1, useBias: true }));

这是它的拓扑结构

{
  "class_name": "Sequential",
  "config": {
    "name": "sequential_1",
    "layers": [
      {
        "class_name": "Dense",
        "config": {
          "units": 1,
          "activation": "linear",
          "use_bias": true,
          "kernel_initializer": {
            "class_name": "VarianceScaling",
            "config": {
              "scale": 1,
              "mode": "fan_avg",
              "distribution": "normal",
              "seed": null
            }
          },
          "bias_initializer": {
            "class_name": "Zeros",
            "config": {}
          },
          "kernel_regularizer": null,
          "bias_regularizer": null,
          "activity_regularizer": null,
          "kernel_constraint": null,
          "bias_constraint": null,
          "name": "dense_Dense1",
          "trainable": true,
          "batch_input_shape": [
            null,
            1
          ],
          "dtype": "float32"
        }
      },
      {
        "class_name": "Dense",
        "config": {
          "units": 1,
          "activation": "linear",
          "use_bias": true,
          "kernel_initializer": {
            "class_name": "VarianceScaling",
            "config": {
              "scale": 1,
              "mode": "fan_avg",
              "distribution": "normal",
              "seed": null
            }
          },
          "bias_initializer": {
            "class_name": "Zeros",
            "config": {}
          },
          "kernel_regularizer": null,
          "bias_regularizer": null,
          "activity_regularizer": null,
          "kernel_constraint": null,
          "bias_constraint": null,
          "name": "dense_Dense2",
          "trainable": true
        }
      }
    ]
  },
  "keras_version": "tfjs-layers 4.2.0",
  "backend": "tensor_flow.js"
}

那么,为什么我不能用tf.loadLayersModel(tf.io.fromMemory(existingModelArtifacts))加载它呢?

zed5wv10

zed5wv101#

好的,我决定通过文件系统:

const createArchive = async (sourceDir: string): Promise<Buffer> => {
   const archive = archiver('zip', { zlib: { level: 9 } });
   const chunks: Buffer[] = [];

   return new Promise<Buffer>((resolve, reject) => {
      archive.on('data', (data) => {
         chunks.push(data);
      });

      archive.on('end', () => {
         resolve(Buffer.concat(chunks));
      });

      archive.directory(sourceDir, false);
      archive.finalize();
   });
};

const extractBuffer = (buffer: Buffer, destinationDir: string): Promise<void> => {
   return new Promise<void>((resolve, reject) => {
      const extractor = unzipper.Extract({ path: destinationDir });

      extractor.on('error', (err) => {
         reject(`Error extracting archive: ${err.message}`);
      });

      extractor.on('close', () => {
         console.log(`Archive extracted to: ${destinationDir}`);
         resolve();
      });

      extractor.write(buffer);
      extractor.end();
   });
};

const modelToBuffer = async (model: any) => {
   const modelSaveTempDir = await mkdtemp(path.join(os.tmpdir(), `tf-${pairName}`));
   await model.save(`file://${modelSaveTempDir}`);

   return createArchive(modelSaveTempDir);
};

const bufferToModel = async (buffer: Buffer): Promise<any> => {
   const modelTempDir = await mkdtemp(path.join(os.tmpdir(), `tf-${pairName}`));
   await extractBuffer(buffer, modelTempDir);
   return await tf.loadLayersModel(`file://${modelTempDir}/model.json`);
};

相关问题