Tensorflow Hub vs Keras应用程序-性能下降 [英] Tensorflow Hub vs Keras application - performance drop

查看:91
本文介绍了Tensorflow Hub vs Keras应用程序-性能下降的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有图像分类问题,我想使用Keras预训练模型进行此任务. 当我使用这样的模型

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

在平衡的数据集上,我很容易获得〜90%的准确度,并且损失极低.但是,如果使用keras.application这样的话:

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

,并在数据生成器中使用适当的tf.keras.application.mobilenet_v2.preprocess_input函数(并使其他所有内容保持不变),它被困在约60%的验证和80%的训练中. 这些方法之间有什么区别?为什么一个优于另一个?

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?

数据生成器:

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

培训:

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

推荐答案

我相信您正在训练两种不同的模型".在您的TensorFlow Hub示例中,您使用了mobilenet的特征向量.据我了解,特征向量与模型并不相同.它是一定长度的一维张量.它可能是mobilenet模型输出之前的最后一层.这与tf.keras示例不同,在tf.keras示例中,您正在调用完整的mobilenet模型.

I believe you are training two different 'models'. In your TensorFlow Hub example, you used mobilenet's feature vector. Feature vector as I understand it, is not the same as a model. It is a 1-D tensor of certain length. It is probably the last layer before the output of the mobilenet model. This is different from the tf.keras example, where you are invoking the full mobilenet model.

这篇关于Tensorflow Hub vs Keras应用程序-性能下降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆