简体   繁体   中英

Keras pre-trained VGG16 low accuracy

I am trying to create a very simple CNN program for classification using the VGG16 pre-trained model. My dataset is the first Pokemon generation found on Kaggle, it has 10.000 images of 149 different classes. The problem is that I am not getting enough accuracy, the maximum I can get is almost 40%.

Here is the code:

import tensorflow as tf
import numpy as np

vgg_model = tf.keras.applications.VGG16(weights='imagenet', include_top=False, input_shape = (224,224,3))
vgg_model.trainable = False

model = tf.keras.models.Sequential()
model.add(vgg_model)
model.add(tf.keras.layers.Flatten(input_shape=vgg_model.output_shape[1:]))
model.add(tf.keras.layers.Dense(256, activation='relu'))
model.add(tf.keras.layers.Dense(149, activation='softmax'))

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

train= tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
test= tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
training_set = train.flow_from_directory('datasets/generation/train', target_size=(224,224), class_mode = 'categorical')
val_set = train.flow_from_directory('datasets/generation/test', target_size=(224,224), class_mode = 'categorical')

history = model.fit_generator(training_set, steps_per_epoch = 64, epochs = 30, validation_data = val_set, validation_steps = 64)

Here's the output:

Epoch 1/30
64/64 [=================] - 57s 885ms/step - loss: 5.0538 - acc: 0.0410 - val_loss: 4.7750 - val_acc: 0.0659
Epoch 2/30
64/64 [=================] - 50s 775ms/step - loss: 4.7432 - acc: 0.0747 - val_loss: 4.5880 - val_acc: 0.1037
...
Epoch 10/30
64/64 [=================] - 50s 788ms/step - loss: 3.0594 - acc: 0.3077 - val_loss: 3,3569 - val_acc: 0.2425
...
Epoch 20/30
64/64 [=================] - 54s 843ms/step - loss: 2.2030 - acc: 0.4628 - val_loss: 2.8968 - val_acc: 0.3565
...
Epoch 25/30
64/64 [=================] - 49s 773ms/step - loss: 1.9324 - acc: 0.5293 - val_loss: 2.6801 - val_acc: 0.3823
...
Epoch 30/30
64/64 [=================] - 52s 814ms/step - loss: 1.6427 - acc: 0.5801 - val_loss: 2.6852 - val_acc: 0.3936

Can someone help me to understand?

This might enhance your validation accuracy:

# Add a fully connected layer with 512 hidden units and ReLU activation
x = keras.layers.Dense(512, activation='relu')(x)
# Add a dropout rate of 0.5
x = keras.layers.Dropout(0.5)(x)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM