如何将我的图像形状转换为我想要的形状?

问题描述 投票:0回答:1

我正在创建一个端到端的手写数字分类器。

import tensorflow as tf
from tensorflow import keras 
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()
X_train = X_train / 255
X_test = X_test / 25
X_train_flattened = X_train.reshape(len(X_train), 28*28)
X_test_flattened = X_test.reshape(len(X_test), 28*28)

model = keras.Sequential([
    keras.layers.Dense(10, input_shape=(784,), activation='sigmoid')
])

model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

转换下载的图像

from PIL import Image 
img = Image.open("C:/Users/hp/OneDrive/Documents/2.jpg")
img = np.array(img) # shape ----> (428, 398, 3)

我的模型在训练和测试数据集中工作正常,但现在我下载了图像并尝试将图像形状转换为 (28,28),但我收到以下错误。

img = img.reshape((28,28))

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[119], line 1
----> 1 img = img.reshape((28,28))

ValueError: cannot reshape array of size 511032 into shape (28,28)
python machine-learning deep-learning
1个回答
0
投票

您只需调整大小,无需重塑形状。

© www.soinside.com 2019 - 2024. All rights reserved.