2

i was building a simple demo that would load a model that i have converted a JSON and binary using tensorfjs. The model is utilizing the following architecture

model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(128, activation='relu'),
    Dense(10, activation='softmax')
])

importing this model in the site gives me this issue

my code:

const model = await tf.loadLayersModel('model.json');

error received : Error occurred: n: An InputLayer should be passed either a batchInputShape or an inputShape. at new n (

This feels like an intercompatibility issue between tensorflow and tensorflow where the json is missing some parameters. Are there any workarounds for this? How do i solve this issue?

what ive tried

  • ive tried adding input_shape to the JSON (not sure if i did it right though)
  • i tried mentioning Input explicitly in the model architecture

2 Answers 2

0

Add an Input layer to define the input shape explicitly:

model = Sequential([
    Input(shape=(28, 28)),
    Flatten(),
    Dense(128, activation='relu'),
    Dense(10, activation='softmax')
])

Reconvert your model and try loading it again—it should work!

New contributor
vidun dulmika is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • thanks! but as mentioned in my query, i have already tried this and retrained and reloaded my model but it didn't work
    – amm4r
    Commented 10 hours ago
0

Use the Functional API instead of Sequential to define your model:

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Flatten, Dense
inputs = Input(shape=(28, 28))
x = Flatten()(inputs)
x = Dense(128, activation='relu')(x)
outputs = Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)

Convert and reload the model. Ensure TensorFlow.js tools are updated and model.json includes batchInputShape.

New contributor
vidun dulmika is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.