亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 MNIST 訓練圖像從 (60000, 28, 28) 重塑為

如何將 MNIST 訓練圖像從 (60000, 28, 28) 重塑為

隔江千里 2023-05-09 10:54:47
我正在嘗試使用 Keras 學習具有簡單密集層的 MNIST 數據集。我希望我的圖像大小為 16*16 而不是 28*28。我用了很多方法,但都不管用。這是簡單的密集網絡:import kerasimport numpy as npimport mnistfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Densefrom tensorflow.keras.utils import to_categoricaltrain_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.5test_images = (test_images / 255) - 0.5print(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')
查看完整描述

1 回答

?
慕田峪4524236

TA貢獻1875條經驗 獲得超5個贊

嘗試使用此方法一次調整所有圖像的大小 -


#!pip install --upgrade tensorflow

#Assuming you are using tensorflow 2


import numpy as np

import tensorflow as tf


#creating dummy images

imgs = np.stack([np.eye(28), np.eye(28)])

print(imgs.shape)

#Output - (2,28,28) 2 images of 28*28



imgt = 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)


查看完整回答
反對 回復 2023-05-09
  • 1 回答
  • 0 關注
  • 195 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號