2

I try to get output layers during the training. I am trying to make a 3d visualization of the model in real time and to make it interactive. I am using google colab with tensorflow 2.0 and python 3.

Here is my code:

Imports

  from __future__ import absolute_import, division, print_function, unicode_literals
 try:
   # Use the %tensorflow_version magic if in colab.
     %tensorflow_version 2.x
 except Exception:
       pass

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

import tensorflow_hub as hub
import tensorflow_datasets as tfds

from tensorflow.keras import datasets, layers, models

from tensorflow.keras import backend as K
from tensorflow.keras.backend import clear_session

from tensorflow.keras.callbacks import Callback as Callback

import logging
logger = tf.get_logger()
logger.setLevel(logging.ERROR)

Get data

splits = tfds.Split.TRAIN.subsplit([70, 30])

(training_set, validation_set), dataset_info = tfds.load('tf_flowers',with_info=True, as_supervised=True, split=splits)
 for i, example in enumerate(training_set.take(5)):
        print('Image {} shape: {} label: {}'.format(i+1, example[0].shape, example[1]))

Check the number of classes and images

 num_classes = dataset_info.features['label'].num_classes

 num_training_examples = 0
 num_validation_examples = 0

 for example in training_set:
   num_training_examples += 1

 for example in validation_set:
   num_validation_examples += 1

 print('Total Number of Classes: {}'.format(num_classes))
 print('Total Number of Training Images: {}'.format(num_training_examples))
 print('Total Number of Validation Images: {} \n'.format(num_validation_examples))

get inception

   IMAGE_RES = 299
   BATCH_SIZE = 32
def format_image(image, label):
   image = tf.image.resize(image, (IMAGE_RES, IMAGE_RES))/255.0
  return image, label

 (training_set, validation_set), dataset_info = tfds.load('tf_flowers', with_info=True, as_supervised=True, split=splits)
  train_batches = training_set.shuffle(num_training_examples//4).map(format_image).batch(BATCH_SIZE).prefetch(1)
    validation_batches = validation_set.map(format_image).batch(BATCH_SIZE).prefetch(1)

URL = "https://tfhub.dev/google/tf2-preview/inception_v3/feature_vector/4"
feature_extractor = hub.KerasLayer(URL,
  input_shape=(IMAGE_RES, IMAGE_RES, 3),
trainable=False)

model_inception = tf.keras.Sequential([
feature_extractor,
layers.Dense(num_classes, activation='softmax')
])

 model_inception.summary()

here is the custom callback where i tried to get the output layers during training

    import datetime
 from keras.callbacks import Callback

class MyCustomCallback(tf.keras.callbacks.Callback):

  def on_train_batch_begin(self, batch, logs=None):
     print('Training: batch {} begins at {}'.format(batch, datetime.datetime.now().time()))

  def on_train_batch_end(self, batch, logs=None):
     for i in range(len(model_inception.layers)):
      inp = self.model.input                                    # input placeholder
      outputs = [layer.output for layer in self.model.layers]     # all layer outputs
      functors = [K.function([inp, K.learning_phase()], [out]) for out in outputs]    # evaluation functions
      input_shape = [1] + list(self.model.input_shape[1:])
      test = np.random.random(input_shape)
      layer_outs = [func([test, 1.]) for func in functors] 
      print('\n Training: batch {} ends at {}'.format( layer_outs , datetime.datetime.now().time()))

  def on_test_batch_begin(self, batch, logs=None):
    print('Evaluating: batch {} begins at {}'.format(batch, datetime.datetime.now().time()))

  def on_test_batch_end(self, batch, logs=None):
   # layer_output = get_3rd_layer_output(self.validation_data)[0]  
    print('Training: batch {} ends at {} with the output layer {}'.format(batch, datetime.datetime.now().time()))

 The problem is in callback of how i can get the output/input of each layer at the end of each batch

here is the model compilation and training with my custom callback

 model_inception.compile(
  optimizer='adam', 
  loss='sparse_categorical_crossentropy',
  metrics=['accuracy'])

 EPOCHS = 2

 history = model_inception.fit(train_batches,
                epochs=EPOCHS,
                steps_per_epoch=20,
                validation_data=validation_batches,callbacks=[MyCustomCallback()])

Current error when i try it run it

AttributeError                            Traceback (most recent call last)
<ipython-input-10-5909c67ba93f> in <module>()
      9                     epochs=EPOCHS,
     10                     steps_per_epoch=20,
---> 11                     validation_data=validation_batches,callbacks=[MyCustomCallback()])
     12 
     13 # #Testing

11 frames
/tensorflow-2.0.0/python3.6/tensorflow_core/python/eager/lift_to_graph.py in <listcomp>(.0)
 247   # Check that the initializer does not depend on any placeholders.
 248   sources = object_identity.ObjectIdentitySet(sources or [])
-->249   visited_ops = set([x.op for x in sources])
 250   op_outputs = collections.defaultdict(set)
 251 

AttributeError: 'int' object has no attribute 'op'
cUser
  • 392
  • 8
  • 25
  • I believe I provide a solution for your question here: https://stackoverflow.com/a/59697739/880783 – bers Jan 11 '20 at 19:23
  • Does this answer your question? [Create keras callback to save model predictions and targets for each batch during training](https://stackoverflow.com/questions/47079111/create-keras-callback-to-save-model-predictions-and-targets-for-each-batch-durin) – bers Jan 11 '20 at 19:23

1 Answers1

4

If you read the source of the custom callbacks, here

there is a property model for every custom callback we define.

You can make use of the model object inside the functions you defined in your Cutomcallbacks.

for example,

def on_train_batch_end(self, batch, logs=None):
    #here you can get the model reference. 
    self.model.predict(dummy_data)

The self.model is an instance of keras.models.Model and the corrresponding function can be called using it.

More reference can be found here and here

Please follow the comments below in order to get the answer.

[EDIT 1]

The code segment from the comments by the OP

def on_train_batch_end(self, batch, logs=None): 
    for i in range(len(model_inception.layers)): 
        get_layer_output = K.function(inputs = self.model.layers[i].input, outputs = self.model.layers[i].output) 
        print('\n Training: output of the layer {} is {} ends at {}'.format(i, get_layer_output.outputs , datetime.datetime.now().time()))
venkata krishnan
  • 1,961
  • 1
  • 13
  • 20
  • 1
    Is any possibility to get output of each layer in the model at batch end ? @venkata – cUser Nov 27 '19 at 10:53
  • 2
    I don't want to predict , i want to get the input/output layers information during the training so after each batch to see the modifications – cUser Nov 27 '19 at 14:14
  • what do you mean by input and output layer information ? You mean to say, the weights ? or they produce some output when you pass an input right ? If you want to see their state for the current set of inputs, call the predict function, with the data for the particular batch, and see the activation map on each layer. – venkata krishnan Nov 28 '19 at 00:46
  • inp = self.model.input # input placeholder outputs = [layer.output for layer in self.model.layers] # all layer outputs functors = [K.function([inp, K.learning_phase()], [out]) for out in outputs] # evaluation functions input_shape = [1] + list(self.model.input_shape[1:]) test = np.random.random(input_shape) layer_outs = [func([test, 1.]) for func in functors] – cUser Nov 28 '19 at 20:30
  • My goal is to get input/output of each layer in real time. – cUser Nov 28 '19 at 20:32
  • that's right. in real time, a set of data is passed and the output is taken right ? While passing the set of data, it processes, updates the weight. With the same set of weights, If you pass the same set of data, you will be able to get the intermediate output (real time). What you are doing now itself, is realtime, i.e. at the run time of the model, you are interrupting and getting the results. – venkata krishnan Nov 29 '19 at 01:09
  • The problem is that is not working. Do you have any idea ? – cUser Nov 29 '19 at 16:15
  • when you say not working ? what do you mean ? where it fails ? what error logs ? – venkata krishnan Dec 02 '19 at 06:50
  • i am not sure, which piece of code, triggered that error, and after logging (Various print statements at each step inside the methods in callbacks), were you able to trace down, which function/line of code triggered this error ? – venkata krishnan Dec 03 '19 at 01:29
  • 1
    Eventually i manage to get the output like this : def on_train_batch_end(self, batch, logs=None): for i in range(len(model_inception.layers)): get_layer_output = K.function(inputs = self.model.layers[i].input, outputs = self.model.layers[i].output) print('\n Training: output of the layer {} is {} ends at {}'.format(i, get_layer_output.outputs , datetime.datetime.now().time())) – cUser Jan 02 '20 at 07:07
  • you are calculating the output as the function of input. – venkata krishnan Jan 02 '20 at 07:12
  • Is it not the right way to get it ? i did not understood your comment :( – cUser Jan 02 '20 at 07:23
  • 1
    it is the right way. i have applied similar way once. – venkata krishnan Jan 02 '20 at 08:43
  • I now try to visualize the tensors in 3d somehow. Do you know a good technique for that. I created a post here https://stackoverflow.com/questions/59559784/how-to-3d-visualize-output-layers-during-training-phase . I will mark this answer as the solution. Thank you for helping me till this point – cUser Jan 02 '20 at 08:51