Question

I have a Deep Learning Network that predicts values. However, along with the prediction i also want it to output some further metrics. For example, for each image i want it to give me the total sum of all pixels. How does one achieve this?

Was it helpful?

Solution

You could create a custom callback function. Let's say you want to find the total sum of all pixels in your validation data. Here's how the callback function would look like:

import numpy as np
from keras.callbacks.callbacks import Callback

class F1Callback(Callback):
    def __init__(self, val_gen, batch_size, valid_freq, freq=1):
        super().__init__()
        self.val_gen = val_gen
        self.batch_size = batch_size
        self.pixel_sum = {}
        """
        pixel_sum will look as such:
        {1: {"1.jpg":5,
             "2.jpg":3,
             .
             .
             .},
         2: 
         """
        self.epoch = 0

    def on_train_begin(self, logs={}):
        self.epoch += 1
        self.pixel_sum[self.epoch] = {}

    def on_epoch_end(self, epoch, logs={}):
        batches = len(self.val_gen)

        # logic to store the pixel values from predictions to self.pixel_sum
        for batch in range(batches):

            xVal, yVal = next(self.val_gen)

            # deriving pixel values after passing it through model
            pixel_values = np.asarray(self.model.predict(xVal))

            # some way of retrieving image paths from val_gen

            # storing
            for pixel_value, image_path from zip(pixel_values, image_paths):
                self.pixel_sum[self.epoch][image_path] = pixel_value

        return
Licensed under: CC-BY-SA with attribution
Not affiliated with datascience.stackexchange
scroll top