WARNING:tensorflow:Model was constructed with shape (None, 224, 224, 3) for input

I am trying to predict disease based on image. image size is 224*224…i am getting error WARNING:tensorflow:Model was constructed with shape (None, 224, 224, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name=‘input_1’), name=‘input_1’, description=“created by layer ‘input_1’”), but it was called on an input with incompatible shape (None, 1).

Define the CNN model

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation=‘relu’, input_shape=(224, 224, 1
)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, kernel_size=(3, 3), activation=‘relu’))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation=‘relu’))
model.add(Dense(1)) # Assuming you want to predict a continuous hemoglobin level

Compile the model

model.compile(loss=‘mean_squared_error’, optimizer=‘adam’)

X = dataset.iloc[:, :-1]
y = dataset.iloc[:, -1]

Perform LOOCV

predictions =

for i, j in loo.split(X):
X_train, X_test = X.iloc[i,:], X.iloc[j,:]

   y_train, y_test = y[i], y[j]  
  
   model.compile(
   optimizer=keras.optimizers.Adam(1e-3),
   loss="binary_crossentropy",
   metrics=["accuracy"],
   )
   
   model.fit(X_train, y_train, batch_size=10, epochs=10, verbose=1)    
   prediction = model.predict(X_test)
   predictions.append(prediction)
1 Like

Interesting project !

You may just share the notebook, is more readable and maybe we can find out easily.

But let’s try some guesses just in case it helps.

I would assume that your array is actually a linear arrange of pixels, or maybe X and Y are swapped ?

For the first case you may have to reshape X

X = X.reshape(-1, 224, 224, 1)

For the second just swap them back.

But in any case you may just print the shape or X and Y for us ? @Sumana_Naskar

https://drive.google.com/file/d/1xy91-iiBTt9ZHTNaH1UPoYrkc4xH12yH/view?usp=sharing

please see this above link of google drive ,please suggest what should i do?

Thanks & Regards

i tried same code in another way…but i am getting same error…please give me suggestion
Thanks & Regards

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

dataset=pd.read_csv(“C:/Users/USER/Documents/Data/image.csv”)
image_paths = dataset[‘image_path’].values
cholesterol_levels = dataset[‘cholesterol’].values

loo = LeaveOneOut()

image_size = (224, 224)
batch_size = 10

#Define a Convolutional Neural Network Model
def make_model(input_shape):
inputs = keras.Input(shape=input_shape)

# Entry block
#x = layers.Rescaling(1.0 / 255)(inputs)
#x = x.reshape(-1, 224, 224, 1)(inputs)
x = layers.Conv2D(128, 3, strides=2, padding="same")(inputs)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)

previous_block_activation = x  # Set aside residual

for size in [256, 512, 728]:
    x = layers.Activation("relu")(x)
    x = layers.SeparableConv2D(size, 3, padding="same")(x)
    x = layers.BatchNormalization()(x)

    x = layers.Activation("relu")(x)
    x = layers.SeparableConv2D(size, 3, padding="same")(x)
    x = layers.BatchNormalization()(x)

    x = layers.MaxPooling2D(3, strides=2, padding="same")(x)

    # Project residual
    residual = layers.Conv2D(size, 1, strides=2, padding="same")(
        previous_block_activation
    )
    x = layers.add([x, residual])  # Add back residual
    previous_block_activation = x  # Set aside next residual

x = layers.SeparableConv2D(1024, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)

x = layers.GlobalAveragePooling2D()(x)
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(units=1, activation="sigmoid")(x)
return keras.Model(inputs, outputs)

model = make_model(input_shape=image_size + (3,))

model.compile(
optimizer=keras.optimizers.Adam(1e-3),
loss=“binary_crossentropy”,
metrics=[“accuracy”],
)

X = dataset.iloc[:, :-1]
y = dataset.iloc[:, -1]

predictions =

for i, j in loo.split(X):
X_train, X_test = X.iloc[i,:], X.iloc[j,:]

   y_train, y_test = y[i], y[j]  
  
  
   
   model.fit(X_train, y_train, batch_size=10, epochs=10, verbose=1)    
   prediction = model.predict(X_test)
   predictions.append(prediction)

predictions = np.array(predictions).flatten()
mse = np.mean((predictions - y) ** 2)
mae = np.mean(np.abs(predictions - y))
r_squared = np.corrcoef(predictions, y)[0, 1] ** 2

print(‘Mean Squared Error (MSE):’, mse)
print(‘Mean Absolute Error (MAE):’, mae)
print(‘R-squared:’, r_squared)[