Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import itertools
- import logging
- import tensorflow as tf
- import keras
- from keras import backend as K
- from keras.models import Sequential
- from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization
- from tensorflow.keras.optimizers import Adam
- from keras.utils import np_utils
- from keras import regularizers
- import matplotlib
- from matplotlib import pyplot as plt
- from sklearn.metrics import precision_recall_fscore_support, confusion_matrix
- import load_data
- import matplotlib
- import time
- matplotlib.use("Agg")
- # Not show the Warning information
- logging.getLogger("tensorflow").setLevel(logging.ERROR)
- # use the GPU to train the network
- CUDA_VISIBEL_DEVICES = 0
- # Models to be passed to Music_Genre_CNN
- song_labels = ["Blues", "Classical", "Country", "Disco", "Hip hop", "Jazz", "Metal", "Pop", "Reggae", "Rock"]
- MODEL_PATH = "CNN/"
- MAX_ITERATION = 100
- # remote interpreter path:
- # /root/miniconda3/envs/myconda/bin/python
- def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
- """
- This function prints and plots the confusion matrix.
- Normalization can be applied by setting `normalize=True`.
- """
- if normalize:
- cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
- print("Normalized confusion matrix")
- else:
- print('Confusion matrix, without normalization')
- print(cm)
- plt.imshow(cm, interpolation='nearest', cmap=cmap)
- plt.title(title)
- plt.colorbar()
- tick_marks = np.arange(len(classes))
- plt.xticks(tick_marks, classes, rotation=45)
- plt.yticks(tick_marks, classes)
- fmt = '.1f' if normalize else 'd'
- thresh = cm.max() / 2.
- for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
- plt.text(j, i, format(cm[i, j], fmt),
- horizontalalignment="center",
- color="white" if cm[i, j] > thresh else "black")
- plt.ylabel('True label')
- plt.xlabel('Predicted label')
- plt.tight_layout()
- def metric(y_true, y_pred):
- return K.mean(K.equal(K.argmax(y_true, axis=1), K.argmax(y_pred, axis=1)))
- def cnn(kernel_size=(4,4), num_genres=10, input_shape=(64, 173, 1), learning_rate=0.01):
- model = Sequential()
- model.add(Conv2D(64, kernel_size=kernel_size, activation='relu', input_shape=input_shape))
- model.add(BatchNormalization())
- model.add(MaxPooling2D(pool_size=(2, 4)))
- model.add(Conv2D(64, (3, 5), activation='relu', kernel_regularizer=regularizers.l2(0.04)))
- model.add(MaxPooling2D(pool_size=(2, 2)))
- model.add(Dropout(0.2))
- model.add(Conv2D(64, (2, 2), activation='relu'))
- # ,kernel_regularizer=regularizers.l2(0.04)
- model.add(BatchNormalization())
- model.add(MaxPooling2D(pool_size=(2, 2)))
- model.add(Dropout(0.2))
- model.add(Flatten())
- model.add(Dense(64, activation='relu', kernel_regularizer=regularizers.l2(0.04)))
- model.add(Dropout(0.5))
- model.add(Dense(32, activation='relu', kernel_regularizer=regularizers.l2(0.04)))
- model.add(Dense(num_genres, activation='softmax'))
- model.compile(loss=keras.losses.categorical_crossentropy,
- optimizer=Adam(learning_rate=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0),
- metrics=[metric])
- return model
- def model_predict(model_name, model, test_x, test_y):
- pred = model.predict(test_x, batch_size=32, verbose=1).argmax(axis=1)
- cnf_matrix = confusion_matrix(np.argmax(test_y, axis=1), pred)
- np.set_printoptions(precision=2)
- # visualization
- plt.figure()
- plot_confusion_matrix(cnf_matrix,
- classes=song_labels,
- normalize=True,
- title='Normalized confusion matrix')
- print(precision_recall_fscore_support(np.argmax(test_y, axis=1), pred, average='macro'))
- plt.savefig(str(model_name) + ".png", dpi=600)
- class Model(object):
- # Main network thingy to train
- @tf.autograph.experimental.do_not_convert
- def __init__(self, ann_model):
- self.model = ann_model()
- t = time.gmtime()
- @tf.autograph.experimental.do_not_convert
- def train_model(self, train_x, train_y, val_x=None, val_y=None, small_batch_size=300, max_iteration=MAX_ITERATION,
- print_interval=1, test_x=None, test_y=None,
- confusion_matrix_img_name=time.strftime("%m-%d %H:%M:%S", t)):
- batch_size = str(small_batch_size)
- train_accuracy_list = []
- train_loss_list = []
- validation_accuracy_list = []
- validation_loss_list = []
- test_accuracy_list = []
- test_loss_list = []
- epoch_list = []
- m = len(train_x)
- for it in range(max_iteration):
- # split training data into even batches
- batch_idx = np.random.permutation(m)
- train_x = train_x[batch_idx]
- train_y = train_y[batch_idx]
- num_batches = int(m / small_batch_size)
- for batch in range(num_batches):
- x_batch = train_x[batch * small_batch_size: (batch + 1) * small_batch_size]
- y_batch = train_y[batch * small_batch_size: (batch + 1) * small_batch_size]
- print("starting batch\t", batch, "\t Epoch:\t", it)
- # tf.autograph.experimental.do_not_convert(func=self.model.train_on_batch(x_batch, y_batch))
- self.model.train_on_batch(x_batch, y_batch)
- if it % print_interval == 0:
- validation_accuracy = self.model.evaluate(val_x, val_y)
- training_accuracy = self.model.evaluate(train_x, train_y)
- testing_accuracy = self.model.evaluate(test_x, test_y)
- # print of test error used only after development of the model
- print("\nTraining accuracy: %f\t Validation accuracy: %f\t Testing Accuracy: %f" %
- (training_accuracy[1], validation_accuracy[1], testing_accuracy[1]))
- print("\nTraining loss: %f \t Validation loss: %f \t Testing Loss: %f \n" %
- (training_accuracy[0], validation_accuracy[0], testing_accuracy[0]))
- print()
- # the data for draw
- epoch_list.append(it)
- train_accuracy_list.append(training_accuracy[1])
- train_loss_list.append(training_accuracy[0])
- validation_accuracy_list.append(validation_accuracy[1])
- validation_loss_list.append(validation_accuracy[0])
- test_accuracy_list.append(testing_accuracy[1])
- test_loss_list.append(testing_accuracy[0])
- # 验证集准确度>.81则输出训练的效果图 matrix
- if validation_accuracy[1] >= 0.8:
- print("Saving confusion data...")
- # 较早版本的
- model_name = "model_" + str(100 * validation_accuracy[1]) + "_" + str(100 * testing_accuracy[1]) + ".h5"
- # self.model.save(model_name)
- # pred = self.model.predict_classes(test_x, verbose=1)
- pred = self.model.predict(test_x, verbose=1).argmax(axis=1)
- cnf_matrix = confusion_matrix(np.argmax(test_y, axis=1), pred)
- np.set_printoptions(precision=2)
- # visualization
- plt.figure()
- plot_confusion_matrix(cnf_matrix,
- classes=song_labels,
- normalize=True,
- title='Normalized confusion matrix')
- print(precision_recall_fscore_support(np.argmax(test_y, axis=1), pred, average='macro'))
- plt.savefig(confusion_matrix_img_name + ".png", dpi=600)
- # break
- # save the png file and analyze the batch_size and the learning_rate
- fig = plt.figure()
- ax1 = fig.add_subplot(111)
- ax1.plot(epoch_list, train_accuracy_list, label="train_accuracy", color='blue')
- ax2 = ax1.twinx()
- ax2.plot(epoch_list, train_loss_list, label="train_loss", color='red')
- learning_rate = 0.01
- title_name = "batch_size=" + batch_size + " learning_rate=" + str(learning_rate)
- plt.legend()
- plt.title(title_name)
- plt.savefig(title_name + ".png", dpi=600)
- print(title_name, "saved successfully!")
- def main():
- # Data stuff
- data = load_data.loadall('melspects.npz')
- # tmp = np.load('melspects.npz')
- x_tr = data['x_tr']
- y_tr = data['y_tr']
- x_te = data['x_te']
- y_te = data['y_te']
- x_cv = data['x_cv']
- y_cv = data['y_cv']
- tr_idx = np.random.permutation(len(x_tr))
- te_idx = np.random.permutation(len(x_te))
- cv_idx = np.random.permutation(len(x_cv))
- x_tr = x_tr[tr_idx]
- y_tr = y_tr[tr_idx]
- x_te = x_te[te_idx]
- y_te = y_te[te_idx]
- x_cv = x_cv[cv_idx]
- y_cv = y_cv[cv_idx]
- x_tr = x_tr[:, :, :, np.newaxis]
- x_te = x_te[:, :, :, np.newaxis]
- x_cv = x_cv[:, :, :, np.newaxis]
- y_tr = np_utils.to_categorical(y_tr)
- y_te = np_utils.to_categorical(y_te)
- y_cv = np_utils.to_categorical(y_cv)
- # training = np.load('gtzan/gtzan_tr.npy')
- # x_tr = np.delete(training, -1, 1)
- # label_tr = training[:,-1]
- # test = np.load('gtzan/gtzan_te.npy')
- # x_te = np.delete(test, -1, 1)
- # label_te = test[:,-1]
- # cv = np.load('gtzan/gtzan_cv.npy')
- # x_cv = np.delete(cv, -1, 1)
- # label_cv = test[:,-1]
- # temp = np.zeros((len(label_tr),10))
- # temp[np.arange(len(label_tr)),label_tr.astype(int)] = 1
- # y_tr = temp
- # temp = np.zeros((len(label_te),10))
- # temp[np.arange(len(label_te)),label_te.astype(int)] = 1
- # y_te = temp
- # temp = np.zeros((len(label_cv),10))
- # temp[np.arange(len(label_cv)),label_cv.astype(int)] = 1
- # y_cv = temp
- # del temp
- #################################################
- # if True:
- # model = keras.models.load_model('model84.082.0.h5', custom_objects={'metric': metric})
- # print("Saving confusion data...")
- # pred = model.predict_classes(x_te, verbose=1)
- # cnf_matrix = confusion_matrix(np.argmax(y_te, axis=1), pred)
- # np.set_printoptions(precision=1)
- # plt.figure()
- # plot_confusion_matrix(cnf_matrix, classes=song_labels, normalize=True, title='Normalized confusion matrix')
- # print(precision_recall_fscore_support(np.argmax(y_te, axis=1),pred, average='macro'))
- # plt.savefig("matrix",format='png', dpi=1000)
- # raise SystemExit
- al = {
- "kernel_size": [],
- "learning_rate": [],
- "batch_size": [],
- }
- for a in [(4, 4), (3, 3), (5, 5)]:
- for b in [0.01, 0.001, 0.1]:
- for c in [300, 200, 400]:
- al["kernel_size"].append(a)
- al["learning_rate"].append(b)
- al["batch_size"].append(c)
- for i in range(0, len(al["kernel_size"])):
- confusion_filename = "kernel_size:" + str(al["kernel_size"][i]) + \
- " lr:" + str(al["learning_rate"][i]) + \
- " bs:" + str(al["batch_size"][i])
- print("train_network:",
- str(al["kernel_size"][i]),
- str(al["learning_rate"][i]),
- str(al["batch_size"][i]),
- confusion_filename
- )
- ml_train = cnn(kernel_size=al["kernel_size"][i], learning_rate=al["learning_rate"][i], input_shape=(64,173,1))
- ann = Model(ml_train)
- ann.train_model(confusion_matrix_img_name=confusion_filename,
- train_x=x_tr, train_y=y_tr, val_x=x_cv, val_y=y_cv, test_x=x_te, test_y=y_te)
- # ann = Model(cnn(kernel_size=(5, 5), learning_rate=0.01))
- # dot_img_file = 'model_cnn_architecture.png'
- # tf.keras.utils.plot_model(ann, to_file=dot_img_file, show_shapes=True)
- # ann.train_model(x_tr, y_tr, val_x=x_cv, val_y=y_cv, test_x=x_te, test_y=y_te)
- # for item in os.listdir():
- # if item.rfind(".h5") != -1:
- # model_predict(item, keras.models.load_model(item, custom_objects={'metric': metric}), x_te, y_te)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement