{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "38a5ef11-0969-481a-948f-76dea6aa60dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From C:\\Users\\Peter\\AppData\\Local\\miniconda3\\Lib\\site-packages\\keras\\src\\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# https://www.geeksforgeeks.org/audio-recognition-in-tensorflow/\n",
    "import os \n",
    "import tensorflow as tf \n",
    "import numpy as np \n",
    "import seaborn as sns \n",
    "import pathlib \n",
    "from IPython import display \n",
    "from matplotlib import pyplot as plt \n",
    "from sklearn.metrics import classification_report\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d39d4708-1f1e-41b7-87b8-a1f4ad4c1dc0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 8000 files belonging to 8 classes.\n",
      "Using 6400 files for training.\n",
      "Using 1600 files for validation.\n",
      "label names: ['down' 'go' 'left' 'no' 'right' 'stop' 'up' 'yes']\n"
     ]
    }
   ],
   "source": [
    "# Using audio_dataset_from_directory function to create dataset with audio data \n",
    "training_set, validation_set = tf.keras.utils.audio_dataset_from_directory( \n",
    "\tdirectory='mini_speech_commands', \n",
    "\tbatch_size=16, \n",
    "\tvalidation_split=0.2, \n",
    "\toutput_sequence_length=16000, \n",
    "\tseed=0, \n",
    "\tsubset='both') \n",
    "\n",
    "# Extracting audio labels \n",
    "label_names = np.array(training_set.class_names) \n",
    "print(\"label names:\", label_names)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "00107449-0ecf-482c-9e61-c713e833f9c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Defining the squeeze function \n",
    "def squeeze(audio, labels): \n",
    "  audio = tf.squeeze(audio, axis=-1) \n",
    "  return audio, labels \n",
    "  \n",
    "# Applying the function on the dataset obtained from previous step \n",
    "training_set = training_set.map(squeeze, tf.data.AUTOTUNE) \n",
    "validation_set = validation_set.map(squeeze, tf.data.AUTOTUNE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3530acf1-007a-4831-8173-adce6d0e8c6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "                <audio  controls=\"controls\" >\n",
       "                    <source src=\"data:audio/wav;base64,[encoded data, 42728 characters]\" type=\"audio/wav\" />\n",
       "                    Your browser does not support the audio element.\n",
       "                </audio>\n",
       "              "
      ],
      "text/plain": [
       "<IPython.lib.display.Audio object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Visualize the waveform \n",
    "audio, label = next(iter(training_set)) \n",
    "display.display(display.Audio(audio[0], rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "74637a53-a921-4d51-9bd5-b6b17d661ada",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 47576 characters]",
      "text/plain": [
       "<Figure size 1000x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 226948 characters]",
      "text/plain": [
       "<Figure size 1000x300 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 54256 characters]",
      "text/plain": [
       "<Figure size 1000x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 245500 characters]",
      "text/plain": [
       "<Figure size 1000x300 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 30920 characters]",
      "text/plain": [
       "<Figure size 1000x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 138000 characters]",
      "text/plain": [
       "<Figure size 1000x300 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot the waveform \n",
    "def plot_wave(waveform, label): \n",
    "\tplt.figure(figsize=(10, 3)) \n",
    "\tplt.title(label) \n",
    "\tplt.plot(waveform) \n",
    "\tplt.xlim([0, 16000]) \n",
    "\tplt.ylim([-1, 1]) \n",
    "\tplt.xlabel('Time') \n",
    "\tplt.ylabel('Amplitude') \n",
    "\tplt.grid(True) \n",
    "\n",
    "# Convert waveform to spectrogram \n",
    "def get_spectrogram(waveform): \n",
    "\tspectrogram = tf.signal.stft(waveform, frame_length=255, frame_step=128) \n",
    "\tspectrogram = tf.abs(spectrogram) \n",
    "\treturn spectrogram[..., tf.newaxis] \n",
    "\n",
    "# Plot the spectrogram \n",
    "def plot_spectrogram(spectrogram, label): \n",
    "\tspectrogram = np.squeeze(spectrogram, axis=-1) \n",
    "\tlog_spec = np.log(spectrogram.T + np.finfo(float).eps) \n",
    "\tplt.figure(figsize=(10, 3)) \n",
    "\tplt.title(label) \n",
    "\tplt.imshow(log_spec, aspect='auto', origin='lower') \n",
    "\tplt.colorbar(format='%+2.0f dB') \n",
    "\tplt.xlabel('Time') \n",
    "\tplt.ylabel('Frequency') \n",
    "\n",
    "for i in range(3):\n",
    "    # Plotting the waveform and the spectrogram of a random sample \n",
    "    audio, label = next(iter(training_set)) \n",
    "    \n",
    "    # Plot the wave with its label name \n",
    "    plot_wave(audio[i], label_names[label[i]]) \n",
    "    \n",
    "    # Plot the spectrogram with its label name \n",
    "    plot_spectrogram(get_spectrogram(audio[i]), label_names[label[i]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "14fd485c-2eaa-45c9-973a-65487725c35b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Creating spectrogram dataset from waveform or audio data \n",
    "def get_spectrogram_dataset(dataset): \n",
    "\tdataset = dataset.map( \n",
    "\t\tlambda x, y: (get_spectrogram(x), y), \n",
    "\t\tnum_parallel_calls=tf.data.AUTOTUNE) \n",
    "\treturn dataset \n",
    "\n",
    "# Applying the function on the audio dataset \n",
    "train_set = get_spectrogram_dataset(training_set) \n",
    "validation_set = get_spectrogram_dataset(validation_set) \n",
    "\n",
    "# Dividing validation set into two equal val and test set \n",
    "val_set = validation_set.take(validation_set.cardinality() // 2) \n",
    "test_set = validation_set.skip(validation_set.cardinality() // 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cbf23516-138b-4189-9a5e-52ec52df50cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train set shape: (None, 124, 129, 1)\n",
      "Validation set shape: (None, 124, 129, 1)\n",
      "Testing set shape: (None, 124, 129, 1)\n"
     ]
    }
   ],
   "source": [
    "train_set_shape = train_set.element_spec[0].shape \n",
    "val_set_shape = val_set.element_spec[0].shape \n",
    "test_set_shape = test_set.element_spec[0].shape \n",
    "\n",
    "print(\"Train set shape:\", train_set_shape) \n",
    "print(\"Validation set shape:\", val_set_shape) \n",
    "print(\"Testing set shape:\", test_set_shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58989d4d-3da1-4be4-8b86-c204b23ac9e0",
   "metadata": {},
   "source": [
    "# *Можно создать последовательную модель*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9e1b1eeb-fae7-452e-8d49-186823802e48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input shape: (124, 129, 1)\n",
      "Model: \"sequential_3\"\n",
      "_________________________________________________________________\n",
      " Layer (type)                Output Shape              Param #   \n",
      "=================================================================\n",
      " resizing_3 (Resizing)       (None, 64, 64, 1)         0         \n",
      "                                                                 \n",
      " normalization_3 (Normaliza  (None, 64, 64, 1)         3         \n",
      " tion)                                                           \n",
      "                                                                 \n",
      " conv2d_30 (Conv2D)          (None, 62, 62, 64)        640       \n",
      "                                                                 \n",
      " conv2d_31 (Conv2D)          (None, 60, 60, 128)       73856     \n",
      "                                                                 \n",
      " conv2d_32 (Conv2D)          (None, 58, 58, 256)       295168    \n",
      "                                                                 \n",
      " max_pooling2d_6 (MaxPoolin  (None, 29, 29, 256)       0         \n",
      " g2D)                                                            \n",
      "                                                                 \n",
      " dropout_9 (Dropout)         (None, 29, 29, 256)       0         \n",
      "                                                                 \n",
      " flatten_3 (Flatten)         (None, 215296)            0         \n",
      "                                                                 \n",
      " dense_12 (Dense)            (None, 512)               110232064 \n",
      "                                                                 \n",
      " dense_13 (Dense)            (None, 256)               131328    \n",
      "                                                                 \n",
      " dropout_10 (Dropout)        (None, 256)               0         \n",
      "                                                                 \n",
      " dense_14 (Dense)            (None, 8)                 2056      \n",
      "                                                                 \n",
      "=================================================================\n",
      "Total params: 110735115 (422.42 MB)\n",
      "Trainable params: 110735112 (422.42 MB)\n",
      "Non-trainable params: 3 (16.00 Byte)\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "# Defining the model \n",
    "def get_model(input_shape, num_labels): \n",
    "\tmodel = tf.keras.Sequential([ \n",
    "\t\ttf.keras.layers.Input(shape=input_shape), \n",
    "\t\t# Resizing the input to a square image of size 64 x 64 and normalizing it \n",
    "\t\ttf.keras.layers.Resizing(64, 64), \n",
    "\t\ttf.keras.layers.Normalization(), \n",
    "\t\t\n",
    "\t\t# Convolution layers followed by MaxPooling layer \n",
    "\t\ttf.keras.layers.Conv2D(64, 3, activation='relu'), \n",
    "\t\ttf.keras.layers.Conv2D(128, 3, activation='relu'), \n",
    "        tf.keras.layers.Conv2D(256, 3, activation='relu'),\n",
    "\t\ttf.keras.layers.MaxPooling2D(), \n",
    "\t\ttf.keras.layers.Dropout(0.5), \n",
    "\t\ttf.keras.layers.Flatten(), \n",
    "\t\t\n",
    "\t\t# Dense layer \n",
    "        tf.keras.layers.Dense(512, activation='relu'),\n",
    "\t\ttf.keras.layers.Dense(256, activation='relu'), \n",
    "\t\ttf.keras.layers.Dropout(0.5), \n",
    "\t\t\n",
    "\t\t# Softmax layer to get the label prediction \n",
    "\t\ttf.keras.layers.Dense(num_labels, activation='softmax') \n",
    "\t]) \n",
    "\t# Printing model summary \n",
    "\tmodel.summary() \n",
    "\treturn model \n",
    "\n",
    "# Getting input shape from the sample audio and number of classes \n",
    "input_shape = next(iter(train_set))[0][0].shape \n",
    "print(\"Input shape:\", input_shape) \n",
    "num_labels = len(label_names) \n",
    "\n",
    "# Creating a model \n",
    "model = get_model(input_shape, num_labels)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca1bdf80-81d8-4712-9db5-999255be28ce",
   "metadata": {},
   "source": [
    "# *Или можно создать ResNet-подобную модель*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "90a211b8-5701-4772-98ed-29bcfc6feb32",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input shape: (124, 129, 1)\n",
      "Model: \"toy_resnet\"\n",
      "__________________________________________________________________________________________________\n",
      " Layer (type)                Output Shape                 Param #   Connected to                  \n",
      "==================================================================================================\n",
      " img (InputLayer)            [(None, 124, 129, 1)]        0         []                            \n",
      "                                                                                                  \n",
      " conv2d_20 (Conv2D)          (None, 122, 127, 32)         320       ['img[0][0]']                 \n",
      "                                                                                                  \n",
      " conv2d_21 (Conv2D)          (None, 120, 125, 64)         18496     ['conv2d_20[0][0]']           \n",
      "                                                                                                  \n",
      " max_pooling2d_4 (MaxPoolin  (None, 40, 41, 64)           0         ['conv2d_21[0][0]']           \n",
      " g2D)                                                                                             \n",
      "                                                                                                  \n",
      " conv2d_22 (Conv2D)          (None, 40, 41, 64)           36928     ['max_pooling2d_4[0][0]']     \n",
      "                                                                                                  \n",
      " conv2d_23 (Conv2D)          (None, 40, 41, 64)           36928     ['conv2d_22[0][0]']           \n",
      "                                                                                                  \n",
      " add_4 (Add)                 (None, 40, 41, 64)           0         ['conv2d_23[0][0]',           \n",
      "                                                                     'max_pooling2d_4[0][0]']     \n",
      "                                                                                                  \n",
      " conv2d_24 (Conv2D)          (None, 40, 41, 64)           36928     ['add_4[0][0]']               \n",
      "                                                                                                  \n",
      " conv2d_25 (Conv2D)          (None, 40, 41, 64)           36928     ['conv2d_24[0][0]']           \n",
      "                                                                                                  \n",
      " add_5 (Add)                 (None, 40, 41, 64)           0         ['conv2d_25[0][0]',           \n",
      "                                                                     'add_4[0][0]']               \n",
      "                                                                                                  \n",
      " conv2d_26 (Conv2D)          (None, 38, 39, 64)           36928     ['add_5[0][0]']               \n",
      "                                                                                                  \n",
      " global_average_pooling2d_2  (None, 64)                   0         ['conv2d_26[0][0]']           \n",
      "  (GlobalAveragePooling2D)                                                                        \n",
      "                                                                                                  \n",
      " dense_8 (Dense)             (None, 256)                  16640     ['global_average_pooling2d_2[0\n",
      "                                                                    ][0]']                        \n",
      "                                                                                                  \n",
      " dropout_6 (Dropout)         (None, 256)                  0         ['dense_8[0][0]']             \n",
      "                                                                                                  \n",
      " dense_9 (Dense)             (None, 8)                    2056      ['dropout_6[0][0]']           \n",
      "                                                                                                  \n",
      "==================================================================================================\n",
      "Total params: 222152 (867.78 KB)\n",
      "Trainable params: 222152 (867.78 KB)\n",
      "Non-trainable params: 0 (0.00 Byte)\n",
      "__________________________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "# https://proproprogs.ru/tensorflow/keras-sozdaem-resnet-podobnuyu-arhitekturu-dlya-klassifikacii-izobrazheniy-cifar-10\n",
    "from tensorflow import keras\n",
    "from tensorflow.keras import layers\n",
    "\n",
    "input_shape = next(iter(train_set))[0][0].shape \n",
    "print(\"Input shape:\", input_shape) \n",
    "num_labels = len(label_names) \n",
    "\n",
    "\n",
    "inputs = keras.Input(shape=input_shape, name=\"img\")\n",
    "x = layers.Conv2D(32, 3, activation=\"relu\")(inputs)\n",
    "x = layers.Conv2D(64, 3, activation=\"relu\")(x)\n",
    "block_1_output = layers.MaxPooling2D(3)(x)\n",
    "x = layers.Conv2D(64, 3, activation=\"relu\", padding=\"same\")(block_1_output)\n",
    "x = layers.Conv2D(64, 3, activation=\"relu\", padding=\"same\")(x)\n",
    "block_2_output = layers.add([x, block_1_output])\n",
    "x = layers.Conv2D(64, 3, activation=\"relu\", padding=\"same\")(block_2_output)\n",
    "x = layers.Conv2D(64, 3, activation=\"relu\", padding=\"same\")(x)\n",
    "block_3_output = layers.add([x, block_2_output])\n",
    "x = layers.Conv2D(64, 3, activation=\"relu\")(block_3_output)\n",
    "x = layers.GlobalAveragePooling2D()(x)\n",
    "x = layers.Dense(256, activation=\"relu\")(x)\n",
    "x = layers.Dropout(0.5)(x)\n",
    "outputs = layers.Dense(num_labels, activation='softmax')(x)\n",
    "\n",
    "model = keras.Model(inputs, outputs, name=\"toy_resnet\")\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "90a3a627-a955-4c4c-b656-468db7f1ae38",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "400/400 [==============================] - 403s 1s/step - loss: 1.4670 - accuracy: 0.4719 - val_loss: 0.8876 - val_accuracy: 0.7150\n",
      "Epoch 2/10\n",
      "400/400 [==============================] - 399s 997ms/step - loss: 0.7097 - accuracy: 0.7528 - val_loss: 0.6048 - val_accuracy: 0.7825\n",
      "Epoch 3/10\n",
      "400/400 [==============================] - 396s 988ms/step - loss: 0.4712 - accuracy: 0.8355 - val_loss: 0.4345 - val_accuracy: 0.8612\n",
      "Epoch 4/10\n",
      "400/400 [==============================] - 395s 987ms/step - loss: 0.3363 - accuracy: 0.8831 - val_loss: 0.4788 - val_accuracy: 0.8575\n",
      "Epoch 5/10\n",
      "400/400 [==============================] - 395s 987ms/step - loss: 0.2835 - accuracy: 0.9028 - val_loss: 0.4224 - val_accuracy: 0.8662\n",
      "Epoch 6/10\n",
      "400/400 [==============================] - 396s 990ms/step - loss: 0.2261 - accuracy: 0.9241 - val_loss: 0.3797 - val_accuracy: 0.8775\n",
      "Epoch 7/10\n",
      "400/400 [==============================] - 395s 987ms/step - loss: 0.1833 - accuracy: 0.9392 - val_loss: 0.4213 - val_accuracy: 0.8662\n",
      "Epoch 8/10\n",
      "400/400 [==============================] - 395s 987ms/step - loss: 0.1614 - accuracy: 0.9461 - val_loss: 0.3902 - val_accuracy: 0.8838\n",
      "Epoch 9/10\n",
      "400/400 [==============================] - 396s 989ms/step - loss: 0.1584 - accuracy: 0.9523 - val_loss: 0.3804 - val_accuracy: 0.8813\n",
      "Epoch 10/10\n",
      "400/400 [==============================] - 401s 1s/step - loss: 0.1256 - accuracy: 0.9620 - val_loss: 0.4524 - val_accuracy: 0.8788\n"
     ]
    }
   ],
   "source": [
    "model.compile( \n",
    "\toptimizer=tf.keras.optimizers.Adam(), \n",
    "\tloss=tf.keras.losses.SparseCategoricalCrossentropy(), \n",
    "\tmetrics=['accuracy'], \n",
    ") \n",
    "\n",
    "EPOCHS = 10\n",
    "history = model.fit( \n",
    "\ttrain_set, \n",
    "\tvalidation_data=val_set, \n",
    "\tepochs=EPOCHS, \n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a063e27a-d131-49d1-92c0-8e47ee80a34b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 56176 characters]",
      "text/plain": [
       "<Figure size 1000x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plotting the history \n",
    "metrics = history.history \n",
    "plt.figure(figsize=(10, 5)) \n",
    "\n",
    "# Plotting training and validation loss \n",
    "plt.subplot(1, 2, 1) \n",
    "plt.plot(history.epoch, metrics['loss'], metrics['val_loss']) \n",
    "plt.legend(['loss', 'val_loss']) \n",
    "plt.xlabel('Epoch') \n",
    "plt.ylabel('Loss') \n",
    "plt.grid()\n",
    "\n",
    "# Plotting training and validation accuracy \n",
    "plt.subplot(1, 2, 2) \n",
    "plt.plot(history.epoch, metrics['accuracy'], metrics['val_accuracy']) \n",
    "plt.legend(['accuracy', 'val_accuracy']) \n",
    "plt.xlabel('Epoch') \n",
    "plt.ylabel('Accuracy')\n",
    "plt.grid()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "1a5fbf9f-0abb-4ed4-b786-4a1c9b9f60ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50/50 [==============================] - 6s 104ms/step\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 35288 characters]",
      "text/plain": [
       "<Figure size 1000x800 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Confusion matrix \n",
    "y_pred = np.argmax(model.predict(test_set), axis=1) \n",
    "y_true = np.concatenate([y for x, y in test_set], axis=0) \n",
    "cm = tf.math.confusion_matrix(y_true, y_pred) \n",
    "\n",
    "# Plotting the confusion matrix \n",
    "plt.figure(figsize=(10, 8)) \n",
    "sns.heatmap(cm, annot=True, fmt='g') \n",
    "plt.xlabel('Predicted') \n",
    "plt.ylabel('Actual') \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "aabea240-518e-4925-9191-18ebf8935edb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.90      0.88      0.89       107\n",
      "           1       0.86      0.81      0.83        88\n",
      "           2       0.85      0.90      0.88       105\n",
      "           3       0.84      0.95      0.89        94\n",
      "           4       0.93      0.96      0.95        84\n",
      "           5       0.94      0.91      0.92       117\n",
      "           6       0.94      0.90      0.92       110\n",
      "           7       0.96      0.89      0.92        95\n",
      "\n",
      "    accuracy                           0.90       800\n",
      "   macro avg       0.90      0.90      0.90       800\n",
      "weighted avg       0.90      0.90      0.90       800\n",
      "\n"
     ]
    }
   ],
   "source": [
    "report = classification_report(y_true, y_pred) \n",
    "print(report)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "4eae2cb7-a4f7-41f5-b1be-85573fbb9da4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "                <audio  controls=\"controls\" >\n",
       "                    <source src=\"data:audio/wav;base64,[encoded data, 42728 characters]\" type=\"audio/wav\" />\n",
       "                    Your browser does not support the audio element.\n",
       "                </audio>\n",
       "              "
      ],
      "text/plain": [
       "<IPython.lib.display.Audio object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 17608 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "path = 'mini_speech_commands/yes/0f7dc557_nohash_0.wav'\n",
    "Input = tf.io.read_file(str(path)) \n",
    "x, sample_rate = tf.audio.decode_wav(Input, desired_channels=1, desired_samples=16000,) \n",
    "audio, labels = squeeze(x, 'yes') \n",
    "\n",
    "waveform = audio \n",
    "display.display(display.Audio(waveform, rate=16000)) \n",
    "\n",
    "x = get_spectrogram(audio) \n",
    "x = tf.expand_dims(x, axis=0) \n",
    "\n",
    "prediction = model(x) \n",
    "plt.bar(label_names, tf.nn.softmax(prediction[0])) \n",
    "plt.title('Prediction : '+ label_names[np.argmax(prediction, axis=1).item()]) \n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
