Ticker

6/recent/ticker-posts

Keras Image Prediction - Fashion Mnist Dataset

 


In this post, we are gonna make neural network using Keras and we would determine the apparels in the fashion_mnist dataset preinstalled in Keras. So we are gonna train our program on the different apparels items and then we would predict some apparels using our self made Neural Network.

we are gonna use the libraries like Keras, Tensorflow, Numpy, Matplotlib to plot our images on the graph.

There's no need to download any dataset as the fashion_mnist dataset that we're using is preinstalled in Keras.


Preview:

  import numpy as np
  import matplotlib.pyplot as plt
  from tensorflow import keras
  import tensorflow as tf 


  data = keras.datasets.fashion_mnist

  # get test and training data

  (train_images, train_labels) , (test_images, test_labels) = data.load_data()

  class_names = ["t-shirt", "trouser", "pullover", "dress", "coat"
               ,"sandal", "shirt", "sneaker", "bag", 'ankleboot']


  # convert the data from 0 and 1

  train_images= train_images/255.0
  train_labels= train_labels/255.0

  # build the model with keras input,hidden and output layers
  model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)), #input layer with flattened data ie no line arrays
    keras.layers.Dense(128, activation="relu"), #hidden layer with 128 nodes
    keras.layers.Dense(10, activation="softmax") #output layer with 10 nodes/neurons
  ])

  # compile the model

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

  # epoch means shuffling the same images from the dataset and giving it again to the 
  #  for more accuracy.
  
  model.fit(train_images, train_labels, epochs=4)

  test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=1)

  print(test_acc)

  prediction = model.predict(test_images)
  for x in range(200,204):
     print(test_labels[x])
     print(class_names[np.argmax(prediction[x])])


Post a Comment

0 Comments