Tensorflow Hub vs Keras application - performance drop

人走茶凉 提交于 2020-03-19 08:00:16

问题


I have image classification problem and i want to use Keras pretrained models for this task. When I use such a model

model = tf.keras.Sequential([
    hub.KerasLayer("https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4",
                   output_shape=[1280],
                   trainable=False),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(num_classes, activation='softmax')
])
model.build([None, image_size[0], image_size[1], 3])

model.compile(
    optimizer=tf.keras.optimizers.Adam(),
    loss='categorical_crossentropy',
    metrics=['acc'])

I easily get ~90% accuracy and very low loss on balanced dataset. However, if use keras.application like that:

`base_model = tf.keras.applications.mobilenet_v2.MobileNetV2(
    input_shape=input_img_size,
    include_top=False,
    weights='imagenet'
)

base_model.trainable = False  

model = tf.keras.layers.Dropout(0.5)(model)

model = tf.keras.layers.Dense(num_classes, activation='softmax')(model)

model = tf.keras.models.Model(inputs=base_model.input, outputs=model)

model.compile(
    optimizer=tf.keras.optimizers.Adam(),
    loss='categorical_crossentropy',
    metrics=['acc'])`

and use a proper tf.keras.application.mobilenet_v2.preprocess_input function in datagenerator (and leaving everything else the same) it is stuck at around 60% validation and 80% training. what is the difference between these approaches? why one is superior to the other?

The data generator:

datagen = tf.keras.preprocessing.image.ImageDataGenerator(
        preprocessing_function = preprocessing_function,
        rotation_range=10,
        zoom_range=0.3,
        width_shift_range=0.2,
        height_shift_range=0.2,
        horizontal_flip=True,
        vertical_flip=True,
        shear_range=0.2,
    )

Training:

 history = model.fit_generator(
    train_generator,
    epochs=nb_epochs,
    verbose=1,
    steps_per_epoch=steps_per_epoch,
    validation_data=valid_generator,
    validation_steps=val_steps_per_epoch,
    callbacks=[
        checkpoint,
        learning_rate_reduction,
        csv_logger,
        tensorboard_callback,
    ],
)

来源:https://stackoverflow.com/questions/58607878/tensorflow-hub-vs-keras-application-performance-drop

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