-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNN_code_1_speaker.py
146 lines (111 loc) · 4.54 KB
/
CNN_code_1_speaker.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 19 10:22:28 2019
@author: Geoffroy Leconte
"""
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
import os
from time import time
import h5py
import keras
from keras import backend as K
from keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, Conv2DTranspose
from keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D
from keras.layers.merge import add, concatenate
from keras.models import load_model, Model
from keras.losses import MSE
from keras.callbacks import TensorBoard
path = 'C:/Users/Geoffroy Leconte/Documents/cours/projet AUDIO/'
#path = '/home/felixgontier/data/PROJET AUDIO/'
data_path = os.path.join(path,'quelques sons/one_speaker/')
h5f_train_data = h5py.File(os.path.join(data_path,'train_data.h5'),'r')
train_data = h5f_train_data['train_data'][:]
h5f_train_data.close()
h5f_test_data = h5py.File(os.path.join(data_path,'test_data.h5'),'r')
test_data = h5f_test_data['test_data'][:]
h5f_test_data.close()
h5f_train_obj = h5py.File(os.path.join(data_path,'train_obj.h5'),'r')
train_obj = h5f_train_obj['train_obj'][:]
h5f_train_obj.close()
h5f_test_obj = h5py.File(os.path.join(data_path,'test_obj.h5'),'r')
test_obj = h5f_test_obj['test_obj'][:]
h5f_test_obj.close()
h5f_train_phase_data = h5py.File(os.path.join(data_path,'train_phase_data.h5'),'r')
train_phase_data = h5f_train_phase_data['train_phase_data'][:]
h5f_train_phase_data.close()
h5f_test_phase_data = h5py.File(os.path.join(data_path,'test_phase_data.h5'),'r')
test_phase_data = h5f_test_phase_data['test_phase_data'][:]
h5f_test_phase_data.close()
h5f_train_phase_obj = h5py.File(os.path.join(data_path,'train_phase_obj.h5'),'r')
train_phase_obj = h5f_train_phase_obj['train_phase_obj'][:]
h5f_train_phase_obj.close()
h5f_test_phase_obj = h5py.File(os.path.join(data_path,'test_phase_obj.h5'),'r')
test_phase_obj = h5f_test_phase_obj['test_phase_obj'][:]
h5f_test_phase_obj.close()
m,n = np.shape(train_data)
# architecture
def cnn_model(width=256, height=256):
k_size = (5,5)
# input layer:
inp = Input(shape=(width, height,1))
# pointeur pour les couches successives
p = inp
p = Conv2D(64, k_size, activation='relu', padding='same')(p)
p = Conv2D(32, k_size, activation='relu', padding='same')(p)
outp = Conv2D(1, k_size, activation='relu', padding='same')(p)
model = Model(inputs=[inp], outputs=[outp])
return model
# paramètres du modèle:
epochs=1
batch_size=128
learning_rate=0.0001
adam_opt = keras.optimizers.Adam(lr=learning_rate)
optimizer = adam_opt
loss = MSE
metrics = [MSE]
model = cnn_model()
model.summary()
model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
#tb_dir = 'C:/Users/Geoffroy Leconte/Documents/cours/projet AUDIO/summaries/'
#tensorboard = TensorBoard(log_dir=os.path.join(tb_dir,'logs/{}') .format(time()),
# update_freq='batch')
train_data = np.reshape(train_data, (m, 256, 256,1))
train_obj = np.reshape(train_obj, (m, 256, 256,1))
m,n = np.shape(test_data)
test_data = np.reshape(test_data, (m, 256, 256,1))
test_obj = np.reshape(test_obj, (m, 256, 256,1))
class LossHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.losses = []
def on_batch_end(self, batch, logs={}):
self.losses.append(logs.get('loss'))
# afficher fonction de coût à chaque batch:
history = LossHistory()
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0,
patience=0, verbose=0, mode='auto')
callbacks = [early_stop, history,
keras.callbacks.ModelCheckpoint(filepath=os.path.join(data_path,'best_model_1speak.h5'),
monitor='val_loss', save_best_only=True)]
model.fit(x=train_data, y=train_obj,
batch_size=batch_size, epochs=epochs,callbacks=callbacks)
plt.plot(history.losses)
plt.title('loss (MSE)')
plt.ylabel('loss')
plt.xlabel('batch_index')
plt.show()
eval_loss = model.evaluate(test_data, test_obj, batch_size=batch_size)
plt.figure(2)
plt.plot(eval_loss)
plt.title('evaluation loss (MSE)')
plt.ylabel('loss')
plt.xlabel('batch_index')
plt.show()
pred_obj = model.predict(test_data, batch_size=batch_size)
pred_obj = np.reshape(pred_obj, (m,256**2))
h5f_pred_obj = h5py.File(os.path.join(data_path, 'pred_obj.h5'), 'w')
h5f_pred_obj.create_dataset('pred_obj', data=pred_obj)
h5f_pred_obj.close()