-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoencoder.py
More file actions
127 lines (89 loc) · 3.76 KB
/
Autoencoder.py
File metadata and controls
127 lines (89 loc) · 3.76 KB
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
# -*- coding: utf-8 -*-
"""
Created on Thu May 16 15:53:50 2019
@author: Sam
"""
#Additional Packages to Install
# !pip install pydrive
# !pip install oauth2client
# !pip install tensorboardcolab
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorboardcolab import *
from keras.layers import Dense,Conv1D,RepeatVector,TimeDistributed,Input
from keras.layers import Conv1D,MaxPooling1D,UpSampling1D
from keras.layers import Activation,BatchNormalization
from keras.models import Model,Sequential
from keras.optimizers import RMSprop
from sklearn.preprocessing import MinMaxScaler
from PreProcessingData import PreProcessingMusic
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
class AudioDenoising:
def __init__(self,audio_filepath,length = 2**9):
#Authenticate/Create PyDrive client
#auth.authenticate_user()
#gauth = GoogleAuth()
#gauth.credentials = GoogleCredentials.get_application_default()
#drive = GoogleDrive(gauth)
self.length = length
proc = PreProcessingMusic(audio_filepath,length = self.length)
self.clean_audio, self.noisy_audio = proc.run()
def build_autoencoder(self):
inputs = Input(shape = (self.length,1))
encoded = Conv1D(32,3, padding = 'same')(inputs)
encoded = BatchNormalization()(encoded)
encoded = Activation('relu')(encoded)
encoded = MaxPooling1D(2,padding = 'same')(encoded)
encoded = Conv1D(16,3,padding = 'same')(encoded)
encoded = Activation('relu')(encoded)
encoded = MaxPooling1D(2,padding = 'same')(encoded)
encoded = Conv1D(8,3, padding = 'same')(encoded)
encoded = Activation('relu')(encoded)
encoded = MaxPooling1D(2,padding = 'same')(encoded)
decoded = Conv1D(8,3,padding = 'same')(encoded)
decoded = Activation('relu')(decoded)
decoded = UpSampling1D(2)(decoded)
decoded = Conv1D(16,3, padding = 'same')(decoded)
decoded = Activation('relu')(decoded)
decoded = UpSampling1D(2)(decoded)
decoded = Conv1D(32,3,padding = 'same')(decoded)
decoded = Activation('relu')(decoded)
decoded = UpSampling1D(2)(decoded)
decoded = Conv1D(1,3,activation = 'sigmoid',padding = 'same')(decoded)
self.autoencoder = Model(inputs,decoded)
def trainNN(self,summary=False,tb=False):
self.epochs = 1
self.optimizer = RMSprop(lr=1e-3)
self.batch_size = 32
#tbc=TensorBoardColab()
self.autoencoder.compile(optimizer = self.optimizer, loss = 'mse')
if summary:
self.autoencoder.summary()
self.autoencoder.fit(self.noisy_audio,self.clean_audio,
epochs=self.epochs,
verbose = 1)
#callbacks=[TensorBoardColabCallback(tbc)])
def saveModel(self):
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
self.autoencoder.save('AudioDenoising.h5')
model_file = drive.CreateFile({'title' : 'AudioDenoising.h5'})
model_file.SetContentFile('AudioDenoising.h5')
model_file.Upload()
self.autoencoder.save_weights('AudioDenoising_weights.h5')
weights_file = drive.CreateFile({'title' : 'AudioDenoising_weights.h5'})
weights_file.SetContentFile('AudioDenoising_weights.h5')
weights_file.Upload()
drive.CreateFile({'id': weights_file.get('id')})
drive.CreateFile({'id': model_file.get('id')})
print('Saved Model')
AE = AudioDenoising('trimed data.zip')
AE.build_autoencoder()
AE.trainNN()
AE.saveModel()