mathlete
mathlete

Reputation: 181

Tensorflow Keras Predict Returning Wrong Shaped Output

I am trying to predict a single image. But my model returns a prediction array with the shape (1,1,1,2048) when it should be (1,10). Any idea what I am doing wrong? My x input shape is correct at (1,32,32,3).

ResNet50V2(): 
  IMG_SHAPE = (32, 32, 3)
  return tf.keras.applications.ResNet50V2(input_shape=IMG_SHAPE, include_top=False, weights=None, classes=10)

model = ResNet50V2()
x = x[None, :]
predictions = model.predict(x)

Upvotes: 1

Views: 468

Answers (3)

Chaos Misit
Chaos Misit

Reputation: 1

A image classification network usually works within 2 steps of processing. The first one is feature extraction, we call that "base", and it consists in a stack of layers to find and reinforce patterns on image(2DConv, Relu and MaxPool). The second one is the "head" and it is used to classify the extracted features from previous step. Your code is getting the raw output of the "base", with no classification, and as the other gentle people stated, the solution is adding a custom "head" or changing the include_top parameter to True.

Upvotes: 0

Niteya Shah
Niteya Shah

Reputation: 1824

That's because you are disabling top with the include top, which removes the final classification layer. You need to either add your own layer with 10 classes or remove the include top parameter and retrain the network with the desired inputs.

Upvotes: 1

tnte
tnte

Reputation: 113

You are loading your keras-model with parameter

include_top=False

which cuts of the fully-connected projection layer that is responsible for projecting the model output to your expected amout of classes. Change the parameter to True.

Upvotes: 1

Related Questions