import numpy as np import matplotlib.pyplot as plt from tensorflow.keras.preprocessing import image from tensorflow.keras.models import load_model import tensorflow as tf # # 加载Fashion-MNIST数据集 # fashion_mnist = tf.keras.datasets.fashion_mnist # (X_train, y_train), (X_test, y_test) = fashion_mnist.load_data() # # # 数据预处理 # X_train = X_train / 255.0 # 将像素值缩放到0-1之间 # X_test = X_test / 255.0 # # # 加载模型 # model = load_model('fashion_mnist_model.h5') # # # 评估模型 # loss, accuracy = model.evaluate(X_test, y_test) # print(f'Test Loss: {loss}') # print(f'Test Accuracy: {accuracy}') # 图像文件路径 img_path = '运动鞋.png' # 加载图像并调整大小 img = image.load_img(img_path, target_size=(28, 28), color_mode='grayscale') # 将PIL图像转换为NumPy数组 img_array = image.img_to_array(img) / 255.0 # 添加批量维度 img_array = np.expand_dims(img_array, axis=0) # 显示图像 plt.imshow(img_array[0, :, :, 0], cmap='gray') plt.show() # 打印图像数组形状 print(f'Image array shape: {img_array.shape}') # 加载训练好的模型 model = load_model('fashion_mnist_model.h5') # 进行预测 predictions = model.predict(img_array) predicted_class = np.argmax(predictions, axis=1) print(f'Predicted class: {predicted_class[0]}')