让我们通过检查模型上存在的方法来检查你是否真的可以保存模型。
function getMethods(o) {
return Object.getOwnPropertyNames(Object.getPrototypeOf(o))
.filter(m => 'function' === typeof o[m])
}
cocoSsd.load().then(model => {
console.log(getMethods(model));
})
输出:
["constructor","getPrefix","load","infer","buildDetectedObjects","calculateMaxScores","detect","dispose"]
查看方法,错误实际上是正确的,save 方法在 cocoSsd 模型上不存在。所以你不能直接将模型保存到本地存储或数据库中。
不用担心,您可以从TensorFlow 2 Detection Model Zoo 的所有可用对象检测模型列表中下载原始模型。它们都在同一个 coco 数据集上进行了训练,因此您可以选择其中任何一个。
例如我选择了centernet_mobilenetv2_fpn_od。
下载文件并解压将产生以下内容:
|-- checkpoint
| |-- checkpoint
| |-- ckpt-301.data-00000-of-00001
| `-- ckpt-301.index
|-- label_map.txt
|-- model.tflite
|-- pipeline.config
`-- saved_model
|-- assets
|-- saved_model.pb
`-- variables
|-- variables.data-00000-of-00001
`-- variables.index
您需要的是 saved_model.pb,您需要将其转换为 Tensorflow.js 可用的格式。为此,您可以使用在终端中运行的tensorflowjs_wizard:
>> tensorflowjs_wizard
Welcome to TensorFlow.js Converter.
? Please provide the path of model file or the directory that contains model files.
If you are converting TFHub module please provide the URL. .
? What is your input model format? (auto-detected format is marked with *) Tensorflow Saved Model *
? What is tags for the saved model? serve
? What is signature name of the model? signature name: serving_default
? Do you want to compress the model? (this will decrease the model precision.) No compression (Higher accuracy)
? Please enter shard size (in bytes) of the weight files? 4194304
? Do you want to skip op validation?
This will allow conversion of unsupported ops,
you can implement them as custom ops in tfjs-converter. No
? Do you want to strip debug ops?
This will improve model execution performance. Yes
? Do you want to enable Control Flow V2 ops?
This will improve branch and loop execution performance. Yes
? Do you want to provide metadata?
Provide your own metadata in the form:
metadata_key:path/metadata.json
Separate multiple metadata by comma.
? Which directory do you want to save the converted model in? /Users/yravindranath/Downloads/centernet_mobilenetv2_fpn_od/saved_model/model
converter command generated:
tensorflowjs_converter --control_flow_v2=True --input_format=tf_saved_model --metadata= --saved_model_tags=serve --signature_name=serving_default --strip_debug_ops=True --weight_shard_size_bytes=4194304 . /Users/yravindranath/Downloads/centernet_mobilenetv2_fpn_od/saved_model/model
这将在指定的保存路径中产生以下内容:
./
|-- group1-shard1of3.bin
|-- group1-shard2of3.bin
|-- group1-shard3of3.bin
`-- model.json
如果您想直接从本地存储中加载模型,您可以使用此函数将模型加载到您的 Javascript 应用程序中:
const tf = require('@tensorflow/tfjs');
const tfnode = require('@tensorflow/tfjs-node');
async function loadModel(){
const model = await loadGraphModel("path to model.json");
// You can even save it to a github repo and load it in using a GET request like so:
// const model = await loadGraphModel("https://raw.githubusercontent.com/hugozanini/TFJS-object-detection/master/models/web_model/model.json");
console.log("Model loaded")
}
loadModel();