深度学习模型的验证精度保持在0.5,而训练精度却在提高

时间:2020-03-07 17:16:58

标签: python tensorflow deep-learning tensorflow2.0

我的TF深度学习模型停留在50%的验证准确度上,而我的训练准确度是65%并在不断提高。以下是一些注意事项-

  1. 问题陈述-我使用(224 * 224)张面孔作为输入并尝试预测性别
  2. 数据大小-我每个人都有29,999张图像。培训数据为80%-47,998,验证数据为20%-11,999
  3. 下面是我的代码。可能有点让人不知所措,请随时问我一些具体问题-

    def generator(positive_dataset, negative_dataset):
    while True:
        for pos_rec, neg_rec in zip(positive_dataset, negative_dataset):
              pos_x, pos_y = pos_rec
              neg_x, neg_y = neg_rec
              x = tf.concat([pos_x, neg_x], axis=0)
              y = tf.concat([pos_y, neg_y], axis=0)
              yield x, y
    train_generator = generator(train_dataset_female, train_dataset_male) 
    test_generator = generator(test_dataset_female, test_dataset_male)
    input_layer = layers.Input(shape=(IMG_SIZE, IMG_SIZE, 3))
    mobilenet_layer = MobileNetV2(weights=None, include_top=False, alpha=0.35)(input_layer)
    landmarks_layer = layers.GlobalAveragePooling2D()(mobilenet_layer)
    landmarks_layer = layers.Flatten()(landmarks_layer)
    landmarks_layer_flat = layers.Dense(68 * 2, activation='sigmoid', name='landmarks_flat')(landmarks_layer)
    landmarks_layer = layers.Reshape(target_shape=(68, 2), name='landmarks')(landmarks_layer_flat)
    
    landmarks_model = tf.keras.models.Model(inputs=input_layer, outputs=landmarks_layer)
    
    prob_face_layer = layers.GlobalAveragePooling2D()(mobilenet_layer)
    prob_face_layer = layers.Flatten()(prob_face_layer)
    prob_face_layer = layers.concatenate([prob_face_layer, landmarks_layer_flat])
    prob_face_layer = layers.Dense(256, activation='relu')(prob_face_layer)
    prob_face_layer = layers.Dense(1, activation='sigmoid')(prob_face_layer)
    
    prob_face_net = tf.keras.models.Model(inputs=input_layer, outputs=prob_face_layer)
    print('Prob Face Net')
    print(prob_face_net.summary())
    
    print('Pose Model')
    pose_model.summary()
    
    print('Landmarks Model')
    landmarks_model.summary()
    
    combined_model = tf.keras.models.Model(inputs=input_layer,outputs[landmarks_layer, pose_layer])
    print('Combined Model')
    combined_model.summary()
    
    full_model = tf.keras.models.Model(inputs=input_layer, outputs=[prob_face_layer, landmarks_layer, pose_layer])
    print('Full Net')
    print(full_model.summary())
    
    
    combined_model.load_weights('/mnt/sachins/apprach2/models/bounding_box_based_model_faster_2.h5')
    for layer in range(0, 8):
        prob_face_net.layers[layer].trainable = False 
    prob_face_net.compile(loss='binary_crossentropy', optimizer='adam', metrics=[ 'accuracy'])
    
    
    history = prob_face_net.fit_generator(
                train_generator,
                epochs=EPOCHS,
                steps_per_epoch=(num_train_imgs_male+ num_train_imgs_female)//(BATCH_SIZE//2),
                validation_data=test_generator,
                validation_steps=(29999*2 - (num_train_imgs_male+num_train_imgs_female))//(BATCH_SIZE//2),
                callbacks=callbacks,
                verbose = 1)
    

1 个答案:

答案 0 :(得分:0)