How long does it typically take to train a MNIST data on a Mac Pro?

My code is below:

# define a simple CNN model
def baseline_model():
    # create model
    model = Sequential()
    model.add(Conv2D(30, (5, 5), input_shape=(1, 28, 28), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Conv2D(15, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(50, activation='relu'))
    model.add(Dense(num_classes, activation='softmax'))
    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model


# build the model
model = baseline_model()
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=500, batch_size=200)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)

My Mac's capacity:

Grphics Radeon Pro 555 2048 MB
Intel HD Graphics 630 1536 MB

Memeory 16 GB 2133 MHz LPDDR3

Processor 2.8 GHz Intel Core i7

It is taking several hours to train this the MNIST image classification dataset on my computer. Is this normal?

Topic mnist cnn

Category Data Science


The arguments for input shape for MNIST are in the wrong order. It should be input_shape=(28, 28, 1)

Additionally, sometimes it makes sense to not write code in functions until the code is working. It is more difficult to debug code in functions.

I have create a working version of your code. It displays a progress bar so you should be able to see how long the code estimates it will take to finish.

import numpy as np
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense

# Model / data parameters
num_classes = 10
input_shape = (28, 28, 1)

# the data, split between train and test sets
(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()

# Scale images to the [0, 1] range
X_train = X_train.astype("float32") / 255
X_test = X_test.astype("float32") / 255
# Make sure images have shape (28, 28, 1)
X_train = np.expand_dims(X_train, -1)
X_test = np.expand_dims(X_test, -1)
print("x_train shape:", X_train.shape)
print(X_train.shape[0], "train samples")
print(X_test.shape[0], "test samples")

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

def baseline_model():
    "Define a simple CNN model"
    
    # create model
    model = Sequential()
    model.add(Conv2D(30, (5, 5), input_shape=(28, 28, 1), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Conv2D(15, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(50, activation='relu'))
    model.add(Dense(num_classes, activation='softmax'))
    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

# Build the model
model = baseline_model()

# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=500, batch_size=200)

# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)

Did you try to do a model summary and check the size of your model. Yes, i may take hours as you are adding lot of layers. Try starting with 10 or 20 epochs and check the accuracy of your model gradually. May reduce your training time.

When you do the above exercise its better to have a validation data set, rather than fitting the model on the training data set time and again.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.