Fashion-MNIST: Get Start with Deep Learning in Keras (Tensorflow based)

    xiaoxiao2022-06-28  140

     

    The code is a material for Holi's tutorial of 'Introduction to Machine Learning', Code and Coffee, GDG Suzhou. Thanks for Tensorflow team to provide the tutorial for us and then make it much easier for us to learn machine learning. Put the efforts into good applications instead.

    (IGNORE!)Just before the beginning, let me introduce myself. I am Holi, a state-of-the-art Machine Learning Engineer and Data science analytics finely educated in England. I have a 4-year experiences in Deep learning etc. from this writing. This tutorial is for the developers who are interested in Machine Learning and would love to learn some basic espects from me. Thank you all for attending the meeting in GDG Suzhou :) The post is for the reminding of the essential steps in the tutorial.

    Requirements:

    (recommended) Anaconda >3Python >3.6Tensorflow >1.8

    *It is important to notice the requirements of the version of Python and Tensorflow is bounded and be careful !!! And the code is written in Python, so if you find the plot-ish code is not available in IPython (Jupyter), remember it was already predicted by me. Just Google/Bing a optional solution for the 'bug'.

    Keras,(option) Jupyter NotebookGraphviz-0.10.1, downloaded from the official site and add it into system environmentPydotplus(windows) refer:http://www.mamicode.com/info-detail-2566461.html

    * pydot does not support python >3.5 anymore as reference says. after 'pip install pydotplus'. Find the filepath for keras.util.vis_utils and copy the file first. then alter the file and change every 'pydot' into 'pydotplus'.

    Pydot_ng-2.0.0 refer:https://blog.csdn.net/u013899126/article/details/88319505(if no conda) Numpy, Matplotlib

    Prepare the dataset Fachion-MNIST

    Download urls:

    1.(recommended) FROM MY DOWNLOAD URL

    2. https://github.com/zalandoresearch/fashion-mnist

    3. https://www.kaggle.com/zalando-research/fashionmnist

    The example format of the dataset downloaded and the sub-filepath saved (./MNIST_data)

    OKAY~ JUST CODE IT!

    Foa, import the packages needed for the tutorial whish should be downloaded in advance.

    import tensorflow as tf from tensorflow import keras import matplotlib.pyplot as plt import numpy as np from keras.utils.vis_utils import plot_model # for plot keras model

    Next, we load Data from keras load() function, easy and clean. The necessary requirement of this step is to download and save the dataset as I presented above, otherwise, it is required to load the data the other way (not instructed in this tutorial).

    We print the shape and the format of the dataset to glimpse at the Fashion

    fashion_mnist = keras.datasets.fashion_mnist (train_images, train_labels),(test_images, test_labels) = fashion_mnist.load_data() print('Training data has a shape of {} with {} labels. \n The format of the training labels is {}'.format(train_images.shape,len(train_labels),train_labels)) print('Testing data has a shape of {} with {} labels. \n The format of the testing labels is {}'.format(test_images.shape,len(test_labels),test_labels)) Training data has a shape of (60000, 28, 28) with 60000 labels. The format of the training labels is [9 0 0 ... 3 0 5] Testing data has a shape of (10000, 28, 28) with 10000 labels. The format of the testing labels is [9 2 1 ... 8 1 5]

    Since this is a classification task, we could have the class names for plotting data. In a case of Chinese readers, I give the names in Chinese.

    class_names = ['T恤','裤子','套衫','裙子','外套','凉鞋','衬衫','运动鞋','包包','裸靴']

    That is the first steps to load data. Then i would like to see what the data or image really is. Actually, we can complish this in only five lines. The code use matplotlib to plot the first training image in a figure.

    plt.figure() plt.imshow(train_images[0]) plt.colorbar() plt.grid(False) plt.show()

    Then i'd like to present you the images for the classes. So I plot 25 images, each corresponding to their class name label.

    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(train_images[i]) plt.xlabel(class_names[train_labels[i]]) plt.show()

    However, cuz i just use Chinese in naming these classes. Well, we would probably get a bunch of unreadable code for presenting the names. So i add two lines to fix the problem.

    # 中文乱码解决 from matplotlib.font_manager import FontProperties font_set = FontProperties(fname=r"c:\windows\fonts\msyh.ttc", size=9) 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(train_images[i]) plt.xlabel(class_names[train_labels[i]],fontproperties=font_set) plt.show()

    Now it should be fine.

    But these colurs are not the actual colors for those stuff. Plus, we don't really need the color labels in this task. So, gray scale it! Why? I would explain it more detailedly in the tutorial. Now I just show you the code and the show.

    # gray scale train_gray = train_images / 255.0 test_gray = test_images / 255.0 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(train_gray[i],cmap=plt.cm.binary) plt.xlabel(class_names[train_labels[i]],fontproperties=font_set) plt.show()

    Okey, so we have these gray-scaled data. Now what?

    x_train = train_gray.reshape(-1, 28, 28,1).astype('float32') x_test = test_gray.reshape(-1,28, 28,1).astype('float32') # if K.image_data_format() == 'channels_first': # x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) # x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) # input_shape = (1, img_rows, img_cols) # else: # x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) # x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) # input_shape = (img_rows, img_cols, 1)

    Then we can establish our kingdom of model in Keras.

    model = keras.Sequential([ keras.layers.Conv2D(16,(3,3), padding = 'same', activation = tf.nn.relu, input_shape=(28,28,1), name = "conv1"), keras.layers.MaxPooling2D(pool_size=(2, 2), name="maxpul1"), keras.layers.Dropout(0.25), keras.layers.Conv2D(32,(3,3), padding = 'same', activation = tf.nn.relu, name = "conv2"), keras.layers.MaxPooling2D(pool_size=(2, 2), name = "maxpul2"), keras.layers.Dropout(0.25), keras.layers.Flatten(), keras.layers.Dense(128,activation = tf.nn.relu), keras.layers.Dropout(0.25), keras.layers.Dense(10,activation= tf.nn.softmax) ])

    Next, compile and fit the model. Be careful for the hyper-parameters in the middle of the construction.

    Keras  API document: https://keras-cn.readthedocs.io/en/latest/legacy/models/model/

    model.compile( optimizer='adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy']) model.fit(x_train, train_labels, batch_size = 32, epochs = 1, shuffle = True, validation_split = 0.1)

    Now we might have the standard output like this 

    Detailed Information listed below.

    WARNING:tensorflow:From C:\Users\lenovo\Anaconda3\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version. Instructions for updating: Colocations handled automatically by placer. WARNING:tensorflow:From C:\Users\lenovo\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\core.py:143: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version. Instructions for updating: Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`. Train on 54000 samples, validate on 6000 samples 2019-05-25 14:34:58.805028: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 Epoch 1/5 54000/54000 [==============================] - 27s 493us/sample - loss: 0.5456 - acc: 0.8007 - val_loss: 0.3466 - val_acc: 0.8700 Epoch 2/5 54000/54000 [==============================] - 30s 562us/sample - loss: 0.3794 - acc: 0.8634 - val_loss: 0.3130 - val_acc: 0.8837 Epoch 3/5 54000/54000 [==============================] - 34s 630us/sample - loss: 0.3329 - acc: 0.8786 - val_loss: 0.2807 - val_acc: 0.8948 Epoch 4/5 4032/54000 [=>............................] - ETA: 24s - loss: 0.3328 - acc: 0.8822

    Save the model, do not let the training effort vanished.

    model.save('./model/fashion_mnist_model.h5') plot_model(model, to_file='./model/model.png')

    Ahah, we finally can test and see what we have trained with the model.

    test_loss, test_acc = model.evaluate(x_test,test_labels) print('Test accuracy is {}'.format(test_acc)) predictions = model.predict(x_test) print("The prediction of the first image is {} with the true label as {}.".format(np.argmax(predictions[0]),test_labels[0])) print("The actual output probability is {}.".format(predictions[0]))

    The average test accuracy with 10000 images is shown below 

    10000/10000 [==============================] - 2s 203us/sample - loss: 0.3723 - acc: 0.8653 Test accuracy is 0.8652999997138977

    I try to print out the result of the first image prediction

    The prediction of the first image is 9 with the true label as 9. The actual output probability is [4.9982407e-07 1.0641853e-08 1.1934756e-08 2.0986131e-07 9.4430469e-08 6.8057090e-04 2.6915556e-08 4.4792579e-03 8.6193342e-07 9.9483842e-01].

    But if we would like to have a close look at the images and make the visualisation more friendly to the users.

    def plot_image(i, predictions_array, true_label, img): predictions_array, true_label, img = predictions_array[i], true_label[i], img[i] plt.grid(False) plt.xticks([]) plt.yticks([]) plt.imshow(img, cmap=plt.cm.binary) 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,fontproperties=font_set) def plot_value_array(i, predictions_array, true_label): predictions_array, true_label = predictions_array[i], true_label[i] plt.grid(False) plt.xticks([]) plt.yticks([]) thisplot = plt.bar(range(10), predictions_array, color="#777777") plt.ylim([0, 1]) predicted_label = np.argmax(predictions_array) thisplot[predicted_label].set_color('red') thisplot[true_label].set_color('blue') i = 0 plt.figure(figsize=(6,3)) plt.subplot(1,2,1) plot_image(i, predictions, test_labels, test_images) plt.subplot(1,2,2) plot_value_array(i, predictions, test_labels) plt.show() # Plot the first X test images, their predicted label, and the true label # Color correct predictions in blue, incorrect predictions in red num_rows = 5 num_cols = 3 num_images = num_rows*num_cols plt.figure(figsize=(2*2*num_cols, 2*num_rows)) for i in range(num_images): plt.subplot(num_rows, 2*num_cols, 2*i+1) plot_image(i, predictions, test_labels, test_images) plt.subplot(num_rows, 2*num_cols, 2*i+2) plot_value_array(i, predictions, test_labels) plt.show()

    Got the single plot

    And the plotting for top 25 results.

    Appreciate for your time! Seeya~


    最新回复(0)