Kalyan M
Kalyan M

Reputation: 325

Appropriate dimensions for the MaxPooling layer?

I'm trying to create a model in tensorflow to train the fashion_mnist dataset. The first layer I created is a conv2D layer:

keras.layers.Conv2D(
    filters=6, 
    kernel_size=3, 
    strides=1, 
    padding='SAME', 
    input_shape=(28, 28, 1))

Now, I want to add a MaxPooling layer after this. Since I've used 6 filters in the conv layer, isn't the conv layer output essentially 3D (2D image and an extra dimension for number of layers)? Should I use MaxPooling2D or MaxPooling3D in this case?

EDIT: This is the code I've written, and it gives an error: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (60000, 28, 28)

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

fashion_mnist = keras.datasets.fashion_mnist
(trainImg, trainLbl), (testImg, testLbl) = fashion_mnist.load_data()

classNames = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

trainImg = trainImg/255.0
trainLbl = trainLbl/255.0

model = keras.Sequential([
    keras.layers.Conv2D(activation=tf.nn.relu, filters=6, kernel_size=3, strides=1, padding='SAME', input_shape=(28, 28, 1)),
    keras.layers.MaxPooling2D(pool_size=2, strides=2, padding='SAME'),
    keras.layers.Conv2D(activation=tf.nn.relu, filters=12, kernel_size=9, strides=1, padding='SAME'),
    keras.layers.MaxPooling2D(pool_size=2, strides=2, padding='SAME'),
    keras.layers.Flatten(),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax),
])

model.compile(optimizer=tf.train.AdamOptimizer(),
             loss='sparse_categorical_crossentropy',
             metrics=['accuracy'])

model.fit(trainImg, trainLbl, epochs=10, batch_size=64)

Upvotes: 0

Views: 187

Answers (1)

Hari Krishnan
Hari Krishnan

Reputation: 2079

From my understanding of your question. You can use MaxPooling2D. You can use something like this

model.add(keras.layers.Conv2D(filters=6, kernel_size=3, strides=1, padding='SAME', input_shape=(28, 28, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))

You can change the pool_size to your liking.Hope this helps

Upvotes: 0

Related Questions