训练此CNN时为什么精度不会增加?

时间:2019-04-27 16:31:57

标签: tensorflow keras conv-neural-network

我一直在尝试训练CNN以识别类型。使用(小型)FMA数据集,已使用librosa将每个30秒钟的乐曲片段转换为一张梅尔声谱图。反过来,这些频谱图已转换为480x640x3矩阵(像素高度,像素宽度,RGB值),又被切成三个第二段,重叠率为50%,从而产生了尺寸为480x64x3的最终输入矩阵。我编写的网络旨在复制本文(https://arxiv.org/pdf/1802.09697.pdf)中描述的网络。

因此,我总共有7197个梅尔频谱图作为输入,分成3s重叠,得到7197 * 19 = 136743个矩阵作为输入,而800 * 19 = 15200个矩阵作为测试数据。网络可以学习八种类型,标记为0-7。

训练时,即使经过几个时期,准确度仍然降低到0,125(等于纯粹的猜测(1/8))。那我在做什么错了?

import keras
#from keras.datasets import mnist
from keras.models import load_model
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
import numpy as np

#THIS ARCHITECTURE IS TAKEN FROM: https://arxiv.org/pdf/1802.09697.pdf
#3s with 50% overlap

batch_size = 64 #The set of examples used in one iteration (that is, one gradient update) of model training.
num_classes = 8 #1,2,3,4,5,6,7,8
epochs = 20   

# input image dimensions
img_rows, img_cols = 480, 64  #480x640 pixlar

# the data, split between train and test sets

(x_train, y_train) = (np.load('x_data_train_3s.npy'), np.load('y_data_train_3s.npy'))
(x_test, y_test) = (np.load('x_data_test_3s.npy'), np.load('y_data_test_3s.npy'))

x_train = x_train.reshape(136743,480,64,3) #this network accepts only 4-dim vector, so we reshape it. the last argument=grayscale. for RGB use 3. 
x_test = x_test.reshape(15200,480,64,3)

print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
#y_train = y_train -5    #otherwise error in np_utils.py
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

#IMAGE DIMENSIONS
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3),   #first layer
                 activation='relu',
                 input_shape=(480,64,3)))
model.add(MaxPooling2D(pool_size=(2, 2))) #second layer, pooling
model.add(Conv2D(64, (3, 5), activation='relu')) #third layer
model.add(Dropout(0.25))     #dropout makes sure there is no overfitting, randomly switches of some neurons
model.add(MaxPooling2D(pool_size=(2, 4))) #fifth layer, pooling
model.add(Flatten())
model.add(Dense(128, activation ='relu'))
model.add(Dense(num_classes, activation='softmax'))


model.compile(loss=keras.losses.categorical_crossentropy,   #compile the model with cross entropy loss function
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

model.save('genres.h5')

正如以上链接的论文,我希望准确度约为0.7,但我只能得到0.125。怎么了?

1 个答案:

答案 0 :(得分:2)

1。由于您的数据集相对较小。您必须进行数据扩充才能获得更好的结果。 2.确定正确的批次大小也是获得更好结果的一个好因素。批次大小为32的模型和批次大小为64的模型可能会产生不同的验证精度。 3.减少正则化参数也有助于获得更好的结果。

您有64个图像大小的5个池化层变成3 * 3大小: 第一层池化为21 * 21 * 64 第五层池化是将64张图像分成2 * 4

有很多池。 尝试5 * 5转换层,最大池为2 * 2块,然后在完全连接的层之前退出。如果提到的步骤不能改善您的结果,请去tflearn一个具有高级Tensorflow API的深度学习库。

相关问题