SHARE
TWEET

keras fashion dataset

mdan Mar 23rd, 2018 (edited) 156 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from keras.datasets import fashion_mnist
  2.  
  3. (train_X, train_Y), (test_X, test_Y) = fashion_mnist.load_data()
  4.  
  5. import numpy as np
  6. from keras.utils import to_categorical
  7. import matplotlib.pyplot as plt
  8.  
  9. print('Training data shape : ', train_X.shape, train_Y.shape)
  10. print('Testing data shape : ', test_X.shape, test_Y.shape)
  11.  
  12.  
  13. classes = np.unique(train_Y)
  14. nClasses = len(classes)
  15. print('Total number of outputs : ', nClasses)
  16. print('Output classes : ', classes)
  17.  
  18. plt.figure(figsize=[5, 5])
  19.  
  20. # Cam așa arată prima imagine din setul de trainig
  21. '''
  22. plt.subplot(121)
  23. plt.imshow(train_X[10, :, :], cmap='gray')
  24. plt.title("Ground Truth : {}".format(train_Y[0]))
  25. plt.savefig('prima_imagine_train.png')
  26. '''
  27. # Cam așa arată prima imagine din setul de test
  28. '''
  29. plt.subplot(122)
  30. plt.imshow(test_X[10, :, :], cmap='gray')
  31. plt.title("Ground Truth : {}".format(test_Y[0]))
  32. plt.savefig('prima_imagine_test.png')
  33. '''
  34.  
  35. train_X = train_X.reshape(-1, 28, 28, 1)
  36. test_X = test_X.reshape(-1, 28, 28, 1)
  37. print(train_X.shape, test_X.shape)
  38.  
  39. train_X = train_X.astype('float32')
  40. test_X = test_X.astype('float32')
  41. train_X = train_X / 255.
  42. test_X = test_X / 255.
  43.  
  44. # Îmi place de mor one-hot encoding
  45. train_Y_one_hot = to_categorical(train_Y)
  46. test_Y_one_hot = to_categorical(test_Y)
  47.  
  48. print('Original label:', train_Y[0])
  49. print('After conversion to one-hot:', train_Y_one_hot[0])
  50.  
  51. from sklearn.model_selection import train_test_split
  52.  
  53. train_X, valid_X, train_label, valid_label = train_test_split(train_X, train_Y_one_hot, test_size=0.2, random_state=13)
  54.  
  55. print(train_X.shape, valid_X.shape, train_label.shape, valid_label.shape)
  56.  
  57. import keras
  58. from keras.models import Sequential, Input, Model
  59. from keras.layers import Dense, Dropout, Flatten
  60. from keras.layers import Conv2D, MaxPooling2D
  61. from keras.layers.normalization import BatchNormalization
  62. from keras.layers.advanced_activations import LeakyReLU
  63.  
  64.  
  65. batch_size = 64
  66. epochs = 20
  67. num_classes = 10
  68.  
  69. fashion_model = Sequential()
  70. fashion_model.add(Conv2D(32, kernel_size=(3, 3), activation='linear', padding='same', input_shape=(28, 28, 1)))
  71. fashion_model.add(LeakyReLU(alpha=0.1))
  72. fashion_model.add(MaxPooling2D((2, 2), padding='same'))
  73. fashion_model.add(Dropout(0.25))
  74. fashion_model.add(Conv2D(64, (3, 3), activation='linear', padding='same'))
  75. fashion_model.add(LeakyReLU(alpha=0.1))
  76. fashion_model.add(MaxPooling2D(pool_size=(2, 2), padding='same'))
  77. fashion_model.add(Dropout(0.25))
  78. fashion_model.add(Conv2D(128, (3, 3), activation='linear', padding='same'))
  79. fashion_model.add(LeakyReLU(alpha=0.1))
  80. fashion_model.add(MaxPooling2D(pool_size=(2, 2), padding='same'))
  81. fashion_model.add(Dropout(0.4))
  82. fashion_model.add(Flatten())
  83. fashion_model.add(Dense(128, activation='linear'))
  84. fashion_model.add(LeakyReLU(alpha=0.1))
  85. fashion_model.add(Dropout(0.3))
  86. fashion_model.add(Dense(num_classes, activation='softmax'))
  87.  
  88. from keras.utils import plot_model
  89. plot_model(fashion_model, to_file='model.png')
  90.  
  91. fashion_model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(),
  92.                       metrics=['accuracy'])
  93.  
  94. print(fashion_model.summary())
  95.  
  96. fashion_train_dropout = fashion_model.fit(train_X, train_label, batch_size=batch_size, epochs=epochs, verbose=1,
  97.                                           validation_data=(valid_X, valid_label))
  98.  
  99. fashion_model.save("fashionistu.h5py")
  100.  
  101. test_eval = fashion_model.evaluate(test_X, test_Y_one_hot, verbose=0)
  102. print('Test loss:', test_eval[0])
  103. print('Test accuracy:', test_eval[1])
  104.  
  105. accuracy = fashion_train_dropout.history['acc']
  106. val_accuracy = fashion_train_dropout.history['val_acc']
  107. loss = fashion_train_dropout.history['loss']
  108. val_loss = fashion_train_dropout.history['val_loss']
  109. epochs = range(len(accuracy))
  110.  
  111. plt.figure(figsize=[5, 5])
  112.  
  113. plt.figure()
  114. plt.subplot(121)
  115. plt.plot(epochs, accuracy, 'bo', label='Training accuracy')
  116. plt.plot(epochs, val_accuracy, 'b', label='Validation accuracy')
  117. plt.title('Training and validation accuracy')
  118. plt.legend()
  119. plt.savefig('UNU.png')
  120. plt.show()
  121.  
  122.  
  123. plt.figure()
  124. plt.subplot(122)
  125. plt.plot(epochs, loss, 'bo', label='Training loss')
  126. plt.plot(epochs, val_loss, 'b', label='Validation loss')
  127. plt.title('Training and validation loss')
  128. plt.legend()
  129. plt.savefig('DOI.png')
  130. plt.show()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top