如何将 MNIST 训练图像从 (60000, 28, 28) 重塑为

我正在尝试使用 Keras 学习具有简单密集层的 MNIST 数据集。我希望我的图像大小为 16*16 而不是 28*28。我用了很多方法,但都不管用。这是简单的密集网络:


import keras

import numpy as np

import mnist

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense

from tensorflow.keras.utils import to_categorical


train_images = mnist.train_images()

train_labels = mnist.train_labels()

test_images = mnist.test_images()

test_labels = mnist.test_labels()


# Normalize the images.

train_images = (train_images / 255) - 0.5

test_images = (test_images / 255) - 0.5

print(train_images.shape)

print(test_images.shape)


# Flatten the images.

train_images = train_images.reshape((-1, 784))

test_images = test_images.reshape((-1, 784))

print(train_images.shape)

print(test_images.shape)

# Build the model.

model = Sequential([

    Dense(10, activation='softmax', input_shape=(784,)),

])

# Compile the model.

model.compile(

    optimizer='adam',

    loss='categorical_crossentropy',

    metrics=['accuracy'],

)


# Train the model.

model.fit(

    train_images,

    to_categorical(train_labels),

    epochs=5,

    batch_size=32,

)


# Evaluate the model.

model.evaluate(

    test_images,

    to_categorical(test_labels)

)


# Save the model to disk.

model.save_weights('model.h5')


隔江千里
浏览 156回答 1
1回答

慕田峪4524236

尝试使用此方法一次调整所有图像的大小 -#!pip install --upgrade tensorflow#Assuming you are using tensorflow 2import numpy as npimport tensorflow as tf#creating dummy imagesimgs = np.stack([np.eye(28), np.eye(28)])print(imgs.shape)#Output - (2,28,28) 2 images of 28*28imgt = imgs.transpose(1,2,0)  #Bring the batch channel to the end (28,28,2)imgs_resize = tf.image.resize(imgt, (16,16)).numpy() #apply resize (14,14,2)imgs2 = imgs_resize.transpose(2,0,1) #bring the batch channel back to front (2,14,14)print(imgs2.shape)#Output - (2,16,16)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python