3

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,
    ],
)
  • Btw i am not asking about the difference between Sequential and Functional APIs, but about TF.Hub and keras.application modules – bartek wojcik Oct 29 '19 at 14:42
  • this answer could be helpful https://stackoverflow.com/questions/60251715/difference-between-keras-and-tensorflow-hub-version-of-mobilenetv2 – lbcommer Mar 10 '20 at 09:05

1 Answers1

1

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.

Rick Blaine
  • 175
  • 1
  • 6