0

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: error

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

Manfred
  • 13
  • 3
  • 1
    I suggest edition your question so that it [contains no image](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors). Your stack trace can be wrapped in a code block. – Minh-Long Luu Mar 05 '23 at 02:17
  • Sounds like you want to do an autoencoder but this model is not structured as one, the shape of the output should be the same as the input. – Dr. Snoopy Mar 05 '23 at 09:03

1 Answers1

0

model.fit first two arguments are x and y, where x is the input and y is the label. You are using model.fit(data, data,...), so your input is also your...label?

Minh-Long Luu
  • 2,393
  • 1
  • 17
  • 39
  • Thanks for the suggestion. However, in my case, the input is the pictures and the output should be the factors extracted. Thus, this is an unsupervised learning case. But the model.fit() method asked me to give two parameters... That's why I did that. Do you have better suggestions on it? – Manfred Mar 05 '23 at 03:16
  • In the code, you use MSE, which is believe one element is the 128 features extracted, but then calculate w.r.t. *what*? – Minh-Long Luu Mar 05 '23 at 03:19
  • @Minh-LongLuu Have you ever heard of autoencoders? There is nothing surprising here. – Dr. Snoopy Mar 05 '23 at 09:02
  • In that case, OP should structure the network so that the output has the same size as the input, like [in this answer](https://stackoverflow.com/questions/55681649/how-to-build-an-unsupervised-cnn-model-with-keras-tensorflow). The current code output 128 features vector, but it should be 128x128 map. – Minh-Long Luu Mar 05 '23 at 09:10