You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.1 KiB

  1. import librosa
  2. import scipy.signal as signal
  3. import tensorflow as tf
  4. from tensorflow.keras.models import Sequential
  5. from tensorflow.keras.layers import Dense
  6. import numpy as np
  7. x_train = []
  8. y_train = []
  9. chords = ['C', 'D', 'E', 'F', 'G', 'A', 'B', 'HC']
  10. path = '/Users/seungwoomun/Documents/MYC/train/Grand Piano/'
  11. paths = '/Users/seungwoomun/Documents/MYC/train/Grand Piano/C_1.wav'
  12. # file_name = ["C_1.wav", "D_1.wav"]
  13. # 도는 [1,0], 레는 [0,1]로 설정한다
  14. lable = [[1, 0, 0, 0, 0, 0, 0, 0], # 도 C
  15. [0, 1, 0, 0, 0, 0, 0, 0], # 레 D
  16. [0, 0, 1, 0, 0, 0, 0, 0], # 미 E
  17. [0, 0, 0, 1, 0, 0, 0, 0], # 파 F
  18. [0, 0, 0, 0, 1, 0, 0, 0], # 솔 G
  19. [0, 0, 0, 0, 0, 1, 0, 0], # 라 A
  20. [0, 0, 0, 0, 0, 0, 1, 0], # 시 B
  21. [0, 0, 0, 0, 0, 0, 0, 1]] # 도 HC
  22. # train data 생성
  23. for index in range(len(chords)):
  24. audio_sample, sampling_rate = librosa.load(path + chords[index] + '_1.wav', sr = 44100)
  25. input_data = np.abs(librosa.stft(audio_sample, n_fft = 2048, hop_length = 1024, win_length = 2048, window=signal.hann))
  26. # input_data의 배열 형태를 알아본다
  27. shape = np.shape(input_data)
  28. nb_samples = shape[0]
  29. nb_windows = shape[1]
  30. # [nb_samples][nb_windows] 형태이므로, 딥러닝을 수행하기 위한 배열 형태 [nb_windows][nb_samples]로 맞춰준다
  31. input_data = input_data.T
  32. # 학습용 레이블 배열을 생성한다
  33. lable_tmp = [lable[index] for row in range(nb_windows)]
  34. # 하나의 배열로 합친다
  35. if index == 0:
  36. x_train = input_data
  37. y_train = lable_tmp
  38. else:
  39. x_train = np.r_[x_train, input_data]
  40. y_train = np.r_[y_train, lable_tmp]
  41. # test data 생성
  42. for indexs in range(len(chords)):
  43. audio_sample2, sampling_rate2 = librosa.load(path + chords[indexs] + '_3.wav', sr = 44100)
  44. input_data2 = np.abs(librosa.stft(audio_sample2, n_fft = 2048, hop_length = 1024, win_length = 2048, window=signal.hann))
  45. # input_data의 배열 형태를 알아본다
  46. shape2 = np.shape(input_data2)
  47. nb_samples2 = shape2[0]
  48. nb_windows2 = shape2[1]
  49. # [nb_samples][nb_windows] 형태이므로, 딥러닝을 수행하기 위한 배열 형태 [nb_windows][nb_samples]로 맞춰준다
  50. input_data2 = input_data2.T
  51. # 학습용 레이블 배열을 생성한다
  52. lable_tmp2 = [lable[indexs] for row in range(nb_windows2)]
  53. # 하나의 배열로 합친다
  54. if indexs == 0:
  55. x_test = input_data2
  56. y_test = lable_tmp2
  57. else:
  58. x_test = np.r_[x_test, input_data2]
  59. y_test = np.r_[y_test, lable_tmp2]
  60. # print(x_train.shape)
  61. # print(y_train.shape)
  62. # print(x_test.shape)
  63. # print(y_test.shape)
  64. # model 생성
  65. model = Sequential()
  66. model.add(Dense(units = 1025, input_dim = 1025)) # input_shape = (513, )
  67. model.add(Dense(units = 128, activation = 'relu'))
  68. model.add(Dense(8, activation = 'softmax'))
  69. # model.add(Dense(128, activation = 'relu'))
  70. # model.add(Dense(8, input_dim = 128, activation = 'relu'))
  71. # model.add(Dense(1, activation = 'sigmoid'))
  72. model.summary()
  73. model.compile(optimizer = 'sgd', loss = 'mse', metrics = ['acc'])
  74. # x_train = [[2, 4, 8], [3, 6, 9], [4, 8, 12]]
  75. # y_train = [[2], [3], [4]]
  76. # x_test = [[2, 4, 8]]
  77. # 학습
  78. model.fit(x_train, y_train, epochs = 500, verbose = 1, validation_data = (x_test, y_test)) # batch_size = 257
  79. # 평가
  80. print("####### Model 평가 #######")
  81. loss, acc = model.evaluate(x_test, y_test)
  82. print("복원된 모델의 정확도: {:5.2f}%".format(100 * acc))
  83. # 검증
  84. print("####### Model 검증 #######")
  85. for i in range(255, 300) :
  86. test = np.array([x_test[i]])
  87. # print(test)
  88. print(model.predict(test))
  89. # model 저장
  90. # model.save('DNN_Model_2048_500.h5')
  91. # W = tf.Variable(tf.random.normal([1]), name="weight")
  92. # b = tf.Variable(tf.random.normal([1]), name="bias")
  93. # hypothesis=x_train*W+b
  94. # cost = tf.reduce_mean(tf.square(hypothesis - y_train))
  95. # sgd = tf.keras.optimizers.SGD(learning_rate=0.01)
  96. # model = Sequential()
  97. # model.add(Dense(1, input_dim = 1))
  98. # model.compile(loss='mean_squared_error',optimizer=sgd)
  99. # model.fit(x_train,y_train,epochs=10)
  100. # print(model.predict(np.array([5])))