VANILA_EVALUATE.ipynb 10.2 KB
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n평가방법 : positive를 높이는방식\\n\\n본논문\\n- STFT magnitude Spectrun\\n- n=40 log mel filter bank\\n\\n다른논문\\n-STFT maginitude spectogram\\n- n=80 mel scaled filter bank\\n- scale log magnitude\\n- batch nomalization (0,1)\\n- subtract mean overtime on spectogram (for remove frequency dependency noise = colored noise)\\n'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "평가방법 : positive를 높이는방식\n",
    "\n",
    "본논문\n",
    "- STFT magnitude Spectrun\n",
    "- n=40 log mel filter bank\n",
    "\n",
    "다른논문\n",
    "-STFT maginitude spectogram\n",
    "- n=80 mel scaled filter bank\n",
    "- scale log magnitude\n",
    "- batch nomalization (0,1)\n",
    "- subtract mean overtime on spectogram (for remove frequency dependency noise = colored noise)\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[name: \"/device:CPU:0\"\n",
      "device_type: \"CPU\"\n",
      "memory_limit: 268435456\n",
      "locality {\n",
      "}\n",
      "incarnation: 6180456371412583691\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "from tensorflow.python.client import device_lib\n",
    "print(device_lib.list_local_devices())\n",
    "\n",
    "from keras.utils.training_utils import multi_gpu_model\n",
    "gpunum = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "#path 관련 라이브러리\n",
    "import glob\n",
    "import csv\n",
    "\n",
    "#csv저장 라이브러리\n",
    "import pandas as pd\n",
    "\n",
    "# Scientific Math 라이브러리  \n",
    "import numpy as np\n",
    "import librosa\n",
    "import librosa.display\n",
    "import os\n",
    "\n",
    "# Visualization 라이브러리\n",
    "import matplotlib.pyplot as plt\n",
    "import IPython.display as ipd\n",
    "\n",
    "#keras\n",
    "from keras.utils import np_utils\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Conv2D, MaxPooling2D, GRU,Dropout, Flatten,Reshape,BatchNormalization\n",
    "from keras.callbacks import ModelCheckpoint, EarlyStopping\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "audio_path2= './SOUNDS/warbler/'\n",
    "n_mels = 40\n",
    "n_frame = 500\n",
    "window_size=1024\n",
    "hop_size=512\n",
    "sample_rate=25600"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preparing walber_test_labels.\n",
      "Done.\n"
     ]
    }
   ],
   "source": [
    "print(\"Preparing walber_test_labels.\")\n",
    "labels_2 = []\n",
    "with open(audio_path2+'labels.csv', mode='r',encoding='utf-8') as f:\n",
    "    reader = csv.reader(f)\n",
    "    for row in reader : \n",
    "        labels_2.append(row)\n",
    "labels_2.sort(key=lambda x:x[0])\n",
    "labels_2 = np.array(labels_2) #아...그냥이렇게하면 넘피배열로 바꿀수있구나ㅠ\n",
    "labels_2 = labels_2[6000:-1,1]\n",
    "print(\"Done.\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preparing walber_melspectogram.\n",
      "Done.\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-9-8d8dc96f9568>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      9\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Done.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mmel_spectogram_2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmel_spectogram_2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     12\u001b[0m \u001b[0mmel_spectogram_2\u001b[0m\u001b[0;34m=\u001b[0m \u001b[0mmel_spectogram_2\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "# prepare waober_test_melspectogram\n",
    "print(\"Preparing walber_melspectogram.\")\n",
    "mel_spectogram_2 = []\n",
    "with open(audio_path2+'mel_spec_4.csv', mode='r',encoding='utf-8') as f:\n",
    "    reader = csv.reader(f)\n",
    "    next(reader)\n",
    "    for row in reader : \n",
    "        mel_spectogram_2.append(row)\n",
    "print(\"Done.\")\n",
    "\n",
    "mel_spectogram_2 = np.array(mel_spectogram_2)\n",
    "mel_spectogram_2= mel_spectogram_2[:,1:]\n",
    "\n",
    "np.shape(mel_spectogram_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test = np.reshape(mel_spectogram_2,(2000,40,500,1))\n",
    "np.shape(X_test)\n",
    "\n",
    "Y_test = labels_2\n",
    "np.shape(Y_test)\n",
    "\n",
    "MODEL_SAVE_FOLDER_PATH = './model/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(None, 40, 500, 96)\n",
      "(None, 8, 500, 96)\n",
      "(None, 8, 500, 96)\n",
      "(None, 4, 500, 96)\n",
      "(None, 4, 500, 96)\n",
      "(None, 2, 500, 96)\n",
      "(None, 2, 500, 96)\n",
      "(None, 1, 500, 96)\n",
      "(None, 96, 500)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/ipykernel_launcher.py:37: UserWarning: Update your `GRU` call to the Keras 2 API: `GRU(return_sequences=True, units=500)`\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(None, 96, 500)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/ipykernel_launcher.py:40: UserWarning: Update your `GRU` call to the Keras 2 API: `GRU(return_sequences=True, units=500)`\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(None, 96, 500)\n",
      "(None, 96, 500, 1)\n",
      "(None, 96, 1, 1)\n",
      "[0.40238386690616607, 0.8035000026226043]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import keras.backend.tensorflow_backend as K\n",
    "with K.tf.device('/device:GPU:0'):\n",
    "\n",
    "    model = Sequential()\n",
    "    model.add(Conv2D(96, kernel_size=(5, 5), input_shape=(40, 500,1), padding='same',activation='relu')) #어쩌면 40,500만해야할지두\n",
    "    print(model.output_shape)\n",
    "    model.add(BatchNormalization())\n",
    "    model.add(MaxPooling2D(pool_size=(5,1)))\n",
    "    model.add(Dropout(0.25))\n",
    "    print(model.output_shape)\n",
    "\n",
    "    model.add(Conv2D(96, (5, 5), padding='same',activation='relu'))\n",
    "    print(model.output_shape)\n",
    "    model.add(BatchNormalization())\n",
    "    model.add(MaxPooling2D(pool_size=(2,1)))\n",
    "    model.add(Dropout(0.25))\n",
    "    print(model.output_shape)\n",
    "\n",
    "    model.add(Conv2D(96, (5, 5), padding='same',activation='relu'))\n",
    "    print(model.output_shape)\n",
    "    model.add(BatchNormalization())\n",
    "    model.add(MaxPooling2D(pool_size=(2,1)))\n",
    "    model.add(Dropout(0.25))\n",
    "    print(model.output_shape)\n",
    "\n",
    "    model.add(Conv2D(96, (5, 5), padding='same', activation='relu'))\n",
    "    print(model.output_shape)\n",
    "    model.add(BatchNormalization())\n",
    "    model.add(MaxPooling2D(pool_size=(2,1)))\n",
    "    model.add(Dropout(0.25))\n",
    "    print(model.output_shape)\n",
    "\n",
    "    model.add(Reshape((96,500))) #문제될거같은데..\n",
    "    print(model.output_shape)\n",
    "\n",
    "    model.add(GRU(output_dim=500, return_sequences=True))\n",
    "    print(model.output_shape)\n",
    "\n",
    "    model.add(GRU(output_dim=500, return_sequences=True))\n",
    "    print(model.output_shape)\n",
    "\n",
    "    model.add(Reshape((96,500,1))) #문제될거같은데..2\n",
    "    print(model.output_shape)\n",
    "\n",
    "    model.add(MaxPooling2D(pool_size=(1,500)))\n",
    "    print(model.output_shape)\n",
    "\n",
    "    model.add(Flatten())\n",
    "    model.add(Dense(1, activation='sigmoid'))\n",
    "\n",
    "    model.compile(loss='binary_crossentropy',\n",
    "                  optimizer='adam',\n",
    "                  metrics=['accuracy'])\n",
    "\n",
    "    model.load_weights(MODEL_SAVE_FOLDER_PATH + 'bird_sound-' + '17-0.3943.hdf5')\n",
    "\n",
    "    score = model.evaluate(X_test,Y_test,batch_size=200,verbose=2)\n",
    "    print(\"%s: %.2f%%\" % (model.metrics_names[1], scores[1]*100))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}