Keras验证准确性为零,但其他指标是正常的

时间:2019-09-05 18:52:46

标签: python machine-learning keras

我正在研究喀拉拉邦的计算机视觉问题,并且遇到了一个有趣的问题。我的val_acc是0.0000e + 00。这特别有趣,因为我的其他指标(例如损耗,acc和val_loss)都正常运行。

当我从Sequence数据生成器切换到我确定可以正常工作的自定义生成器时,就开始发生这种情况。

我的问题与这个validation accuracy is 0 with Keras fit_generator非常相似,但是在该线程中未找到答案。

我已经检查以确保我的激活和损失指标适合我的特定问题。我正在使用:loss ='categorical_crossentropy'metrics = ['accuracy'],并试图预测某个频谱图来自的月份。

验证数据的加载方式与训练数据,所以我真的不知道发生了什么,即使是随机猜测也应该给出1/12 val_acc对吗?不能为零。

这是我的模型架构:

x = (Convolution2D(32,5,5,activation='relu',input_shape=(501,501,1)))(input_img)
x = (MaxPooling2D(pool_size=(2,2)))(x)
x = (Convolution2D(32,5,5,activation='relu'))(x)
x = (MaxPooling2D(pool_size=(2,2)))(x)
x = (Dropout(0.25))(x)
x = (Flatten())(x)
x = (Dense(128,activation='relu'))(x)
x = (Dropout(0.5))(x)
classify = (Dense(12,activation='softmax', kernel_regularizer=regularizers.l1_l2(l1 = 0.001,l2 = 0.001)))(x)

model = Model(input_img,classify)
model.compile(loss='categorical_crossentropy',optimizer='nadam',metrics=['accuracy'])

这是我给fit_generator的电话:

model.fit_generator(generator = pd.data_generator(folder,'train'),
                    validation_data = pd.data_generator(folder,'test'),
                    steps_per_epoch=(120),
                    validation_steps=(24),
                    nb_epoch=20,
                    verbose=1,
                    shuffle=True,
                    callbacks=[tensorboard_callback,early_stop_callback])

最后是我的数据生成器的重要部分:


if mode == 'test':
        print('test')
        while True:

            for things in up.unpickle_batch(folder,50,6000,7200): #The last 1200 things in batches of 50
                random.shuffle(things)
                test_spect = []
                test_months = []
                for thing in things:
                    test_spect.append(thing.spect) #GET BATCH DATA
                    test_months.append(thing.month-1) #this is is here because the months go from 1-12 but should go from 0-11 for to_categorical   
                x_test = np.asarray(test_spect) #PREPARE BATCH DATA
                x_test =  x_test.astype('float32')
                x_test /= np.amax(x_test) #- 0.5
                X_test = np.reshape(x_test, (-1,501, 501,1))


                Y_test = np_utils.to_categorical(test_months,12)

                yield X_test,Y_test #RETURN BATCH DATA

1 个答案:

答案 0 :(得分:0)

检查不良数据。

请确保您的数据符合您的想法-改组,分发的内容与验证和/或测试集相同,且没有误导/错误/矛盾的样本。您可能会生成一个防故障的数据集(例如,将深色图像与浅色图像区分开,或者将清晰图像与模糊图像区分开来),并证明除数据之外的所有数据都可以。如果不能,请仔细查看您的代码。但是,这听起来像是数据问题。

我只是在一个简单的3层MLP网络中解决了一个类似的问题,该网络的训练损失和准确性朝着合理的方向发展,验证损失跟随训练损失(但滞后),而验证准确性却徘徊在零。我的训练数据集生成(一个较大的样本脚本)中出现了一个错误的错误,这意味着一种类型的整个样本块中的一个样本具有另一种类型的下一个块的标签。在500个样本中,有499个正确的样本不足以使培训步入正轨。