Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fine-tuning a keras model using active learning #151

Open
Tchaikovic opened this issue Jan 4, 2022 · 2 comments
Open

Fine-tuning a keras model using active learning #151

Tchaikovic opened this issue Jan 4, 2022 · 2 comments

Comments

@Tchaikovic
Copy link

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)

@cosmic-cortex
Copy link
Member

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.

@ZhuYuqicheng
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants