WARNING:tensorflow:Model was constructed with shape (4, 112, 112, 3) for input …, but it was called on an input with incompatible shape ((None, 112)

We Are Going To Discuss About WARNING:tensorflow:Model was constructed with shape (4, 112, 112, 3) for input …, but it was called on an input with incompatible shape ((None, 112). So lets Start this Python Article.

WARNING:tensorflow:Model was constructed with shape (4, 112, 112, 3) for input …, but it was called on an input with incompatible shape ((None, 112)

Advertisements
  1. How to solve WARNING:tensorflow:Model was constructed with shape (4, 112, 112, 3) for input …, but it was called on an input with incompatible shape ((None, 112)

    This error came from shape of input, You can try this:
    import tensorflow as tf import cv2 IMAGE_CHANNEL = 1 # or 3 def prepare(filepath): IMG_SIZE = 112 img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE) new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, IMAGE_CHANNEL) x = tf.keras.Input(shape=(112,112,IMAGE_CHANNEL)) y = tf.keras.layers.Dense(16, activation='softmax')(x) model = tf.keras.Model(x, y) model.summary() prediction = model.predict([prepare("test.jpg")]) print(prediction)
    Output:
    Model: "model_11" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_17 (InputLayer) [(None, 112, 112, 1)] 0 dense_13 (Dense) (None, 112, 112, 16) 32 ================================================================= Total params: 32 Trainable params: 32 Non-trainable params: 0 _________________________________________________________________ [[[[2.40530210e-15 1.25257872e-18 2.81339079e-01 ... 1.10927344e-20 1.28210900e-22 3.45369773e-24] [1.21484684e-15 5.40451430e-19 2.79041141e-01 ... 4.33733043e-21 4.56826763e-23 1.14132918e-24] [3.09763760e-16 1.00567346e-19 2.74375856e-01 ... 6.62814917e-22 5.79706900e-24 1.24585123e-25] ...

  2. WARNING:tensorflow:Model was constructed with shape (4, 112, 112, 3) for input …, but it was called on an input with incompatible shape ((None, 112)

    This error came from shape of input, You can try this:
    import tensorflow as tf import cv2 IMAGE_CHANNEL = 1 # or 3 def prepare(filepath): IMG_SIZE = 112 img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE) new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, IMAGE_CHANNEL) x = tf.keras.Input(shape=(112,112,IMAGE_CHANNEL)) y = tf.keras.layers.Dense(16, activation='softmax')(x) model = tf.keras.Model(x, y) model.summary() prediction = model.predict([prepare("test.jpg")]) print(prediction)
    Output:
    Model: "model_11" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_17 (InputLayer) [(None, 112, 112, 1)] 0 dense_13 (Dense) (None, 112, 112, 16) 32 ================================================================= Total params: 32 Trainable params: 32 Non-trainable params: 0 _________________________________________________________________ [[[[2.40530210e-15 1.25257872e-18 2.81339079e-01 ... 1.10927344e-20 1.28210900e-22 3.45369773e-24] [1.21484684e-15 5.40451430e-19 2.79041141e-01 ... 4.33733043e-21 4.56826763e-23 1.14132918e-24] [3.09763760e-16 1.00567346e-19 2.74375856e-01 ... 6.62814917e-22 5.79706900e-24 1.24585123e-25] ...

Solution 1

Advertisements

This error came from shape of input, You can try this:

import tensorflow as tf
import cv2

IMAGE_CHANNEL = 1 # or 3

def prepare(filepath):
    IMG_SIZE = 112
    img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
    return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, IMAGE_CHANNEL)

x = tf.keras.Input(shape=(112,112,IMAGE_CHANNEL))
y = tf.keras.layers.Dense(16, activation='softmax')(x)
model = tf.keras.Model(x, y)
model.summary()


prediction = model.predict([prepare("test.jpg")])
print(prediction)

Output:

Model: "model_11"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_17 (InputLayer)       [(None, 112, 112, 1)]     0         
                                                                 
 dense_13 (Dense)            (None, 112, 112, 16)      32        
                                                                 
=================================================================
Total params: 32
Trainable params: 32
Non-trainable params: 0
_________________________________________________________________
[[[[2.40530210e-15 1.25257872e-18 2.81339079e-01 ... 1.10927344e-20
    1.28210900e-22 3.45369773e-24]
   [1.21484684e-15 5.40451430e-19 2.79041141e-01 ... 4.33733043e-21
    4.56826763e-23 1.14132918e-24]
   [3.09763760e-16 1.00567346e-19 2.74375856e-01 ... 6.62814917e-22
    5.79706900e-24 1.24585123e-25]
   ...

Original Author I’mahdi Of This Content

Solution 2

Advertisements

the error is saying you are passing a wronly shapped image, input with 3 channels. This might work if your input image has 3 channels:

def prepare(filepath):
  IMG_SIZE = 112
  img_array = cv2.imread(filepath)
  img_array = cv2.cvtColor(img_array ,cv2.COLOR_BGR2RGB)
  new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
  return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 3)

Original Author Sadra Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Advertisements
ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment