Jonny_92
Jonny_92

Reputation: 159

Differences on features extraction + logistic regression vs features extraction + dense layer with softmax

I have a small database of images for a classification problem, therefore I opt for the transfer learning methodology. I started with the classic approach in Tensorflow:

  1. A pretrained ResNet50 on the ImageNet database
  2. A GlobalMaxPooling2D layer
  3. A dense layer with the number of classes with a softmax activation function
  4. Compilation with the Adam compiler, the crossentropy loss, and the accuracy as a metrics.

Then I need to optimize the hyperparamenters like batch size and epochs.

A snippet of code is

import tensorflow as tf

data = load and preprocess your data

inputs = tf.keras.Input(shape=(None, None, 3))
x= base_resnet(x)
outputs = tf.keras.layers.GlobalMaxPooling2D()(x)
extractor = tf.keras.Model(inputs, outputs)
x = tf.keras.layers.Dense(nClasses)(x)
outputs = tf.keras.layers.Activation(activation="softmax", dtype = 'float32')(x)
model = tf.keras.Model(inputs, outputs)
model.compile(optimizer="adam", metrics=["accuracy"], loss = "categorical_crossentropy")

The after the hyperparamenter optimization I will do

model.fit(...)
model.evaluate(...)

Maybe it is the Columbus, egg but I have tried the extract features approach plus a logistic regression from scikit-learn. A code snippet is:

from sklearn.linear_model import LogisticRegression

x= base_resnet(x)
outputs = tf.keras.layers.GlobalMaxPooling2D()(x)
extractor = tf.keras.Model(inputs, outputs)
features = extractor.predict(data)    # features extractor

# logistic regression
log_reg = LogisticRegression(max_iter=500)
log_reg.fit(features, y)

log_reg.predict(features_test)

I have obtained nearly the same good performances (nearly 91% of accuracy on the test set) in both the approaches, but the second one is 10 times faster. Am I missing something? Is my approach correct?

Upvotes: 0

Views: 59

Answers (0)

Related Questions