Error in Python: Function call stack: train_function

Viewed 139

I am running a snippet of code that is supposed to train a manuscript recognition model, in Python language in a COLAB work environment. The code loads a database of pictures of literature written by people from MNIST and practices them. The code:

import numpy
from keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras import utils as np_utils
from tensorflow.keras import backend as K
K.set_image_data_format('channels_first')
from matplotlib import pyplot as plt

#load data from mnist
(xTrain,yTrain),(xTest,yTest)=mnist.load_data()
#reshape the images to be 28*28 pixels
xTrain=xTrain.reshape(xTrain.shape[0],1,28,28).astype('float32')
xTest=xTest.reshape(xTest.shape[0],1,28,28).astype('float32')
#normalize inputs from 0-255 to 0-1
xTrain=xTrain/255
xTest=xTest/255
#one hot encode outputs
yTrain=np_utils.to_categorical(yTrain)
yTest=np_utils.to_categorical(yTest)
num_classes=yTest.shape[1]

    
def baseline_model():
  #create model
  model=Sequential()
  model.add(Conv2D(32,(5,5), input_shape=(1,28,28),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(num_classes, activation='softmax'))
  #complie model
  model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
  return model


model=baseline_model()
model.fit(xTrain, yTrain, validation_data=(xTest, yTest), epochs=1, batch_size=200, verbose=2)

The problem is that the code returns an error in the last line. The error:

    InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-111-ec22b5dcc4e3> in <module>()
     63 
     64 model=baseline_model()
---> 65 model.fit(xTrain, yTrain, validation_data=(xTest, yTest), epochs=1, batch_size=200, verbose=2)

6 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError:  Default MaxPoolingOp only supports NHWC on device type CPU
     [[node sequential_36/max_pooling2d_21/MaxPool (defined at <ipython-input-111-ec22b5dcc4e3>:65) ]] [Op:__inference_train_function_10696]

Function call stack:
train_function

Would appreciate help, Thank you

1 Answers

InvalidArgumentError: Default MaxPoolingOp only supports NHWC on device type CPU

As the error clearly says, it can't work with NCHW (i.e. batch_size, channels, height, width) on CPU.

If you want to use CPU, then you should change the format as NHWC (i.e.batch_size, height, width, channels) then input should be (28,28,1).

One more suggestion never mix imports from tensorflow and keras.

Where as on GPU, it supports with NCHW. You can refer working code as shown below

import numpy
from tensorflow.keras.datasets import mnist # import from TF
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras import utils as np_utils
from tensorflow.keras import backend as K
K.set_image_data_format('channels_first')
from matplotlib import pyplot as plt

#load data from mnist
(xTrain,yTrain),(xTest,yTest)=mnist.load_data()
#reshape the images to be 28*28 pixels
xTrain=xTrain.reshape(xTrain.shape[0],1,28,28).astype('float32')
xTest=xTest.reshape(xTest.shape[0],1,28,28).astype('float32')
#normalize inputs from 0-255 to 0-1
xTrain=xTrain/255
xTest=xTest/255
#one hot encode outputs
yTrain=np_utils.to_categorical(yTrain)
yTest=np_utils.to_categorical(yTest)
num_classes=yTest.shape[1]

    
def baseline_model():
  #create model
  model=Sequential()
  model.add(Conv2D(32,(5,5), input_shape=(1,28,28),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(num_classes, activation='softmax'))
  #complie model
  model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
  return model


model=baseline_model()
model.fit(xTrain, yTrain, validation_data=(xTest, yTest), epochs=1, batch_size=200, verbose=2)

output:

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 0s 0us/step
300/300 - 34s - loss: 0.2372 - accuracy: 0.9304 - val_loss: 0.0756 - val_accuracy: 0.9760
Related