user641812
user641812

Reputation: 335

How to fix: doesn't predict images stored locally in fashion mnist tensor flow sample

Input images Input images input images in jpg

when I output them after prediction you can see white changes into black and black into white out put when three images

I have used tensorflow and fashionmist database from keras to train my model. Now i try to predict the images that i took sample from internet. They are predicted wrong. Also when i plot the images i see image white changes into black and black into wite

   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()

Upvotes: 0

Views: 303

Answers (1)

BabaYaga
BabaYaga

Reputation: 111

Instead of initializing as zeros, replace
batch_holder = np.zeros((2,28, 28)) as
batch_holder = 255*np.ones((2,28, 28)) and change the dtype to uint8. Also if you are setting it to grayscale then consider shape as ((1,28,28)).

Upvotes: 0

Related Questions