-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
66 lines (60 loc) · 2.95 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import numpy as np
import tensorflow
from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, Flatten, Dense, InputLayer, BatchNormalization, Dropout, GlobalAveragePooling2D
from keras.callbacks import ModelCheckpoint
import os
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import image
import librosa
import librosa.display
class raag_pred():
def pred(self, x):
samples, sampling_rate = librosa.load(x)
stft = np.abs(librosa.stft(samples))**2
M = librosa.feature.melspectrogram(S=stft)
M = librosa.feature.melspectrogram(y=samples, sr=sampling_rate, n_mels=128, fmax=8000)
log_M = librosa.power_to_db(M, ref=np.max)
plt.figure(figsize=(7, 6))
plt.subplot('211')
librosa.display.specshow(log_M, y_axis='mel', fmax=8000, x_axis='time')
plt.tight_layout()
# t='.jpg'
# name=x[:-4]+t
plt.savefig('C:/Users/Gopi Maguluri/Raag Identification and Understanding/FRIU/static/intrf/predf.jpg')
img = plt.imread('C:/Users/Gopi Maguluri/Raag Identification and Understanding/FRIU/static/intrf/predf.jpg')
data=[]
data.append(img)
dat = np.array(data)
cnn = Sequential()
cnn.add(Conv2D(50, kernel_size=(3,3), strides=(3,3), padding='same', activation='relu', input_shape=(432, 504, 3)))
cnn.add(Conv2D(75, kernel_size=(3,3), strides=(3,3), padding='same', activation='relu'))
cnn.add(MaxPool2D(pool_size=(2,2)))
cnn.add(Dropout(0.25))
cnn.add(Conv2D(105, kernel_size=(3,3), strides=(3,3), padding='same', activation='relu'))
cnn.add(MaxPool2D(pool_size=(2,2)))
cnn.add(Dropout(0.25))
cnn.add(Conv2D(150, kernel_size=(3,3), strides=(3,3), padding='same', activation='relu'))
cnn.add(MaxPool2D(pool_size=(2,2)))
cnn.add(Dropout(0.25))
cnn.add(Conv2D(190, kernel_size=(3,3), strides=(3,3), padding='same', activation='relu'))
cnn.add(Dropout(0.25))
cnn.add(Conv2D(240, kernel_size=(3,3), strides=(3,3), padding='same', activation='relu'))
cnn.add(Conv2D(300, kernel_size=(3,3), strides=(3,3), padding='same', activation='relu'))
cnn.add(Conv2D(330, kernel_size=(3,3), strides=(3,3), padding='same', activation='relu'))
cnn.add(Dropout(0.25))
cnn.add(GlobalAveragePooling2D())
cnn.add(Dense(150, activation='relu'))
cnn.add(Dropout(0.4))
cnn.add(Dense(80, activation='relu'))
cnn.add(Dropout(0.3))
cnn.add(Dense(5, activation='softmax'))
cnn.load_weights('C:/Users/Gopi Maguluri/Raag Identification and Understanding/FRIU/static/intrf/5cls_cnn.hdf5')
pred = cnn.predict(dat)
p=[]
for i in range(len(pred[0])):
p.append(pred[0][i])
labels = {0:'Alahiya Bilaval', 1:'Bhup', 2:'Malkauns', 3:'Miyan Malhar', 4:'Yaman Kalyan'}
g = p.index(max(p))
return labels[g]