使用TensorFlow.js LoadLayersModel加载预训练的Keras模型

时间:2020-05-12 09:30:47

标签: tensorflow.js

我正在尝试将预先训练的keras模型集成到chrome扩展程序中。

我尝试加载在exports.d.ts:https://storage.googleapis.com/tfjs-models/tfjs/iris_v1/model.json文档中找到的模型,并且工作正常,因此我认为我的模型有些奇怪。

该模型是在Google colab中开发的,并通过

保存
!pip install tensorflowjs
import tensorflowjs as tfjs
tfjs.converters.save_keras_model(model, "/path/to/drive/files")

使用此命令创建了两个文件,一个是model.json文件,另一个是group1-shard1of1.bin文件。我启动了一个http-server -c1 --cors。在我同时拥有两个文件的目录中,并尝试使用

加载模型
import 'babel-polyfill';
import * as tf from '@tensorflow/tfjs';

const MODEL_URL = "http://localhost:8080/model.json";

class DelayClassifier {
  constructor() {
    this.loadModel();
  }
  async loadModel() {
    const model = await tf.loadLayersModel(MODEL_URL);
  };
}

在浏览器中加载扩展程序后,服务器会收到针对它们的GET调用

"GET /model.json"
"GET /group1-shard1of1.bin"

然后出现此错误:

Uncaught (in promise) Error
    at new t (background.js:28482)
    at assert (background.js:28508)
    at t.fromConfig (background.js:31970)
    at deserializeKerasObject (background.js:28590)
    at deserialize (background.js:30720)
    at background.js:33250
    at background.js:28423
    at Object.next (background.js:28435)
    at o (background.js:28324)

const MODEL_URL = "https://storage.googleapis.com/tfjs-models/tfjs/iris_v1/model.json";替换时,不会显示探测。

我的模型包含输入,嵌入,连接和密集层。这是完整的model.json:

{"format": "layers-model", "generatedBy": "keras v2.3.0-tf", "convertedBy": "TensorFlow.js Converter v1.7.4r1", "modelTopology": {"keras_version": "2.3.0-tf", "backend": "tensorflow", "model_config": {"class_name": "Model", "config": {"name": "model_2", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null], "dtype": "float32", "sparse": false, "ragged": false, "name": "carrier_code_input"}, "name": "carrier_code_input", "inbound_nodes": []}, {"class_name": "InputLayer", "config": {"batch_input_shape": [null], "dtype": "float32", "sparse": false, "ragged": false, "name": "day_input"}, "name": "day_input", "inbound_nodes": []}, {"class_name": "InputLayer", "config": {"batch_input_shape": [null], "dtype": "float32", "sparse": false, "ragged": false, "name": "weekday_input"}, "name": "weekday_input", "inbound_nodes": []}, {"class_name": "InputLayer", "config": {"batch_input_shape": [null], "dtype": "float32", "sparse": false, "ragged": false, "name": "month_input"}, "name": "month_input", "inbound_nodes": []}, {"class_name": "InputLayer", "config": {"batch_input_shape": [null], "dtype": "float32", "sparse": false, "ragged": false, "name": "origin_airport_input"}, "name": "origin_airport_input", "inbound_nodes": []}, {"class_name": "InputLayer", "config": {"batch_input_shape": [null], "dtype": "float32", "sparse": false, "ragged": false, "name": "destination_airport_input"}, "name": "destination_airport_input", "inbound_nodes": []}, {"class_name": "Embedding", "config": {"name": "carrier_code_emb", "trainable": true, "batch_input_shape": [null, null], "dtype": "float32", "input_dim": 10, "output_dim": 5, "embeddings_initializer": {"class_name": "RandomNormal", "config": {"mean": 0.0, "stddev": 0.05, "seed": null}}, "embeddings_regularizer": null, "activity_regularizer": null, "embeddings_constraint": null, "mask_zero": false, "input_length": null}, "name": "carrier_code_emb", "inbound_nodes": [[["carrier_code_input", 0, 0, {}]]]}, {"class_name": "Embedding", "config": {"name": "day_emb", "trainable": true, "batch_input_shape": [null, null], "dtype": "float32", "input_dim": 31, "output_dim": 15, "embeddings_initializer": {"class_name": "RandomNormal", "config": {"mean": 0.0, "stddev": 0.05, "seed": null}}, "embeddings_regularizer": null, "activity_regularizer": null, "embeddings_constraint": null, "mask_zero": false, "input_length": null}, "name": "day_emb", "inbound_nodes": [[["day_input", 0, 0, {}]]]}, {"class_name": "Embedding", "config": {"name": "weekday_emb", "trainable": true, "batch_input_shape": [null, null], "dtype": "float32", "input_dim": 7, "output_dim": 3, "embeddings_initializer": {"class_name": "RandomNormal", "config": {"mean": 0.0, "stddev": 0.05, "seed": null}}, "embeddings_regularizer": null, "activity_regularizer": null, "embeddings_constraint": null, "mask_zero": false, "input_length": null}, "name": "weekday_emb", "inbound_nodes": [[["weekday_input", 0, 0, {}]]]}, {"class_name": "Embedding", "config": {"name": "month_emb", "trainable": true, "batch_input_shape": [null, null], "dtype": "float32", "input_dim": 2, "output_dim": 1, "embeddings_initializer": {"class_name": "RandomNormal", "config": {"mean": 0.0, "stddev": 0.05, "seed": null}}, "embeddings_regularizer": null, "activity_regularizer": null, "embeddings_constraint": null, "mask_zero": false, "input_length": null}, "name": "month_emb", "inbound_nodes": [[["month_input", 0, 0, {}]]]}, {"class_name": "Embedding", "config": {"name": "origin_airport_emb", "trainable": true, "batch_input_shape": [null, null], "dtype": "float32", "input_dim": 360, "output_dim": 50, "embeddings_initializer": {"class_name": "RandomNormal", "config": {"mean": 0.0, "stddev": 0.05, "seed": null}}, "embeddings_regularizer": null, "activity_regularizer": null, "embeddings_constraint": null, "mask_zero": false, "input_length": null}, "name": "origin_airport_emb", "inbound_nodes": [[["origin_airport_input", 0, 0, {}]]]}, {"class_name": "Embedding", "config": {"name": "destination_airport_emb", "trainable": true, "batch_input_shape": [null, null], "dtype": "float32", "input_dim": 360, "output_dim": 50, "embeddings_initializer": {"class_name": "RandomNormal", "config": {"mean": 0.0, "stddev": 0.05, "seed": null}}, "embeddings_regularizer": null, "activity_regularizer": null, "embeddings_constraint": null, "mask_zero": false, "input_length": null}, "name": "destination_airport_emb", "inbound_nodes": [[["destination_airport_input", 0, 0, {}]]]}, {"class_name": "Concatenate", "config": {"name": "concatenate", "trainable": true, "dtype": "float32", "axis": -1}, "name": "concatenate", "inbound_nodes": [[["carrier_code_emb", 0, 0, {}], ["day_emb", 0, 0, {}], ["weekday_emb", 0, 0, {}], ["month_emb", 0, 0, {}], ["origin_airport_emb", 0, 0, {}], ["destination_airport_emb", 0, 0, {}]]]}, {"class_name": "InputLayer", "config": {"batch_input_shape": [null, 10], "dtype": "float32", "sparse": false, "ragged": false, "name": "simple_input"}, "name": "simple_input", "inbound_nodes": []}, {"class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 128, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"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", "inbound_nodes": [[["concatenate", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_2", "trainable": true, "dtype": "float32", "units": 8, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"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_2", "inbound_nodes": [[["simple_input", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_1", "trainable": true, "dtype": "float32", "units": 64, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"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_1", "inbound_nodes": [[["dense", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_3", "trainable": true, "dtype": "float32", "units": 4, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"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_3", "inbound_nodes": [[["dense_2", 0, 0, {}]]]}, {"class_name": "Concatenate", "config": {"name": "concatenate_1", "trainable": true, "dtype": "float32", "axis": -1}, "name": "concatenate_1", "inbound_nodes": [[["dense_1", 0, 0, {}], ["dense_3", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_4", "trainable": true, "dtype": "float32", "units": 64, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"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_4", "inbound_nodes": [[["concatenate_1", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_5", "trainable": true, "dtype": "float32", "units": 32, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"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_5", "inbound_nodes": [[["dense_4", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_6", "trainable": true, "dtype": "float32", "units": 16, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"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_6", "inbound_nodes": [[["dense_5", 0, 0, {}]]]}, {"class_name": "Dropout", "config": {"name": "dropout", "trainable": true, "dtype": "float32", "rate": 0.5, "noise_shape": null, "seed": null}, "name": "dropout", "inbound_nodes": [[["dense_6", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_7", "trainable": true, "dtype": "float32", "units": 16, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"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_7", "inbound_nodes": [[["dropout", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_8", "trainable": true, "dtype": "float32", "units": 8, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"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_8", "inbound_nodes": [[["dense_7", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_9", "trainable": true, "dtype": "float32", "units": 4, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"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_9", "inbound_nodes": [[["dense_8", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_10", "trainable": true, "dtype": "float32", "units": 1, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"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_10", "inbound_nodes": [[["dense_9", 0, 0, {}]]]}], "input_layers": [[["carrier_code_input", 0, 0], ["day_input", 0, 0], ["weekday_input", 0, 0], ["month_input", 0, 0], ["origin_airport_input", 0, 0], ["destination_airport_input", 0, 0]], ["simple_input", 0, 0]], "output_layers": [["dense_10", 0, 0]]}}, "training_config": {"loss": "binary_crossentropy", "metrics": ["accuracy"], "weighted_metrics": null, "loss_weights": null, "sample_weight_mode": null, "optimizer_config": {"class_name": "Adam", "config": {"name": "Adam", "learning_rate": 0.0010000000474974513, "decay": 0.0, "beta_1": 0.8999999761581421, "beta_2": 0.9990000128746033, "epsilon": 1e-07, "amsgrad": false}}}}, "weightsManifest": [{"paths": ["group1-shard1of1.bin"], "weights": [{"name": "carrier_code_emb/embeddings", "shape": [10, 5], "dtype": "float32"}, {"name": "day_emb/embeddings", "shape": [31, 15], "dtype": "float32"}, {"name": "dense/kernel", "shape": [124, 128], "dtype": "float32"}, {"name": "dense/bias", "shape": [128], "dtype": "float32"}, {"name": "dense_1/kernel", "shape": [128, 64], "dtype": "float32"}, {"name": "dense_1/bias", "shape": [64], "dtype": "float32"}, {"name": "dense_10/kernel", "shape": [4, 1], "dtype": "float32"}, {"name": "dense_10/bias", "shape": [1], "dtype": "float32"}, {"name": "dense_2/kernel", "shape": [10, 8], "dtype": "float32"}, {"name": "dense_2/bias", "shape": [8], "dtype": "float32"}, {"name": "dense_3/kernel", "shape": [8, 4], "dtype": "float32"}, {"name": "dense_3/bias", "shape": [4], "dtype": "float32"}, {"name": "dense_4/kernel", "shape": [68, 64], "dtype": "float32"}, {"name": "dense_4/bias", "shape": [64], "dtype": "float32"}, {"name": "dense_5/kernel", "shape": [64, 32], "dtype": "float32"}, {"name": "dense_5/bias", "shape": [32], "dtype": "float32"}, {"name": "dense_6/kernel", "shape": [32, 16], "dtype": "float32"}, {"name": "dense_6/bias", "shape": [16], "dtype": "float32"}, {"name": "dense_7/kernel", "shape": [16, 16], "dtype": "float32"}, {"name": "dense_7/bias", "shape": [16], "dtype": "float32"}, {"name": "dense_8/kernel", "shape": [16, 8], "dtype": "float32"}, {"name": "dense_8/bias", "shape": [8], "dtype": "float32"}, {"name": "dense_9/kernel", "shape": [8, 4], "dtype": "float32"}, {"name": "dense_9/bias", "shape": [4], "dtype": "float32"}, {"name": "destination_airport_emb/embeddings", "shape": [360, 50], "dtype": "float32"}, {"name": "month_emb/embeddings", "shape": [2, 1], "dtype": "float32"}, {"name": "origin_airport_emb/embeddings", "shape": [360, 50], "dtype": "float32"}, {"name": "weekday_emb/embeddings", "shape": [7, 3], "dtype": "float32"}]}]}

0 个答案:

没有答案