本文介绍了如果图像具有 (28,28,3) 形状,我如何将其转换为 (28.28,1)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用手写数字的mnist数据集,并且试图预测自己写的数字.问题是我的数字的形状是 (28,28,3),而我的神经网络的预期形状是 (28,28,1).如何转换?
I'm using the mnist dataset of handwritten digits and I am trying to predict a digit that I wrote. The problem is that my digit is of shape (28,28,3) and the expected shape for my neural network is (28,28,1). How can I convert it?
我的代码:
import tensorflow as to
from tensorflow import keras
from keras.datasets import mnist
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import cv2
data = mnist.load_data()
(x_train, y_train), (x_test, y_test) = data
classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
x_train = x_train / 255
x_test = x_test / 255
model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=(28,28)))
model.add(keras.layers.Dense(128, activation='relu'))
model.add(keras.layers.Dense(10, activation='softmax'))
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
model.fit(x_train, y_train, epochs=7)
test_loss, test_acc = model.evaluate(x_test, y_test)
print('\nTest Loss:', test_loss)
print('Test accuracy:', test_acc)
img = Image.open("7.jpg").convert('L')
img_array = cv2.imread('7.jpg')
new_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY)
new_array = cv2.resize(new_array, (28,28))
print(new_array.shape)
print(x_test[0].shape)
plt.imshow(new_array, cmap='gray')
plt.show()
predictions = model.predict(new_array)
plt.grid(False)
plt.imshow(new_array, cmap='gray')
plt.title("Prediction: " + classes[np.argmax(predictions)])
plt.show()
推荐答案
假设 img 的形状为(28,28,3),您可以执行以下操作:
Assuming img has shape of (28, 28, 3) you can do this:
gray = cv2. cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.expand_dims(gray, 2)
这会将其转换为(28,28,1)的形状
This will convert it to a shape of (28, 28, 1)
这篇关于如果图像具有 (28,28,3) 形状,我如何将其转换为 (28.28,1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!