【问题标题】:Vgg16 for gender detection (male,female)Vgg16 用于性别检测(男、女)
【发布时间】:2020-03-09 04:07:56
【问题描述】:

我们使用 vgg16 并冻结顶层,并在性别数据集 12k 男性和 12k 女性上重新训练最后 4 层。它的准确性非常低,尤其是对于男性。我们正在使用 IMDB 数据集。在女性测试数据上,它给出女性作为输出,但在男性上它给出相同的输出。

vgg_conv=VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

Freeze the layers except the last 4 layers
for layer in vgg_conv.layers[:-4]: 
    layer.trainable = False

Create the model
model = models.Sequential()

Add the vgg convolutional base model
model.add(vgg_conv)

Add new layers
model.add(layers.Flatten()) 
model.add(layers.Dense(4096, activation='relu')) 
model.add(layers.Dense(4096, activation='relu')) 
model.add(layers.Dropout(0.5)) model.add(layers.Dense(2, activation='softmax'))

nTrain=16850 nTest=6667

train_datagen = image.ImageDataGenerator(rescale=1./255)

test_datagen = image.ImageDataGenerator(rescale=1./255)

batch_size = 12 batch_size1 = 12

train_generator = train_datagen.flow_from_directory(train_dir, target_size=(224, 224), batch_size=batch_size, class_mode='categorical', shuffle=False)

test_generator = test_datagen.flow_from_directory(test_dir, target_size=(224, 224), batch_size=batch_size1, class_mode='categorical', shuffle=False)

model.compile(optimizer=optimizers.RMSprop(lr=1e-6), loss='categorical_crossentropy', metrics=['acc'])

history = model.fit_generator( train_generator, steps_per_epoch=train_generator.samples/train_generator.batch_size, epochs=3, validation_data=test_generator, validation_steps=test_generator.samples/test_generator.batch_size, verbose=1)

model.save('gender.h5')

测试代码:

model=load_model('age.h5') 
img=load_img('9358807_1980-12-28_2010.jpg', target_size=(224,224)) 
img=img_to_array(img) 
img=img.reshape((1,img.shape[0],img.shape[1],img.shape[2])) 
img=preprocess_input(img) 
yhat=model.predict(img) 
print(yhat.size) 
label=decode_predictions(yhat)

label=label[0][0]

print('%s(%.2f%%)'% (label[1],label[2]*100))

【问题讨论】:

  • 你应该重新缩进你的预测代码以获得更好的可读性。
  • 问题是它预测女性为女性,但男性也为女性。

标签: python tensorflow deep-learning vgg-net


【解决方案1】:

首先,您将模型保存为gender.h5,并在测试期间加载模型age.h5。可能您在这里添加了不同的测试代码。

即将提高程序的准确性 -

  1. 最重要的是您使用的是loss = 'categorical_crossentropy',将model.compile 中的loss = 'binary_crossentropy' 更改为loss = 'binary_crossentropy',因为您只有2 个类。所以你的 model.compile(optimizer="adam",loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), metrics=['accuracy']) 看起来像这样。
  2. 还将class_mode='categorical' 中的class_mode='binary' 更改为flow_from_directory
  3. 由于categorical_crossentropy 与最后一层的softmax 激活并驾齐驱,如果将损失更改为binary_crossentropy,最后的激活也应更改为sigmoid。所以最后一层应该是Dense(1, activation='sigmoid')
  4. 您已添加 2 个Dense4096,这将添加模型要学习的4096 * 4096 = ‭16,777,216‬ 权重。分别减少到1026512
  5. 您在0.5 中添加了Dropout 层,即在epoch 期间保持50% 的神经元关闭。这是一个巨大的数字。最好去掉Dropout 层,仅当您的模型为overfitting 时才使用。
  6. 设置batch_size = 1。由于您的输入非常少,因此每个 epoch 的步数都与输入记录相同。
  7. 使用 horizontal_flipvertical_flipshear_rangezoom_rangeImageDataGenerator 等数据增强技术在每个 epoch 期间生成新批次的训练和验证图像。
  8. 针对大量epoch 训练您的模型。你只是在为epoch=3 训练,这对于学习权重来说太少了。为epoch=50 训练,然后修剪数字。

希望这能回答您的问题。快乐学习。

【讨论】:

    猜你喜欢
    • 2017-04-13
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    相关资源
    最近更新 更多