Keras custom metrics self.validation_data is none , when using data generators

Viewed 1372

I have been trying to train a model and calculate precision and recall at the end of each epoch.

The custom metric

class Metrics(keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.precision = []
        self.recall = []

    def on_epoch_end(self, epoch, logs={}):
        print(type(self.validation_data))
        print(self.validation_data)
        predict = np.round(np.asarray(self.model.predict(self.validation_data[0])))
        targ = self.validation_data[1]

        precision_score = sklm.precision_score(targ, predict)
        recall = sklm.recall_score(targ, predict)
        self.precision.append(precision_score)
        self.recall.append(recall)

    def avg_precision_score(self):
        return np.mean(self.precision_score)

    def avg_recall_score(self):
        return np.mean(self.recall)

and when training I am using data generator .

   training_set = train_datagen.flow_from_directory('train/',
                                                     target_size=(dim_x,dim_y),
                                                     batch_size=8, # 16 32
                                                     class_mode='categorical')

    test_set = test_datagen.flow_from_directory('test/',
                                                 target_size=(dim_x,dim_y),
                                                 batch_size=8, # 16 32
                                                 class_mode='categorical')
    metrics = Metrics()
    history = classifier.fit_generator(
                training_set,
                steps_per_epoch=2,#50,
                epochs=1, # 25
                validation_data=test_set,
                validation_steps=10,
                callbacks=[metrics]
                )

But this is giving the self.validation as of None type . What am I doing wrong ?

1 Answers

Found the solution to this problem. Mention in issue comments https://github.com/keras-team/keras/issues/10472

class Metrics(Callback):

    def __init__(self, val_data, batch_size = 20):
        super().__init__()
        self.validation_data = val_data
        self.batch_size = batch_size

Initializing the validation data solves the problem when using data generators.

Related