使用ImageDataGanarator和flow_from_directory的验证准确性非常低

时间:2019-06-05 00:14:29

标签: validation tensorflow keras

我正在尝试用立体声数据训练3D resnet-50,即网络的输入是根据深度轴连接的两个RGB图像(2,224,224,3),以获得6个灰度图像(6,224,224,1)。由于我正在大型数据库上训练网络,因此我使用了ImageDataGenerator flow_from_directory方法从文件夹中获取标签。我的数据是22390对,分为2239类(每类10张图像)。 通过将每个班级的两个中间图像拿到验证集中,我将它们手动分成0.8个训练和0.2个验证。在培训期间,我没有使用混洗,因为我必须从两个目录中正确提取立体声对的相应输入。我的问题是,在训练过程中,训练的准确性不断提高,而损失却在减少,这很好。实际上,验证准确性非常低(约0.001),并且没有任何改善。另外,验证损失非常高,并且仍然振荡成非常高的值!! 我想知道这是否是由于:

  • 我的数据包含大量类,每个类中的图像很少?
  • 我不是在拆分之前或拆分过程中对数据进行混洗吗?
  • 其他原因。 这是我的代码:

    import numpy as np 
    from keras_preprocessing.image import ImageDataGenerator
    from resnet3d import Resnet3DBuilder
    import keras 
    
    data_path_l='.\TRAIN\left\'
    data_path_r='.\TRAIN\right\'
    test_data_path_l ='.\TEST\left\'
    test_data_path_r='.\TEST\right\'
    num_classes=2239
    batch_size=32
    epochs=500
    train_images=17912
    val_images=4478
    
    input_imgen = ImageDataGenerator()
    def generate_generator_multiple(generator,dir1, dir2, batch_size):
           genX1 = generator.flow_from_directory(directory=dir1,target_size=(224,224)
                                         color_mode="rgb",
                                          batch_size=batch_size,
                                          class_mode="categorical",
                                          shuffle=False)
    
    
    
         genX2 = generator.flow_from_directory(directory=dir2,target_size=(224,224),
                                          color_mode="rgb",
                                          batch_size=batch_size,
                                          class_mode="categorical",
                                          shuffle=False)
    
          while True:
                 X1i = genX1.next()
                 X2i = genX2.next()
                 Xsum = np.concatenate((X1i[0],X2i[0]), axis=3)
                 Xsum = np.expand_dims(Xsum, axis=1)
                 Xsum=np.swapaxes(Xsum,4,1)
                 yield Xsum, X2i[1]  #Yield both images and their mutual label
    
    
    def validation_generate_generator_multiple(generator,dir1, dir2, batch_size):
          val_genX1 = generator.flow_from_directory(directory=dir1,target_size=(224,224),
                                          color_mode="rgb",
                                          batch_size=batch_size,
                                          class_mode="categorical",
                                          shuffle=False)
    
    
          val_genX2 = generator.flow_from_directory(directory=dir2,target_size=(224,224),
                                          color_mode="rgb",
                                          batch_size=batch_size,
                                          class_mode="categorical",
                                          shuffle=False)
    
          while True:
                 X1i = val_genX1.next()
                 X2i = val_genX2.next()
                 Xsum = np.concatenate((X1i[0],X2i[0]), axis=3)
                 Xsum = np.expand_dims(Xsum, axis=1)
                Xsum=np.swapaxes(Xsum,4,1)
                yield Xsum, X2i[1]  #Yield both images and their mutual label
    
    
    inputgenerator=generate_generator_multiple(generator=input_imgen,
                                           dir1=data_path_l,
                                           dir2=data_path_r,
                                           batch_size=batch_size) 
    
    
    
    validation_inputgenerator= validation_generate_generator_multiple(generator=input_imgen,
                                           dir1=test_data_path_l,
                                           dir2=test_data_path_r,
                                           batch_size=batch_size)
    
    
    model=Resnet3DBuilder.build_resnet_50((6,224,224,1), num_classes)
    model.compile(loss='categorical_crossentropy',
                            optimizer='adam',
                            metrics=['accuracy'])
     history=model.fit_generator(inputgenerator,
                        steps_per_epoch= train_images // batch_size,
                        validation_data = validation_inputgenerator, 
                        validation_steps = val_images// batch_size,
                        epochs = epochs,
                        shuffle=False)
    model.save('resnet3D_6sample_lr.h5')
    

这是培训过程的一部分

    Epoch 153/500
    559/559 [==============================] - 1927s 3s/step -                 
     loss: 1.1738 - acc: 0.9620 - val_loss: 12.9716 - val_acc: 0.0038

    Epoch 154/500
    559/559 [==============================] - 1929s 3s/step - 
    loss: 1.1810 - acc: 0.9594 - val_loss: 12.4732 - val_acc: 0.0065

    Epoch 155/500
    559/559 [==============================] - 2161s 4s/step - 
    loss: 1.1146 - acc: 0.9681 - val_loss: 11.9151 - val_acc: 0.0076

    Epoch 156/500
    559/559 [==============================] - 2165s 4s/step - 
    loss: 1.0637 - acc: 0.9734 - val_loss: 10.2526 - val_acc: 0.0135

    Epoch 157/500
    559/559 [==============================] - 2192s 4s/step - 
    loss: 1.0324 - acc: 0.9745 - val_loss: 9.6518 - val_acc: 0.0148

    Epoch 158/500
    559/559 [==============================] - 1921s 3s/step - 
    loss: 1.1294 - acc: 0.9516 - val_loss: 11.7690 - val_acc: 0.0090

    Epoch 159/500
    559/559 [==============================] - 1504s 3s/step - 
    loss: 1.1739 - acc: 0.9520 - val_loss: 11.2503 - val_acc: 0.0090

    Epoch 160/500
    559/559 [==============================] - 2139s 4s/step - 
    loss: 1.0292 - acc: 0.9732 - val_loss: 11.8762 - val_acc: 0.0106

    Epoch 161/500
    559/559 [==============================] - 1835s 3s/step - 
    loss: 1.0494 - acc: 0.9671 - val_loss: 10.1109 - val_acc: 0.0151

    Epoch 162/500
    559/559 [==============================] - 1583s 3s/step - 
    loss: 0.9970 - acc: 0.9737 - val_loss: 12.5503 - val_acc: 0.0049

0 个答案:

没有答案