0

I'm trying get flatten layer as input to convd2d and predicting the output for 10 class classification problem on Densenet with cifar-10 dataset. following code snippet where I'm getting the error.

global compression
    BatchNorm = layers.BatchNormalization()(input)
    relu = layers.Activation('relu')(BatchNorm)
    AvgPooling = layers.AveragePooling2D(pool_size=(2,2))(relu)
    flat = layers.Flatten()(AvgPooling)
    # output = layers.Dense(num_classes, activation='softmax')(flat)
    output = layers.Conv2D(filters=10,kernel_size=3,strides=1,activation='softmax',padding='valid')(flat)

I'm getting the following error

ValueError: Input 0 is incompatible with layer conv2d_513: expected ndim=4, found ndim=2

Can anyone tell me how to resolve it. Thanks in advance.

Saranraj K
  • 412
  • 1
  • 7
  • 19
  • Why do you flatten the data before the 2D convolution? It is normally the other way around. For Conv2D you need a 2D input and the flattening layer converts 2D to 1D. – mac13k Jun 06 '20 at 18:12

1 Answers1

-1
output = layers.Conv2D(filters=10, kernel_size=(1,1),strides =(2,2))

This code will change your dense layer to respective Conv2D layer. But to avoid any error you need to add softmax as different layer. Which should look like this:

not_final = layers.Activation('softmax')(output)

result = layers.Flatten()(not_final)
desertnaut
  • 57,590
  • 26
  • 140
  • 166