Nathan e
Nathan e

Reputation: 81

How to implement 2D conv with already flattened input data

I want to train a CNN model with TensorFlow on a dataset with images of shape 28x28, already flattened as vectors of length 784.

I want to use Conv2D layers in Tensorflow but as my input is already flattened, I don't know what is the best way to do this.

Is there a kind of layer that performs the opposite of flatten ? Should I write a custom model with subclassing API and numpy's reshape ? Or is it possible to perform 2D convolution with a 1D array ?

Thank you in advance for your help!

Upvotes: 0

Views: 888

Answers (1)

Jaky
Jaky

Reputation: 41

I'm not sure why you want to do that, but here is the approach that might be helpful. Working with TensorFlow 2 and Keras:

model = models.Sequential()
model.add(layers.Conv2D(8,(3,3),padding='same',input_shape=([28,28,3])))
model.add(layers.Flatten())
model.add(layers.Reshape([28,28,8]))
model.add(layers.Conv2D(3,(3,3),padding='same',input_shape=([28,28,3])))

The only downside is that you should exactly know the dimension which you what to reshape here.

If you need more information you have to provide more in your question. This is as far as I could write with the limited amount of information you provided.

Upvotes: 1

Related Questions