我创建了带有mfccs音乐片段及其标签的TFrecord文件。但我遇到了一个男高音的问题。在写入TFrecord文件之前,我确定mfcc的形状是[256,12]。似乎请求的形状是从文件读取的张量的一半。我无法找到错误,也不知道原因。你能给我一些建议吗?感谢
enter code here
def read_tfRecord(file_tfRecord):
queue = tf.train.string_input_producer([file_tfRecord])
reader = tf.TFRecordReader()
_,serialized_example = reader.read(queue)
features = tf.parse_single_example(serialized_example,features={'micsegment': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.int64)})
mic=tf.decode_raw(features['micsegment'],tf.float32)
mic=tf.reshape(mic,[256,12,1])
label=tf.cast(features['label'], tf.int64)
return mic,label
[mic,label]=read_tfRecord('D:/360MoveData/training.tfrecords')
[testmic,testlabel]=read_tfRecord('D:/360MoveData/test1.tfrecords')
sess = tf.InteractiveSession()
x=tf.placeholder(tf.float32, [256*12])
y_actual = tf.placeholder(tf.float32, [None, num_classes])
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool(x,ksize):
return tf.nn.max_pool(x, ksize,strides=[1, 1, 1, 1], padding='SAME')
x_mic=tf.reshape(x,[-1,256,12,1])
W_conv1 = weight_variable([3, 3, 1, 16])
b_conv1 = bias_variable([16])
h_conv1 = tf.nn.relu(conv2d(x_mic, W_conv1) + b_conv1) #第一个卷积层
h_pool1 = max_pool(h_conv1,ksize=[1,2,2,1])
W_conv2 = weight_variable([3, 3, 16, 32])
b_conv2 = bias_variable([32])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool(h_conv2,ksize=[1,2,3,1])
W_conv3 = weight_variable([3, 3, 32, 32])
b_conv3 = bias_variable([32])
h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3)
h_pool3 = max_pool(h_conv3,ksize=[1,2,2,1])
W_fc1 = weight_variable([32*1*32, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool3, [-1, 32*1*32])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_predict=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) #softmax
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_predict* tf.log(y_actual), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(y_predict, y_actual)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.global_variables_initializer())
# start the threads used for reading files
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess,coord=coord)
# start training
nSteps=1000
for i in range(nSteps):
batch_xs, batch_ys=sess.run([mic_batches,label_batches])
# run the training step with feed of images
train_step.run(feed_dict={x: batch_xs, y_actual: batch_ys, keep_prob: 0.5})
if (i+1)%100==0: # then perform validation
# get a validation batch
vbatch_xs, vbatch_ys = sess.run([mictest_batches,labeltest_batches])
train_accuracy = accuracy.eval(feed_dict={x:vbatch_xs, y_actual: vbatch_ys, keep_prob: 1.0})
print("step %d, training accuracy %g"%(i+1, train_accuracy))
# finalise
coord.request_stop()
coord.join(threads)
sess.close()
这是我的计算mfccs和编写Tfrecord文件的代码
enter code here
def load_file(example_list_file):
lines = np.genfromtxt(example_list_file,delimiter="*",dtype=[('col1','S200'), ('col2', 'i8')])
examples = []
labels = []
for example,label in lines:
examples.append(example)
labels.append(label)
return np.asarray(examples),np.asarray(labels),len(lines)
enter code here
_examples,_labels,examples_num = load_file(train_file)
filename = name + '.tfrecords'
writer= tf.python_io.TFRecordWriter(filename)
for i,[example,label] in enumerate(zip(_examples,_labels)):
micseg = compute_spectrograms(example)
micsegment=micseg.tostring()
example = tf.train.Example(features=tf.train.Features(feature={ 'micsegment':_bytes_feature(micsegment), 'label': _int64_feature(label)}))
writer.write(example.SerializeToString())
writer.close()