I now have a picture data of size (419,128,128),which is 419 pics and each pic is 128*128. I want to build a CNN model to do a feature selection. (p.s. extract 128 important features from each of the pictures) This is my code:
import tensorflow as tf
# shape of input data
input_shape = (None, 128, 128, 1)
# Define the CNN model
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(128, 3, activation='relu', padding='same', input_shape=input_shape[1:]),
tf.keras.layers.Conv2D(128, 4, activation='relu', padding='same'),
tf.keras.layers.Conv2D(128, 5, activation='relu', padding='same'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
# Output layer, since we need to extract 128 important factors, the number of output nodes is 128
tf.keras.layers.Dense(128, activation='sigmoid')
])
# Compile the model
model.compile(optimizer='adam', loss='mse')
# My input data is sp_pics, shape is (419,128,128)
data_input = np.expand_dims(sp_pics, axis=-1)
# Train the model
model.fit(data, data, epochs=10, batch_size=32)
# Extract the important factors
important_factors = model.predict(data)
Now I'm getting the error message of:
As far as i'm concerned, the error is caused by the shape difference between my input(419,128,128) and output (419,128). Is this true? How can I fix it?
Thank you a lot for your help!!!
I tried different batch sizes and different loss function. But that didn't help