I am trying to train the mnist dataset on ResNet50 using the Keras
library.
The shape of mnist is (28, 28, 1)
however resnet50 required the shape to be (32, 32, 3)
How can I convert the mnist dataset to the required shape?
JavaScript
x
7
1
(x_train, y_train), (x_test, y_test) = mnist.load_data()
2
3
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1)
4
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 1)
5
x_train = x_train/255.0
6
x_test = x_test/255.0
7
JavaScript
1
4
1
from keras.utils import to_categorical
2
y_train = to_categorical(y_train)
3
y_test = to_categorical(y_test)
4
JavaScript
1
19
19
1
model = models.Sequential()
2
# model.add(InputLayer(input_shape=(28, 28)))
3
# model.add(Reshape(target_shape=(32, 32, 3)))
4
# model.add(Conv2D())
5
model.add(conv_base)
6
model.add(Flatten())
7
model.add(BatchNormalization())
8
model.add(Dense(128, activation='relu'))
9
model.add(Dropout(0.5))
10
model.add(BatchNormalization())
11
model.add(Dense(64, activation='relu'))
12
model.add(Dropout(0.5))
13
model.add(BatchNormalization())
14
model.add(Dense(10, activation='softmax'))
15
16
model.compile(optimizer=optimizers.RMSprop(lr=2e-5), loss='binary_crossentropy', metrics=['acc'])
17
18
history = model.fit(x_train, y_train, epochs=5, batch_size=20, validation_data=(x_test, y_test))
19
JavaScript
1
2
1
ValueError: Input 0 is incompatible with layer sequential_10: expected shape=(None, 32, 32, 3), found shape=(20, 28, 28, 1)
2
Advertisement
Answer
You need to resize the MNIST
data set. Note that minimum size actually depends on the ImageNet model. For example: Xception
requires at least 72
, where ResNet
is asking for 32
. Apart from that, the MNIST
is a grayscale image, but it may conflict if you’re using the pretrained weight of these models. So, good and safe side is to resize and convert grayscale to RGB.
Full working code for you.
Data Set
We will resize MNIST
from 28 to 32. Also, make 3 channels instead of keeping 1.
JavaScript
1
22
22
1
import tensorflow as tf
2
import numpy as np
3
4
(x_train, y_train), (_, _) = tf.keras.datasets.mnist.load_data()
5
6
# expand new axis, channel axis
7
x_train = np.expand_dims(x_train, axis=-1)
8
9
# [optional]: we may need 3 channel (instead of 1)
10
x_train = np.repeat(x_train, 3, axis=-1)
11
12
# it's always better to normalize
13
x_train = x_train.astype('float32') / 255
14
15
# resize the input shape , i.e. old shape: 28, new shape: 32
16
x_train = tf.image.resize(x_train, [32,32]) # if we want to resize
17
18
# one hot
19
y_train = tf.keras.utils.to_categorical(y_train , num_classes=10)
20
21
print(x_train.shape, y_train.shape)
22
JavaScript
1
2
1
(60000, 32, 32, 3) (60000, 10)
2
ResNet 50
JavaScript
1
13
13
1
input = tf.keras.Input(shape=(32,32,3))
2
efnet = tf.keras.applications.ResNet50(weights='imagenet',
3
include_top = False,
4
input_tensor = input)
5
# Now that we apply global max pooling.
6
gap = tf.keras.layers.GlobalMaxPooling2D()(efnet.output)
7
8
# Finally, we add a classification layer.
9
output = tf.keras.layers.Dense(10, activation='softmax', use_bias=True)(gap)
10
11
# bind all
12
func_model = tf.keras.Model(efnet.input, output)
13
Train
JavaScript
1
7
1
func_model.compile(
2
loss = tf.keras.losses.CategoricalCrossentropy(),
3
metrics = tf.keras.metrics.CategoricalAccuracy(),
4
optimizer = tf.keras.optimizers.Adam())
5
# fit
6
func_model.fit(x_train, y_train, batch_size=128, epochs=5, verbose = 2)
7
JavaScript
1
12
12
1
Epoch 1/5
2
469/469 - 56s - loss: 0.1184 - categorical_accuracy: 0.9690
3
Epoch 2/5
4
469/469 - 21s - loss: 0.0648 - categorical_accuracy: 0.9844
5
Epoch 3/5
6
469/469 - 21s - loss: 0.0503 - categorical_accuracy: 0.9867
7
Epoch 4/5
8
469/469 - 21s - loss: 0.0416 - categorical_accuracy: 0.9888
9
Epoch 5/5
10
469/469 - 21s - loss: 0.1556 - categorical_accuracy: 0.9697
11
<tensorflow.python.keras.callbacks.History at 0x7f316005a3d0>
12