Selim Bamri
Selim Bamri

Reputation: 23

An InputLayer should be passed either a `batchInputShape` or an `inputShape`

I saved a CNN model using tfjs. But when I tried to load the model in js I got the following error

tfjs Uncaught (in promise) Error: An InputLayer should be passed either a `batchInputShape` or an `inputShape`.

Here is the architecture of my model

model= Sequential()
model.add(Conv2D(64,kernel_size=(3,3),activation='relu',input_shape=(28,28,1),padding='same'))
model.add(Conv2D(64,kernel_size=(3,3),activation='relu',padding='same'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.15))
model.add(BatchNormalization())
model.add(Conv2D(128,kernel_size=(3,3),activation='relu',padding='same'))
model.add(Conv2D(128,kernel_size=(3,3),activation='relu',padding='same'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.15))
model.add(BatchNormalization())
model.add(Conv2D(256,kernel_size=(3,3),activation='relu',padding='same'))
model.add(Conv2D(256,kernel_size=(3,3),activation='relu',padding='same'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.15))
model.add(BatchNormalization())
model.add(GaussianNoise(0.25))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.15))
model.add(BatchNormalization())
model.add(GaussianNoise(0.25))
model.add(Dense(10,activation='softmax'))
model.summary()

I don't understand what's causing the error as I have precised the input_shape in the first convolutional layer.

Upvotes: 2

Views: 2545

Answers (4)

dykwon
dykwon

Reputation: 1

python version 3.10 / node version 18

pip uninstall keras-tf tensorflowjs tensorflow
pip install tensorflowjs
pip install tensorFlow==2.15.0
pip install tensorflow-decision-forests==1.8.1

Upvotes: 0

snoopy_doo
snoopy_doo

Reputation: 11

You could try checking you model.json file and manually change the name of the input_shape parameter to

"batch_input_shape" = [null, 28, 28, 1]

Upvotes: 1

Alex
Alex

Reputation: 23

I was dealing with the same issue. What happened with me is that I trained my model with a virtual environment of python 3.12 before realizing that tfjs is suited to older versions of python like python 3.8. So, I switched my virtual environment midway to python 3.8 and then this error arose. What I failed to realize is that since I switched my virtual environment to python 3.8 midway, my model was still trained on python 3.12, so my tfjs version of my model was finicky and had problems, which resulted in the same error you're describing. To fix this, I retrained my model on python 3.8 and my tfjs version of this new model had no issues. The error disappeared. Hope this helps

Upvotes: 2

우성민
우성민

Reputation: 1

I've been figuring this issue more than 2 weeks.

install this first

!pip install tensorflowjs
!pip install TensorFlow==2.15.0
!pip install tensorflow-decision-forests==1.8.1

and then training model with this code

tf.keras.backend.clear_session()

It worked for me.

Upvotes: 0

Related Questions