qq_39716743
2021-03-18 16:11
采纳率: 100%
浏览 1.3k

keras训练二分类模型acc一直0.5 loss一直是0.69怎么办?

import os
import tensorflow as tf
from tensorflow.keras import Sequential, regularizers
from tensorflow.keras.layers import MaxPooling2D, Conv2D, Flatten, Dense, Dropout, Activation, \
    BatchNormalization
from tensorflow.keras.optimizers import Adam, SGD
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow_core.python.keras import Model




if __name__ == '__main__':
    os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
    image_generator = ImageDataGenerator(rescale=1. / 255)
    image_generator2 = ImageDataGenerator(rescale=1. / 255)

    train_data = image_generator.flow_from_directory("/Users/kongbai/temp/data/train", batch_size=20,
                                                     color_mode='grayscale',
                                                     shuffle=False,

                                                     #
                                                     save_to_dir="/Users/kongbai/temp/data/gen/t",

                                                     save_prefix='train_',

                                                     target_size=(100, 100), class_mode="categorical")
    test_data = image_generator.flow_from_directory("/Users/kongbai/temp/data/test", batch_size=1,
                                                    color_mode='grayscale',
                                                    shuffle=False,

                                                    # save_to_dir=SAVE_PATH,
                                                    #
                                                    # save_prefix='gen',

                                                    target_size=(100, 100), class_mode='categorical')
    vailted_data = image_generator.flow_from_directory("/Users/kongbai/temp/data/vailted", batch_size=20,
                                                       color_mode='grayscale',
                                                       shuffle=True,

                                                       save_to_dir="/Users/kongbai/temp/data/gen/v",

                                                       save_prefix='vailted_',

                                                       target_size=(100, 100), class_mode='categorical')

    model = Sequential()
    model.add(Conv2D(32, 3, 1, input_shape=(100, 100, 1), activation='relu'))

    model.add(MaxPooling2D(3))
    model.add(Conv2D(128, 1, padding='same'))
    model.add(Conv2D(64, 1))

    model.add(Dropout(0.1))

    model.add(Flatten())
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.4))
    model.add(Dense(2, activation='softmax', ))

    model.compile(loss=tf.keras.losses.CategoricalCrossentropy(from_logits=False),
                  optimizer=tf.keras.optimizers.SGD(1e-4),
                  metrics=['accuracy'])
    model.summary()
    model.fit_generator(train_data, shuffle=True, steps_per_epoch=20, epochs=20, workers=16,
                        validation_data=vailted_data,
                        validation_steps=20)



    model.save("demo.h5")

训练的acc一直在0.5,loss直接固定在0.69了...............这就是个识别一张照片上的两个人是否是同一个人的网络呀,也没什么东西了呀  损失函数也改过Adam 也是一样的  有大佬帮忙看看吗?

2条回答 默认 最新

相关推荐 更多相似问题