如何在SincNet的keras实现中使用评估和预测功能?

2024-09-28 05:22:50 发布

您现在位置:Python中文网/ 问答频道 /正文

谢谢你的提醒,我正在使用SincNet开发一个自动说话人识别系统

Ravanelli, M., & Bengio, Y. (2018, December). Speaker recognition from raw waveform with sincnet. In 2018 IEEE Spoken Language Technology Workshop (SLT) (pp. 1021-1028). IEEE.

因为网络是用Pytorch编码的,所以我在这里搜索并找到了Keras实现https://github.com/grausof/keras-sincnet。我修改了train.py代码,用Tensorflow 2.0中我自己的数据来训练Sincnet,并且工作得很好,我只保存了我训练过的网络的权重,我的训练数据的形状为1283200,1用于输入,128用于每批标签

#Creates a Sincnet model with input_size=3200 (wlen), num_classes=40, fs=16000
redsinc = create_model(wlen,num_classes,fs)

#Saves only weights and stopearly callback
checkpointer = ModelCheckpoint(filepath='checkpoints/SincNetBiomex3.hdf5',verbose=1, 
save_best_only=True, monitor='val_accuracy',save_weights_only=True)
stopearly = EarlyStopping(monitor='val_accuracy',patience=3,verbose=1)
callbacks = [checkpointer,stopearly]

# optimizer = RMSprop(lr=learnrate, rho=0.9, epsilon=1e-8)
optimizer = Adam(learning_rate=learnrate)

# Creates generator of training batches
train_generator = batchGenerator(batch_size,train_inputs,train_labels,wlen)
validinputs, validlabels = create_batches_rnd(validation_labels.shape[0],
validation_inputs,validation_labels,wlen)

#Compiling model and train with function fit_generator
redsinc.compile(loss='sparse_categorical_crossentropy',  optimizer=optimizer, metrics=['accuracy'])

history = redsinc.fit_generator(train_generator, steps_per_epoch=N_batches, epochs = epochs, 
verbose = 1, callbacks=callbacks, validation_data=(validinputs,validlabels))

当我尝试评估网络时出现了问题,我没有使用test.py中的代码,我只加载了之前保存的权重并使用了evaluate函数,我的测试数据的形状为12003200,1,输入为1,标签为1200

# Create a Sincnet model and load previously saved weights
redsinc = create_model(wlen,num_clases,fs)
redsinc.load_weights('checkpoints/SincNetBiomex3.hdf5')

test_loss, test_accuracy = redsinc.evaluate(x=eval_in,y=eval_lab)

RuntimeError: You must compile your model before training/testing. Use `model.compile(optimizer, 
loss)`.

然后我添加了用于培训的相同编译代码:

optimizer = Adam(learning_rate=0.001)
redsinc.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

然后重新运行测试代码并获得以下结果:

 WARNING:tensorflow:From C:\Users\atenc\Anaconda3\envs\py3.7-tf2.0gpu\lib\site- 
 packages\tensorflow_core\python\ops\resource_variable_ops.py:1781: calling 
 BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is 
 deprecated and will be removed in a future version.

 Instructions for updating:
 If using Keras pass *_constraint arguments to layers.

 ValueError: A tf.Variable created inside your tf.function has been garbage-collected. Your code needs to keep Python references to variables created inside `tf.function`s.

A common way to raise this error is to create and return a variable only referenced inside your function:

@tf.function
def f():
  v = tf.Variable(1.0)
  return v

v = f()  # Crashes with this error message!

The reason this crashes is that @tf.function annotated function returns a **`tf.Tensor`** with the **value** of the variable when the function is called rather than the variable instance itself. As such there is no code holding a reference to the `v` created inside the function and Python garbage collects it.

The simplest way to fix this issue is to create variables outside the function and capture them:

v = tf.Variable(1.0)

@tf.function
def f():
  return v

f()  # <tf.Tensor: ... numpy=1.>
v.assign_add(1.)
f()  # <tf.Tensor: ... numpy=2.>

我不理解这个错误,因为我评估过其他具有相同功能的网络,从未遇到任何问题。然后我决定使用predict函数将预测的标签与正确的标签匹配,并用我自己的代码获得所有度量,但我遇到了另一个错误

# Create a Sincnet model and load previously saved weights
redsinc = create_model(wlen,num_clases,fs)
redsinc.load_weights('checkpoints/SincNetBiomex3.hdf5')
print('Model loaded')

#Predict labels with test data 
predict_labels = redsinc.predict(eval_in)

Error while reading resource variable _AnonymousVar212 from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/_AnonymousVar212/class tensorflow::Var does not exist.
 [[node sinc_conv1d/concat_104/ReadVariableOp (defined at \Users\atenc\Anaconda3\envs\py3.7-tf2.0gpu\lib\site-packages\tensorflow_core\python\framework\ops.py:1751) ]] [Op:__inference_keras_scratch_graph_13649]

 Function call stack:
 keras_scratch_graph

我希望有人能告诉我这些错误意味着什么以及如何解决它们,我已经找到了解决它们的方法,但我找到的大多数方法似乎与我的问题无关,所以我无法应用这些方法。我猜错误是由Sincnet层代码引起的,因为它是一个自定义编码层。Sincnet层的代码可以在github存储库的Sincnet.py文件中找到

我感谢所有能得到的帮助,再次感谢你的鼓励


Tags: andtheto代码modeltfcreatewith
1条回答
网友
1楼 · 发布于 2024-09-28 05:22:50

你应该降低你的tf和keras版本,当我面临同样的问题时,它对我有效。 试试这个keras==2.1.6;tensorflow gpu==1.13.1

相关问题 更多 >

    热门问题