程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

在 tensorflow.js 中加载移动网络(keras)

发布于2022-11-28 11:35     阅读(1034)     评论(0)     点赞(10)     收藏(5)


我正在尝试在 Tensorflow.js 中加载基于 mobilen 的 keras 模型。遗憾的是,这不起作用我使用以下 python 代码生成了模型


import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
import tensorflowjs as tfjs 

input_shape = 224,224 #sorry of all lowercase
num_classes = 2

mobile_net = tf.keras.applications.MobileNetV2(
    input_shape=input_shape+(3,),
    alpha=1.0,
    include_top=False,
    weights="imagenet",
    input_tensor=None,
    pooling=None,
    classes=2,
    classifier_activation="softmax"
)
model = keras.Sequential(
    [
        keras.Input(shape=input_shape+(3,)),
        layers.Rescaling(1./255),
        mobile_net,
        layers.Flatten(),
        layers.Dense(num_classes, activation="softmax")
    ]
)
        
model.build((None,)+input_shape+(3,))
tfjs.converters.save_keras_model(model,'./model.json')
print(model.summary())

之后我尝试使用以下命令在 node.js REPL 中加载模型

const tf = require('@tensorflow/tfjs')
async function predict(){
   const model = await tf.loadLayersModel('file:///./model.json');
}
predict()

但是我收到错误消息。

Uncaught TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:14294:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  cause: Error: not implemented... yet...
      at makeNetworkError (node:internal/deps/undici/undici:6789:35)
      at schemeFetch (node:internal/deps/undici/undici:13774:18)
      at node:internal/deps/undici/undici:13654:26
      at mainFetch (node:internal/deps/undici/undici:13671:11)
      at fetching (node:internal/deps/undici/undici:13628:7)
      at fetch2 (node:internal/deps/undici/undici:13506:20)
      at Object.fetch (node:internal/deps/undici/undici:14292:18)
      at fetch (node:internal/process/pre_execution:238:25)
      at PlatformNode.fetch (/Users/z003cyub/Documents/projects/FY2022/aufsteller/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:7542:33)
      at HTTPRequest.<anonymous> (/Users/z003cyub/Documents/projects/FY2022/aufsteller/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:8406:55) {
    [cause]: undefined

我想把它归咎于模型结构,但是,我用一个非常简单的模型得到了同样的错误

model2 = keras.Sequential(
    [
        keras.Input(shape=input_shape+(3,)),
        layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
        layers.MaxPooling2D(pool_size=(2, 2)),
        layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
        layers.MaxPooling2D(pool_size=(2, 2)),
        layers.Flatten(),
        layers.Dropout(0.5),
        layers.Dense(num_classes, activation="softmax"),
    ]
)

解决方案


不是层或模型问题,更多的是nodejs问题。最新版本的 node define global fetch,但实现仍不完整,缺乏对file://. 并且tfjs将使用全局获取(如果已定义)。尝试使用node --no-experimental-fetchso global fetchis not defined 并且tfjs将改用内部方法。



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/1862917/3446b37dcb1502dcd468/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

10 0
收藏该文
已收藏

评论内容:(最多支持255个字符)