SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from keras.utils import to_categorical
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.metrics import confusion_matrix
  4.  
  5. import keras
  6. from keras.models import Sequential
  7. from keras.layers import Dense
  8. from keras.layers import Activation
  9. from keras.layers import Conv2D
  10. from keras.layers import MaxPooling2D
  11. from keras.layers import Dropout
  12. from keras.layers import Flatten
  13. from keras.layers import BatchNormalization
  14. import numpy as np
  15.  
  16. X = np.load('x_gtzan_npy.npy')
  17. y = np.load('y_grzan_npy.npy')
  18. y = to_categorical(y)
  19.  
  20. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify = y)
  21. X_val = X_train[:3800]
  22. y_val = y_train[:3800]
  23. X_train = X_train[3800:]
  24. y_train = y_train[3800:]
  25.  
  26.  
  27. # Model Definition
  28. input_shape = X_train[0].shape
  29. print (input_shape)
  30.  
  31. model = Sequential()
  32. # Conv Block 1
  33. model.add(Conv2D(8, kernel_size=(3, 3), strides=(1, 1),
  34.                  activation='relu', input_shape=input_shape))
  35. model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
  36. model.add(Dropout(0.25))
  37.  
  38. # Conv Block 2
  39. model.add(Conv2D(16, (3, 3), strides=(1, 1), activation='relu'))
  40. model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), data_format="channels_last"))
  41. model.add(Dropout(0.25))
  42.  
  43. # Conv Block 3
  44. model.add(Conv2D(32, (3, 3), strides=(1, 1), activation='relu'))
  45. model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), data_format="channels_last"))
  46. model.add(Dropout(0.25))
  47.  
  48. # Conv Block 4
  49. model.add(Conv2D(64, (3, 3), strides=(1, 1), activation='relu'))
  50. model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), data_format="channels_last"))
  51. model.add(Dropout(0.25))
  52.  
  53. # Conv Block 5
  54. model.add(Conv2D(32, (3, 3), strides=(1, 1), activation='relu'))
  55. model.add(MaxPooling2D(pool_size=(1, 1), strides=(1, 1), data_format="channels_last"))
  56. model.add(Dropout(0.25))
  57.  
  58. # MLP
  59. model.add(Flatten())
  60. model.add(Dense(10, activation='softmax'))
  61.  
  62. model.summary()
  63.  
  64. model.compile(loss=keras.losses.categorical_crossentropy,
  65.               optimizer=keras.optimizers.Adam(),
  66.               metrics=['accuracy'])
  67.  
  68. hist = model.fit(X_train, y_train,
  69.           batch_size=32,
  70.           epochs=50,
  71.           verbose=1,
  72.           validation_data=(X_val, y_val))
  73.  
  74. score = model.evaluate(X_test, y_test, verbose=0)
  75. print("val_loss = {:.3f} and val_acc = {:.3f}".format(score[0], score[1]))
  76.  
  77. input_shape = X_train[0].shape
  78.  
  79. def cnn_vgg16(input_shape, freezed_layers):
  80.     input_tensor = Input(shape=input_shape)
  81.     vgg16 = VGG16(include_top=False, weights='imagenet',
  82.                   input_tensor=input_tensor)
  83.  
  84.     top = Sequential()
  85.     top.add(Flatten(input_shape=vgg16.output_shape[1:]))
  86.     top.add(Dense(128, activation='relu'))
  87.     top.add(Dropout(0.5))
  88.     top.add(Dense(10, activation='softmax'))
  89.  
  90.     model = Model(inputs=vgg16.input, outputs=top(vgg16.output))
  91.     for layer in model.layers[:freezed_layers]:
  92.         layer.trainable = False
  93.  
  94.     return model
  95.  
  96. model = cnn_vgg16(input_shape, 5)
  97. model.summary()
  98.  
  99. model.compile(loss=keras.losses.categorical_crossentropy,
  100.               optimizer=keras.optimizers.Adam(),
  101.               metrics=['accuracy'])
  102.  
  103. hist = model.fit(X_train, y_train,
  104.           batch_size=128,
  105.           epochs=20,
  106.           verbose=1,
  107.           validation_data=(X_val, y_val))
  108.  
  109. score = model.evaluate(X_test, y_test, verbose=0)
  110. print("val_loss = {:.3f} and val_acc = {:.3f}".format(score[0], score[1]))
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