首页 > 解决方案 > Tensorflow JS - 加载模型,TypeError:未定义不是对象(评估'inboundNodesData.forEach')

问题描述

我用 python 编译了一个模型并以“h5”格式保存,然后使用本指南https://www.tensorflow.org/js/tutorials/conversion/import_keras我将模型转换为 json 和 4 个 bin 文件以加载权重他们稍后在我用 React Native 编写的应用程序中。

这是我的 Json 文件:

{"format": "layers-model", "generatedBy": "keras v2.4.0", "convertedBy": "TensorFlow.js Converter v3.8.0", "modelTopology": {"keras_version": "2.4.0", "backend": "tensorflow", "model_config": {"class_name": "Model", "config": {"name": "sequential_9", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 180, 180, 3], "dtype": "float32", "sparse": false, "ragged": false, "name": "sequential_8_input"}}, {"class_name": "Sequential", "config": {"name": "sequential_8", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 180, 180, 3], "dtype": "float32", "sparse": false, "ragged": false, "name": "random_flip_4_input"}}, {"class_name": "RandomFlip", "config": {"name": "random_flip_4", "trainable": true, "batch_input_shape": [null, 180, 180, 3], "dtype": "float32", "mode": "horizontal", "seed": null}}, {"class_name": "RandomRotation", "config": {"name": "random_rotation_4", "trainable": true, "dtype": "float32", "factor": 0.1, "fill_mode": "reflect", "interpolation": "bilinear", "seed": null}}, {"class_name": "RandomZoom", "config": {"name": "random_zoom_4", "trainable": true, "dtype": "float32", "height_factor": 0.1, "width_factor": null, "fill_mode": "reflect", "interpolation": "bilinear", "seed": null}}]}}, {"class_name": "Rescaling", "config": {"name": "rescaling_9", "trainable": true, "dtype": "float32", "scale": 0.00392156862745098, "offset": 0.0}}, {"class_name": "Conv2D", "config": {"name": "conv2d_12", "trainable": true, "dtype": "float32", "filters": 16, "kernel_size": [3, 3], "strides": [1, 1], "padding": "same", "data_format": "channels_last", "dilation_rate": [1, 1], "groups": 1, "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}}, {"class_name": "MaxPooling2D", "config": {"name": "max_pooling2d_12", "trainable": true, "dtype": "float32", "pool_size": [2, 2], "padding": "valid", "strides": [2, 2], "data_format": "channels_last"}}, {"class_name": "Conv2D", "config": {"name": "conv2d_13", "trainable": true, "dtype": "float32", "filters": 32, "kernel_size": [3, 3], "strides": [1, 1], "padding": "same", "data_format": "channels_last", "dilation_rate": [1, 1], "groups": 1, "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}}, {"class_name": "MaxPooling2D", "config": {"name": "max_pooling2d_13", "trainable": true, "dtype": "float32", "pool_size": [2, 2], "padding": "valid", "strides": [2, 2], "data_format": "channels_last"}}, {"class_name": "Conv2D", "config": {"name": "conv2d_14", "trainable": true, "dtype": "float32", "filters": 64, "kernel_size": [3, 3], "strides": [1, 1], "padding": "same", "data_format": "channels_last", "dilation_rate": [1, 1], "groups": 1, "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}}, {"class_name": "MaxPooling2D", "config": {"name": "max_pooling2d_14", "trainable": true, "dtype": "float32", "pool_size": [2, 2], "padding": "valid", "strides": [2, 2], "data_format": "channels_last"}}, {"class_name": "Dropout", "config": {"name": "dropout_4", "trainable": true, "dtype": "float32", "rate": 0.2, "noise_shape": null, "seed": null}}, {"class_name": "Flatten", "config": {"name": "flatten_4", "trainable": true, "dtype": "float32", "data_format": "channels_last"}}, {"class_name": "Dense", "config": {"name": "dense_8", "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}}, {"class_name": "Dense", "config": {"name": "dense_9", "trainable": true, "dtype": "float32", "units": 34, "activation": "linear", "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}}]}}, "training_config": {"loss": {"class_name": "SparseCategoricalCrossentropy", "config": {"reduction": "auto", "name": "sparse_categorical_crossentropy", "from_logits": true}}, "metrics": ["accuracy"], "weighted_metrics": null, "loss_weights": 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-shard1of4.bin", "group1-shard2of4.bin", "group1-shard3of4.bin", "group1-shard4of4.bin"], "weights": [{"name": "conv2d_12/kernel", "shape": [3, 3, 3, 16], "dtype": "float32"}, {"name": "conv2d_12/bias", "shape": [16], "dtype": "float32"}, {"name": "conv2d_13/kernel", "shape": [3, 3, 16, 32], "dtype": "float32"}, {"name": "conv2d_13/bias", "shape": [32], "dtype": "float32"}, {"name": "conv2d_14/kernel", "shape": [3, 3, 32, 64], "dtype": "float32"}, {"name": "conv2d_14/bias", "shape": [64], "dtype": "float32"}, {"name": "dense_8/kernel", "shape": [30976, 128], "dtype": "float32"}, {"name": "dense_8/bias", "shape": [128], "dtype": "float32"}, {"name": "dense_9/kernel", "shape": [128, 34], "dtype": "float32"}, {"name": "dense_9/bias", "shape": [34], "dtype": "float32"}]}]}

我正在通过 https 加载:

const model = await tf.loadLayersModel(
  "https://pathexample.com/model.json"
);

但它没有加载,它给了我这个错误:

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'inboundNodesData.forEach')]
at node_modules\@tensorflow\tfjs-layers\dist\tf-layers.node.js:23960:28 in processLayer
at node_modules\@tensorflow\tfjs-layers\dist\tf-layers.node.js:23976:24 in Container.fromConfig
at node_modules\@tensorflow\tfjs-layers\dist\tf-layers.node.js:17085:38 in deserializeKerasObject
at node_modules\@tensorflow\tfjs-layers\dist\tf-layers.node.js:27062:39 in __generator$argument_1
at node_modules\@tensorflow\tfjs-layers\dist\tf-layers.node.js:16710:17 in step
at node_modules\@tensorflow\tfjs-layers\dist\tf-layers.node.js:16681:47 in fulfilled
at node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 in tryCallOne
at node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14 in _callImmediatesPass
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6 in __callImmediates
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue
at [native code]:null in flushedQueue
at [native code]:null in callFunctionReturnFlushedQueue

早些时候我遇到了另一个错误[Unhandled promise rejection: Error: Unknown layer: RandomFlip. This may be due to one of the following reasons:],我通过将 modelTopology.model_config.class_name 的 json 文件中的值从 Sequential 更改为 Model 来解决它,如本文所述:Uncaught (in promise) Error: Unknown layer

我试图寻找解决方案,但我一无所获,有人可以帮助我吗?谢谢

标签: javascriptreactjsreact-nativetensorflowtensorflow.js

解决方案


推荐阅读