回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我编写了一个语音识别python,我使用了tenserflow keras模型</p>
<p>当我在运行异常引发的200个历元后向模型提供输入时,我试图解决该问题,但它不起作用</p>
<p>我收到此错误,无法pickle weakref对象</p>
<p>异常跟踪</p>
<pre><code>Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Conda\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "G:\code\speaker_identification.py", line 93, in record
pickle.dump([regressor] ,mf)
TypeError: can't pickle weakref objects
</code></pre>
<p>这是我的完整代码,我已经尝试过了</p>
<pre><code>from tkinter import *
from tkinter import filedialog
from keras.layers import *
from keras.models import Sequential
import sounddevice as sd
import pickle
import scipy.io.wavfile as wav
import numpy as np
import speechpy
import os
import time
window=Tk()
window.title('Project')
fs=44100
duration=5
i=[]
def features(fs,signal):
#fs, signal = wav.read(file_name)
#signal = signal[:,0]
signal_preemphasized = speechpy.processing.preemphasis(signal, cof=0.98)
frames = speechpy.processing.stack_frames(signal, sampling_frequency=fs, frame_length=0.020, frame_stride=0.01, filter=lambda x: np.ones((x,)), zero_padding=True)
power_spectrum = speechpy.processing.power_spectrum(frames, fft_points=512)
mfcc = speechpy.feature.mfcc(signal, sampling_frequency=fs, frame_length=0.020, frame_stride=0.01, num_filters=40, fft_length=512, low_frequency=0, high_frequency=None)
mfcc_cmvn = speechpy.processing.cmvnw(mfcc,win_size=301,variance_normalization=True)
mfcc_feature_cube = speechpy.feature.extract_derivative_feature(mfcc)
logenergy = speechpy.feature.lmfe(signal, sampling_frequency=fs, frame_length=0.020, frame_stride=0.01, num_filters=40, fft_length=512, low_frequency=0, high_frequency=None)
logenergy_feature_cube = speechpy.feature.extract_derivative_feature(logenergy)
return [fs, signal, frames, power_spectrum, mfcc, mfcc_cmvn, mfcc_feature_cube, logenergy, logenergy_feature_cube]
def getSample():
try:
with open('names.pickle','rb') as f:
names=pickle.load(f)
except:
names=[]
n=t1.get('1.0','end-1c')
if ((n in names) or n==''):
t10.delete('1.0','end-1c')
t10.insert(END,'Username Already used')
else:
names.append(n)
with open('names.pickle','wb') as f:
pickle.dump(names,f)
train=Tk()
train.title('Add Sample')
tl1=Label(train,text='Press The button to record 5 Second sample')
tl1.grid(row=0,column=0)
def record():
global duration,fs
rec=sd.rec(int(duration * fs), samplerate=fs, channels=1, dtype='int16')
sd.wait()
sd.play(rec,fs)
sd.wait()
rec=list(map(float,rec))
try:
with open('mat.pickle','rb') as tf:
k, feature=pickle.load(tf)
except:
k=0
feature=np.array([])
k+=1
s=features(fs,np.array(rec))
ss=s[-1]
ss=ss.reshape((498,40,3))
feature=list(feature)
feature.append(ss)
feature=np.array(feature)
with open('mat.pickle','wb') as tf:
pickle.dump([k,feature],tf)
regressor=Sequential()
regressor.add(Conv2D(32,kernel_size=3,activation='relu',input_shape=(498,40,3)))
regressor.add(MaxPooling2D(pool_size=(4,4)))
regressor.add(Conv2D(64,kernel_size=3,activation='relu'))
regressor.add(MaxPooling2D(pool_size=(4,4)))
regressor.add(Dense(10))
regressor.add(Dropout(rate=0.5))
regressor.add(Dense(5))
regressor.add(Flatten())
regressor.add(Dense(k,activation='sigmoid'))
regressor.compile(optimizer='rmsprop',loss='mean_squared_error')
ytrain=np.zeros((k,k),int)
np.fill_diagonal(ytrain,1)
regressor.fit(x=feature,y=ytrain,batch_size=32,epochs=200)
with open('model.pickle','wb') as mf:
pickle.dump([regressor] ,mf)
t10.delete('1.0','end-1c')
t10.insert(END,'Username Added and Model Trained')
train.destroy()
tb1=Button(train,text='Record',command=record)
tb1.grid(row=1,column=0, padx=10,pady=10)
train.mainloop()
def recognize():
recog=Tk()
recog.title('Recognize the voice')
try:
with open('names.pickle','rb') as f:
names=pickle.load(f)
except:
t10.delete('1.0','end-1c')
t10.insert(END,'No Samples in Dataset')
recog.destroy()
def recordrecog():
r=sd.rec(int(duration * fs), samplerate=fs, channels=1, dtype='int16')
sd.wait()
sd.play(r,fs)
sd.wait()
r=list(map(float,r))
s=features(fs,np.array(r))
ss=s[-1]
ss=ss.reshape((1,498,40,3))
with open('model.pickle','rb') as mf:
regressor=pickle.load(mf)
regressor=regressor[0]
predictionval=regressor.predict(ss)
t10.delete('1.0','end-1c')
global i
i=list(list(predictionval)[0])
n=names[i.index(max(i))]
t10.insert(END,n)
rl1=Label(recog,text='Record a 5 Sec sample by clicking the following button')
rl1.grid(row=0,column=0)
rb1=Button(recog,text='Record Sample',command=recordrecog)
rb1.grid(row=1,column=0,padx=10,pady=10)
recog.mainloop()
def deleteAll():
os.remove('names.pickle')
os.remove('mat.pickle')
os.remove('model.pickle')
t10.delete('1.0','end-1c')
t10.insert(END,'All Files and Users Deleted')
l1=Label(window,text='Name')
l1.grid(row=0,column=0)
b1=Button(window,text='ADD',command=getSample)
b1.grid(row=1,column=1)
t1=Text(window, height=2, width=35, selectbackground='blue')
t1.grid(row=1,column=0, padx=10,pady=10)
b2=Button(window,text='Recognize the User',command=recognize)
b2.grid(row=2,column=0, padx=10,pady=30)
l10=Label(window,text='Status')
l10.grid(row=9,column=0)
t10=Text(window,height=1, width=50)
t10.grid(row=10,column=0,padx=10,pady=10)
b10=Button(window,text='Delete all Data',command=deleteAll)
b10.grid(row=10,column=1,padx=10,pady=10)
#l1=Label(window,text='Training')
#l1.grid(row=0,column=0)
window.mainloop()
</code></pre>
<p>使用joblib时出现新错误或几乎相同</p>
<pre><code>Epoch 200/200
1/1 [==============================] - 0s 15ms/step - loss: 0.0000e+00
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Conda\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "G:\code\speaker_identification.py", line 89, in record
joblib.dump([regressor], 'model.pkl')
File "C:\Conda\Anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 480, in dump
NumpyPickler(f, protocol=protocol).dump(value)
File "C:\Conda\Anaconda3\lib\pickle.py", line 437, in dump
self.save(obj)
File "C:\Conda\Anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Conda\Anaconda3\lib\pickle.py", line 504, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Conda\Anaconda3\lib\pickle.py", line 816, in save_list
self._batch_appends(obj)
File "C:\Conda\Anaconda3\lib\pickle.py", line 843, in _batch_appends
save(tmp[0])
File "C:\Conda\Anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Conda\Anaconda3\lib\pickle.py", line 549, in save
self.save_reduce(obj=obj, *rv)
File "C:\Conda\Anaconda3\lib\pickle.py", line 662, in save_reduce
save(state)
File "C:\Conda\Anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Conda\Anaconda3\lib\pickle.py", line 504, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Conda\Anaconda3\lib\pickle.py", line 856, in save_dict
self._batch_setitems(obj.items())
File "C:\Conda\Anaconda3\lib\pickle.py", line 882, in _batch_setitems
save(v)
File "C:\Conda\Anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Conda\Anaconda3\lib\pickle.py", line 549, in save
self.save_reduce(obj=obj, *rv)
File "C:\Conda\Anaconda3\lib\pickle.py", line 662, in save_reduce
save(state)
File "C:\Conda\Anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Conda\Anaconda3\lib\pickle.py", line 504, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Conda\Anaconda3\lib\pickle.py", line 856, in save_dict
self._batch_setitems(obj.items())
File "C:\Conda\Anaconda3\lib\pickle.py", line 882, in _batch_setitems
save(v)
File "C:\Conda\Anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Conda\Anaconda3\lib\pickle.py", line 504, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Conda\Anaconda3\lib\pickle.py", line 856, in save_dict
self._batch_setitems(obj.items())
File "C:\Conda\Anaconda3\lib\pickle.py", line 881, in _batch_setitems
save(k)
File "C:\Conda\Anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Conda\Anaconda3\lib\pickle.py", line 524, in save
rv = reduce(self.proto)
TypeError: can't pickle weakref objects
</code></pre>