import tensorflow as tf
from tensorflow.keras import layers
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import librosa
import librosa.display
import matplotlib.pyplot as plt
import os
from os.path import isdir, join
import random
import copy
tf.__version__
use_colab = True
assert use_colab in [True, False]
from google.colab import drive
drive.mount('/content/drive')
if use_colab:
DATASET_PATH = "/content/drive/MyDrive/Datasets"
else:
DATASET_PATH = "./"
data_wav = np.load(os.path.join(DATASET_PATH, "speech_spec_8000.npy"))
print(data_wav.shape)
# 50620, 130, 126, 1
(-1, data_wav.shape[1], data_wav.shape[2], 1)
librosa.display.specshow(librosa.amplitude_to_db(data_wav[219], ref=np.max), x_axis='time')
plt.title('Power spectrogram')
plt.colorbar(format='%+2.0f dB')
plt.tight_layout()
plt.show()
['yes', 'no', 'up', 'down', 'left', 'right', 'on', 'off', 'stop', 'go', 'unknown', 'silence']
data_label = np.load(os.path.join(DATASET_PATH, "speech_label_8000.npy"))
print(data_label.shape)
# label 전처리
target_list = ['yes', 'no', 'up', 'down', 'left', 'right', 'on', 'off', 'stop', 'go', 'unknown', 'silence']
##################################################
##### 주어진 label => idx 형태로 변경해주셔야합니다. #####
##################################################
# TODO
train_wav, test_wav, train_label, test_label = train_test_split(#)
# reshape for conv layers Conv2D -> 차원이 더 늘어납니다. 데이터 shape도 바뀝니다!
train_wav = train_wav.reshape(# TODO)
test_wav = test_wav.reshape(# TODO)
print(train_wav.shape)
print(test_wav.shape)
print(train_label.shape)
print(test_label.shape)
del data_wav # 메모리 관리를 위해 변수 삭제
del data_label
print('Train_Wav Demension : ' + str(np.shape(train_wav)))
print('Train_Label Demension : ' + str(np.shape(train_label)))
print('Test_Wav Demension : ' + str(np.shape(test_wav)))
print('Test_Label Demension : ' + str(np.shape(test_label)))
print('Number Of Labels : ' + str(len(label_value)))
# the save point
if use_colab:
checkpoint_dir ='./drive/MyDrive/train_ckpt/spectrogram/exp1'
if not os.path.isdir(checkpoint_dir):
os.makedirs(checkpoint_dir)
else:
checkpoint_dir = 'spectrogram/exp1'
# 전체 데이터셋 구성
batch_size = # TODO
# for train
train_dataset = # TODO
print(train_dataset)
# for test
test_dataset = # TODO
print(test_dataset)
#<BatchDataset shapes: ((None, 130, 126, 1), (None, 12)), types: (tf.float32, tf.float32)>
#<BatchDataset shapes: ((None, 130, 126, 1), (None, 12)), types: (tf.float32, tf.float32)>
제시된 모델을 구현해보고, 더 좋은 성능으로 튜닝해보자.
input_tensor = # TODO
x = # TODO
print(x.shape)
output_tensor = # TODO
model = tf.keras.Model(# TODO)
model.compile(# TODO)
# without training, just inference a model:
predictions = model(train_wav[0:1], training=False)
print("Predictions: ", predictions.numpy())
model.summary()
cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_dir,
save_weights_only=True,
monitor='val_loss',
mode='auto', # max, min, auto
save_best_only=True,
verbose=1)
# 추가 callback 함수를 구현해보세요!
# TODO
# model.fit model.fit_generator는 model.fit으로 통일되었습니다.
# tf.data.Dataset은 generator 입니다.
history = model.fit(# TODO)
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss=history.history['loss']
val_loss=history.history['val_loss']
epochs_range = range(len(acc))
plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
model.load_weights(checkpoint_dir)
results = model.evaluate(test_dataset)
def final_score():
print("Model params num : " + str(model.count_params()))
print("Accuracy : " + str(results[1]))
s = (model.count_params() * 32) / (1024 ** 2)
score = 50 * (results[1] + min((1/s), 1))
print("score : " + str(score))
final_score()