如何解决:无法预测时尚mnist张量流样本中本地存储的图像

时间:2019-10-03 10:07:13

标签: tensorflow

输入图像 Input images input images in jpg

当我在预测后输出它们时,您可以看到白色变成黑色,黑色变成白色 out put when three images

我已经使用了来自keras的tensorflow和fashionmist数据库来训练我的模型。现在,我尝试预测从互联网上采样的图像。他们被预测为错误的。另外,当我绘制图像时,我看到图像白色变成黑色,黑色变成白色

   import os
   import PIL
   from keras_preprocessing.image import load_img
   import tensorflow as tf
   from tensorflow import keras
   from PIL import Image

   import pandas as pd
   import numpy as np
   import matplotlib.pyplot as plt

   fashion_mnist = keras.datasets.fashion_mnist
   (train_images, train_label),(test_images, test_label) = 
    fashion_mnist.load_data()
  class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
           'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

  train_images = train_images/255
   test_images = test_images/255
  model = keras.models.Sequential(
[keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation="relu"),
keras.layers.Dense(10, activation="softmax")
])
      model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
      model.fit(train_images, train_label, epochs=5)

  # load the image
  batch_holder = np.zeros((2,28, 28))
 img_dir= 'C:/images/'
   for i,img in enumerate(os.listdir(img_dir)):
         img = load_img(os.path.join(img_dir,img), target_size= 
          (28,28),color_mode="grayscale")
           batch_holder[i, :] = img
  batch_holder = batch_holder/255
 fig = plt.figure(figsize=(20, 20))

    result = model.predict(batch_holder)
    for i, img in enumerate(batch_holder):
        fig.add_subplot(4, 5, i + 1)
        plt.title(class_names[np.argmax(result[i])])
       plt.imshow(img, cmap=plt.cm.binary)
plt.show()

1 个答案:

答案 0 :(得分:0)

代替初始化为零,代替
batch_holder = np.zeros((2,28,28))as
batch_holder = 255 * np.ones((2,28,28))并将dtype更改为uint8。另外,如果将其设置为灰度,则将形状视为((1,28,28))。