modAL icon indicating copy to clipboard operation
modAL copied to clipboard

Fine-tuning a keras model using active learning

Open Tchaikovic opened this issue 3 years ago • 2 comments

Hi there,

I would like to apply active learning to a keras model trained with another script.

The model, with architecture, is stored in a .h5 file. I followed the keras example, but in my case I want to set the initial training set to None since my only aim is to get the selected indices for new samples.

I get the following error:

AttributeError: 'KerasClassifier' object has no attribute 'model'

Any help would be much appreciated.

from keras.wrappers.scikit_learn import KerasClassifier
from modAL.uncertainty import uncertainty_sampling
from modAL.models import ActiveLearner

  def get_model():
      model = keras.models.load_model(model_path)
      return model

classifier = KerasClassifier(get_model)

# initialize ActiveLearner
    learner = ActiveLearner(
        estimator=classifier,
        verbose=1
    )

  X_pool = get_image_data_to_ndarray(folder_path=folder_path, img_size=img_size)

  query_idx, query_instance = learner.query(X_pool, n_instances=100, verbose=0)

Tchaikovic avatar Jan 04 '22 09:01 Tchaikovic

According to comments in the Keras source code, the scikit-learn wrapper is deprecated and the scikeras external lib should be used. (As I was not following Keras development, this surprised me as well, I think it is a recent decision.)

Can you try using scikeras?

Just a side-note, I am planning on adding custom minimal wrappers that will make sure things like this doesn't happen in the future.

cosmic-cortex avatar Jan 07 '22 07:01 cosmic-cortex

I think you can also write a customer Class, whose structure is just like sklearn model:

For example a transferlearning model using Keras could be:

class TransferLearning():
	"""
	pure active transfer learning
	"""
	def __init__(self) -> None:
		# load the pre-trained encoder
		model_path = "./Encoder_models/27_02_2022__23_06_08"
		base_model = keras.models.load_model(model_path)
		# fix the non-trainable part
		self.fixed_model = tf.keras.models.Model(inputs=base_model.input, outputs=base_model.get_layer("flatten_12").output)
		self.fixed_model.trainable = False
		self.feature_num = base_model.get_layer("feature").output.get_shape().as_list()[1]
		
	def fit(self, X, y):
		# add the trainable part on the top
		self.extractor = Sequential()
		self.extractor.add(self.fixed_model)
		self.extractor.add(Dense(self.feature_num, activation='relu', name="feature"))
		self.extractor.add(Dense(y.shape[1], activation='softmax', name="prob"))
		self.extractor.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
		# fine-tune the extractor
		self.extractor.fit(X, y, epochs=1, batch_size=32, verbose=0)

	def predict(self, X):
		return self.extractor.predict(X)
	
	def score(self, X, y):
		_, accuracy = self.extractor.evaluate(X, y, verbose=0)
		return accuracy
	
	def predict_proba(self, X):
		predictor = Model(inputs=self.extractor.input, outputs=self.extractor.get_layer("prob").output)
		return predictor.predict(X)

ZhuYuqicheng avatar Mar 04 '22 22:03 ZhuYuqicheng