Vgg16 for gender detection (male,female)

扶醉桌前 提交于 2019-12-24 16:42:56

问题


We have used vgg16 and freeze top layers and retrain last 4 layers on gender dataset 12k male and 12k female. It gives very low accuracy especially for male.we are using IMDB dataset. On female test data it gives female as output but on male it gives same output.

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')

Testing Code:

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))

来源:https://stackoverflow.com/questions/58835778/vgg16-for-gender-detection-male-female

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!