Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we include_top=False while using pretrained models like InceptionResnetV2 in keras?

Using a pretrained Imagenet model for image classification. I am not able to understand the include_top = False in the arguments.I know that it removes fully connected layers at the end. Also I would like to know how to decide which pretrained model to use for which kind of image classification task ?

like image 682
raghav_agr Avatar asked Nov 26 '25 23:11

raghav_agr


2 Answers

Your question is related to transfer learning. In general CNN models for image classification can be divided into 2 parts:

  1. The first part is the feature extractor, this part job is to find the features in the image (or encode the image into lower space of features) intuitive, the output of this part should tell if you have a tail, eyes, face e.g... in the image.
  2. The second part is the classifier, this part job is to take the encoded image (output of the feature extractor) and classify the image to the desired classes, for example, if it sees tail, cat eyes, and cat face it can tell it a cat.

When you do transfer learning you wish to take the trained feature extractor because it's job is to find features that most of the time will be at your classification task (this sentence comes with a big assumption that the input to your task is somewhat similar to the input that the feature extractor was trained on). When you do transfer learning you need to re-train the classifier because now it should classify different classes and train only the classifier on your data.

In CNN models the feature extractor most of the time is the convolution's layers and the fully connected are the classifier, this is the case on the ImageNet models anyway. This is why they remove the fully connect layers when doing transfer learning and retrain them.

For your second question, most of the time you should take the best available model (depending on your performance/memory limits) because it probably has the strongest feature extractor.

More on transfer learning (just google it): ftp://ftp.cs.wisc.edu/machine-learning/shavlik-group/torrey.handbook09.pdf

like image 114
JumbaMumba Avatar answered Nov 28 '25 16:11

JumbaMumba


I am not able to understand the include_top = False in the

If you look into the code, it is a simple if-else condition:

if include_top:
    # Classification block
    x = layers.Flatten(name='flatten')(x)
    x = layers.Dense(4096, activation='relu', name='fc1')(x)
    x = layers.Dense(4096, activation='relu', name='fc2')(x)
    x = layers.Dense(classes, activation='softmax', name='predictions')(x)
else:
    if pooling == 'avg':
        x = layers.GlobalAveragePooling2D()(x)
    elif pooling == 'max':
        x = layers.GlobalMaxPooling2D()(x)

If you set include_top=True, it creates a classification layer (for fine-tuning purposes) otherwise, the output of the previous layer is used (for feature-extraction) through a GlobalAvreagePooling2D().

I would like to know how to decide which pretrained model to use for which kind of image classification task?

Every model has its own pros and cons. The number of parameters, training time, inference time, accuracy, and some other things are a few things that caused a researcher to favor one model over another. There is no model which excels on every task or dataset [see no free launch theorem].

like image 41
Amir Avatar answered Nov 28 '25 17:11

Amir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!