在使用Keras进行训练之前调整MNIST图像的大小 [英] Resize MNIST images before training using Keras

查看:179
本文介绍了在使用Keras进行训练之前调整MNIST图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在从事涉及CNN及其权重的项目,并且我一直在尝试减少CNN中存在的权重数量.我想在训练CNN之前将MNIST图像的尺寸从28x28调整为14x14,但是我不知道如何在Keras中进行操作.

I have been working on a project involving CNN and its weights and I have been trying to reduce the number of weights present in the CNN. I want to resize the MNIST images from 28x28 into 14x14 before training the CNN but I have no idea how to do it in Keras.

以下是用于导入MNIST数据集和构建CNN的代码示例:

Here is a sample of the code used in importing the MNIST dataset and building the CNN:

# LOAD MNIST DATA
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# RESHAPE TO [SAMPLES][PIXELS][WIDTH][HEIGHT]
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')

# NORMALIZE 0-255 TO 0-1
X_train = X_train / 255
X_test = X_test / 255
# ONE HOT ENCODE
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]

#DEFINE MODEL
def larger_model():
  # CREATE MODEL
  model = Sequential()
  model.add(Conv2D(2, (5, 5), input_shape=(1, 28, 28), activation='relu', 
padding="same"))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Conv2D(2, (5, 5), activation='relu', padding="same"))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.2))
  model.add(Flatten())
  model.add(Dense(16, activation='relu'))
  model.add(Dense(num_classes, activation='softmax'))
  # COMPILE MODEL
  model.compile(loss='categorical_crossentropy', optimizer='adam', metrics= 
 ['accuracy'])
  return model

# BUILD MODEL
model = larger_model()
model.summary()

X_train变量是模型训练中使用的变量.在培训开始之前,我应该做些什么调整以将X_train的大小减小到14x14?

The X_train variable is the one used in the training of the model. What adjustments should I make to reduce the size of the X_train into 14x14 before the training starts?

谢谢!

推荐答案

默认的load_data函数没有即时修改的任何选项,例如调整大小.由于现在有了NumPy数组,因此必须进行预处理,将图像调整为数组大小.这是有关将NumPy数组的大小调整为图像的帖子.

The default load_data function doesn't have any options for on the fly modification such as resize. Since you have NumPy arrays now, you have to pre-process, resize the images as arrays. Here is a post about resizing NumPy arrays as images.

这篇关于在使用Keras进行训练之前调整MNIST图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆