Reputation: 159
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:
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