How can I reshape my image for a feedforward neural network
Question:
I am working on a Covid-19 image classification problem. I have Covid-19 and Normal CXRs images. Now I want to build a Feedforward Neural Network. This code i wrote keeps giving me an error and I am not sure what exactly I am doing wrong.
My x_train.shape
is 160,256,256,3
while x_test
is 40,256,256,3
This is the error I am getting
ValueError:
Input 0 of layer "sequential_4" is incompatible with the layer:
expected shape=(None, 196608), found shape=(None, 256, 256, 3)
PS I am new to machine learning/deep learning.
This is a snippet of my code
import numpy as np
from keras.models import Sequential
from keras.layers import Flatten, Dense
# Define the architecture of the FNN model
model = Sequential()
model.add(Flatten(input_shape=(256,256,3)))
model.add(Dense(64, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile the FNN model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the FNN model
model.fit(x_train, y_train, epochs=10, batch_size=32)
Answers:
ValueError: Input 0 of layer “sequential_4” is incompatible with the layer: expected shape=(None, 196608), found shape=(None, 256, 256, 3)
The error above is coming as it seems that the flattening layer is expecting the input in a 1D array with 196608 values, the same as 256 X 256 X 3 = 196608.
Using numpy.reshape()
should work for flattening the image array.
You can do it as follows:
# Flatten the image array values in x_train
flattened_x_train = np.reshape(x_train, (x_train.shape[0], -1))
flattened_x_test = np.reshape(x_test, (x_test.shape[0],-1))
You can use these flattened arrays as input to the FNN.
I am working on a Covid-19 image classification problem. I have Covid-19 and Normal CXRs images. Now I want to build a Feedforward Neural Network. This code i wrote keeps giving me an error and I am not sure what exactly I am doing wrong.
My x_train.shape
is 160,256,256,3
while x_test
is 40,256,256,3
This is the error I am getting
ValueError:
Input 0 of layer "sequential_4" is incompatible with the layer:
expected shape=(None, 196608), found shape=(None, 256, 256, 3)
PS I am new to machine learning/deep learning.
This is a snippet of my code
import numpy as np
from keras.models import Sequential
from keras.layers import Flatten, Dense
# Define the architecture of the FNN model
model = Sequential()
model.add(Flatten(input_shape=(256,256,3)))
model.add(Dense(64, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile the FNN model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the FNN model
model.fit(x_train, y_train, epochs=10, batch_size=32)
ValueError: Input 0 of layer “sequential_4” is incompatible with the layer: expected shape=(None, 196608), found shape=(None, 256, 256, 3)
The error above is coming as it seems that the flattening layer is expecting the input in a 1D array with 196608 values, the same as 256 X 256 X 3 = 196608.
Using numpy.reshape()
should work for flattening the image array.
You can do it as follows:
# Flatten the image array values in x_train
flattened_x_train = np.reshape(x_train, (x_train.shape[0], -1))
flattened_x_test = np.reshape(x_test, (x_test.shape[0],-1))
You can use these flattened arrays as input to the FNN.