{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":7634,"databundleVersionId":46676,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"##### This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"execution":{"iopub.status.busy":"2024-03-24T07:12:35.252163Z","iopub.execute_input":"2024-03-24T07:12:35.252582Z","iopub.status.idle":"2024-03-24T07:12:36.370827Z","shell.execute_reply.started":"2024-03-24T07:12:35.25255Z","shell.execute_reply":"2024-03-24T07:12:36.369981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pyunpack\n!pip install patool","metadata":{"execution":{"iopub.status.busy":"2024-03-24T07:12:36.714031Z","iopub.execute_input":"2024-03-24T07:12:36.714656Z","iopub.status.idle":"2024-03-24T07:13:08.707791Z","shell.execute_reply.started":"2024-03-24T07:12:36.71462Z","shell.execute_reply":"2024-03-24T07:13:08.706211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pyunpack import Archive\n\nos.makedirs(\"./data\", exist_ok = True)\nArchive(\"../input/tensorflow-speech-recognition-challenge/train.7z\").extractall(\"./data\")\nArchive(\"../input/tensorflow-speech-recognition-challenge/test.7z\").extractall(\"./data\")\nprint(\"Extracted!\")","metadata":{"execution":{"iopub.status.busy":"2024-03-24T07:13:08.710157Z","iopub.execute_input":"2024-03-24T07:13:08.710502Z","iopub.status.idle":"2024-03-24T08:44:51.897527Z","shell.execute_reply.started":"2024-03-24T07:13:08.71047Z","shell.execute_reply":"2024-03-24T08:44:51.893923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = \"./data/train/audio/\"\ntest_dir = \"./data/test/audio/\"\nclasses1 = os.listdir(train_dir)\nclasses1.remove(\"_background_noise_\")\nclasses2 = os.listdir(test_dir)\nclasses2.remove(\"_background_noise_\")","metadata":{"execution":{"iopub.status.busy":"2024-03-24T08:44:51.903571Z","iopub.execute_input":"2024-03-24T08:44:51.904252Z","iopub.status.idle":"2024-03-24T08:44:52.504494Z","shell.execute_reply.started":"2024-03-24T08:44:51.904207Z","shell.execute_reply":"2024-03-24T08:44:52.501675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classes1)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T08:44:52.505494Z","iopub.status.idle":"2024-03-24T08:44:52.505902Z","shell.execute_reply.started":"2024-03-24T08:44:52.505693Z","shell.execute_reply":"2024-03-24T08:44:52.50571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classes2)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T08:44:52.508385Z","iopub.status.idle":"2024-03-24T08:44:52.508949Z","shell.execute_reply.started":"2024-03-24T08:44:52.50866Z","shell.execute_reply":"2024-03-24T08:44:52.508682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nimport numpy as np\nimport pandas as pd\nimport os\nimport shutil\nimport glob\nimport random\nfrom tqdm import tqdm\nfrom collections import Counter\nfrom sklearn.preprocessing import LabelEncoder\nimport IPython\nfrom numpy.fft import rfft, irfft\nimport itertools\n\nfrom scipy.io import wavfile\nimport IPython.display as ipd\nimport matplotlib.pyplot as plt\nimport scipy as sp\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2024-03-24T08:44:52.510792Z","iopub.status.idle":"2024-03-24T08:44:52.511444Z","shell.execute_reply.started":"2024-03-24T08:44:52.511095Z","shell.execute_reply":"2024-03-24T08:44:52.511135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Mean Squared\n\ndef ms(x):\n    return (np.abs(x)**2.0).mean()\n\ndef normalize(y, x=None):\n    if x is not None:\n        x = ms(x)\n    else:\n        x = 1.0\n    return y * np.sqrt( x / ms(y) )\n\ndef white_noise(N, state=None):\n    state = np.random.RandomState() if state is None else state\n    return state.randn(N)\n\ndef  pink_noise(N, state=None):\n    state = np.random.RandomState() if state is None else state\n    uneven = N%2\n    X = state.randn(N//2+1+uneven) + 1j * state.randn(N//2+1+uneven)\n    S = np.sqrt(np.arange(len(X))+1.) # 1+ to avoid divide by zero\n    y = (irfft(X/S)).real\n    if uneven:\n        y = y[:-1]\n    return normalize(y)\n\ndef blue_noise(N, state=None):\n    state = np.random.RandomState() if state is None else state\n    uneven = N%2\n    X = state.randn(N//2+1+uneven) + 1j * state.randn(N//2+1+uneven)\n    S = np.sqrt(np.arange(len(X))) # Filter\n    y = (irfft(X*S)).real\n    if uneven:\n        y = y[:-1]\n    return normalize(y)\n\ndef brown_noise(N, state=None):\n    state = np.random.RandomState() if state is None else state\n    uneven = N%2\n    X = state.randn(N//2+1+uneven) + 1j * state.randn(N//2+1+uneven)\n    S = (np.arange(len(X)) +1) # Filter\n    y = (irfft(X*S)).real\n    if uneven:\n        y = y[:-1]\n    return nomalize(y)\n\ndef brown_noise(N, state=None):\n    state = np.random.RandomState() if state is None else state\n    uneven = N%2\n    X = state.randn(N//2+1+uneven) + 1j * state.randn(N//2+1+uneven)\n    S = (np.arange(len(X))+1) # Filter\n    y = (irfft(X/S)).real\n    if uneven:\n        y = y[:-1]\n    return normalize(y)\n\ndef violet_noise(N, state=None):\n    state = np.random.RandomState() if state is None else state\n    uneven = N%2\n    X = state.randn(N//2+1+uneven) + 1j * state.randn(N//2+1+uneven)\n    S = (np.arange(len(X))) # Filter\n    y = (irfft(X*S)).real\n    if uneven:\n        y = y[:-1]\n    retrun normalize(y)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T08:44:52.532474Z","iopub.status.idle":"2024-03-24T08:44:52.532846Z","shell.execute_reply.started":"2024-03-24T08:44:52.532662Z","shell.execute_reply":"2024-03-24T08:44:52.532677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Tensorflow 기본 동작 수행을 위한 함수들","metadata":{}},{"cell_type":"code","source":"# 사용할 GPU와 프로세스가 사용할 수 있는 메모리 양을 선택하는 함수\ndef get_tensorflow_configuration(device=\"0\", memory_fraction=1):\n    devive = str(device)\n    config = tf.ConfigProto()\n    config.allow_soft_placement = Trun\n    config.gpu_options.per_process_gpu_memory_fraction = memory_fraction\n    config.gpu_options.visible_device_list = device\n    return(config)\n\n# 사용할 GPU 장치와 사전 할당될 메모리 비율을 처리하는 TF 세션을 시작하는 함수\ndef start_tensorflow_session(device=\"0\", memory_fraction=1):\n    return(tf.Session(config=get_tensorflow_configuration(device=device, memory_fraction*memory_fraction)))\n\n# 리포팅 함수 ( 기록 )\ndef get_summary_writer(sessio, logs_path, project_id, version_id):\n    path = os.path.join(logs_path, \"{}_{}\".format(project_id, version_id))\n    if os.path.exists(path):\n        shutil.rmtree(path)\n    summary_writer = tf.summary.FileWriter(path, graph_def=session.graph_def)\n    return(summary_writer)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T08:44:52.535629Z","iopub.status.idle":"2024-03-24T08:44:52.536052Z","shell.execute_reply.started":"2024-03-24T08:44:52.53581Z","shell.execute_reply":"2024-03-24T08:44:52.535826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 데이터 Path와 관련된 함수들","metadata":{}},{"cell_type":"code","source":"# 정규화\n# 경로 검색 기느으이 출력을 정규화하는 데 사용하기 위한 데코레이터 함수,\n# 슬래시 / 백슬래시 창의 경우를 수정하는 데 유용\ndef _norm_path(path):\n    def normalize_path(*args, **kwargs):\n        return os.path.normpath(path(*args, **kwargs))\n    return normalize_path\n\n# 경로 검색 기능의 출력이 있는지 확인하기 위한 데코레이터 함수,\n# 슬래시 / 백슬래시 창의 경우를 수정하는 데 유용\ndef _assure_path_exists(path):\n    def assure_exists(*args, **kwargs):\n        p=path(*args, **kwargs)\n        assert os.path.exists(p), \"the following path does not exist: '{}'\".format(p)\n        return p\n    return assure_exists\n\n# ","metadata":{"execution":{"iopub.status.busy":"2024-03-24T08:44:52.537022Z","iopub.status.idle":"2024-03-24T08:44:52.537405Z","shell.execute_reply.started":"2024-03-24T08:44:52.537217Z","shell.execute_reply":"2024-03-24T08:44:52.537232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}