Home > Blockchain >  Load mobile net (keras) in tensorflow.js
Load mobile net (keras) in tensorflow.js

Time:11-27

I'm trying to load a keras model which is based on mobilen in Tensorflow.js. Sadly this does not work I generated the model with the following python code


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())

afterwards I'm trying to load the model in node.js REPL with the following commands

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

however I get the error message.

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

I would like to blame it on the model structure, however, I get the same error with a freakingly simple model

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"),
    ]
)

CodePudding user response:

not a layers or a model problem per-say, more of a nodejs problem. latest version of node define global fetch, but implementation is still not complete and it lacks support for file://. and tfjs will use global fetch if its defined. try using node --no-experimental-fetch so global fetch is not defined and tfjs will use internal methods insteads.

  • Related