# -*- coding: utf-8 -*- """Classification_Fashion_MNIST.ipynb """ !pip uninstall tensorflow """Installing TensorFlow 2.0.0.""" !pip install tensorflow===2.0.0 """Check TensorFolow Version:""" import tensorflow as tf print(tf.__version__) """Now the next step is to load MNIST dataset and then prepare it. As a part of data preparation process integers from the sample data set has to be converted to floating-point numbers. Here is the code:""" import tensorflow as tf mnist = tf.keras.datasets.fashion_mnist (x_train, labels_train), (x_test, labels_test) = mnist.load_data() x_train, x_test = x_train/225.0, x_test/255.0 """Variables to hold image categories:""" class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress','Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] """Check shape of the image:""" print(x_train[0].shape) """Print Image:""" import matplotlib.pyplot as plt plt.figure() plt.imshow(x_train[0]) plt.colorbar() plt.grid(False) plt.show() """Images categories: Print some of the images with category name: """ plt.figure(figsize=(10,10)) for i in range(25): plt.subplot(5,5,i+1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(x_train[i]) plt.xlabel(class_names[labels_train[i]]) plt.show() """If you want to show the grey image use following code:""" plt.figure(figsize=(10,10)) for i in range(25): plt.subplot(5,5,i+1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(x_train[i], cmap=plt.cm.binary) plt.xlabel(class_names[labels_train[i]]) plt.show() """Now we will create model with following code:""" model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10, activation='softmax') ]) """Here is the code to display model summary:""" model.summary() """Now we will compile our model with following code:""" model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) """Finally run the training with following code:""" model.fit(x_train, y_train, epochs=5) """Now let's evaluate our model with following code:""" model.evaluate(x_test, y_test) """Run the predictions on the test data with following code:""" predictions = model.predict(x_test) """Print the pridiction result:""" print(predictions[3]) """Print""" import numpy as np def plot_image(i, predictions_array, true_label, img): predictions_array, true_label, img = predictions_array, true_label[i], img[i] plt.grid(False) plt.xticks([]) plt.yticks([]) plt.imshow(img) predicted_label = np.argmax(predictions_array) if predicted_label == true_label: color = 'blue' else: color = 'red' plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label], 100*np.max(predictions_array), class_names[true_label]), color=color) #print the images num_rows = 5 num_cols = 4 num_images = num_rows*num_cols plt.figure(figsize=(2*num_cols, 2*num_rows)) for i in range(num_images): plt.subplot(num_rows, num_cols, i+1) plot_image(i, predictions[i], labels_test, x_test) plt.tight_layout() plt.show()